Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resourcemanager: adding "google_folders" data source #10261

Merged
merged 4 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions google/data_source_google_folders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package google

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceGoogleFolders() *schema.Resource {
return &schema.Resource{
Read: dataSourceGoogleFoldersRead,
Schema: map[string]*schema.Schema{
"parent_id": {
Type: schema.TypeString,
Required: true,
},
"folders": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},
"parent": {
Type: schema.TypeString,
Computed: true,
},
"display_name": {
Type: schema.TypeString,
Computed: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
},
"update_time": {
Type: schema.TypeString,
Computed: true,
},
"delete_time": {
Type: schema.TypeString,
Computed: true,
},
"etag": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}

func dataSourceGoogleFoldersRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}

params := make(map[string]string)
folders := make([]map[string]interface{}, 0)

for {
params["parent"] = d.Get("parent_id").(string)
url := "https://cloudresourcemanager.googleapis.com/v3/folders"

url, err := addQueryParams(url, params)
if err != nil {
return err
}

res, err := sendRequest(config, "GET", "", url, userAgent, nil)
if err != nil {
return fmt.Errorf("Error retrieving folders: %s", err)
}

pageFolders := flattenDataSourceGoogleFoldersList(res["folders"])
folders = append(folders, pageFolders...)

pToken, ok := res["nextPageToken"]
if ok && pToken != nil && pToken.(string) != "" {
params["pageToken"] = pToken.(string)
} else {
break
}
}

if err := d.Set("folders", folders); err != nil {
return fmt.Errorf("Error retrieving folders: %s", err)
}

d.SetId(d.Get("parent_id").(string))

return nil
}

func flattenDataSourceGoogleFoldersList(v interface{}) []map[string]interface{} {
if v == nil {
return make([]map[string]interface{}, 0)
}

ls := v.([]interface{})
folders := make([]map[string]interface{}, 0, len(ls))
for _, raw := range ls {
f := raw.(map[string]interface{})

var mState, mName, mCreateTime, mUpdateTime, mDeleteTime, mParent, mDisplayName, mEtag interface{}
if fName, ok := f["name"]; ok {
mName = fName
}
if fState, ok := f["state"]; ok {
mState = fState
}
if fCreateTime, ok := f["createTime"]; ok {
mCreateTime = fCreateTime
}
if fUpdateTime, ok := f["updateTime"]; ok {
mUpdateTime = fUpdateTime
}
if fDeleteTime, ok := f["deleteTime"]; ok {
mDeleteTime = fDeleteTime
}
if fParent, ok := f["parent"]; ok {
mParent = fParent
}
if fDisplayName, ok := f["displayName"]; ok {
mDisplayName = fDisplayName
}
if fEtag, ok := f["etag"]; ok {
mEtag = fEtag
}
folders = append(folders, map[string]interface{}{
"name": mName,
"state": mState,
"create_time": mCreateTime,
"update_time": mUpdateTime,
"delete_time": mDeleteTime,
"parent": mParent,
"display_name": mDisplayName,
"etag": mEtag,
})
}

return folders
}
50 changes: 50 additions & 0 deletions google/data_source_google_folders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package google

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceGoogleFolders_basic(t *testing.T) {
t.Parallel()

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: testAccCheckGoogleFoldersConfig(parent, displayName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.google_folders.root-test", "folders.0.name"),
resource.TestCheckResourceAttrSet("data.google_folders.root-test", "folders.0.display_name"),
resource.TestCheckResourceAttrSet("data.google_folders.root-test", "folders.0.state"),
resource.TestCheckResourceAttrSet("data.google_folders.root-test", "folders.0.parent"),
resource.TestCheckResourceAttrSet("data.google_folders.root-test", "folders.0.create_time"),
resource.TestCheckResourceAttrSet("data.google_folders.root-test", "folders.0.update_time"),
// resource.TestCheckResourceAttrSet("data.google_folders.root-test", "folders.0.delete_time"),
// deleteTime will only be set on a deleted folder
resource.TestCheckResourceAttrSet("data.google_folders.root-test", "folders.0.etag"),
),
},
},
})
}

func testAccCheckGoogleFoldersConfig(parent string, displayName string) string {
return fmt.Sprintf(`
resource "google_folder" "foobar" {
parent = "%s"
display_name = "%s"
}

data "google_folders" "root-test" {
parent_id = "%s"
}
`, parent, displayName, parent)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ func Provider() *schema.Provider {
"google_kms_secret": dataSourceGoogleKmsSecret(),
"google_kms_secret_ciphertext": dataSourceGoogleKmsSecretCiphertext(),
"google_folder": dataSourceGoogleFolder(),
"google_folders": dataSourceGoogleFolders(),
"google_folder_organization_policy": dataSourceGoogleFolderOrganizationPolicy(),
"google_monitoring_notification_channel": dataSourceMonitoringNotificationChannel(),
"google_monitoring_cluster_istio_service": dataSourceMonitoringServiceClusterIstio(),
Expand Down
51 changes: 51 additions & 0 deletions website/docs/d/folders.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
subcategory: "Cloud Platform"
layout: "google"
page_title: "Google: google_folders"
sidebar_current: "docs-google-datasource-folders"
description: |-
Retrieve a set of folders based on a parent ID.
---

# google\_folders

Retrieve information about a set of folders based on a parent ID. See the
mikelaramie marked this conversation as resolved.
Show resolved Hide resolved
[REST API](https://cloud.google.com/resource-manager/reference/rest/v3/folders/list)
for more details.

## Example Usage - searching for folders at the root of an org

```hcl
data "google_folders" "my-org-folders" {
parent_id = "organizations/${var.organization_id}"
}

data "google_folder" "first-folder" {
folder = data.google_folders.my-org-folders.folders[0].name
}
```

## Argument Reference

The following arguments are supported:

* `parent_id` - (Required) A string parent as defined in the [REST API](https://cloud.google.com/resource-manager/reference/rest/v3/folders/list#query-parameters).


## Attributes Reference

The following attributes are exported:

* `folders` - A list of projects matching the provided filter. Structure is defined below.

The `folders` block supports:

* `name` - The id of the folder
* `parent` - The parent id of the folder
* `display_name` - The display name of the folder
* `state` - The lifecycle state of the folder
* `create_time` - The timestamp of when the folder was created
* `update_time` - The timestamp of when the folder was last modified
* `delete_time` - The timestamp of when the folder was requested to be deleted (if applicable)
* `etag` - Entity tag identifier of the folder

4 changes: 4 additions & 0 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,10 @@
<a href="/docs/providers/google/d/folder_organization_policy.html">google_folder_organization_policy</a>
</li>

<li>
<a href="/docs/providers/google/d/folders.html">google_folders</a>
</li>

<li>
<a href="/docs/providers/google/d/iam_policy.html">google_iam_policy</a>
</li>
Expand Down