diff --git a/build/terraform b/build/terraform index 4f7decf1e0ce..5e77548407a0 160000 --- a/build/terraform +++ b/build/terraform @@ -1 +1 @@ -Subproject commit 4f7decf1e0ceb9eb2efd2b4929b3eeb63f15a938 +Subproject commit 5e77548407a0b172cf7b70d0983f26168ec9c655 diff --git a/build/terraform-beta b/build/terraform-beta index 54a56b5c2cae..df945d316084 160000 --- a/build/terraform-beta +++ b/build/terraform-beta @@ -1 +1 @@ -Subproject commit 54a56b5c2cae3a272e2150112b107edd4daa6471 +Subproject commit df945d3160847a182463b2d16606f2d1d0a88edd diff --git a/third_party/terraform/data_sources/data_source_google_projects.go b/third_party/terraform/data_sources/data_source_google_projects.go new file mode 100644 index 000000000000..b4eba543c5bf --- /dev/null +++ b/third_party/terraform/data_sources/data_source_google_projects.go @@ -0,0 +1,78 @@ +package google + +import ( + "fmt" + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGoogleProjects() *schema.Resource { + return &schema.Resource{ + Read: datasourceGoogleProjectsRead, + Schema: map[string]*schema.Schema{ + "filter": { + Type: schema.TypeString, + Required: true, + }, + "projects": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "project_id": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func datasourceGoogleProjectsRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + params := make(map[string]string) + + params["filter"] = d.Get("filter").(string) + url := "https://cloudresourcemanager.googleapis.com/v1/projects" + + url, err := addQueryParams(url, params) + if err != nil { + return err + } + + res, err := sendRequest(config, "GET", url, nil) + if err != nil { + return fmt.Errorf("Error retrieving projects: %s", err) + } + + if err := d.Set("projects", flattenDatasourceGoogleProjectsProjects(res["projects"], d)); err != nil { + return fmt.Errorf("Error retrieving projects: %s", err) + } + + d.SetId(d.Get("filter").(string)) + + return nil +} + +func flattenDatasourceGoogleProjectsProjects(v interface{}, d *schema.ResourceData) interface{} { + if v == nil { + return v + } + + l := v.([]interface{}) + transformed := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + if len(original) < 1 { + // Do not include empty json objects coming back from the api + continue + } + transformed = append(transformed, map[string]interface{}{ + "project_id": original["projectId"], + }) + } + + return transformed +} diff --git a/third_party/terraform/tests/data_source_google_projects_test.go b/third_party/terraform/tests/data_source_google_projects_test.go new file mode 100644 index 000000000000..d71562bcc3e4 --- /dev/null +++ b/third_party/terraform/tests/data_source_google_projects_test.go @@ -0,0 +1,36 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceGoogleProjects_basic(t *testing.T) { + t.Parallel() + + project := getTestProjectFromEnv() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckGoogleProjectsConfig(project), + Check: resource.ComposeTestCheckFunc( + // We can't guarantee no project won't have our project ID as a prefix, so we'll check set-ness rather than correctness + resource.TestCheckResourceAttrSet("data.google_projects.my-project", "projects.0.project_id"), + ), + }, + }, + }) +} + +func testAccCheckGoogleProjectsConfig(project string) string { + return fmt.Sprintf(` +data "google_projects" "my-project" { + filter = "projectId:%s" +} +`, project) +} diff --git a/third_party/terraform/utils/provider.go.erb b/third_party/terraform/utils/provider.go.erb index efb81b97b83c..b648f2610767 100644 --- a/third_party/terraform/utils/provider.go.erb +++ b/third_party/terraform/utils/provider.go.erb @@ -115,6 +115,7 @@ func Provider() terraform.ResourceProvider { "google_netblock_ip_ranges": dataSourceGoogleNetblockIpRanges(), "google_organization": dataSourceGoogleOrganization(), "google_project": dataSourceGoogleProject(), + "google_projects": dataSourceGoogleProjects(), "google_project_services": dataSourceGoogleProjectServices(), "google_service_account": dataSourceGoogleServiceAccount(), "google_service_account_key": dataSourceGoogleServiceAccountKey(), diff --git a/third_party/terraform/website-compiled/google.erb b/third_party/terraform/website-compiled/google.erb index 9d8ef0f82d3d..7efd6f93bd64 100644 --- a/third_party/terraform/website-compiled/google.erb +++ b/third_party/terraform/website-compiled/google.erb @@ -143,6 +143,9 @@