Skip to content

Commit

Permalink
Finish up
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Oct 18, 2022
1 parent cdd0cfe commit e45977c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 21 deletions.
6 changes: 5 additions & 1 deletion .changelog/1973.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
```release-note:enhancement
resource/cloudflare_pages_domain: add Pages project domain importer
resource/cloudflare_pages_domain: add Pages project domain importer.
```

```release-note:bug
resource/cloudflare_pages_project: add defaults to Pages project deployment config
```
2 changes: 1 addition & 1 deletion docs/resources/pages_domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ resource "cloudflare_pages_domain" "my-domain" {

Import is supported using the following syntax:
```shell
$ terraform import cloudflare_pages_domain.example <account_id>/<project_name>
$ terraform import cloudflare_pages_domain.example <account_id>/<project_name>/<domain-name>
```
2 changes: 1 addition & 1 deletion examples/resources/cloudflare_pages_domain/import.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
$ terraform import cloudflare_pages_domain.example <account_id>/<project_name>
$ terraform import cloudflare_pages_domain.example <account_id>/<project_name>/<domain-name>
39 changes: 21 additions & 18 deletions internal/provider/resource_cloudflare_pages_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func resourceCloudflarePagesDomain() *schema.Resource {
CreateContext: resourceCloudflarePagesDomainCreate,
ReadContext: resourceCloudflarePagesDomainRead,
DeleteContext: resourceCloudflarePagesDomainDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceCloudflarePagesDomainImport,
},
Description: heredoc.Doc(`
Provides a resource for managing Cloudflare Pages domains.
`),
Expand Down Expand Up @@ -85,34 +88,34 @@ func resourceCloudflarePagesDomainImport(ctx context.Context, d *schema.Resource
client := meta.(*cloudflare.API)

// split the id so we can look up
idAttr := strings.SplitN(d.Id(), "/", 2)
idAttr := strings.SplitN(d.Id(), "/", 3)
var accountID string
var projectName string
if len(idAttr) == 2 {
var domainName string
if len(idAttr) == 3 {
accountID = idAttr[0]
projectName = idAttr[1]
domainName = idAttr[2]
} else {
return nil, fmt.Errorf("invalid id %q specified, should be in format \"accountID/project-name\" for import", d.Id())
return nil, fmt.Errorf("invalid id %q specified, should be in format \"accountID/project-name/domain\" for import", d.Id())
}

domains, err := client.GetPagesDomains(ctx, cloudflare.PagesDomainsParameters{AccountID: accountID, ProjectName: projectName})
domain, err := client.GetPagesDomain(ctx, cloudflare.PagesDomainParameters{
AccountID: accountID,
ProjectName: projectName,
DomainName: domainName,
})
if err != nil {
return nil, fmt.Errorf("unable to find domains for project %q: %w", d.Id(), err)
return nil, fmt.Errorf("unable to find domain for project %q: %w", d.Id(), err)
}

tflog.Info(ctx, fmt.Sprintf("Found domains: %+v", domains))

resourceData := []*schema.ResourceData{}
tflog.Info(ctx, fmt.Sprintf("Found domain: %+v", domain))

for _, domain := range domains {
d.SetId(domain.ID)
d.Set("account_id", accountID)
d.Set("project_name", projectName)
d.Set("domain", domain.Name)
resourceCloudflarePagesDomainRead(ctx, d, meta)

resourceData = append(resourceData, d)
}
d.SetId(domain.ID)
d.Set("account_id", accountID)
d.Set("project_name", projectName)
d.Set("domain", domain.Name)
resourceCloudflarePagesDomainRead(ctx, d, meta)

return resourceData, nil
return []*schema.ResourceData{d}, nil
}

0 comments on commit e45977c

Please sign in to comment.