Skip to content

Commit f754c62

Browse files
Add data source for retrieving all organizations (#12740) (#20965)
[upstream:3250f7ce91e2ea62d0450ab92c7e8af2dfbb9aec] Signed-off-by: Modular Magician <[email protected]>
1 parent 84cee6d commit f754c62

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

.changelog/12740.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-datasource
2+
`google_organizations`
3+
```

google/provider/provider_mmv1_resources.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
291291
"google_oracle_database_cloud_vm_clusters": oracledatabase.DataSourceOracleDatabaseCloudVmClusters(),
292292
"google_oracle_database_cloud_vm_cluster": oracledatabase.DataSourceOracleDatabaseCloudVmCluster(),
293293
"google_organization": resourcemanager.DataSourceGoogleOrganization(),
294+
"google_organizations": resourcemanager.DataSourceGoogleOrganizations(),
294295
"google_privateca_certificate_authority": privateca.DataSourcePrivatecaCertificateAuthority(),
295296
"google_privileged_access_manager_entitlement": privilegedaccessmanager.DataSourceGooglePrivilegedAccessManagerEntitlement(),
296297
"google_project": resourcemanager.DataSourceGoogleProject(),
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// Copyright (c) HashiCorp, Inc.
4+
// SPDX-License-Identifier: MPL-2.0
5+
package resourcemanager
6+
7+
import (
8+
"context"
9+
"fmt"
10+
"path/filepath"
11+
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
14+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
15+
16+
"google.golang.org/api/cloudresourcemanager/v1"
17+
)
18+
19+
func DataSourceGoogleOrganizations() *schema.Resource {
20+
return &schema.Resource{
21+
Read: datasourceGoogleOrganizationsRead,
22+
Schema: map[string]*schema.Schema{
23+
"filter": {
24+
Optional: true,
25+
Type: schema.TypeString,
26+
},
27+
"organizations": {
28+
Computed: true,
29+
Type: schema.TypeList,
30+
Elem: &schema.Resource{
31+
Schema: map[string]*schema.Schema{
32+
"directory_customer_id": {
33+
Computed: true,
34+
Type: schema.TypeString,
35+
},
36+
"display_name": {
37+
Computed: true,
38+
Type: schema.TypeString,
39+
},
40+
"lifecycle_state": {
41+
Computed: true,
42+
Type: schema.TypeString,
43+
},
44+
"name": {
45+
Computed: true,
46+
Type: schema.TypeString,
47+
},
48+
"org_id": {
49+
Computed: true,
50+
Type: schema.TypeString,
51+
},
52+
},
53+
},
54+
},
55+
},
56+
}
57+
}
58+
59+
func datasourceGoogleOrganizationsRead(d *schema.ResourceData, meta interface{}) error {
60+
config := meta.(*transport_tpg.Config)
61+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
62+
if err != nil {
63+
return err
64+
}
65+
66+
organizations := make([]map[string]interface{}, 0)
67+
68+
filter := ""
69+
if v, ok := d.GetOk("filter"); ok {
70+
filter = v.(string)
71+
}
72+
73+
request := config.NewResourceManagerClient(userAgent).Organizations.Search(&cloudresourcemanager.SearchOrganizationsRequest{
74+
Filter: filter,
75+
})
76+
77+
err = request.Pages(context.Background(), func(organizationList *cloudresourcemanager.SearchOrganizationsResponse) error {
78+
for _, organization := range organizationList.Organizations {
79+
directoryCustomerId := ""
80+
if organization.Owner != nil {
81+
directoryCustomerId = organization.Owner.DirectoryCustomerId
82+
}
83+
84+
organizations = append(organizations, map[string]interface{}{
85+
"directory_customer_id": directoryCustomerId,
86+
"display_name": organization.DisplayName,
87+
"lifecycle_state": organization.LifecycleState,
88+
"name": organization.Name,
89+
"org_id": filepath.Base(organization.Name),
90+
})
91+
}
92+
return nil
93+
})
94+
95+
if err != nil {
96+
return fmt.Errorf("Error retrieving organizations: %s", err)
97+
}
98+
99+
if err := d.Set("organizations", organizations); err != nil {
100+
return fmt.Errorf("Error setting organizations: %s", err)
101+
}
102+
103+
if filter == "" {
104+
filter = "empty_filter"
105+
}
106+
d.SetId(filter)
107+
108+
return nil
109+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package resourcemanager_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
9+
"github.com/hashicorp/terraform-provider-google/google/acctest"
10+
)
11+
12+
func TestAccDataSourceGoogleOrganizations_basic(t *testing.T) {
13+
t.Parallel()
14+
15+
acctest.VcrTest(t, resource.TestCase{
16+
PreCheck: func() { acctest.AccTestPreCheck(t) },
17+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
18+
Steps: []resource.TestStep{
19+
{
20+
Config: `data "google_organizations" "test" {}`,
21+
Check: resource.ComposeTestCheckFunc(
22+
// We assume that every principal finds at least one organization and we'll only check set-ness
23+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.directory_customer_id"),
24+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.display_name"),
25+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.lifecycle_state"),
26+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.name"),
27+
resource.TestCheckResourceAttrSet("data.google_organizations.test", "organizations.0.org_id"),
28+
),
29+
},
30+
},
31+
})
32+
}
Lines changed: 45 additions & 0 deletions
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)