diff --git a/third_party/terraform/data_sources/data_source_google_folder_organization_policy.go b/third_party/terraform/data_sources/data_source_google_folder_organization_policy.go new file mode 100644 index 000000000000..3d976c97cc42 --- /dev/null +++ b/third_party/terraform/data_sources/data_source_google_folder_organization_policy.go @@ -0,0 +1,27 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGoogleFolderOrganizationPolicy() *schema.Resource { + // Generate datasource schema from resource + dsSchema := datasourceSchemaFromResourceSchema(resourceGoogleFolderOrganizationPolicy().Schema) + + addRequiredFieldsToSchema(dsSchema, "folder") + addRequiredFieldsToSchema(dsSchema, "constraint") + + return &schema.Resource{ + Read: datasourceGoogleFolderOrganizationPolicyRead, + Schema: dsSchema, + } +} + +func datasourceGoogleFolderOrganizationPolicyRead(d *schema.ResourceData, meta interface{}) error { + + d.SetId(fmt.Sprintf("%s:%s", d.Get("folder"), d.Get("constraint"))) + + return resourceGoogleFolderOrganizationPolicyRead(d, meta) +} diff --git a/third_party/terraform/data_sources/data_source_google_project_organization_policy.go b/third_party/terraform/data_sources/data_source_google_project_organization_policy.go new file mode 100644 index 000000000000..3e472bec5d42 --- /dev/null +++ b/third_party/terraform/data_sources/data_source_google_project_organization_policy.go @@ -0,0 +1,27 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceGoogleProjectOrganizationPolicy() *schema.Resource { + // Generate datasource schema from resource + dsSchema := datasourceSchemaFromResourceSchema(resourceGoogleProjectOrganizationPolicy().Schema) + + addRequiredFieldsToSchema(dsSchema, "project") + addRequiredFieldsToSchema(dsSchema, "constraint") + + return &schema.Resource{ + Read: datasourceGoogleProjectOrganizationPolicyRead, + Schema: dsSchema, + } +} + +func datasourceGoogleProjectOrganizationPolicyRead(d *schema.ResourceData, meta interface{}) error { + + d.SetId(fmt.Sprintf("%s:%s", d.Get("project"), d.Get("constraint"))) + + return resourceGoogleProjectOrganizationPolicyRead(d, meta) +} diff --git a/third_party/terraform/tests/data_source_google_folder_organization_policy_test.go b/third_party/terraform/tests/data_source_google_folder_organization_policy_test.go new file mode 100644 index 000000000000..7a631d1d7f7c --- /dev/null +++ b/third_party/terraform/tests/data_source_google_folder_organization_policy_test.go @@ -0,0 +1,91 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceGoogleFolderOrganizationPolicy_basic(t *testing.T) { + folder := acctest.RandomWithPrefix("tf-test") + org := getTestOrgFromEnv(t) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleFolderOrganizationPolicy_basic(org, folder), + Check: testAccDataSourceGoogleOrganizationPolicyCheck( + "data.google_folder_organization_policy.data", + "google_folder_organization_policy.resource"), + }, + }, + }) +} + +func testAccDataSourceGoogleOrganizationPolicyCheck(dataSourceName string, resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + ds, ok := s.RootModule().Resources[dataSourceName] + if !ok { + return fmt.Errorf("root module has no resource called %s", dataSourceName) + } + + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("can't find %s in state", resourceName) + } + + dsAttr := ds.Primary.Attributes + rsAttr := rs.Primary.Attributes + + cloudFuncAttrToCheck := []string{ + "name", + "folder", + "constraint", + "version", + "list_policy", + "restore_policy", + "boolean_policy", + } + + for _, attr := range cloudFuncAttrToCheck { + if dsAttr[attr] != rsAttr[attr] { + return fmt.Errorf( + "%s is %s; want %s", + attr, + dsAttr[attr], + rsAttr[attr], + ) + } + } + + return nil + } +} + +func testAccDataSourceGoogleFolderOrganizationPolicy_basic(org, folder string) string { + return fmt.Sprintf(` +resource "google_folder" "orgpolicy" { + display_name = "%s" + parent = "%s" +} + +resource "google_folder_organization_policy" "resource" { + folder = "${google_folder.orgpolicy.name}" + constraint = "serviceuser.services" + + restore_policy { + default = true + } +} + +data "google_folder_organization_policy" "data" { + folder = "${google_folder.orgpolicy.name}" + constraint = "serviceuser.services" +} + `, folder, "organizations/"+org) +} diff --git a/third_party/terraform/tests/data_source_google_project_organization_policy_test.go b/third_party/terraform/tests/data_source_google_project_organization_policy_test.go new file mode 100644 index 000000000000..d4113b82bdc0 --- /dev/null +++ b/third_party/terraform/tests/data_source_google_project_organization_policy_test.go @@ -0,0 +1,47 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceGoogleProjectOrganizationPolicy_basic(t *testing.T) { + project := getTestProjectFromEnv() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleProjectOrganizationPolicy_basic(project), + Check: testAccDataSourceGoogleOrganizationPolicyCheck( + "data.google_project_organization_policy.data", + "google_project_organization_policy.resource"), + }, + }, + }) +} + +func testAccDataSourceGoogleProjectOrganizationPolicy_basic(project string) string { + return fmt.Sprintf(` + + +resource "google_project_organization_policy" "resource" { + project = "%s" + constraint = "constraints/compute.trustedImageProjects" + + list_policy { + allow { + all = true + } + } +} + +data "google_project_organization_policy" "data" { + project = "%s" + constraint = "constraints/compute.trustedImageProjects" +} + `, project, project) +} diff --git a/third_party/terraform/utils/provider.go.erb b/third_party/terraform/utils/provider.go.erb index d960b2c2203f..98c12e1d2ac1 100644 --- a/third_party/terraform/utils/provider.go.erb +++ b/third_party/terraform/utils/provider.go.erb @@ -112,10 +112,12 @@ func Provider() terraform.ResourceProvider { "google_kms_key_ring": dataSourceGoogleKmsKeyRing(), "google_kms_crypto_key": dataSourceGoogleKmsCryptoKey(), "google_folder": dataSourceGoogleFolder(), + "google_folder_organization_policy": dataSourceGoogleFolderOrganizationPolicy(), "google_netblock_ip_ranges": dataSourceGoogleNetblockIpRanges(), "google_organization": dataSourceGoogleOrganization(), "google_project": dataSourceGoogleProject(), "google_projects": dataSourceGoogleProjects(), + "google_project_organization_policy": dataSourceGoogleProjectOrganizationPolicy(), "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 ae7be03f7cc6..e24acec0f393 100644 --- a/third_party/terraform/website-compiled/google.erb +++ b/third_party/terraform/website-compiled/google.erb @@ -83,6 +83,9 @@