Skip to content

Commit 9cb0377

Browse files
bestefreundniharika-98
authored andcommitted
Add data source for retrieving all organizations (GoogleCloudPlatform#12740)
1 parent 2e1c37c commit 9cb0377

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ var handwrittenDatasources = map[string]*schema.Resource{
187187
"google_oracle_database_cloud_vm_clusters": oracledatabase.DataSourceOracleDatabaseCloudVmClusters(),
188188
"google_oracle_database_cloud_vm_cluster": oracledatabase.DataSourceOracleDatabaseCloudVmCluster(),
189189
"google_organization": resourcemanager.DataSourceGoogleOrganization(),
190+
"google_organizations": resourcemanager.DataSourceGoogleOrganizations(),
190191
{{- if ne $.TargetVersionName "ga" }}
191192
"google_parameter_manager_parameter": parametermanager.DataSourceParameterManagerParameter(),
192193
"google_parameter_manager_regional_parameter": parametermanagerregional.DataSourceParameterManagerRegionalRegionalParameter(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
package resourcemanager
4+
5+
import (
6+
"context"
7+
"fmt"
8+
"path/filepath"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
12+
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
13+
14+
"google.golang.org/api/cloudresourcemanager/v1"
15+
)
16+
17+
func DataSourceGoogleOrganizations() *schema.Resource {
18+
return &schema.Resource{
19+
Read: datasourceGoogleOrganizationsRead,
20+
Schema: map[string]*schema.Schema{
21+
"filter": {
22+
Optional: true,
23+
Type: schema.TypeString,
24+
},
25+
"organizations": {
26+
Computed: true,
27+
Type: schema.TypeList,
28+
Elem: &schema.Resource{
29+
Schema: map[string]*schema.Schema{
30+
"directory_customer_id": {
31+
Computed: true,
32+
Type: schema.TypeString,
33+
},
34+
"display_name": {
35+
Computed: true,
36+
Type: schema.TypeString,
37+
},
38+
"lifecycle_state": {
39+
Computed: true,
40+
Type: schema.TypeString,
41+
},
42+
"name": {
43+
Computed: true,
44+
Type: schema.TypeString,
45+
},
46+
"org_id": {
47+
Computed: true,
48+
Type: schema.TypeString,
49+
},
50+
},
51+
},
52+
},
53+
},
54+
}
55+
}
56+
57+
func datasourceGoogleOrganizationsRead(d *schema.ResourceData, meta interface{}) error {
58+
config := meta.(*transport_tpg.Config)
59+
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
60+
if err != nil {
61+
return err
62+
}
63+
64+
organizations := make([]map[string]interface{}, 0)
65+
66+
filter := ""
67+
if v, ok := d.GetOk("filter"); ok {
68+
filter = v.(string)
69+
}
70+
71+
request := config.NewResourceManagerClient(userAgent).Organizations.Search(&cloudresourcemanager.SearchOrganizationsRequest{
72+
Filter: filter,
73+
})
74+
75+
err = request.Pages(context.Background(), func(organizationList *cloudresourcemanager.SearchOrganizationsResponse) error {
76+
for _, organization := range organizationList.Organizations {
77+
directoryCustomerId := ""
78+
if organization.Owner != nil {
79+
directoryCustomerId = organization.Owner.DirectoryCustomerId
80+
}
81+
82+
organizations = append(organizations, map[string]interface{}{
83+
"directory_customer_id": directoryCustomerId,
84+
"display_name": organization.DisplayName,
85+
"lifecycle_state": organization.LifecycleState,
86+
"name": organization.Name,
87+
"org_id": filepath.Base(organization.Name),
88+
})
89+
}
90+
return nil
91+
})
92+
93+
if err != nil {
94+
return fmt.Errorf("Error retrieving organizations: %s", err)
95+
}
96+
97+
if err := d.Set("organizations", organizations); err != nil {
98+
return fmt.Errorf("Error setting organizations: %s", err)
99+
}
100+
101+
if filter == "" {
102+
filter = "empty_filter"
103+
}
104+
d.SetId(filter)
105+
106+
return nil
107+
}
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)