Skip to content

Commit

Permalink
Update google_iap_brand documentation about import and name formats (
Browse files Browse the repository at this point in the history
…#6586)

* Update import format for google_iap_brand resource

* Update documentation for google_iap_brand name attribute

* Clarify description of name attr; make more clear which format the API returns.

* Implement import of `google_iap_brand` using name in 2-part format

* Update import to set 4-part id, to match unimported resources
  • Loading branch information
SarahFrench authored Sep 23, 2022
1 parent d283835 commit dacfb79
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 4 additions & 4 deletions mmv1/products/iap/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ objects:
- !ruby/object:Api::Type::String
name: 'name'
description: |
Output only. Identifier of the brand, in the format
`projects/{project_number}/brands/{brand_id}`. NOTE: The brand
identification corresponds to the project number as only one
brand per project can be created.
Output only. Identifier of the brand, in the format `projects/{project_number}/brands/{brand_id}`
NOTE: The name can also be expressed as `projects/{project_id}/brands/{brand_id}`, e.g. when importing.
NOTE: The brand identification corresponds to the project number as only one
brand can be created per project.
output: true
properties:
- !ruby/object:Api::Type::String
Expand Down
2 changes: 1 addition & 1 deletion mmv1/products/iap/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides
Destroying a Terraform-managed Brand will remove it from state
but *will not delete it from Google Cloud.*
id_format: '{{name}}'
import_format: ['{{name}}']
import_format: ["projects/{{project_number}}/brands/{{brand_id}}", "projects/{{project_id}}/brands/{{brand_id}}"]
skip_delete: true
examples:
- !ruby/object:Provider::Terraform::Examples
Expand Down
23 changes: 20 additions & 3 deletions mmv1/templates/terraform/custom_import/iap_brand.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,32 @@ if err := parseImportId([]string{"(?P<name>.+)"}, d, config); err != nil {
}

nameParts := strings.Split(d.Get("name").(string), "/")
if len(nameParts) != 4 {
if len(nameParts) != 4 && len(nameParts) != 2 {
return nil, fmt.Errorf(
"Saw %s when the name is expected to have shape %s",
"Saw %s when the name is expected to have either shape %s or %s",
d.Get("name"),
"projects/{{project}}/brands/{{name}}",
"{{project}}/{{name}}",
)
}

if err := d.Set("project", nameParts[1]); err != nil {
var project string
if len(nameParts) == 4 {
project = nameParts[1]
}
if len(nameParts) == 2 {
project = nameParts[0] // Different index

// Set `name` (and `id`) as a 4-part format so Read func produces valid URL
brand := nameParts[1]
name := fmt.Sprintf("projects/%s/brands/%s", project, brand)
if err := d.Set("name", name); err != nil {
return nil, fmt.Errorf("Error setting name: %s", err)
}
d.SetId(name)
}

if err := d.Set("project", project); err != nil {
return nil, fmt.Errorf("Error setting project: %s", err)
}
return []*schema.ResourceData{d}, nil

0 comments on commit dacfb79

Please sign in to comment.