Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
emilymye committed Aug 5, 2019
1 parent 6ad0768 commit 3839e54
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
13 changes: 5 additions & 8 deletions third_party/terraform/data_sources/data_source_google_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import (
"strings"

"github.com/hashicorp/terraform/helper/schema"

resourceManagerV2Beta1 "google.golang.org/api/cloudresourcemanager/v2beta1"
)

func dataSourceGoogleFolder() *schema.Resource {
Expand Down Expand Up @@ -63,7 +61,7 @@ func dataSourceFolderRead(d *schema.ResourceData, meta interface{}) error {
}

if v, ok := d.GetOk("lookup_organization"); ok && v.(bool) {
organization, err := lookupOrganizationName(folder, config)
organization, err := lookupOrganizationName(d.Id(), d, config)
if err != nil {
return err
}
Expand All @@ -82,17 +80,16 @@ func canonicalFolderName(ba string) string {
return "folders/" + ba
}

func lookupOrganizationName(folder *resourceManagerV2Beta1.Folder, config *Config) (string, error) {
parent := folder.Parent
func lookupOrganizationName(parent string, d *schema.ResourceData, config *Config) (string, error) {
if parent == "" || strings.HasPrefix(parent, "organizations/") {
return parent, nil
} else if strings.HasPrefix(parent, "folders/") {
parentFolder, err := getGoogleFolder(parent, config).Do()
parentFolder, err := getGoogleFolder(parent, d, config)
if err != nil {
return "", fmt.Errorf("Error getting parent folder '%s': %s", parent, err)
}
return lookupOrganizationName(parentFolder, config)
return lookupOrganizationName(parentFolder.Parent, d, config)
} else {
return "", fmt.Errorf("Unknown parent type '%s' on folder '%s'", parent, folder.Name)
return "", fmt.Errorf("Unknown parent type '%s' on folder '%s'", parent, d.Id())
}
}
8 changes: 5 additions & 3 deletions third_party/terraform/resources/resource_google_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func resourceGoogleFolderCreate(d *schema.ResourceData, meta interface{}) error
func resourceGoogleFolderRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

folder, err := getGoogleFolder(d, config)
folder, err := getGoogleFolder(d.Id(), d, config)
if err != nil {
return handleNotFoundError(err, d, d.Id())
}
Expand Down Expand Up @@ -200,11 +200,13 @@ func resourceGoogleFolderImportState(d *schema.ResourceData, m interface{}) ([]*
return []*schema.ResourceData{d}, nil
}

func getGoogleFolder(d *schema.ResourceData, config *Config) (*resourceManagerV2Beta1.Folder, error){
// Util to get a Folder resource from API. Note that folder described by name is not necessarily the
// ResourceData resource.
func getGoogleFolder(folderName string, d *schema.ResourceData, config *Config) (*resourceManagerV2Beta1.Folder, error){
var folder *resourceManagerV2Beta1.Folder
err := retryTimeDuration(func() error {
var reqErr error
folder, reqErr = config.clientResourceManagerV2Beta1.Folders.Get(d.Id()).Do()
folder, reqErr = config.clientResourceManagerV2Beta1.Folders.Get(folderName).Do()
return reqErr
}, d.Timeout(schema.TimeoutRead))
if err != nil {
Expand Down

0 comments on commit 3839e54

Please sign in to comment.