forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_google_organization.go
97 lines (84 loc) · 2.44 KB
/
data_source_google_organization.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package google
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/cloudresourcemanager/v1"
)
func dataSourceGoogleOrganization() *schema.Resource {
return &schema.Resource{
Read: dataSourceOrganizationRead,
Schema: map[string]*schema.Schema{
"domain": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"organization"},
},
"organization": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"domain"},
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"directory_customer_id": {
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
},
"lifecycle_state": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
var organization *cloudresourcemanager.Organization
if v, ok := d.GetOk("domain"); ok {
filter := fmt.Sprintf("domain=%s", v.(string))
resp, err := config.clientResourceManager.Organizations.Search(&cloudresourcemanager.SearchOrganizationsRequest{
Filter: filter,
}).Do()
if err != nil {
return fmt.Errorf("Error reading organization: %s", err)
}
if len(resp.Organizations) == 0 {
return fmt.Errorf("Organization not found: %s", v)
}
if len(resp.Organizations) > 1 {
return fmt.Errorf("More than one matching organization found")
}
organization = resp.Organizations[0]
} else if v, ok := d.GetOk("organization"); ok {
resp, err := config.clientResourceManager.Organizations.Get(canonicalOrganizationName(v.(string))).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Organization Not Found : %s", v))
}
organization = resp
} else {
return fmt.Errorf("one of domain or organization must be set")
}
d.SetId(GetResourceNameFromSelfLink(organization.Name))
d.Set("name", organization.Name)
d.Set("domain", organization.DisplayName)
d.Set("create_time", organization.CreationTime)
d.Set("lifecycle_state", organization.LifecycleState)
if organization.Owner != nil {
d.Set("directory_customer_id", organization.Owner.DirectoryCustomerId)
}
return nil
}
func canonicalOrganizationName(ba string) string {
if strings.HasPrefix(ba, "organizations/") {
return ba
}
return "organizations/" + ba
}