-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Handle aggregatedList case for generated sweepers #2941
Changes from all 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 |
---|---|---|
|
@@ -13,6 +13,12 @@ import ( | |
<% | ||
sweeper_name = product_ns + object.name | ||
wrap_path = object&.nested_query&.keys&.first || object.collection_url_key | ||
listUrlTemplate = object.__product.base_url + object.base_url | ||
|
||
listUrlTemplate.sub! "zones/{{zone}}", "aggregated" | ||
aggregatedList = listUrlTemplate.include? "/aggregated/" | ||
|
||
deleteUrlTemplate = object.__product.base_url + object.delete_uri | ||
-%> | ||
|
||
func init() { | ||
|
@@ -39,7 +45,7 @@ func testSweep<%= sweeper_name -%>(region string) error { | |
return err | ||
} | ||
|
||
listTemplate := strings.Split("<%= object.__product.base_url + object.base_url -%>","?")[0] | ||
listTemplate := strings.Split("<%= listUrlTemplate -%>","?")[0] | ||
|
||
d := &ResourceDataMock{ | ||
FieldsInSchema: map[string]interface{}{ | ||
|
@@ -72,8 +78,24 @@ func testSweep<%= sweeper_name -%>(region string) error { | |
log.Printf("[INFO] Nothing found in response.") | ||
return nil | ||
} | ||
<% if aggregatedList -%> | ||
var rl []interface{} | ||
zones := resourceList.(map[string]interface{}) | ||
// Loop through every zone in the list response | ||
for _, zonesValue := range zones { | ||
zone := zonesValue.(map[string]interface{}) | ||
for k, v := range zone { | ||
// Zone map either has resources or a warning stating there were no resources found in the zone | ||
if k != "warning" { | ||
resourcesInZone := v.([]interface{}) | ||
rl = append(rl, resourcesInZone...) | ||
} | ||
} | ||
} | ||
<% else -%> | ||
|
||
rl := resourceList.([]interface{}) | ||
<% end -%> | ||
|
||
log.Printf("[INFO] Found %d items in %s list response.", len(rl), resourceName) | ||
// items who don't match the tf-test prefix | ||
|
@@ -93,7 +115,18 @@ func testSweep<%= sweeper_name -%>(region string) error { | |
nonPrefixCount++ | ||
continue | ||
} | ||
deleteTemplate := "<%= object.__product.base_url + object.delete_uri -%>" | ||
deleteTemplate := "<%= deleteUrlTemplate -%>" | ||
<% if aggregatedList -%> | ||
|
||
if obj["zone"] == nil { | ||
log.Printf("[INFO] %s resource zone was nil", resourceName) | ||
return nil | ||
} | ||
zoneSegs := strings.Split(obj["zone"].(string),"/") | ||
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. I think this is just https://github.com/terraform-providers/terraform-provider-google/blob/master/google/self_link_helpers.go#L78, right? Can we use that instead? 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. Didn't see that helper! I actually do the same thing here to parse the name. I will replace both instances at the same time in a separate PR since it will cause diffs for a ton of existing sweeper files |
||
zone := zoneSegs[len(zoneSegs)-1] | ||
deleteTemplate = strings.Replace(deleteTemplate, "{{zone}}", zone, -1) | ||
|
||
<% end -%> | ||
deleteUrl, err := replaceVars(d, config, deleteTemplate) | ||
if err != nil { | ||
log.Printf("[INFO] error preparing delete url: %s", err) | ||
|
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.
I'm assuming there's basically no non-GCE zonal resources, so we'll only replace GCE resources with this?
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.
Yep, other resources use
location/
in the base url and they don't support aggregatedList. I am assuming some consistency in the future, otherwise we will have to use some manual flag.