Skip to content

Commit

Permalink
Allow data lookup on google_active_folder without providing a parent
Browse files Browse the repository at this point in the history
Google folder lookup API finds folders perfectly fine without providing the folder parent:

```
"query": "lifecycleState=ACTIVE AND displayName=\"Engineering\""

{
  "folders": [
    {
      "name": "folders/<Redacted>",
      "parent": "organizations/<Redacted>",
      "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/<Redacted>",
      "parent": "folders/<Redacted>",
      "displayName": "Global Dev",
      "lifecycleState": "ACTIVE",
      "createTime": "2020-06-16T11:41:45.998Z"
    }
  ]
}
```
  • Loading branch information
Nataliia Godec committed Oct 15, 2020
1 parent 71c2368 commit c4b07cc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
12 changes: 9 additions & 3 deletions google/data_source_google_active_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
Expand Down
33 changes: 33 additions & 0 deletions google/data_source_google_active_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand All @@ -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)
}
20 changes: 19 additions & 1 deletion website/docs/d/active_folder.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,38 @@ 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

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

Expand Down

0 comments on commit c4b07cc

Please sign in to comment.