diff --git a/google/data_source_google_active_folder.go b/google/data_source_google_active_folder.go new file mode 100644 index 00000000000..a3bdfdbc852 --- /dev/null +++ b/google/data_source_google_active_folder.go @@ -0,0 +1,59 @@ +package google + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1" + "google.golang.org/api/googleapi" +) + +func dataSourceGoogleActiveFolder() *schema.Resource { + return &schema.Resource{ + Read: dataSourceGoogleActiveFolderRead, + + Schema: map[string]*schema.Schema{ + "parent": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "display_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func dataSourceGoogleActiveFolderRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + parent := d.Get("parent").(string) + displayName := d.Get("display_name").(string) + + queryString := fmt.Sprintf("lifecycleState=ACTIVE AND parent=%s AND displayName=%s", parent, displayName) + searchRequest := &resourceManagerV2Beta1.SearchFoldersRequest{ + Query: queryString, + } + searchResponse, err := config.clientResourceManagerV2Beta1.Folders.Search(searchRequest).Do() + if err != nil { + if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { + return fmt.Errorf("Folder Not Found : %s", d.Get("name")) + } + + return fmt.Errorf("Error reading folders: %s", err) + } + + folders := searchResponse.Folders + if len(folders) != 1 { + return fmt.Errorf("More than one folder found") + } + + d.SetId(folders[0].Name) + d.Set("name", folders[0].Name) + return nil +} diff --git a/google/data_source_google_active_folder_test.go b/google/data_source_google_active_folder_test.go new file mode 100644 index 00000000000..319d282a6af --- /dev/null +++ b/google/data_source_google_active_folder_test.go @@ -0,0 +1,74 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccDataSourceGoogleActiveFolder(t *testing.T) { + skipIfEnvNotSet(t, "GOOGLE_ORG") + + parent := fmt.Sprintf("organizations/%s", org) + displayName := "terraform-test-" + acctest.RandString(10) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccDataSourceGoogleActiveFolderConfig(parent, displayName), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceGoogleActiveFolderCheck("data.google_active_folder.my_folder", "google_folder.foobar"), + ), + }, + }, + }) +} + +func testAccDataSourceGoogleActiveFolderCheck(data_source_name string, resource_name string) resource.TestCheckFunc { + return func(s *terraform.State) error { + ds, ok := s.RootModule().Resources[data_source_name] + if !ok { + return fmt.Errorf("root module has no resource called %s", data_source_name) + } + + rs, ok := s.RootModule().Resources[resource_name] + if !ok { + return fmt.Errorf("can't find %s in state", resource_name) + } + + ds_attr := ds.Primary.Attributes + rs_attr := rs.Primary.Attributes + folder_attrs_to_test := []string{"parent", "display_name", "name"} + + for _, attr_to_check := range folder_attrs_to_test { + if ds_attr[attr_to_check] != rs_attr[attr_to_check] { + return fmt.Errorf( + "%s is %s; want %s", + attr_to_check, + ds_attr[attr_to_check], + rs_attr[attr_to_check], + ) + } + } + return nil + } +} + +func testAccDataSourceGoogleActiveFolderConfig(parent string, displayName string) string { + return fmt.Sprintf(` +resource "google_folder" "foobar" { + parent = "%s" + display_name = "%s" +} + +data "google_active_folder" "my_folder" { + parent = "${google_folder.foobar.parent}" + display_name = "${google_folder.foobar.display_name}" +} +`, parent, displayName) +} diff --git a/google/provider.go b/google/provider.go index 7977baeba7b..0a6d20b5d37 100644 --- a/google/provider.go +++ b/google/provider.go @@ -57,6 +57,7 @@ func Provider() terraform.ResourceProvider { "google_compute_zones": dataSourceGoogleComputeZones(), "google_compute_instance_group": dataSourceGoogleComputeInstanceGroup(), "google_container_engine_versions": dataSourceGoogleContainerEngineVersions(), + "google_active_folder": dataSourceGoogleActiveFolder(), "google_iam_policy": dataSourceGoogleIamPolicy(), "google_storage_object_signed_url": dataSourceGoogleSignedUrl(), }, diff --git a/website/docs/d/datasource_folder.html.markdown b/website/docs/d/datasource_folder.html.markdown new file mode 100644 index 00000000000..caa6ca25fe5 --- /dev/null +++ b/website/docs/d/datasource_folder.html.markdown @@ -0,0 +1,39 @@ +--- +layout: "google" +page_title: "Google: google_active_folder" +sidebar_current: "docs-google-datasource-folder" +description: |- + Get a folder within GCP. +--- + +# google\_active\_folder + +Get a folder within GCP by `display_name` and `parent`. + +## Example Usage + +```tf +resource "google_folder" "new-folder" { + display_name = "new-folder" + parent = "folders/some-folder-id" +} + +data "google_active_folder" "new-folder" { + display_name = "${google_folder.new-folder.display_name}" + parent = "${google_folder.new-folder.parent}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `display_name` - (Required) The folder's display name. + +* `parent` - (Required) The resource name of the parent Folder or Organization. + +## Attributes Reference + +In addition to the arguments listed above, the following attributes are exported: + +* `name` - The resource name of the Folder. This uniquely identifies the folder.