|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | +package resourcemanager |
| 4 | + |
| 5 | +import ( |
| 6 | + "crypto/sha256" |
| 7 | + "encoding/hex" |
| 8 | + "fmt" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-provider-google/google/tpgresource" |
| 13 | + transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" |
| 14 | + |
| 15 | + "google.golang.org/api/cloudresourcemanager/v1" |
| 16 | +) |
| 17 | + |
| 18 | +func DataSourceGoogleOrganizations() *schema.Resource { |
| 19 | + return &schema.Resource{ |
| 20 | + Read: datasourceGoogleOrganizationsRead, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "filter": { |
| 23 | + Optional: true, |
| 24 | + Type: schema.TypeString, |
| 25 | + }, |
| 26 | + "organizations": { |
| 27 | + Computed: true, |
| 28 | + Type: schema.TypeList, |
| 29 | + Elem: &schema.Resource{ |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "directory_customer_id": { |
| 32 | + Computed: true, |
| 33 | + Type: schema.TypeString, |
| 34 | + }, |
| 35 | + "display_name": { |
| 36 | + Computed: true, |
| 37 | + Type: schema.TypeString, |
| 38 | + }, |
| 39 | + "lifecycle_state": { |
| 40 | + Computed: true, |
| 41 | + Type: schema.TypeString, |
| 42 | + }, |
| 43 | + "name": { |
| 44 | + Computed: true, |
| 45 | + Type: schema.TypeString, |
| 46 | + }, |
| 47 | + "org_id": { |
| 48 | + Computed: true, |
| 49 | + Type: schema.TypeString, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func datasourceGoogleOrganizationsRead(d *schema.ResourceData, meta interface{}) error { |
| 59 | + config := meta.(*transport_tpg.Config) |
| 60 | + userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + organizations := make([]map[string]interface{}, 0) |
| 66 | + |
| 67 | + filter := "" |
| 68 | + if v, ok := d.GetOk("filter"); ok { |
| 69 | + filter = v.(string) |
| 70 | + } |
| 71 | + |
| 72 | + organizationList, err := config.NewResourceManagerClient(userAgent).Organizations.Search(&cloudresourcemanager.SearchOrganizationsRequest{ |
| 73 | + Filter: filter, |
| 74 | + }).Do() |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("Error retrieving organizations: %s", err) |
| 77 | + } |
| 78 | + |
| 79 | + for _, organization := range organizationList.Organizations { |
| 80 | + organizations = append(organizations, map[string]interface{}{ |
| 81 | + "directory_customer_id": organization.Owner.DirectoryCustomerId, |
| 82 | + "display_name": organization.DisplayName, |
| 83 | + "lifecycle_state": organization.LifecycleState, |
| 84 | + "name": organization.Name, |
| 85 | + "org_id": filepath.Base(organization.Name), |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + if err := d.Set("organizations", organizations); err != nil { |
| 90 | + return fmt.Errorf("Error retrieving organizations: %s", err) |
| 91 | + } |
| 92 | + |
| 93 | + // Ensure that a non-empty identifier based on the filter input gets created |
| 94 | + hash := sha256.Sum256([]byte(filter)) |
| 95 | + d.SetId(hex.EncodeToString(hash[:])) |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
0 commit comments