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

Add importer for pages projects #1886

Merged
merged 16 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/1886.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/cloudflare_pages_project: Adds importer for pages_project
```
6 changes: 6 additions & 0 deletions docs/resources/pages_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,10 @@ Optional:
- `production_deployment_enabled` (Boolean) Enable production deployments.
- `repo_name` (String) Project repository name.

## Import

Import is supported using the following syntax:
```shell
# Use account ID, and project name.
$ terraform import cloudflare_pages_project.example <account_id>/<project_name>
```
2 changes: 2 additions & 0 deletions examples/resources/cloudflare_pages_project/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Use account ID, and project name.
$ terraform import cloudflare_pages_project.example <account_id>/<project_name>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package provider

import (
"fmt"
"os"
"testing"

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

func TestAccCloudflarePagesProject_Import(t *testing.T) {
accountID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
rnd := generateRandomResourceName()
name := fmt.Sprintf("cloudflare_pages_project.%s", rnd)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheckEmail(t)
testAccPreCheckApiKey(t)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testPagesProjectDirectUpload(rnd, accountID),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", accountID),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"allow_overwrite"},
},
},
})
}
2 changes: 2 additions & 0 deletions internal/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ func testAccPreCheckWorkspaceOne(t *testing.T) {
}

func testAccPreCheckPages(t *testing.T) {
testAccPreCheckAccount(t)

if v := os.Getenv("CLOUDFLARE_PAGES_OWNER"); v == "" {
t.Fatal("CLOUDFLARE_PAGES_OWNER must be set for this acceptance test")
}
Expand Down
38 changes: 37 additions & 1 deletion internal/provider/resource_cloudflare_pages_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"strings"
"time"

"github.com/MakeNowJust/heredoc/v2"
Expand All @@ -18,6 +20,9 @@ func resourceCloudflarePagesProject() *schema.Resource {
ReadContext: resourceCloudflarePagesProjectRead,
UpdateContext: resourceCloudflarePagesProjectUpdate,
DeleteContext: resourceCloudflarePagesProjectDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceCloudflarePagesProjectImport,
},
Description: heredoc.Doc(`
Provides a resource which manages Cloudflare Pages projects.
`),
Expand Down Expand Up @@ -170,7 +175,7 @@ func resourceCloudflarePagesProjectRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(fmt.Errorf("error reading cloudflare pages project %q: %w", projectName, err))
}

d.SetId(res.ID)
Cyb3r-Jak3 marked this conversation as resolved.
Show resolved Hide resolved
d.SetId(res.Name)
d.Set("subdomain", res.SubDomain)
d.Set("created_on", res.CreatedOn.Format(time.RFC3339))
d.Set("domains", res.Domains)
Cyb3r-Jak3 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -217,3 +222,34 @@ func resourceCloudflarePagesProjectDelete(ctx context.Context, d *schema.Resourc
}
return nil
}

func resourceCloudflarePagesProjectImport(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())
}

project, err := client.PagesProject(ctx, accountID, projectName)
if err != nil {
return nil, fmt.Errorf("Unable to find record with ID %q: %w", d.Id(), err)
}

tflog.Info(ctx, fmt.Sprintf("Found project: %s", project.Name))

d.SetId(project.Name)
Cyb3r-Jak3 marked this conversation as resolved.
Show resolved Hide resolved
d.Set("name", project.Name)
d.Set("account_id", accountID)
d.Set("production_branch", project.ProductionBranch)
Cyb3r-Jak3 marked this conversation as resolved.
Show resolved Hide resolved

resourceCloudflarePagesProjectRead(ctx, d, meta)

return []*schema.ResourceData{d}, nil
}
9 changes: 4 additions & 5 deletions internal/provider/resource_cloudflare_pages_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,14 @@ func testPagesProjectDeploymentConfig(resourceID, accountID, projectName string)
`, resourceID, accountID, projectName)
}

func testPagesProjectDirectUpload(resourceID, accountID, projectName string) string {
func testPagesProjectDirectUpload(resourceID, accountID string) string {
return fmt.Sprintf(`
resource "cloudflare_pages_project" "%[1]s" {
account_id = "%[2]s"
name = "%[3]s"
name = "%[1]s"
production_branch = "main"
}
`, resourceID, accountID, projectName)
`, resourceID, accountID)
}

func testPagesProjectPreviewOnly(resourceID, accountID, projectName string) string {
Expand Down Expand Up @@ -324,12 +324,11 @@ func TestAccTestPagesProject_DirectUpload(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckPages(t)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testPagesProjectDirectUpload(rnd, accountID, rnd),
Config: testPagesProjectDirectUpload(rnd, accountID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "name", rnd),
resource.TestCheckResourceAttr(name, "account_id", accountID),
Expand Down