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

Escape the display name in active folder data source (in case of spaces, etc) #1261

Merged
merged 7 commits into from
Apr 9, 2018
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
17 changes: 9 additions & 8 deletions google/data_source_google_active_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"net/url"

"github.com/hashicorp/terraform/helper/schema"
resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1"
Expand Down Expand Up @@ -34,7 +35,7 @@ func dataSourceGoogleActiveFolderRead(d *schema.ResourceData, meta interface{})
parent := d.Get("parent").(string)
displayName := d.Get("display_name").(string)

queryString := fmt.Sprintf("lifecycleState=ACTIVE AND parent=%s AND displayName=%s", parent, displayName)
queryString := fmt.Sprintf("lifecycleState=ACTIVE AND parent=%s AND displayName=%s", parent, url.QueryEscape(displayName))
searchRequest := &resourceManagerV2Beta1.SearchFoldersRequest{
Query: queryString,
}
Expand All @@ -43,12 +44,12 @@ func dataSourceGoogleActiveFolderRead(d *schema.ResourceData, meta interface{})
return handleNotFoundError(err, d, fmt.Sprintf("Folder Not Found : %s", displayName))
}

folders := searchResponse.Folders
if len(folders) != 1 {
return fmt.Errorf("More than one folder found")
for _, folder := range searchResponse.Folders {
if folder.DisplayName == displayName {
d.SetId(folder.Name)
d.Set("name", folder.Name)
return nil
}
}

d.SetId(folders[0].Name)
d.Set("name", folders[0].Name)
return nil
return fmt.Errorf("Folder not found")
}
22 changes: 21 additions & 1 deletion google/data_source_google_active_folder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccDataSourceGoogleActiveFolder(t *testing.T) {
func TestAccDataSourceGoogleActiveFolder_default(t *testing.T) {
org := getTestOrgFromEnv(t)

parent := fmt.Sprintf("organizations/%s", org)
Expand All @@ -29,6 +29,26 @@ func TestAccDataSourceGoogleActiveFolder(t *testing.T) {
})
}

func TestAccDataSourceGoogleActiveFolder_space(t *testing.T) {
org := getTestOrgFromEnv(t)

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]
Expand Down