-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from planetscale/joem/branch-safe-migrations
feat: add data source and resource `planetscale_branch_safe_migrations`
- Loading branch information
Showing
22 changed files
with
653 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,4 @@ linters: | |
- unconvert | ||
- unparam | ||
- unused | ||
- vet | ||
- govet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
examples/data-sources/planetscale_branch_safe_migrations/data-source.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
2 changes: 2 additions & 0 deletions
2
examples/resources/planetscale_branch_safe_migrations/import.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
examples/resources/planetscale_branch_safe_migrations/resource.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
internal/provider/branch_safe_migrations_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.