Skip to content

Commit 892e230

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

File tree

4 files changed

+170
-0
lines changed

4 files changed

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