Skip to content

Commit

Permalink
Add Pages project domain importer. Closes cloudflare#1958
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Oct 15, 2022
1 parent bb4f84d commit 8992ded
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .changelog/1973.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
```release-note:bug
resource/cloudflare_pages_project: add defaults to deployment_config. Closes #1924
```release-note:enhancement
resource/cloudflare_pages_domain: add Pages project domain importer. Closes #1958
```
5 changes: 5 additions & 0 deletions docs/resources/pages_domain.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ resource "cloudflare_pages_domain" "my-domain" {
- `id` (String) The ID of this resource.
- `status` (String) Status of the custom domain.

## Import

Import is supported using the following syntax:
```shell
$ terraform import cloudflare_pages_domain.example <account_id>/<project_name>
```
1 change: 1 addition & 0 deletions examples/resources/cloudflare_pages_domain/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$ terraform import cloudflare_pages_domain.example <account_id>/<project_name>
69 changes: 69 additions & 0 deletions internal/provider/import_cloudflare_pages_import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package provider

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func testPagesDomains(accountID, projectName, resourceOneId, domainOne, resourceTwoId, domainTwo string) string {
return fmt.Sprintf(`
resource "cloudflare_pages_domain" "%[3]s" {
account_id = "%[1]s"
project_name = "%[2]s"
domain = "%[4]s"
}
resource "cloudflare_pages_domain" "%[5]s" {
account_id = "%[1]s"
project_name = "%[2]s"
domain = "%[6]s"
}
`, accountID, projectName, resourceOneId, domainOne, resourceTwoId, domainTwo)
}

func TestAccCloudflarePagesDomain_Import(t *testing.T) {
skipPagesProjectForNonConfiguredDefaultAccount(t)

resourceOneId := generateRandomResourceName()
resourceTwoId := generateRandomResourceName()
resourceOneName := fmt.Sprintf("cloudflare_pages_domain.%s", resourceOneId)
resourceTwoName := fmt.Sprintf("cloudflare_pages_domain.%s", resourceTwoId)
domain := os.Getenv("CLOUDFLARE_DOMAIN")
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
projectName := generateRandomResourceName()

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckEmail(t)
testAccPreCheckApiKey(t)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testPagesDomains(
accountID,
projectName,
resourceOneId,
resourceOneId+"."+domain,
resourceTwoId,
resourceTwoId+"."+domain,
),
},
{
ResourceName: resourceOneName,
ImportStateIdPrefix: fmt.Sprintf("%s/", accountID),
ImportState: true,
ImportStateVerify: true,
},
{
ResourceName: resourceTwoName,
ImportStateIdPrefix: fmt.Sprintf("%s/", accountID),
ImportState: true,
ImportStateVerify: true,
},
},
})
}
38 changes: 38 additions & 0 deletions internal/provider/resource_cloudflare_pages_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package provider
import (
"context"
"fmt"
"strings"

"github.com/MakeNowJust/heredoc/v2"
"github.com/cloudflare/cloudflare-go"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -78,3 +80,39 @@ func resourceCloudflarePagesDomainDelete(ctx context.Context, d *schema.Resource
}
return nil
}

func resourceCloudflarePagesDomainImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
client := meta.(*cloudflare.API)

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

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

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

resourceData := []*schema.ResourceData{}

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)
}

return resourceData, nil
}

0 comments on commit 8992ded

Please sign in to comment.