Skip to content

Commit

Permalink
Merge pull request #956 from cloudflare/allow-import-of-zone-and-acco…
Browse files Browse the repository at this point in the history
…unt-access-policy

resource/access_policy: support importing zone and account level resources
  • Loading branch information
jacobbednarz authored Feb 16, 2021
2 parents 29aafe0 + 0bb52cc commit 58c2a74
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
20 changes: 13 additions & 7 deletions cloudflare/resource_cloudflare_access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,23 @@ func resourceCloudflareAccessPolicyDelete(d *schema.ResourceData, meta interface
}

func resourceCloudflareAccessPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
attributes := strings.SplitN(d.Id(), "/", 3)

if len(attributes) != 3 {
return nil, fmt.Errorf("invalid id (\"%s\") specified, should be in format \"accountID/accessApplicationID/accessPolicyID\"", d.Id())
attributes := strings.SplitN(d.Id(), "/", 4)

if len(attributes) != 4 {
return nil, fmt.Errorf(
"invalid id (%q) specified, should be in format %q or %q",
d.Id(),
"account/accountID/accessApplicationID/accessPolicyID",
"zone/zoneID/accessApplicationID/accessPolicyID",
)
}

accountID, accessAppID, accessPolicyID := attributes[0], attributes[1], attributes[2]
identifierType, identifierID, accessAppID, accessPolicyID := attributes[0], attributes[1], attributes[2], attributes[3]

log.Printf("[DEBUG] Importing Cloudflare Access Policy: accountID %q, appID %q, accessPolicyID %q", accountID, accessAppID, accessPolicyID)
log.Printf("[DEBUG] Importing Cloudflare Access Policy: %s %q, appID %q, accessPolicyID %q", identifierType, identifierID, accessAppID, accessPolicyID)

d.Set("account_id", accountID)
//lintignore:R001
d.Set(fmt.Sprintf("%s_id", identifierType), identifierID)
d.Set("application_id", accessAppID)
d.SetId(accessPolicyID)

Expand Down
17 changes: 8 additions & 9 deletions website/docs/r/access_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,14 @@ The following arguments are supported:

## Import

Access Policies can be imported using a composite ID formed of zone
ID, application ID and policy ID.
Access Policies can be imported using a composite ID formed of identifier type
(`zone` or `account`), identifier ID (`zone_id` or `account_id`), application ID
and policy ID.

```
$ terraform import cloudflare_access_policy.staging cb029e245cfdd66dc8d2e570d5dd3322/d41d8cd98f00b204e9800998ecf8427e/67ea780ce4982c1cfbe6b7293afc765d
```

where
# import a zone level Access policy
$ terraform import cloudflare_access_policy.staging zone/cb029e245cfdd66dc8d2e570d5dd3322/d41d8cd98f00b204e9800998ecf8427e/67ea780ce4982c1cfbe6b7293afc765d
* `cb029e245cfdd66dc8d2e570d5dd3322` - Zone ID
* `d41d8cd98f00b204e9800998ecf8427e` - Access Application ID
* `67ea780ce4982c1cfbe6b7293afc765d` - Access Policy ID
# import an account level Access policy
$ terraform import cloudflare_access_policy.production account/0d599f0ec05c3bda8c3b8a68c32a1b47/d41d8cd98f00b204e9800998ecf8427e/67ea780ce4982c1cfbe6b7293afc765d
```

0 comments on commit 58c2a74

Please sign in to comment.