Skip to content

Commit

Permalink
fix(rdb): add validation on database name and explicit depends_on
Browse files Browse the repository at this point in the history
  • Loading branch information
remyleone committed May 13, 2022
1 parent 9d114cd commit d392918
Show file tree
Hide file tree
Showing 4 changed files with 1,641 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/resources/rdb_privilege.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ resource "scaleway_rdb_privilege" "priv" {
user_name = "my-db-user"
database_name = "my-db-name"
permission = "all"
depends_on = [scaleway_rdb_user.main, scaleway_rdb_database.main]
}
resource "scaleway_rdb_user" "main" {
instance_id = scaleway_rdb_instance.pgsql.id
name = "foobar"
password = "thiZ_is_v&ry_s3cret"
is_admin = false
}
resource "scaleway_rdb_database" "main" {
instance_id = scaleway_rdb_instance.pgsql.id
name = "foobar"
}
```

Expand Down
17 changes: 17 additions & 0 deletions scaleway/resource_rdb_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package scaleway
import (
"context"
"fmt"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
Expand Down Expand Up @@ -40,6 +42,21 @@ func resourceScalewayRdbDatabase() *schema.Resource {
Description: "Database name",
Required: true,
ForceNew: true,
ValidateFunc: validation.All(
validation.StringLenBetween(1, 63),
validation.StringNotInSlice([]string{
"information_schema",
"mysql",
"performance_schema",
"postgres",
"rdb",
"rdb",
"sys",
"template0",
"template1",
}, false),
validation.StringMatch(regexp.MustCompile(`^[a-zA-Z\d_$-]*$`), ""),
),
},
"managed": {
Type: schema.TypeBool,
Expand Down
52 changes: 52 additions & 0 deletions scaleway/resource_rdb_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,58 @@ func TestAccScalewayRdbDatabase_Basic(t *testing.T) {
})
}

func TestAccScalewayRdbDatabase_ManualDelete(t *testing.T) {
tt := NewTestTools(t)
defer tt.Cleanup()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: tt.ProviderFactories,
CheckDestroy: testAccCheckScalewayRdbInstanceDestroy(tt),
Steps: []resource.TestStep{
{
Config: `
resource "scaleway_rdb_instance" "pgsql" {
name = "bug"
node_type = "db-dev-m"
engine = "PostgreSQL-13"
is_ha_cluster = false
disable_backup = true
user_name = "admin"
password = "thiZ_is_v&ry_s3cret"
tags = ["bug"]
}
resource "scaleway_rdb_user" "bug" {
instance_id = scaleway_rdb_instance.pgsql.id
name = "bug"
password = "thiZ_is_v&ry_s3cret"
is_admin = false
}
resource "scaleway_rdb_database" "bug" {
instance_id = scaleway_rdb_instance.pgsql.id
name = "bug"
}
resource "scaleway_rdb_privilege" "bug" {
instance_id = scaleway_rdb_instance.pgsql.id
user_name = "bug"
database_name = "bug"
permission = "all"
depends_on = [scaleway_rdb_user.bug, scaleway_rdb_database.bug]
}
`,
Check: resource.ComposeTestCheckFunc(
testAccCheckRdbDatabaseExists(tt, "scaleway_rdb_instance.pgsql", "scaleway_rdb_database.bug"),
resource.TestCheckResourceAttr("scaleway_rdb_database.bug", "name", "bug"),
),
},
},
})
}

func testAccCheckRdbDatabaseExists(tt *TestTools, instance string, database string) resource.TestCheckFunc {
return func(state *terraform.State) error {
instanceResource, ok := state.RootModule().Resources[instance]
Expand Down

0 comments on commit d392918

Please sign in to comment.