From 49e4db18c063e4026e62b7fe84e00043c2fdbd02 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 6 Oct 2017 10:25:20 -0400 Subject: [PATCH 1/5] Add 'google_organization' data source. --- google/data_source_google_organization.go | 99 +++++++++++++++++++ .../data_source_google_organization_test.go | 34 +++++++ google/provider.go | 1 + google/resourcemanager_helpers.go | 14 +++ google/resourcemanager_helpers_test.go | 32 ++++++ .../docs/d/google_organization.html.markdown | 42 ++++++++ website/google.erb | 5 +- 7 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 google/data_source_google_organization.go create mode 100644 google/data_source_google_organization_test.go create mode 100644 google/resourcemanager_helpers.go create mode 100644 google/resourcemanager_helpers_test.go create mode 100644 website/docs/d/google_organization.html.markdown diff --git a/google/data_source_google_organization.go b/google/data_source_google_organization.go new file mode 100644 index 00000000000..8d288a2a1f5 --- /dev/null +++ b/google/data_source_google_organization.go @@ -0,0 +1,99 @@ +package google + +import ( + "fmt" + "net/http" + "strings" + + "github.com/hashicorp/terraform/helper/schema" + + "google.golang.org/api/cloudresourcemanager/v1" + "google.golang.org/api/googleapi" +) + +func dataSourceGoogleOrganization() *schema.Resource { + return &schema.Resource{ + Read: dataSourceOrganizationRead, + Schema: map[string]*schema.Schema{ + "domain": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "directory_customer_id": { + Type: schema.TypeString, + Computed: true, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "lifecycle_state": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + domain, domainOk := d.GetOk("domain") + name, nameOk := d.GetOk("name") + if domainOk == nameOk { + return fmt.Errorf("One of ['domain', 'name'] must be set to read organizations") + } + + var organization *cloudresourcemanager.Organization + if domainOk { + filter := fmt.Sprintf("domain=%s", domain.(string)) + resp, err := config.clientResourceManager.Organizations.Search(&cloudresourcemanager.SearchOrganizationsRequest{ + Filter: filter, + }).Do() + if err != nil { + return fmt.Errorf("Error reading organization: %s", err) + } + + if len(resp.Organizations) == 0 { + return fmt.Errorf("Organization not found: %s", domain) + } + if len(resp.Organizations) > 1 { + return fmt.Errorf("More than one matching organization found") + } + + organization = resp.Organizations[0] + } else { + resp, err := config.clientResourceManager.Organizations.Get(name.(string)).Do() + if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound { + return fmt.Errorf("Organization not found: %s", name) + } + + return fmt.Errorf("Error reading organization: %s", err) + } + + organization = resp + } + + parts := strings.Split(organization.Name, "/") + if len(parts) != 2 { + return fmt.Errorf("Invalid organization name. Expecting organizations/{organization_id}") + } + + d.SetId(parts[1]) + d.Set("name", organization.Name) + d.Set("domain", organization.DisplayName) + d.Set("create_time", organization.CreationTime) + d.Set("lifecycle_state", organization.LifecycleState) + if organization.Owner != nil { + d.Set("directory_customer_id", organization.Owner.DirectoryCustomerId) + } + + return nil +} diff --git a/google/data_source_google_organization_test.go b/google/data_source_google_organization_test.go new file mode 100644 index 00000000000..f4f582fdc15 --- /dev/null +++ b/google/data_source_google_organization_test.go @@ -0,0 +1,34 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceGoogleOrganization_basic(t *testing.T) { + orgId := getTestOrgFromEnv(t) + name := "organizations/" + orgId + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleOrganization_basic(name), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.google_organization.org", "id", orgId), + resource.TestCheckResourceAttr("data.google_organization.org", "name", name), + ), + }, + }, + }) +} + +func testAccCheckGoogleOrganization_basic(name string) string { + return fmt.Sprintf(` +data "google_organization" "org" { + name = "%s" +}`, name) +} diff --git a/google/provider.go b/google/provider.go index d3a47efd96f..b7c002f1a74 100644 --- a/google/provider.go +++ b/google/provider.go @@ -76,6 +76,7 @@ func Provider() terraform.ResourceProvider { "google_active_folder": dataSourceGoogleActiveFolder(), "google_iam_policy": dataSourceGoogleIamPolicy(), "google_kms_secret": dataSourceGoogleKmsSecret(), + "google_organization": dataSourceGoogleOrganization(), "google_storage_object_signed_url": dataSourceGoogleSignedUrl(), }, diff --git a/google/resourcemanager_helpers.go b/google/resourcemanager_helpers.go new file mode 100644 index 00000000000..003a3c095f7 --- /dev/null +++ b/google/resourcemanager_helpers.go @@ -0,0 +1,14 @@ +package google + +import ( + "fmt" + + "google.golang.org/api/cloudresourcemanager/v1" +) + +func getResourceName(resourceId *cloudresourcemanager.ResourceId) string { + if resourceId == nil { + return "" + } + return fmt.Sprintf("%s/%s", resourceId.Type, resourceId.Id) +} diff --git a/google/resourcemanager_helpers_test.go b/google/resourcemanager_helpers_test.go new file mode 100644 index 00000000000..ecc908cf1ab --- /dev/null +++ b/google/resourcemanager_helpers_test.go @@ -0,0 +1,32 @@ +package google + +import ( + "testing" + + "google.golang.org/api/cloudresourcemanager/v1" +) + +func TestGetResourceName(t *testing.T) { + cases := map[string]struct { + ResourceId *cloudresourcemanager.ResourceId + ExpectedResourceName string + }{ + "nil resource ID": { + ResourceId: nil, + ExpectedResourceName: "", + }, + "valid resource ID": { + ResourceId: &cloudresourcemanager.ResourceId{ + Type: "project", + Id: "abcd1234", + }, + ExpectedResourceName: "project/abcd1234", + }, + } + + for tn, tc := range cases { + if rn := getResourceName(tc.ResourceId); rn != tc.ExpectedResourceName { + t.Fatalf("bad: %s, expected resource name to be '%s' but got '%s'", tn, tc.ExpectedResourceName, rn) + } + } +} diff --git a/website/docs/d/google_organization.html.markdown b/website/docs/d/google_organization.html.markdown new file mode 100644 index 00000000000..a809173337f --- /dev/null +++ b/website/docs/d/google_organization.html.markdown @@ -0,0 +1,42 @@ +--- +layout: "google" +page_title: "Google: google_organization" +sidebar_current: "docs-google-datasource-organization" +description: |- + Get information about a Google Cloud Organization. +--- + +# google\_organization + +Use this data source to get information about a Google Cloud Organization. + +```hcl +data "google_organization" "org" { + domain = "example.com" +} + +resource "google_folder" "sales" { + display_name = "Sales" + parent = "${data.google_organization.org.name}" +} +``` + +## Argument Reference + +The arguments of this data source act as filters for querying the available Organizations. +The given filters must match exactly one Organizations whose data will be exported as attributes. +The following arguments are supported: + +* `name` (Optional) - The resource name of the Organization in the form `organizations/{organization_id}`. +* `domain` (Optional) - The domain name of the Organization. + +~> **NOTE:** One of `name` or `domain` must be specified. + +## Attributes Reference + +The following additional attributes are exported: + +* `id` - The Organization ID. +* `directory_customer_id` - The Google for Work customer ID of the Organization. +* `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". +* `lifecycle_state` - The Organization's current lifecycle state. diff --git a/website/google.erb b/website/google.erb index 677bfb41fde..aeef058c7eb 100644 --- a/website/google.erb +++ b/website/google.erb @@ -61,6 +61,9 @@ > google_kms_secret + > + google_organization + > google_storage_object_signed_url @@ -136,7 +139,7 @@ > google_service_account - + > google_service_account_iam_binding From 07b08c0109a2a69dead6b50866189442f6362243 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 21 Dec 2017 11:36:27 -0500 Subject: [PATCH 2/5] Use 'GetResourceNameFromSelfLink'. --- google/data_source_google_organization.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/google/data_source_google_organization.go b/google/data_source_google_organization.go index 8d288a2a1f5..562babeb341 100644 --- a/google/data_source_google_organization.go +++ b/google/data_source_google_organization.go @@ -3,7 +3,6 @@ package google import ( "fmt" "net/http" - "strings" "github.com/hashicorp/terraform/helper/schema" @@ -81,12 +80,7 @@ func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error organization = resp } - parts := strings.Split(organization.Name, "/") - if len(parts) != 2 { - return fmt.Errorf("Invalid organization name. Expecting organizations/{organization_id}") - } - - d.SetId(parts[1]) + d.SetId(GetResourceNameFromSelfLink(organization.Name)) d.Set("name", organization.Name) d.Set("domain", organization.DisplayName) d.Set("create_time", organization.CreationTime) From 4721be8e9a0ad781d1197dc4148eb37b651afe1b Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 21 Dec 2017 11:43:38 -0500 Subject: [PATCH 3/5] Remove 'resourcemanager_helpers'. --- google/resourcemanager_helpers.go | 14 ----------- google/resourcemanager_helpers_test.go | 32 -------------------------- 2 files changed, 46 deletions(-) delete mode 100644 google/resourcemanager_helpers.go delete mode 100644 google/resourcemanager_helpers_test.go diff --git a/google/resourcemanager_helpers.go b/google/resourcemanager_helpers.go deleted file mode 100644 index 003a3c095f7..00000000000 --- a/google/resourcemanager_helpers.go +++ /dev/null @@ -1,14 +0,0 @@ -package google - -import ( - "fmt" - - "google.golang.org/api/cloudresourcemanager/v1" -) - -func getResourceName(resourceId *cloudresourcemanager.ResourceId) string { - if resourceId == nil { - return "" - } - return fmt.Sprintf("%s/%s", resourceId.Type, resourceId.Id) -} diff --git a/google/resourcemanager_helpers_test.go b/google/resourcemanager_helpers_test.go deleted file mode 100644 index ecc908cf1ab..00000000000 --- a/google/resourcemanager_helpers_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package google - -import ( - "testing" - - "google.golang.org/api/cloudresourcemanager/v1" -) - -func TestGetResourceName(t *testing.T) { - cases := map[string]struct { - ResourceId *cloudresourcemanager.ResourceId - ExpectedResourceName string - }{ - "nil resource ID": { - ResourceId: nil, - ExpectedResourceName: "", - }, - "valid resource ID": { - ResourceId: &cloudresourcemanager.ResourceId{ - Type: "project", - Id: "abcd1234", - }, - ExpectedResourceName: "project/abcd1234", - }, - } - - for tn, tc := range cases { - if rn := getResourceName(tc.ResourceId); rn != tc.ExpectedResourceName { - t.Fatalf("bad: %s, expected resource name to be '%s' but got '%s'", tn, tc.ExpectedResourceName, rn) - } - } -} From ecb382fb4be4915421a5231011b9375c0a02ee71 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 21 Dec 2017 14:26:36 -0500 Subject: [PATCH 4/5] Use 'ConflictsWith' in schema. --- google/data_source_google_organization.go | 34 +++++++++++------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/google/data_source_google_organization.go b/google/data_source_google_organization.go index 562babeb341..5124d2cbc6a 100644 --- a/google/data_source_google_organization.go +++ b/google/data_source_google_organization.go @@ -15,14 +15,16 @@ func dataSourceGoogleOrganization() *schema.Resource { Read: dataSourceOrganizationRead, Schema: map[string]*schema.Schema{ "domain": { - Type: schema.TypeString, - Optional: true, - Computed: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ConflictsWith: []string{"name"}, }, "name": { - Type: schema.TypeString, - Optional: true, - Computed: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ConflictsWith: []string{"domain"}, }, "directory_customer_id": { Type: schema.TypeString, @@ -43,15 +45,9 @@ func dataSourceGoogleOrganization() *schema.Resource { func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) - domain, domainOk := d.GetOk("domain") - name, nameOk := d.GetOk("name") - if domainOk == nameOk { - return fmt.Errorf("One of ['domain', 'name'] must be set to read organizations") - } - var organization *cloudresourcemanager.Organization - if domainOk { - filter := fmt.Sprintf("domain=%s", domain.(string)) + if v, ok := d.GetOk("domain"); ok { + filter := fmt.Sprintf("domain=%s", v.(string)) resp, err := config.clientResourceManager.Organizations.Search(&cloudresourcemanager.SearchOrganizationsRequest{ Filter: filter, }).Do() @@ -60,24 +56,26 @@ func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error } if len(resp.Organizations) == 0 { - return fmt.Errorf("Organization not found: %s", domain) + return fmt.Errorf("Organization not found: %s", v) } if len(resp.Organizations) > 1 { return fmt.Errorf("More than one matching organization found") } organization = resp.Organizations[0] - } else { - resp, err := config.clientResourceManager.Organizations.Get(name.(string)).Do() + } else if v, ok := d.GetOk("name"); ok { + resp, err := config.clientResourceManager.Organizations.Get(v.(string)).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound { - return fmt.Errorf("Organization not found: %s", name) + return fmt.Errorf("Organization not found: %s", v) } return fmt.Errorf("Error reading organization: %s", err) } organization = resp + } else { + return fmt.Errorf("one of domain or name must be set") } d.SetId(GetResourceNameFromSelfLink(organization.Name)) From 0e734eef423d057d91952412123d811de8e7f68d Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Thu, 21 Dec 2017 14:42:11 -0500 Subject: [PATCH 5/5] Add 'organization' argument and make 'name' an output-only attribute. --- google/data_source_google_organization.go | 24 ++++++--- .../data_source_google_organization_test.go | 51 +++++++++++++++++-- .../docs/d/google_organization.html.markdown | 5 +- 3 files changed, 68 insertions(+), 12 deletions(-) diff --git a/google/data_source_google_organization.go b/google/data_source_google_organization.go index 5124d2cbc6a..4ea15e97553 100644 --- a/google/data_source_google_organization.go +++ b/google/data_source_google_organization.go @@ -3,6 +3,7 @@ package google import ( "fmt" "net/http" + "strings" "github.com/hashicorp/terraform/helper/schema" @@ -18,14 +19,17 @@ func dataSourceGoogleOrganization() *schema.Resource { Type: schema.TypeString, Optional: true, Computed: true, - ConflictsWith: []string{"name"}, + ConflictsWith: []string{"organization"}, }, - "name": { + "organization": { Type: schema.TypeString, Optional: true, - Computed: true, ConflictsWith: []string{"domain"}, }, + "name": { + Type: schema.TypeString, + Computed: true, + }, "directory_customer_id": { Type: schema.TypeString, Computed: true, @@ -63,8 +67,8 @@ func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error } organization = resp.Organizations[0] - } else if v, ok := d.GetOk("name"); ok { - resp, err := config.clientResourceManager.Organizations.Get(v.(string)).Do() + } else if v, ok := d.GetOk("organization"); ok { + resp, err := config.clientResourceManager.Organizations.Get(canonicalOrganizationName(v.(string))).Do() if err != nil { if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound { return fmt.Errorf("Organization not found: %s", v) @@ -75,7 +79,7 @@ func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error organization = resp } else { - return fmt.Errorf("one of domain or name must be set") + return fmt.Errorf("one of domain or organization must be set") } d.SetId(GetResourceNameFromSelfLink(organization.Name)) @@ -89,3 +93,11 @@ func dataSourceOrganizationRead(d *schema.ResourceData, meta interface{}) error return nil } + +func canonicalOrganizationName(ba string) string { + if strings.HasPrefix(ba, "organizations/") { + return ba + } + + return "organizations/" + ba +} diff --git a/google/data_source_google_organization_test.go b/google/data_source_google_organization_test.go index f4f582fdc15..6c422794d8d 100644 --- a/google/data_source_google_organization_test.go +++ b/google/data_source_google_organization_test.go @@ -2,12 +2,14 @@ package google import ( "fmt" + "regexp" "testing" + "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" ) -func TestAccDataSourceGoogleOrganization_basic(t *testing.T) { +func TestAccDataSourceGoogleOrganization_byFullName(t *testing.T) { orgId := getTestOrgFromEnv(t) name := "organizations/" + orgId @@ -16,7 +18,7 @@ func TestAccDataSourceGoogleOrganization_basic(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccCheckGoogleOrganization_basic(name), + Config: testAccCheckGoogleOrganization_byName(name), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("data.google_organization.org", "id", orgId), resource.TestCheckResourceAttr("data.google_organization.org", "name", name), @@ -26,9 +28,50 @@ func TestAccDataSourceGoogleOrganization_basic(t *testing.T) { }) } -func testAccCheckGoogleOrganization_basic(name string) string { +func TestAccDataSourceGoogleOrganization_byShortName(t *testing.T) { + orgId := getTestOrgFromEnv(t) + name := "organizations/" + orgId + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleOrganization_byName(orgId), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.google_organization.org", "id", orgId), + resource.TestCheckResourceAttr("data.google_organization.org", "name", name), + ), + }, + }, + }) +} + +func TestAccDataSourceGoogleOrganization_byDomain(t *testing.T) { + name := acctest.RandString(16) + ".com" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleOrganization_byDomain(name), + ExpectError: regexp.MustCompile("Organization not found: " + name), + }, + }, + }) +} + +func testAccCheckGoogleOrganization_byName(name string) string { + return fmt.Sprintf(` +data "google_organization" "org" { + organization = "%s" +}`, name) +} + +func testAccCheckGoogleOrganization_byDomain(name string) string { return fmt.Sprintf(` data "google_organization" "org" { - name = "%s" + domain = "%s" }`, name) } diff --git a/website/docs/d/google_organization.html.markdown b/website/docs/d/google_organization.html.markdown index a809173337f..3f9d63721b7 100644 --- a/website/docs/d/google_organization.html.markdown +++ b/website/docs/d/google_organization.html.markdown @@ -27,16 +27,17 @@ The arguments of this data source act as filters for querying the available Orga The given filters must match exactly one Organizations whose data will be exported as attributes. The following arguments are supported: -* `name` (Optional) - The resource name of the Organization in the form `organizations/{organization_id}`. +* `organization` (Optional) - The name of the Organization in the form `{organization_id}` or `organizations/{organization_id}`. * `domain` (Optional) - The domain name of the Organization. -~> **NOTE:** One of `name` or `domain` must be specified. +~> **NOTE:** One of `organization` or `domain` must be specified. ## Attributes Reference The following additional attributes are exported: * `id` - The Organization ID. +* `name` - The resource name of the Organization in the form `organizations/{organization_id}`. * `directory_customer_id` - The Google for Work customer ID of the Organization. * `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". * `lifecycle_state` - The Organization's current lifecycle state.