Skip to content

Commit d6cc18c

Browse files
committed
feat(resourcemanager): add data source for retrieving all organizations
1 parent 06d9610 commit d6cc18c

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed

mmv1/third_party/terraform/provider/provider_mmv1_resources.go.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
184184
"google_oracle_database_cloud_vm_clusters": oracledatabase.DataSourceOracleDatabaseCloudVmClusters(),
185185
"google_oracle_database_cloud_vm_cluster": oracledatabase.DataSourceOracleDatabaseCloudVmCluster(),
186186
"google_organization": resourcemanager.DataSourceGoogleOrganization(),
187+
"google_organizations": resourcemanager.DataSourceGoogleOrganizations(),
187188
{{- if ne $.TargetVersionName "ga" }}
188189
"google_parameter_manager_regional_parameter": parametermanagerregional.DataSourceParameterManagerRegionalRegionalParameter(),
189190
{{- end }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package resourcemanager_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
"github.com/hashicorp/terraform-provider-google/google/acctest"
8+
)
9+
10+
func TestAccDataSourceGoogleOrganizations_basic(t *testing.T) {
11+
t.Parallel()
12+
13+
acctest.VcrTest(t, resource.TestCase{
14+
PreCheck: func() { acctest.AccTestPreCheck(t) },
15+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
16+
Steps: []resource.TestStep{
17+
{
18+
Config: `data "google_organizations" "test" {}`,
19+
Check: resource.ComposeTestCheckFunc(
20+
// We assume that every principal finds at least one organization and we'll only check set-ness
21+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.directory_customer_id"),
22+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.display_name"),
23+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.lifecycle_state"),
24+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.name"),
25+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.org_id"),
26+
),
27+
},
28+
},
29+
})
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
subcategory: "Cloud Platform"
3+
description: |-
4+
Get all organizations.
5+
---
6+
7+
8+
# google_organizations
9+
10+
Gets a list of all organizations.
11+
See [the official documentation](https://cloud.google.com/resource-manager/docs/creating-managing-organization)
12+
and [API](https://cloud.google.com/resource-manager/reference/rest/v1/organizations/search).
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "google_organizations" "example" {
18+
filter = "domain:example.com"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `filter` - (Optional) An optional query string used to filter the Organizations to return in the response. Filter rules are case-insensitive. Further information can be found in the [REST API](https://cloud.google.com/resource-manager/reference/rest/v1/organizations/search#request-body).
27+
28+
29+
## Attributes Reference
30+
31+
The following attributes are exported:
32+
33+
* `organizations` - A list of all retrieved organizations. Structure is [defined below](#nested_organizations).
34+
35+
<a name="nested_organizations"></a>The `organizations` block supports:
36+
37+
* `directory_customer_id` - The Google for Work customer ID of the Organization.
38+
39+
* `display_name` - A human-readable string that refers to the Organization in the Google Cloud console. The string will be set to the primary domain (for example, `"google.com"`) of the G Suite customer that owns the organization.
40+
41+
* `lifecycle_state` - The Organization's current lifecycle state.
42+
43+
* `name` - The resource name of the Organization in the form `organizations/{organization_id}`.
44+
45+
* `org_id` - The Organization ID.

0 commit comments

Comments
 (0)