forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_google_active_folder.go
55 lines (47 loc) · 1.4 KB
/
data_source_google_active_folder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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")
}