From c86a85750f9b7a431a7ed54c0237d65765d46205 Mon Sep 17 00:00:00 2001 From: Zhenhua Li Date: Wed, 13 Sep 2023 22:12:58 -0700 Subject: [PATCH] Handle labels field inside flatten object --- mmv1/api/resource.rb | 22 +++++++++++++++---- mmv1/api/type.rb | 2 +- mmv1/products/bigquery/Job.yaml | 2 +- mmv1/provider/terraform.rb | 2 +- .../examples/base_configs/test_file.go.erb | 3 ++- .../terraform/expand_property_method.erb | 1 + .../bigquery/resource_bigquery_job_test.go | 2 +- 7 files changed, 25 insertions(+), 9 deletions(-) diff --git a/mmv1/api/resource.rb b/mmv1/api/resource.rb index bf97e331a466..5540052edc44 100644 --- a/mmv1/api/resource.rb +++ b/mmv1/api/resource.rb @@ -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 @@ -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 @@ -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 @@ -527,6 +527,20 @@ def build_terraform_labels_field(name, min_version) ) end + def igore_read_labels_fields(props) + 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)) + end + end + fields + end + # ==================== # Version-related methods # ==================== diff --git a/mmv1/api/type.rb b/mmv1/api/type.rb index 803fa476549f..0d81c8335929 100644 --- a/mmv1/api/type.rb +++ b/mmv1/api/type.rb @@ -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 diff --git a/mmv1/products/bigquery/Job.yaml b/mmv1/products/bigquery/Job.yaml index cf12ae9fdf3d..016f76d03a72 100644 --- a/mmv1/products/bigquery/Job.yaml +++ b/mmv1/products/bigquery/Job.yaml @@ -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. diff --git a/mmv1/provider/terraform.rb b/mmv1/provider/terraform.rb index 4b89fb535374..512a26b353bf 100644 --- a/mmv1/provider/terraform.rb +++ b/mmv1/provider/terraform.rb @@ -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? || diff --git a/mmv1/templates/terraform/examples/base_configs/test_file.go.erb b/mmv1/templates/terraform/examples/base_configs/test_file.go.erb index 3e9b14c491df..2140797bd2c6 100644 --- a/mmv1/templates/terraform/examples/base_configs/test_file.go.erb +++ b/mmv1/templates/terraform/examples/base_configs/test_file.go.erb @@ -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)) # Use explicit version for the example if given. # Otherwise, use object version. diff --git a/mmv1/templates/terraform/expand_property_method.erb b/mmv1/templates/terraform/expand_property_method.erb index 880242320bd5..d74d7bcc777c 100644 --- a/mmv1/templates/terraform/expand_property_method.erb +++ b/mmv1/templates/terraform/expand_property_method.erb @@ -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 { diff --git a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_job_test.go b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_job_test.go index 9de93bb6a2f7..3e8b4b832e3f 100644 --- a/mmv1/third_party/terraform/services/bigquery/resource_bigquery_job_test.go +++ b/mmv1/third_party/terraform/services/bigquery/resource_bigquery_job_test.go @@ -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"}, }, }, })