mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 09:36:44 +00:00
![Thom Knowles](/assets/img/avatar_default.png)
* escape the folder name (in case of spaces, etc) * add test case for folder with space * add missing args * make separate tests for each folder test, get folder name length under API limits * further abstract out the resource name to prevent test collisions * workaround multiple results returning for a given query by looping over return * split test cases into separate funcs
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1"
|
|
)
|
|
|
|
func dataSourceGoogleActiveFolder() *schema.Resource {
|
|
return &schema.Resource{
|
|
Read: dataSourceGoogleActiveFolderRead,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"parent": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
},
|
|
"display_name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
},
|
|
"name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func dataSourceGoogleActiveFolderRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
parent := d.Get("parent").(string)
|
|
displayName := d.Get("display_name").(string)
|
|
|
|
queryString := fmt.Sprintf("lifecycleState=ACTIVE AND parent=%s AND displayName=%s", parent, url.QueryEscape(displayName))
|
|
searchRequest := &resourceManagerV2Beta1.SearchFoldersRequest{
|
|
Query: queryString,
|
|
}
|
|
searchResponse, err := config.clientResourceManagerV2Beta1.Folders.Search(searchRequest).Do()
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Folder Not Found : %s", displayName))
|
|
}
|
|
|
|
for _, folder := range searchResponse.Folders {
|
|
if folder.DisplayName == displayName {
|
|
d.SetId(folder.Name)
|
|
d.Set("name", folder.Name)
|
|
return nil
|
|
}
|
|
}
|
|
return fmt.Errorf("Folder not found")
|
|
}
|