Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'google_organization' data source #887

Merged
merged 5 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions google/data_source_google_organization.go
Original file line number Diff line number Diff line change
@@ -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])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as suggested.

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
}
34 changes: 34 additions & 0 deletions google/data_source_google_organization_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},

Expand Down
14 changes: 14 additions & 0 deletions google/resourcemanager_helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package google

import (
"fmt"

"google.golang.org/api/cloudresourcemanager/v1"
)

func getResourceName(resourceId *cloudresourcemanager.ResourceId) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any usage for this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this code must have come from something else I was noodling on. Removed those 2 source files.

if resourceId == nil {
return ""
}
return fmt.Sprintf("%s/%s", resourceId.Type, resourceId.Id)
}
32 changes: 32 additions & 0 deletions google/resourcemanager_helpers_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
42 changes: 42 additions & 0 deletions website/docs/d/google_organization.html.markdown
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 4 additions & 1 deletion website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@
<li<%= sidebar_current("docs-google-kms-secret") %>>
<a href="/docs/providers/google/d/google_kms_secret.html">google_kms_secret</a>
</li>
<li<%= sidebar_current("docs-google-datasource-organization") %>>
<a href="/docs/providers/google/d/google_organization.html">google_organization</a>
</li>
<li<%= sidebar_current("docs-google-datasource-signed_url") %>>
<a href="/docs/providers/google/d/signed_url.html">google_storage_object_signed_url</a>
</li>
Expand Down Expand Up @@ -136,7 +139,7 @@
</li>
<li<%= sidebar_current("docs-google-service-account") %>>
<a href="/docs/providers/google/r/google_service_account.html">google_service_account</a>
</li>
</li>
<li<%= sidebar_current("docs-google-service-account-iam") %>>
<a href="/docs/providers/google/r/google_service_account_iam.html">google_service_account_iam_binding</a>
</li>
Expand Down