Skip to content

Commit

Permalink
Merge pull request #110 from planetscale/joem/branch-fix-promote-on-c…
Browse files Browse the repository at this point in the history
…reate

fix creating planetscale_branch with production = true
  • Loading branch information
joemiller authored Feb 18, 2025
2 parents a0fefb5 + ab9ed93 commit 4f0b69f
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 21 deletions.
1 change: 0 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Visit https://golangci-lint.run/ for usage documentation
# and information on other useful linters
issues:
max-per-linter: 0
max-same-issues: 0

linters:
Expand Down
13 changes: 13 additions & 0 deletions internal/provider/branch_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,19 @@ func (r *branchResource) Create(ctx context.Context, req resource.CreateRequest,
return
}

// After branch is ready, check if we need to promote it to a production branch
if !data.Production.IsNull() && data.Production.ValueBool() {
res, err := r.client.PromoteBranch(ctx, org.ValueString(), database.ValueString(), name.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Unable to promote branch during creation",
fmt.Sprintf("Branch %s could not be promoted to production: %s", name.ValueString(), err),
)
return
}
branch = res.Branch
}

data = branchResourceFromClient(ctx, &branch, data.Organization, data.Database, resp.Diagnostics)
if resp.Diagnostics.HasError() {
return
Expand Down
104 changes: 84 additions & 20 deletions internal/provider/branch_resource_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,84 @@
package provider

import (
"bytes"
"context"
"fmt"
"testing"
"text/template"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccBranchResource(t *testing.T) {
dbName := acctest.RandomWithPrefix("testacc-branch")
dbName := acctest.RandomWithPrefix("testacc-branch-db")
branchName := acctest.RandomWithPrefix("branch")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create and Read testing
// Initial creation with required fields
{
Config: testAccBranchResourceConfig(dbName, branchName),
Config: testAccBranchResourceConfigTemplate(map[string]string{
"organization": testAccOrg,
"database": dbName,
"name": branchName,
"parent_branch": "main",
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("planetscale_branch.test", "name", branchName),
resource.TestCheckResourceAttr("planetscale_branch.test", "parent_branch", "main"),
resource.TestCheckResourceAttr("planetscale_branch.test", "sharded", "false"),
resource.TestCheckResourceAttr("planetscale_branch.test", "production", "false"),
),
},
// ImportState testing
{
ResourceName: "planetscale_branch.test",
ImportStateId: fmt.Sprintf("%s,%s,%s", testAccOrg, dbName, branchName),
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"updated_at", "schema_last_updated_at"},
},
// Update in-place to production branch
{
Config: testAccBranchResourceConfigTemplate(map[string]string{
"organization": testAccOrg,
"database": dbName,
"name": branchName,
"parent_branch": "main",
"production": "true",
}),
ConfigPlanChecks: checkExpectUpdate("planetscale_branch.test"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("planetscale_branch.test", "production", "true"),
),
},
},
})
}

// TestAccBranchResource_ProductionCreate tests the creation of a branch with
// the production flag set to true.
func TestAccBranchResource_ProductionCreate(t *testing.T) {
dbName := acctest.RandomWithPrefix("testacc-branch-db")
branchName := acctest.RandomWithPrefix("branch")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccBranchResourceConfigTemplate(map[string]string{
"organization": testAccOrg,
"database": dbName,
"name": branchName,
"parent_branch": "main",
"production": "true",
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("planetscale_branch.test", "production", "true"),
),
},
// ImportState testing
Expand All @@ -34,9 +89,6 @@ func TestAccBranchResource(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"updated_at", "schema_last_updated_at"},
},
// Update and Read testing
// TODO: Implement an update test.
// Delete testing automatically occurs in TestCase
},
})
}
Expand All @@ -55,14 +107,19 @@ func TestAccBranchResource_OutOfBandDelete(t *testing.T) {
Steps: []resource.TestStep{
// Create and Read testing
{
Config: testAccBranchResourceConfig(dbName, branchName),
Config: testAccBranchResourceConfigTemplate(map[string]string{
"organization": testAccOrg,
"database": dbName,
"name": branchName,
"parent_branch": "main",
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("planetscale_branch.test", "name", branchName),
resource.TestCheckResourceAttr("planetscale_branch.test", "parent_branch", "main"),
resource.TestCheckResourceAttr("planetscale_branch.test", "sharded", "false"),
),
},
// Test out-of-bands deletion of the database should produce a plan to recreate, not error.
// Test out-of-bands deletion of the branch should produce a plan to recreate, not error
{
ResourceName: "planetscale_branch.test",
RefreshState: true,
Expand All @@ -78,20 +135,27 @@ func TestAccBranchResource_OutOfBandDelete(t *testing.T) {
})
}

func testAccBranchResourceConfig(dbName, branchName string) string {
return fmt.Sprintf(`
func testAccBranchResourceConfigTemplate(settings map[string]string) string {
const tmpl = `
resource "planetscale_database" "test" {
organization = "%s"
name = "%s"
cluster_size = "PS-10"
default_branch = "main"
organization = "{{.organization}}"
name = "{{.database}}"
cluster_size = "PS-10"
}
resource "planetscale_branch" "test" {
organization = "%s"
database = planetscale_database.test.name
name = "%s"
parent_branch = planetscale_database.test.default_branch
organization = "{{.organization}}"
database = planetscale_database.test.name
name = "{{.name}}"
parent_branch = "{{.parent_branch}}"
{{if .production}}production = {{.production}}{{end}}
}
`, testAccOrg, dbName, testAccOrg, branchName)
`
t := template.Must(template.New("config").Parse(tmpl))
var buf bytes.Buffer
err := t.Execute(&buf, settings)
if err != nil {
return ""
}
return buf.String()
}

0 comments on commit 4f0b69f

Please sign in to comment.