-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
rosbo
merged 5 commits into
hashicorp:master
from
ewbankkit:google_organization-data-source
Dec 22, 2017
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
49e4db1
Add 'google_organization' data source.
ewbankkit 07b08c0
Use 'GetResourceNameFromSelfLink'.
ewbankkit 4721be8
Remove 'resourcemanager_helpers'.
ewbankkit ecb382f
Use 'ConflictsWith' in schema.
ewbankkit 0e734ee
Add 'organization' argument and make 'name' an output-only attribute.
ewbankkit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see any usage for this method? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use: https://github.com/terraform-providers/terraform-provider-google/blob/master/google/self_link_helpers.go#L68
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed as suggested.