|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/schema" |
| 9 | + |
| 10 | + "google.golang.org/api/cloudresourcemanager/v1" |
| 11 | + "google.golang.org/api/googleapi" |
| 12 | +) |
| 13 | + |
| 14 | +func dataSourceGoogleOrganization() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceOrganizationRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "domain": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Optional: true, |
| 21 | + Computed: true, |
| 22 | + ConflictsWith: []string{"organization"}, |
| 23 | + }, |
| 24 | + "organization": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + ConflictsWith: []string{"domain"}, |
| 28 | + }, |
| 29 | + "name": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Computed: true, |
| 32 | + }, |
| 33 | + "directory_customer_id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + }, |
| 37 | + "create_time": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "lifecycle_state": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error { |
| 50 | + config := meta.(*Config) |
| 51 | + |
| 52 | + var organization *cloudresourcemanager.Organization |
| 53 | + if v, ok := d.GetOk("domain"); ok { |
| 54 | + filter := fmt.Sprintf("domain=%s", v.(string)) |
| 55 | + resp, err := config.clientResourceManager.Organizations.Search(&cloudresourcemanager.SearchOrganizationsRequest{ |
| 56 | + Filter: filter, |
| 57 | + }).Do() |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("Error reading organization: %s", err) |
| 60 | + } |
| 61 | + |
| 62 | + if len(resp.Organizations) == 0 { |
| 63 | + return fmt.Errorf("Organization not found: %s", v) |
| 64 | + } |
| 65 | + if len(resp.Organizations) > 1 { |
| 66 | + return fmt.Errorf("More than one matching organization found") |
| 67 | + } |
| 68 | + |
| 69 | + organization = resp.Organizations[0] |
| 70 | + } else if v, ok := d.GetOk("organization"); ok { |
| 71 | + resp, err := config.clientResourceManager.Organizations.Get(canonicalOrganizationName(v.(string))).Do() |
| 72 | + if err != nil { |
| 73 | + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound { |
| 74 | + return fmt.Errorf("Organization not found: %s", v) |
| 75 | + } |
| 76 | + |
| 77 | + return fmt.Errorf("Error reading organization: %s", err) |
| 78 | + } |
| 79 | + |
| 80 | + organization = resp |
| 81 | + } else { |
| 82 | + return fmt.Errorf("one of domain or organization must be set") |
| 83 | + } |
| 84 | + |
| 85 | + d.SetId(GetResourceNameFromSelfLink(organization.Name)) |
| 86 | + d.Set("name", organization.Name) |
| 87 | + d.Set("domain", organization.DisplayName) |
| 88 | + d.Set("create_time", organization.CreationTime) |
| 89 | + d.Set("lifecycle_state", organization.LifecycleState) |
| 90 | + if organization.Owner != nil { |
| 91 | + d.Set("directory_customer_id", organization.Owner.DirectoryCustomerId) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +func canonicalOrganizationName(ba string) string { |
| 98 | + if strings.HasPrefix(ba, "organizations/") { |
| 99 | + return ba |
| 100 | + } |
| 101 | + |
| 102 | + return "organizations/" + ba |
| 103 | +} |
0 commit comments