diff --git a/overrides/terraform/resource_override.rb b/overrides/terraform/resource_override.rb index 76e8853516d7..834cbd779d18 100644 --- a/overrides/terraform/resource_override.rb +++ b/overrides/terraform/resource_override.rb @@ -76,7 +76,7 @@ def validate @examples ||= [] check :legacy_name, type: String - check :id_format, type: String, default: '{{name}}' + check :id_format, type: String check :examples, item_type: Provider::Terraform::Examples, type: Array, default: [] check :virtual_fields, item_type: Provider::Terraform::VirtualFields, diff --git a/products/resourcemanager/terraform.yaml b/products/resourcemanager/terraform.yaml index c4970ca439f4..7e8fe24942d7 100644 --- a/products/resourcemanager/terraform.yaml +++ b/products/resourcemanager/terraform.yaml @@ -18,6 +18,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides autogen_async: true exclude: true Lien: !ruby/object:Overrides::Terraform::ResourceOverride + id_format: "{{name}}" import_format: ["{{parent}}/{{name}}"] examples: - !ruby/object:Provider::Terraform::Examples diff --git a/provider/terraform.rb b/provider/terraform.rb index d890ddc9cfea..4adbe277dd35 100644 --- a/provider/terraform.rb +++ b/provider/terraform.rb @@ -246,5 +246,12 @@ def build_object_data(object, output_folder, version) def extract_identifiers(url) url.scan(/\{\{(\w+)\}\}/).flatten end + + # Returns the id format of an object, or self_link_uri if none is explicitly defined + # We prefer the long name of a resource as the id so that users can reference + # resources in a standard way, and most APIs accept short name, long name or self_link + def id_format(object) + object.id_format || object.self_link_uri + end end end diff --git a/templates/terraform/iam_policy.go.erb b/templates/terraform/iam_policy.go.erb index e74a336827c0..b6feb6d05fa5 100644 --- a/templates/terraform/iam_policy.go.erb +++ b/templates/terraform/iam_policy.go.erb @@ -223,7 +223,7 @@ func (u *<%= resource_name -%>IamUpdater) qualify<%= object.name -%>Url(methodId } func (u *<%= resource_name -%>IamUpdater) GetResourceId() string { - return fmt.Sprintf("<%= object.id_format.gsub('{{name}}', "{{#{object.name.underscore}}}").gsub(/({{)(\w+)(}})/, '%s') -%>", <%= string_qualifiers -%>) + return fmt.Sprintf("<%= id_format(object).gsub('{{name}}', "{{#{object.name.underscore}}}").gsub(/({{)(\w+)(}})/, '%s') -%>", <%= string_qualifiers -%>) } func (u *<%= resource_name -%>IamUpdater) GetMutexKey() string { diff --git a/templates/terraform/resource.erb b/templates/terraform/resource.erb index 0eaf2f530220..180b1f54af0b 100644 --- a/templates/terraform/resource.erb +++ b/templates/terraform/resource.erb @@ -191,7 +191,7 @@ func resource<%= resource_name -%>Create(d *schema.ResourceData, meta interface{ } // Store the ID now - id, err := replaceVars(d, config, "<%= object.id_format -%>") + id, err := replaceVars(d, config, "<%= id_format(object) -%>") if err != nil { return fmt.Errorf("Error constructing id: %s", err) } @@ -630,7 +630,7 @@ func resource<%= resource_name -%>Import(d *schema.ResourceData, meta interface{ } // Replace import id for the resource id - id, err := replaceVars(d, config, "<%= object.id_format -%>") + id, err := replaceVars(d, config, "<%= id_format(object) -%>") if err != nil { return nil, fmt.Errorf("Error constructing id: %s", err) } diff --git a/templates/terraform/resource_iam.html.markdown.erb b/templates/terraform/resource_iam.html.markdown.erb index 896b7df6a3af..d41c4eec5f33 100644 --- a/templates/terraform/resource_iam.html.markdown.erb +++ b/templates/terraform/resource_iam.html.markdown.erb @@ -167,11 +167,11 @@ exported: <%= product_ns -%> <%= object.name.downcase -%> IAM resources can be imported using the project, resource identifiers, role and member. ``` -$ terraform import <%= resource_ns_iam -%>_policy.editor <%= object.id_format.gsub('{{name}}', "{{#{object.name.underscore}}}") %> +$ terraform import <%= resource_ns_iam -%>_policy.editor <%= id_format(object).gsub('{{name}}', "{{#{object.name.underscore}}}") %> -$ terraform import <%= resource_ns_iam -%>_binding.editor "<%= object.id_format.gsub('{{name}}', "{{#{object.name.underscore}}}") -%> <%= object.iam_policy.allowed_iam_role -%>" +$ terraform import <%= resource_ns_iam -%>_binding.editor "<%= id_format(object).gsub('{{name}}', "{{#{object.name.underscore}}}") -%> <%= object.iam_policy.allowed_iam_role -%>" -$ terraform import <%= resource_ns_iam -%>_member.editor "<%= object.id_format.gsub('{{name}}', "{{#{object.name.underscore}}}") -%> <%= object.iam_policy.allowed_iam_role -%> jane@example.com" +$ terraform import <%= resource_ns_iam -%>_member.editor "<%= id_format(object).gsub('{{name}}', "{{#{object.name.underscore}}}") -%> <%= object.iam_policy.allowed_iam_role -%> jane@example.com" ``` -> If you're importing a resource with beta features, make sure to include `-provider=google-beta`