Skip to content

Commit

Permalink
Fix flattened custom patchable resources (#1847)
Browse files Browse the repository at this point in the history
Merged PR #1847.
  • Loading branch information
chrisst authored and modular-magician committed May 30, 2019
1 parent 6f8ccbf commit 068a85a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
13 changes: 13 additions & 0 deletions provider/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,19 @@ def properties_by_custom_update(properties)
end
end

# Takes a update_url and returns the list of custom updatable properties
# that can be updated at that URL. This allows flattened objects
# to determine which parent property in the API should be updated with
# the contents of the flattened object
def custom_update_properties_by_url(properties, update_url)
properties_by_custom_update(properties).select do |k, _|
k[:update_url] == update_url
end.first.last
# .first is to grab the element from the select which returns a list
# .last is because properties_by_custom_update returns a list of
# [{update_url}, [properties,...]] and we only need the 2nd part
end

def update_url(resource, url_part)
return resource.self_link_url if url_part.nil?

Expand Down
9 changes: 2 additions & 7 deletions templates/terraform/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func resource<%= resource_name -%>Read(d *schema.ResourceData, meta interface{})
return nil
}

<% if updatable?(object, properties) -%>
<% if updatable?(object, object.root_properties) -%>
func resource<%= resource_name -%>Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand All @@ -287,8 +287,7 @@ func resource<%= resource_name -%>Update(d *schema.ResourceData, meta interface{
<% properties_by_custom_update(object.root_properties).each do |key, props| -%>
if <%= props.map { |prop| "d.HasChange(\"#{prop.name.underscore}\")" }.join ' || ' -%> {
obj := make(map[string]interface{})
<% props.each do |prop| -%>
<% if update_body_properties.include? prop -%>
<% custom_update_properties_by_url(properties, key[:update_url]).each do |prop| -%>
<% schemaPrefix = prop.flatten_object ? "nil" : "d.Get( \"#{prop.name.underscore}\" )" -%>
<%= prop.api_name -%>Prop, err := expand<%= resource_name -%><%= titlelize_property(prop) -%>(<%= schemaPrefix -%>, d, config)
if err != nil {
Expand Down Expand Up @@ -317,10 +316,6 @@ if <%= props.map { |prop| "d.HasChange(\"#{prop.name.underscore}\")" }.join ' ||
<% end -%>
obj["<%= prop.api_name -%>"] = <%= prop.api_name -%>Prop
}
<% else # if update_body_properties.include? prop -%>
<%= prop.api_name -%>Prop := d.Get("<%= prop.name.underscore -%>")
obj["<%= prop.api_name -%>"] = <%= prop.api_name -%>Prop
<% end # if update_body_properties.include? prop -%>
<% end # props.each -%>

<% if object.mutex -%>
Expand Down

0 comments on commit 068a85a

Please sign in to comment.