forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_google_active_folder.go
59 lines (49 loc) · 1.48 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
56
57
58
59
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1"
"google.golang.org/api/googleapi"
)
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, displayName)
searchRequest := &resourceManagerV2Beta1.SearchFoldersRequest{
Query: queryString,
}
searchResponse, err := config.clientResourceManagerV2Beta1.Folders.Search(searchRequest).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
return fmt.Errorf("Folder Not Found : %s", d.Get("name"))
}
return fmt.Errorf("Error reading folders: %s", err)
}
folders := searchResponse.Folders
if len(folders) != 1 {
return fmt.Errorf("More than one folder found")
}
d.SetId(folders[0].Name)
d.Set("name", folders[0].Name)
return nil
}