diff --git a/provider/terraform_kcc.rb b/provider/terraform_kcc.rb index 77adc1a8babf..dace30364048 100644 --- a/provider/terraform_kcc.rb +++ b/provider/terraform_kcc.rb @@ -103,11 +103,7 @@ def guess_metadata_mapping_name(object) # remove {{project}}, {{region}} if present, KCC handles them specially final_import_parts -= ['{{project}}', '{{region}}'] - if final_import_parts.length == 1 - final_import_parts.first.gsub('{{', '').gsub('}}', '') - else - nil - end + final_import_parts.first.gsub('{{', '').gsub('}}', '') if final_import_parts.length == 1 end def format_id_template(id_template, object) @@ -117,10 +113,18 @@ def format_id_template(id_template, object) id_template_formatted = id_template_parts.join('/') # transform refs from {{bucket}} to {{bucketRef.name}} form - prop_names = id_template.scan(/{{[[:word:]]+}}/).map{ |p| p.gsub('{{', '').gsub('}}', '') } + prop_names = id_template.scan(/{{[[:word:]]+}}/).map { |p| p.gsub('{{', '').gsub('}}', '') } # probably won't catch overriden names - object.all_properties.reject { |p| p.name == 'zone' }.select { |p| p.is_a?(Api::Type::ResourceRef) && (prop_names.include?(p.name) || prop_names.include?(p.name.camelize(:lower))) }.each do |prop| - id_template_formatted = id_template_formatted.gsub("{{#{prop.name}}}", "{{#{prop.name}Ref.name}}").gsub("{{#{prop.name.camelize(:lower)}}}", "{{#{prop.name}Ref.name}}") + object.all_properties + .reject { |p| p.name == 'zone' } # exclude special fields + .select { |p| p.is_a?(Api::Type::ResourceRef) } # select resource refs + .select { |p| prop_names.include?(p.name.camelize(:lower)) } # canonical name + .each do |prop| + id_template_formatted = id_template_formatted + .gsub( + "{{#{prop.name.camelize(:lower)}}}", + "{{#{prop.name}Ref.name}}" + ) end id_template_formatted end