Skip to content

Commit

Permalink
Escape the display name in active folder data source (in case of spac…
Browse files Browse the repository at this point in the history
…es, etc) (#1261)

* escape the folder name (in case of spaces, etc)

* add test case for folder with space

* add missing args

* make separate tests for each folder test, get folder name length under API limits

* further abstract out the resource name to prevent test collisions

* workaround multiple results returning for a given query by looping over return

* split test cases into separate funcs
  • Loading branch information
ts-tek authored and danawillow committed Apr 9, 2018
1 parent d79addb commit 024e0b7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
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

0 comments on commit 024e0b7

Please sign in to comment.