From c4b07cc5cf021d5ae3033aa49a19d72c479cc76b Mon Sep 17 00:00:00 2001 From: Nataliia Godec Date: Tue, 13 Oct 2020 14:58:29 +0100 Subject: [PATCH] Allow data lookup on google_active_folder without providing a parent Google folder lookup API finds folders perfectly fine without providing the folder parent: ``` "query": "lifecycleState=ACTIVE AND displayName=\"Engineering\"" { "folders": [ { "name": "folders/", "parent": "organizations/", "displayName": "Engineering", "lifecycleState": "ACTIVE", "createTime": "2019-06-14T09:20:35.233Z" } ] } "query": "lifecycleState=ACTIVE AND displayName=\"Global Dev\"" Results returned just fine: { "folders": [ { "name": "folders/", "parent": "folders/", "displayName": "Global Dev", "lifecycleState": "ACTIVE", "createTime": "2020-06-16T11:41:45.998Z" } ] } ``` --- google/data_source_google_active_folder.go | 12 +++++-- .../data_source_google_active_folder_test.go | 33 +++++++++++++++++++ website/docs/d/active_folder.html.markdown | 20 ++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) diff --git a/google/data_source_google_active_folder.go b/google/data_source_google_active_folder.go index fc0a3f2c3b2..74c58b29531 100644 --- a/google/data_source_google_active_folder.go +++ b/google/data_source_google_active_folder.go @@ -14,7 +14,7 @@ func dataSourceGoogleActiveFolder() *schema.Resource { Schema: map[string]*schema.Schema{ "parent": { Type: schema.TypeString, - Required: true, + Optional: true, }, "display_name": { Type: schema.TypeString, @@ -35,10 +35,16 @@ func dataSourceGoogleActiveFolderRead(d *schema.ResourceData, meta interface{}) return err } - parent := d.Get("parent").(string) displayName := d.Get("display_name").(string) + queryString := "" + + // parent is optional + if parent, ok := d.GetOk("parent"); ok { + queryString := fmt.Sprintf("lifecycleState=ACTIVE AND parent=%s AND displayName=\"%s\"", parent.(string), displayName) + } else { + queryString := fmt.Sprintf("lifecycleState=ACTIVE AND displayName=\"%s\"", displayName) + } - queryString := fmt.Sprintf("lifecycleState=ACTIVE AND parent=%s AND displayName=\"%s\"", parent, displayName) searchRequest := &resourceManagerV2Beta1.SearchFoldersRequest{ Query: queryString, } diff --git a/google/data_source_google_active_folder_test.go b/google/data_source_google_active_folder_test.go index 9a0e856d23c..7d040329ecc 100644 --- a/google/data_source_google_active_folder_test.go +++ b/google/data_source_google_active_folder_test.go @@ -98,6 +98,26 @@ func testAccDataSourceGoogleActiveFolderCheck(data_source_name string, resource_ } } +func TestAccDataSourceGoogleActiveFolder_no_parent(t *testing.T) { + org := getTestOrgFromEnv(t) + + parent := fmt.Sprintf("organizations/%s", org) + displayName := "terraform-test-" + randString(t, 10) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceGoogleActiveFolderConfig(parent, displayName), + Check: resource.ComposeTestCheckFunc( + testAccDataSourceGoogleActiveFolderCheck("data.google_active_folder.my_folder", "google_folder.foobar"), + ), + }, + }, + }) +} + func testAccDataSourceGoogleActiveFolderConfig(parent string, displayName string) string { return fmt.Sprintf(` resource "google_folder" "foobar" { @@ -111,3 +131,16 @@ data "google_active_folder" "my_folder" { } `, parent, displayName) } + +func testAccDataSourceGoogleActiveFolderConfig_no_parent(parent string, displayName string) string { + return fmt.Sprintf(` +resource "google_folder" "foobar" { + parent = "%s" + display_name = "%s" +} + +data "google_active_folder" "my_folder" { + display_name = google_folder.foobar.display_name +} +`, parent, displayName) +} diff --git a/website/docs/d/active_folder.html.markdown b/website/docs/d/active_folder.html.markdown index 9053e831fb9..3b0c2fcb65e 100644 --- a/website/docs/d/active_folder.html.markdown +++ b/website/docs/d/active_folder.html.markdown @@ -13,12 +13,30 @@ Get an active folder within GCP by `display_name` and `parent`. ## Example Usage +To find a folder by its name: + +```tf +data "google_active_folder" "department1" { + display_name = "Department 1" +} +``` + +To find a folder in the organization: + ```tf data "google_active_folder" "department1" { display_name = "Department 1" parent = "organizations/1234567" } ``` +To find a folder nested in another folder: + +```tf +data "google_active_folder" "department1" { + display_name = "Department 1" + parent = "folders/12345" +} +``` ## Argument Reference @@ -26,7 +44,7 @@ The following arguments are supported: * `display_name` - (Required) The folder's display name. -* `parent` - (Required) The resource name of the parent Folder or Organization. +* `parent` - (Optional) The resource id of the parent Folder or Organization. ## Attributes Reference