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) (#4712)

* 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

Signed-off-by: Modular Magician <magic-modules@google.com>

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Sep 23, 2022
1 parent b3287ba commit 357207c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .changelog/6586.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
iap: added ability to import `google_iap_brand` using ID in {{project}}/{{brand_id}} format
```
31 changes: 24 additions & 7 deletions google-beta/resource_iap_brand.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ is an owner of the specified group in Cloud Identity.`,
"name": {
Type: schema.TypeString,
Computed: true,
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.`,
Description: `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.`,
},
"org_internal_only": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -266,15 +266,32 @@ func resourceIapBrandImport(d *schema.ResourceData, meta interface{}) ([]*schema
}

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
Expand Down
12 changes: 7 additions & 5 deletions website/docs/r/iap_brand.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ In addition to the arguments listed above, the following computed attributes are
Whether the brand is only intended for usage inside the GSuite organization only.

* `name` -
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.


## Timeouts
Expand All @@ -114,7 +114,9 @@ This resource provides the following
Brand can be imported using any of these accepted formats:

```
$ terraform import google_iap_brand.default {{name}}
$ terraform import google_iap_brand.default projects/{{project_id}}/brands/{{brand_id}}
$ terraform import google_iap_brand.default projects/{{project_number}}/brands/{{brand_id}}
$ terraform import google_iap_brand.default {{project_number}}/{{brand_id}}
```

## User Project Overrides
Expand Down

0 comments on commit 357207c

Please sign in to comment.