Skip to content

Commit

Permalink
Handle labels field inside flatten object
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed Sep 14, 2023
1 parent 2ccb8fc commit bc3006d
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
20 changes: 16 additions & 4 deletions mmv1/api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def add_labels_related_fields(props, parent)
elsif p.is_a? Api::Type::KeyValueAnnotations
add_annotations_fields(props, parent, p)
elsif (p.is_a? Api::Type::NestedObject) && !p.all_properties.nil?
p.properties = add_labels_related_fields(p.all_properties, p.name)
p.properties = add_labels_related_fields(p.all_properties, p)
end
end
props
Expand All @@ -471,9 +471,9 @@ def add_labels_fields(props, parent, labels)
labels.ignore_write = true

@custom_diff ||= []
if parent.nil?
if parent.nil? || parent.flatten_object
@custom_diff.append('tpgresource.SetLabelsDiff')
elsif parent == 'metadata'
elsif parent.name == 'metadata'
@custom_diff.append('tpgresource.SetMetadataLabelsDiff')
end

Expand All @@ -489,7 +489,7 @@ def add_annotations_fields(props, parent, annotations)
@custom_diff ||= []
if parent.nil?
@custom_diff.append('tpgresource.SetAnnotationsDiff')
elsif parent == 'metadata'
elsif parent.name == 'metadata'
@custom_diff.append('tpgresource.SetMetadataAnnotationsDiff')
end

Expand Down Expand Up @@ -527,6 +527,18 @@ def build_terraform_labels_field(name, min_version)
)
end

def igore_read_labels_fields(props, parent)
fields = []
props.each do |p|
if (p.is_a? Api::Type::KeyValueLabels) || (p.is_a? Api::Type::KeyValueTerraformLabels) || (p.is_a? Api::Type::KeyValueAnnotations)
fields << p.terraform_lineage
elsif (p.is_a? Api::Type::NestedObject) && !p.all_properties.nil?
fields.concat(igore_read_labels_fields(p.all_properties, p))
end
end
fields
end

# ====================
# Version-related methods
# ====================
Expand Down
2 changes: 1 addition & 1 deletion mmv1/api/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ def lineage
# Prints the access path of the field in the configration eg: metadata.0.labels
# The only intended purpose is to get the value of the labes field by calling d.Get().
def terraform_lineage
return name&.underscore if __parent.nil?
return name&.underscore if __parent.nil? || __parent.flatten_object

"#{__parent.terraform_lineage}.0.#{name&.underscore}"
end
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/bigquery/Job.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ properties:
name: 'jobTimeoutMs'
description: |
Job timeout in milliseconds. If this time limit is exceeded, BigQuery may attempt to terminate the job.
- !ruby/object:Api::Type::KeyValuePairs
- !ruby/object:Api::Type::KeyValueLabels
name: 'labels'
description: |
The labels associated with this job. You can use these to organize and group your jobs.
Expand Down
2 changes: 1 addition & 1 deletion mmv1/provider/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def updatable?(resource, properties)
end

def force_new?(property, resource)
!property.output &&
(!property.output || property.is_a?(Api::Type::KeyValueEffectiveLabels)) &&
(property.immutable || (resource.immutable && property.update_url.nil? &&
property.immutable.nil? &&
(property.parent.nil? ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ object.examples
test_slug = "#{resource_name}_#{example.name.camelize(:lower)}Example"

ignore_read = object.all_user_properties
.select{|p| p.url_param_only || p.ignore_read || p.is_a?(Api::Type::ResourceRef) || p.is_a?(Api::Type::KeyValueLabels) || p.is_a?(Api::Type::KeyValueAnnotations) || p.is_a?(Api::Type::KeyValueTerraformLabels)}
.select{|p| p.url_param_only || p.ignore_read || p.is_a?(Api::Type::ResourceRef)}
.map { |p| p.name.underscore }
.concat(example.ignore_read_extra)
.concat(object.igore_read_labels_fields(object.properties_with_excluded, nil))

# Use explicit version for the example if given.
# Otherwise, use object version.
Expand Down
1 change: 1 addition & 0 deletions mmv1/templates/terraform/expand_property_method.erb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d t
func expand<%= prefix -%><%= titlelize_property(property) -%>(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
transformed := make(map[string]interface{})
<% property.nested_properties.each do |prop| -%>
<% next if prop.is_a?(Api::Type::KeyValuePairs) && prop.ignore_write -%>
<% schemaPrefix = prop.flatten_object ? "nil" : "d.Get( \"#{prop.name.underscore}\" )" -%>
transformed<%= titlelize_property(prop) -%>, err := expand<%= prefix -%><%= titlelize_property(property) -%><%= titlelize_property(prop) -%>(<%= schemaPrefix -%>, d, config)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAccBigQueryJob_withLocation(t *testing.T) {
ImportStateId: importID,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"etag", "status.0.state"},
ImportStateVerifyIgnore: []string{"etag", "status.0.state", "labels", "terraform_labels"},
},
},
})
Expand Down

0 comments on commit bc3006d

Please sign in to comment.