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

fix(rdb): add validation on database name and explicit depends_on #1273

Merged
merged 3 commits into from
May 13, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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
21 changes: 21 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_$-]*$`), "database name must contain only alphanumeric characters, underscores and dashes and it must start with a letter"),
),
},
"managed": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -121,6 +138,10 @@ func getDatabase(ctx context.Context, api *rdb.API, r scw.Region, instanceID, db
return nil, err
}

if len(res.Databases) == 0 {
return nil, fmt.Errorf("database %s not found", dbName)
}

return res.Databases[0], nil
}

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