Skip to content

Commit cb6f3e8

Browse files
committed
fix(rdb): add validation on database name and explicit depends_on (scaleway#1273)
1 parent cba3d4f commit cb6f3e8

File tree

4 files changed

+1645
-0
lines changed

4 files changed

+1645
-0
lines changed

docs/resources/rdb_privilege.md

+14
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ resource "scaleway_rdb_privilege" "priv" {
1818
user_name = "my-db-user"
1919
database_name = "my-db-name"
2020
permission = "all"
21+
22+
depends_on = [scaleway_rdb_user.main, scaleway_rdb_database.main]
23+
}
24+
25+
resource "scaleway_rdb_user" "main" {
26+
instance_id = scaleway_rdb_instance.pgsql.id
27+
name = "foobar"
28+
password = "thiZ_is_v&ry_s3cret"
29+
is_admin = false
30+
}
31+
32+
resource "scaleway_rdb_database" "main" {
33+
instance_id = scaleway_rdb_instance.pgsql.id
34+
name = "foobar"
2135
}
2236
```
2337

scaleway/resource_rdb_database.go

+21
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package scaleway
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67
"strings"
78
"time"
89

910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1214
"github.com/scaleway/scaleway-sdk-go/api/rdb/v1"
1315
"github.com/scaleway/scaleway-sdk-go/scw"
1416
)
@@ -40,6 +42,21 @@ func resourceScalewayRdbDatabase() *schema.Resource {
4042
Description: "Database name",
4143
Required: true,
4244
ForceNew: true,
45+
ValidateFunc: validation.All(
46+
validation.StringLenBetween(1, 63),
47+
validation.StringNotInSlice([]string{
48+
"information_schema",
49+
"mysql",
50+
"performance_schema",
51+
"postgres",
52+
"rdb",
53+
"rdb",
54+
"sys",
55+
"template0",
56+
"template1",
57+
}, false),
58+
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"),
59+
),
4360
},
4461
"managed": {
4562
Type: schema.TypeBool,
@@ -121,6 +138,10 @@ func getDatabase(ctx context.Context, api *rdb.API, r scw.Region, instanceID, db
121138
return nil, err
122139
}
123140

141+
if len(res.Databases) == 0 {
142+
return nil, fmt.Errorf("database %s not found", dbName)
143+
}
144+
124145
return res.Databases[0], nil
125146
}
126147

scaleway/resource_rdb_database_test.go

+52
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,58 @@ func TestAccScalewayRdbDatabase_Basic(t *testing.T) {
4343
})
4444
}
4545

46+
func TestAccScalewayRdbDatabase_ManualDelete(t *testing.T) {
47+
tt := NewTestTools(t)
48+
defer tt.Cleanup()
49+
50+
resource.ParallelTest(t, resource.TestCase{
51+
PreCheck: func() { testAccPreCheck(t) },
52+
ProviderFactories: tt.ProviderFactories,
53+
CheckDestroy: testAccCheckScalewayRdbInstanceDestroy(tt),
54+
Steps: []resource.TestStep{
55+
{
56+
Config: `
57+
resource "scaleway_rdb_instance" "pgsql" {
58+
name = "bug"
59+
node_type = "db-dev-m"
60+
engine = "PostgreSQL-13"
61+
is_ha_cluster = false
62+
disable_backup = true
63+
user_name = "admin"
64+
password = "thiZ_is_v&ry_s3cret"
65+
tags = ["bug"]
66+
}
67+
68+
resource "scaleway_rdb_user" "bug" {
69+
instance_id = scaleway_rdb_instance.pgsql.id
70+
name = "bug"
71+
password = "thiZ_is_v&ry_s3cret"
72+
is_admin = false
73+
}
74+
75+
resource "scaleway_rdb_database" "bug" {
76+
instance_id = scaleway_rdb_instance.pgsql.id
77+
name = "bug"
78+
}
79+
80+
resource "scaleway_rdb_privilege" "bug" {
81+
instance_id = scaleway_rdb_instance.pgsql.id
82+
user_name = "bug"
83+
database_name = "bug"
84+
permission = "all"
85+
86+
depends_on = [scaleway_rdb_user.bug, scaleway_rdb_database.bug]
87+
}
88+
`,
89+
Check: resource.ComposeTestCheckFunc(
90+
testAccCheckRdbDatabaseExists(tt, "scaleway_rdb_instance.pgsql", "scaleway_rdb_database.bug"),
91+
resource.TestCheckResourceAttr("scaleway_rdb_database.bug", "name", "bug"),
92+
),
93+
},
94+
},
95+
})
96+
}
97+
4698
func testAccCheckRdbDatabaseExists(tt *TestTools, instance string, database string) resource.TestCheckFunc {
4799
return func(state *terraform.State) error {
48100
instanceResource, ok := state.RootModule().Resources[instance]

0 commit comments

Comments
 (0)