Skip to content

Commit

Permalink
Merge pull request #101 from planetscale/joem/branch-safe-migrations
Browse files Browse the repository at this point in the history
feat: add data source and resource `planetscale_branch_safe_migrations`
  • Loading branch information
joemiller authored Jan 22, 2025
2 parents ab21ca1 + 25b4a20 commit 7f3c4ba
Show file tree
Hide file tree
Showing 22 changed files with 653 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ linters:
- unconvert
- unparam
- unused
- vet
- govet
4 changes: 4 additions & 0 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ lint:
testacc:
TF_ACC=1 go test -parallel=2 ./... -v $(TESTARGS) -timeout 120m

.PHONY: generate-docs
generate-docs:
go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs

.PHONY: generate
generate:
bash ./script/update_openapi_spec
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/branch.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ output "branch" {
- `region` (Attributes) The region in which this branch lives. (see [below for nested schema](#nestedatt--region))
- `restore_checklist_completed_at` (String) When a user last marked a backup restore checklist as completed.
- `restored_from_branch` (Attributes) (see [below for nested schema](#nestedatt--restored_from_branch))
- `safe_migrations` (Boolean) Whether safe migrations are enabled for this branch.
- `schema_last_updated_at` (String) When the schema for the branch was last updated.
- `shard_count` (Number) The number of shards in the branch.
- `sharded` (Boolean) Whether or not the branch is sharded.
Expand Down
38 changes: 38 additions & 0 deletions docs/data-sources/branch_safe_migrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "planetscale_branch_safe_migrations Data Source - terraform-provider-planetscale"
subcategory: ""
description: |-
Safe migration state on a PlanetScale branch.
---

# planetscale_branch_safe_migrations (Data Source)

Safe migration state on a PlanetScale branch.

## Example Usage

```terraform
data "planetscale_branch_safe_migrations" "example" {
organization = "example.com"
database = "example_db"
branch = "main"
}
output "safe_migrations" {
value = data.planetscale_branch_safe_migrations.example
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `branch` (String) The name of the branch this safe migrations configuration belongs to.
- `database` (String) The database this branch belongs to.
- `organization` (String) The organization this branch belongs to.

### Read-Only

- `enabled` (Boolean) Whether safe migrations are enabled for this branch.
1 change: 1 addition & 0 deletions docs/data-sources/branches.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Read-Only:
- `region` (Attributes) The region in which this branch lives. (see [below for nested schema](#nestedatt--branches--region))
- `restore_checklist_completed_at` (String) When a user last marked a backup restore checklist as completed.
- `restored_from_branch` (Attributes) (see [below for nested schema](#nestedatt--branches--restored_from_branch))
- `safe_migrations` (Boolean) Whether safe migrations are enabled for this branch.
- `schema_last_updated_at` (String) When the schema for the branch was last updated.
- `shard_count` (Number) The number of shards in the branch.
- `sharded` (Boolean) Whether or not the branch is sharded.
Expand Down
9 changes: 9 additions & 0 deletions docs/resources/branch.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,12 @@ Read-Only:
- `id` (String) The ID for the resource.
- `name` (String) The name for the resource.
- `updated_at` (String) When the resource was last updated.

## Import

Import is supported using the following syntax:

```shell
# Branches can be imported using "org,database,branch" as the identifier.
terraform import planetscale_branch.example "org,database,branch"
```
67 changes: 67 additions & 0 deletions docs/resources/branch_safe_migrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "planetscale_branch_safe_migrations Resource - terraform-provider-planetscale"
subcategory: ""
description: |-
Manages safe migrations settings for a PlanetScale branch.
---

# planetscale_branch_safe_migrations (Resource)

Manages safe migrations settings for a PlanetScale branch.

## Example Usage

```terraform
# Enable safe_migrations on default branch:
resource "planetscale_database" "example" {
organization = "example"
name = "example"
default_branch = "main"
}
resource "planetscale_branch_safe_migrations" "main" {
organization = planetscale_branch.example.organization
database = planetscale_branch.example.database
branch = planetscale_branch.example.default_branch
enabled = true
}
# Enable safe_migrations on a branch:
resource "planetscale_branch" "staging" {
organization = planetscale_branch.example.organization
database = planetscale_branch.example.database
parent_branch = planetscale_branch.example.default_branch
name = "staging"
}
resource "planetscale_branch_safe_migrations" "staging" {
database = planetscale_database.example.name
organization = planetscale_database.example.organization
branch = planetscale_branch.staging.name
enabled = true
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `branch` (String) The name of the branch to configure safe migrations on..
- `database` (String) The database this branch belongs to.
- `enabled` (Boolean) Whether safe migrations are enabled for this branch.
- `organization` (String) The organization this branch belongs to.

### Read-Only

- `id` (String) The ID of the branch.

## Import

Import is supported using the following syntax:

```shell
# Safe migrations configuration can be imported using "org,database,branch" as the identifier.
terraform import planetscale_branch_safe_migrations.example "org,database,branch"
```
9 changes: 9 additions & 0 deletions docs/resources/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,12 @@ Required:
- `database` (String) The name of the database imported from.
- `hostname` (String) The hostname where the database lives.
- `port` (String) The port on which the database listens on the host.

## Import

Import is supported using the following syntax:

```shell
# Databases can be imported using "org,database" as the identifier.
terraform import planetscale_branch.example "org,database"
```
10 changes: 10 additions & 0 deletions docs/resources/password.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,13 @@ Read-Only:
- `provider` (String) Provider for the region (ex. AWS).
- `public_ip_addresses` (List of String) Public IP addresses for the region.
- `slug` (String) The slug of the region.

## Import

Import is supported using the following syntax:

```shell
# Passwords can be imported using "org,database,branch,id" as the identifier.
# NOTE: 'id' is the 12-character password id (eg: k4fhp4kvj0mz) and not the password's name.
terraform import planetscale_branch.example "org,database,branch,id"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
data "planetscale_branch_safe_migrations" "example" {
organization = "example.com"
database = "example_db"
branch = "main"
}

output "safe_migrations" {
value = data.planetscale_branch_safe_migrations.example
}
2 changes: 2 additions & 0 deletions examples/resources/planetscale_branch/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Branches can be imported using "org,database,branch" as the identifier.
terraform import planetscale_branch.example "org,database,branch"
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Safe migrations configuration can be imported using "org,database,branch" as the identifier.
terraform import planetscale_branch_safe_migrations.example "org,database,branch"
28 changes: 28 additions & 0 deletions examples/resources/planetscale_branch_safe_migrations/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Enable safe_migrations on default branch:
resource "planetscale_database" "example" {
organization = "example"
name = "example"
default_branch = "main"
}

resource "planetscale_branch_safe_migrations" "main" {
organization = planetscale_branch.example.organization
database = planetscale_branch.example.database
branch = planetscale_branch.example.default_branch
enabled = true
}

# Enable safe_migrations on a branch:
resource "planetscale_branch" "staging" {
organization = planetscale_branch.example.organization
database = planetscale_branch.example.database
parent_branch = planetscale_branch.example.default_branch
name = "staging"
}

resource "planetscale_branch_safe_migrations" "staging" {
database = planetscale_database.example.name
organization = planetscale_database.example.organization
branch = planetscale_branch.staging.name
enabled = true
}
2 changes: 2 additions & 0 deletions examples/resources/planetscale_database/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Databases can be imported using "org,database" as the identifier.
terraform import planetscale_branch.example "org,database"
3 changes: 3 additions & 0 deletions examples/resources/planetscale_password/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Passwords can be imported using "org,database,branch,id" as the identifier.
# NOTE: 'id' is the 12-character password id (eg: k4fhp4kvj0mz) and not the password's name.
terraform import planetscale_branch.example "org,database,branch,id"
76 changes: 76 additions & 0 deletions internal/provider/branch_safe_migrations_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/planetscale/terraform-provider-planetscale/internal/client/planetscale"
)

var (
_ datasource.DataSource = &branchSafeMigrationsDataSource{}
_ datasource.DataSourceWithConfigure = &branchSafeMigrationsDataSource{}
)

func newbranchSafeMigrationsDataSource() datasource.DataSource {
return &branchSafeMigrationsDataSource{}
}

type branchSafeMigrationsDataSource struct {
client *planetscale.Client
}

func (d *branchSafeMigrationsDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_branch_safe_migrations"
}

func (d *branchSafeMigrationsDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Safe migration state on a PlanetScale branch.",
MarkdownDescription: "Safe migration state on a PlanetScale branch.",
Attributes: branchSafeMigrationsDataSourceSchemaAttribute,
}
}

func (d *branchSafeMigrationsDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*planetscale.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Data Source Configure Type",
fmt.Sprintf("Expected *planetscale.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
d.client = client
}

func (d *branchSafeMigrationsDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data *branchSafeMigrationsDataSourceModel
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

res, err := d.client.GetBranch(ctx, data.Organization.ValueString(), data.Database.ValueString(), data.Branch.ValueString())
if err != nil {
resp.Diagnostics.AddError("Unable to read branch safe migrations status", err.Error())
return
}
if res == nil {
resp.Diagnostics.AddError("Unable to read branch safe migrations status", "no data")
return
}

state := branchSafeMigrationsFromClient(&res.Branch, data.Organization.ValueString(), data.Database.ValueString())
if resp.Diagnostics.HasError() {
return
}

diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}
43 changes: 43 additions & 0 deletions internal/provider/branch_safe_migrations_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package provider

import (
"fmt"
"testing"

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

func TestAccBranchSafeMigrationsDataSource(t *testing.T) {
dbName := acctest.RandomWithPrefix("testacc-safe-mig-ds")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccBranchSafeMigrationsDataSourceConfig(dbName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.planetscale_branch_safe_migrations.test", "enabled", "false"),
),
},
},
})
}

func testAccBranchSafeMigrationsDataSourceConfig(dbName string) string {
return fmt.Sprintf(`
resource "planetscale_database" "test" {
organization = "%s"
name = "%s"
cluster_size = "PS-10"
default_branch = "main"
}
data "planetscale_branch_safe_migrations" "test" {
organization = "%s"
database = planetscale_database.test.name
branch = planetscale_database.test.default_branch
}
`, testAccOrg, dbName, testAccOrg)
}
Loading

0 comments on commit 7f3c4ba

Please sign in to comment.