Skip to content

Commit 49a0008

Browse files
ewbankkitrosbo
authored andcommitted
Add 'google_organization' data source (#887)
* Add 'google_organization' data source. * Use 'GetResourceNameFromSelfLink'. * Remove 'resourcemanager_helpers'. * Use 'ConflictsWith' in schema. * Add 'organization' argument and make 'name' an output-only attribute.
1 parent 72ee97e commit 49a0008

5 files changed

+227
-0
lines changed
+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"strings"
7+
8+
"github.com/hashicorp/terraform/helper/schema"
9+
10+
"google.golang.org/api/cloudresourcemanager/v1"
11+
"google.golang.org/api/googleapi"
12+
)
13+
14+
func dataSourceGoogleOrganization() *schema.Resource {
15+
return &schema.Resource{
16+
Read: dataSourceOrganizationRead,
17+
Schema: map[string]*schema.Schema{
18+
"domain": {
19+
Type: schema.TypeString,
20+
Optional: true,
21+
Computed: true,
22+
ConflictsWith: []string{"organization"},
23+
},
24+
"organization": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
ConflictsWith: []string{"domain"},
28+
},
29+
"name": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
"directory_customer_id": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
},
37+
"create_time": {
38+
Type: schema.TypeString,
39+
Computed: true,
40+
},
41+
"lifecycle_state": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
},
45+
},
46+
}
47+
}
48+
49+
func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error {
50+
config := meta.(*Config)
51+
52+
var organization *cloudresourcemanager.Organization
53+
if v, ok := d.GetOk("domain"); ok {
54+
filter := fmt.Sprintf("domain=%s", v.(string))
55+
resp, err := config.clientResourceManager.Organizations.Search(&cloudresourcemanager.SearchOrganizationsRequest{
56+
Filter: filter,
57+
}).Do()
58+
if err != nil {
59+
return fmt.Errorf("Error reading organization: %s", err)
60+
}
61+
62+
if len(resp.Organizations) == 0 {
63+
return fmt.Errorf("Organization not found: %s", v)
64+
}
65+
if len(resp.Organizations) > 1 {
66+
return fmt.Errorf("More than one matching organization found")
67+
}
68+
69+
organization = resp.Organizations[0]
70+
} else if v, ok := d.GetOk("organization"); ok {
71+
resp, err := config.clientResourceManager.Organizations.Get(canonicalOrganizationName(v.(string))).Do()
72+
if err != nil {
73+
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound {
74+
return fmt.Errorf("Organization not found: %s", v)
75+
}
76+
77+
return fmt.Errorf("Error reading organization: %s", err)
78+
}
79+
80+
organization = resp
81+
} else {
82+
return fmt.Errorf("one of domain or organization must be set")
83+
}
84+
85+
d.SetId(GetResourceNameFromSelfLink(organization.Name))
86+
d.Set("name", organization.Name)
87+
d.Set("domain", organization.DisplayName)
88+
d.Set("create_time", organization.CreationTime)
89+
d.Set("lifecycle_state", organization.LifecycleState)
90+
if organization.Owner != nil {
91+
d.Set("directory_customer_id", organization.Owner.DirectoryCustomerId)
92+
}
93+
94+
return nil
95+
}
96+
97+
func canonicalOrganizationName(ba string) string {
98+
if strings.HasPrefix(ba, "organizations/") {
99+
return ba
100+
}
101+
102+
return "organizations/" + ba
103+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package google
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform/helper/acctest"
9+
"github.com/hashicorp/terraform/helper/resource"
10+
)
11+
12+
func TestAccDataSourceGoogleOrganization_byFullName(t *testing.T) {
13+
orgId := getTestOrgFromEnv(t)
14+
name := "organizations/" + orgId
15+
16+
resource.Test(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccCheckGoogleOrganization_byName(name),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttr("data.google_organization.org", "id", orgId),
24+
resource.TestCheckResourceAttr("data.google_organization.org", "name", name),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func TestAccDataSourceGoogleOrganization_byShortName(t *testing.T) {
32+
orgId := getTestOrgFromEnv(t)
33+
name := "organizations/" + orgId
34+
35+
resource.Test(t, resource.TestCase{
36+
PreCheck: func() { testAccPreCheck(t) },
37+
Providers: testAccProviders,
38+
Steps: []resource.TestStep{
39+
{
40+
Config: testAccCheckGoogleOrganization_byName(orgId),
41+
Check: resource.ComposeTestCheckFunc(
42+
resource.TestCheckResourceAttr("data.google_organization.org", "id", orgId),
43+
resource.TestCheckResourceAttr("data.google_organization.org", "name", name),
44+
),
45+
},
46+
},
47+
})
48+
}
49+
50+
func TestAccDataSourceGoogleOrganization_byDomain(t *testing.T) {
51+
name := acctest.RandString(16) + ".com"
52+
53+
resource.Test(t, resource.TestCase{
54+
PreCheck: func() { testAccPreCheck(t) },
55+
Providers: testAccProviders,
56+
Steps: []resource.TestStep{
57+
{
58+
Config: testAccCheckGoogleOrganization_byDomain(name),
59+
ExpectError: regexp.MustCompile("Organization not found: " + name),
60+
},
61+
},
62+
})
63+
}
64+
65+
func testAccCheckGoogleOrganization_byName(name string) string {
66+
return fmt.Sprintf(`
67+
data "google_organization" "org" {
68+
organization = "%s"
69+
}`, name)
70+
}
71+
72+
func testAccCheckGoogleOrganization_byDomain(name string) string {
73+
return fmt.Sprintf(`
74+
data "google_organization" "org" {
75+
domain = "%s"
76+
}`, name)
77+
}

google/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func Provider() terraform.ResourceProvider {
7777
"google_active_folder": dataSourceGoogleActiveFolder(),
7878
"google_iam_policy": dataSourceGoogleIamPolicy(),
7979
"google_kms_secret": dataSourceGoogleKmsSecret(),
80+
"google_organization": dataSourceGoogleOrganization(),
8081
"google_storage_object_signed_url": dataSourceGoogleSignedUrl(),
8182
},
8283

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
layout: "google"
3+
page_title: "Google: google_organization"
4+
sidebar_current: "docs-google-datasource-organization"
5+
description: |-
6+
Get information about a Google Cloud Organization.
7+
---
8+
9+
# google\_organization
10+
11+
Use this data source to get information about a Google Cloud Organization.
12+
13+
```hcl
14+
data "google_organization" "org" {
15+
domain = "example.com"
16+
}
17+
18+
resource "google_folder" "sales" {
19+
display_name = "Sales"
20+
parent = "${data.google_organization.org.name}"
21+
}
22+
```
23+
24+
## Argument Reference
25+
26+
The arguments of this data source act as filters for querying the available Organizations.
27+
The given filters must match exactly one Organizations whose data will be exported as attributes.
28+
The following arguments are supported:
29+
30+
* `organization` (Optional) - The name of the Organization in the form `{organization_id}` or `organizations/{organization_id}`.
31+
* `domain` (Optional) - The domain name of the Organization.
32+
33+
~> **NOTE:** One of `organization` or `domain` must be specified.
34+
35+
## Attributes Reference
36+
37+
The following additional attributes are exported:
38+
39+
* `id` - The Organization ID.
40+
* `name` - The resource name of the Organization in the form `organizations/{organization_id}`.
41+
* `directory_customer_id` - The Google for Work customer ID of the Organization.
42+
* `create_time` - Timestamp when the Organization was created. A timestamp in RFC3339 UTC "Zulu" format, accurate to nanoseconds. Example: "2014-10-02T15:01:23.045123456Z".
43+
* `lifecycle_state` - The Organization's current lifecycle state.

website/google.erb

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@
6464
<li<%= sidebar_current("docs-google-kms-secret") %>>
6565
<a href="/docs/providers/google/d/google_kms_secret.html">google_kms_secret</a>
6666
</li>
67+
<li<%= sidebar_current("docs-google-datasource-organization") %>>
68+
<a href="/docs/providers/google/d/google_organization.html">google_organization</a>
69+
</li>
6770
<li<%= sidebar_current("docs-google-datasource-signed_url") %>>
6871
<a href="/docs/providers/google/d/signed_url.html">google_storage_object_signed_url</a>
6972
</li>

0 commit comments

Comments
 (0)