Skip to content

Commit

Permalink
Change default id format from {{name}} to self_link_uri (#2461)
Browse files Browse the repository at this point in the history
* Change default id format from {{name}} to self_link_uri
  • Loading branch information
slevenick authored and rileykarson committed Nov 11, 2019
1 parent f108bfc commit 90e1b6f
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion overrides/terraform/resource_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions products/resourcemanager/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions provider/terraform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion templates/terraform/iam_policy.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,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 {
Expand Down
4 changes: 2 additions & 2 deletions templates/terraform/resource.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions templates/terraform/resource_iam.html.markdown.erb
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,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`
Expand Down

0 comments on commit 90e1b6f

Please sign in to comment.