Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource/access_policy: support importing zone and account level resources #956

Merged
merged 2 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
```