-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 dataSourceGoogleFolder() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceGoogleFolderRead, | ||
|
||
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 dataSourceGoogleFolderRead(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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you rename the data source to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do, that makes a lot of sense. |
||
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccDataSourceGoogleFolder(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And all these methods here too |
||
skipIfEnvNotSet(t, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this can be simplified to |
||
[]string{ | ||
"GOOGLE_ORG", | ||
}..., | ||
) | ||
|
||
parent := fmt.Sprintf("organizations/%s", os.Getenv("GOOGLE_ORG")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use the global test variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you point me to the right place? Where does that live? I don't seem to be able to find it. Update: no need, just found it. |
||
displayName := "terraform-test-" + acctest.RandString(10) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccDataSourceGoogleFolderConfig(parent, displayName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccDataSourceGoogleFolderCheck("data.google_folder.my_folder", "google_folder.foobar"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceGoogleFolderCheck(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 testAccDataSourceGoogleFolderConfig(parent string, displayName string) string { | ||
return fmt.Sprintf(` | ||
resource "google_folder" "foobar" { | ||
parent = "%s" | ||
display_name = "%s" | ||
} | ||
|
||
data "google_folder" "my_folder" { | ||
parent = "${google_folder.foobar.parent}" | ||
display_name = "${google_folder.foobar.display_name}" | ||
} | ||
`, parent, displayName) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
layout: "google" | ||
page_title: "Google: google_folder" | ||
sidebar_current: "docs-google-datasource-folder" | ||
description: |- | ||
Get a folder within GCP. | ||
--- | ||
|
||
# google\_folder | ||
|
||
Get a folder within GCP by display_name and parent. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add tick around |
||
|
||
## Example Usage | ||
|
||
```tf | ||
resource "google_folder" "new-folder" { | ||
display_name = "new-folder" | ||
parent = "folders/some-folder-id" | ||
} | ||
|
||
data "google_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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename this method to match the new resource name along with the read method