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

Add folder data source #738

Merged
merged 4 commits into from
Nov 15, 2017
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
59 changes: 59 additions & 0 deletions google/data_source_google_active_folder.go
Original file line number Diff line number Diff line change
@@ -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
}
74 changes: 74 additions & 0 deletions google/data_source_google_active_folder_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/datasource_folder.html.markdown
Original file line number Diff line number Diff line change
@@ -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.