Skip to content

Commit

Permalink
Specify the orverride import order
Browse files Browse the repository at this point in the history
There are some situations where the order that resources are overridden is
critical. We have gotten lucky up until now that a strategy of "deepest first"
was good enough. This will let us force the order when two or more resources
depend on a specific ordering.
  • Loading branch information
chrisst committed Oct 13, 2018
1 parent 355fbf1 commit 3868e3e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ overrides: !ruby/object:Provider::ResourceOverrides
default_from_api: true
custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb'
sourceImage: !ruby/object:Provider::Terraform::PropertyOverride
# sourceImage must be overridden first because it creates the 'image' property
override_order: -1
name: image
diff_suppress_func: 'diskImageDiffSuppress'
image: !ruby/object:Provider::Terraform::PropertyOverride
override_order: 5
description: |
The image from which to initialize this disk. This can be
one of: the image's `self_link`, `projects/{project}/global/images/{image}`,
Expand Down
10 changes: 10 additions & 0 deletions provider/property_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ module Provider
class PropertyOverride < Api::Object
attr_reader :new_type

# Ordering of imports can be important so this property can make the
# ordering more deterministic. It supports positive or negative numbers
# with the lowest number getting imported first.
# The default is equal to the negative depth of the metric. This makes
# sure that more deeply nested fields happen first.
# eg:
# sourceSnapshotEncryptionKey.rawKey has an order of -2
# sourceSnapshotEncryptionKey has an order of -1
attr_reader :override_order

include Api::Type::Fields
# To allow overrides for type-specific fields, include those type's
# fields with an 'include' directive here.
Expand Down
2 changes: 1 addition & 1 deletion provider/resource_overrides.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def override_properties(api_object, override)
# before we have applied the overrides to its child properties (and
# therefore can no longer find the child property, since the
# parent name has changed)
sorted_props = override.properties.sort_by { |path, _| -path.count('.') }
sorted_props = override.properties.sort_by { |path, p| p.override_order || -path.count('.') }
sorted_props.each do |property_path, property_override|
check_property_value "properties['#{property_path}']",
property_override, Provider::PropertyOverride
Expand Down

0 comments on commit 3868e3e

Please sign in to comment.