Skip to content

Commit 16fd58c

Browse files
authored
fix: add validation for connection_limit and valid_until (#29)
* fix: add validation for connection_limit and valid_until * chore: update comment * fix: ci * chore: release 0.0.6-beta
1 parent a1595ad commit 16fd58c

File tree

8 files changed

+34
-16
lines changed

8 files changed

+34
-16
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.6-alpha
1+
0.0.6-beta

docs/resources/database_role.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ resource "bytebase_database_role" "admin" {
4848

4949
- `password` (String) The password for the role.
5050
- `connection_limit` (Number) The connection limit for the role.
51-
- `valid_until` (String) The `valid_until` sets a date and time after which the role's password is no longer valid.
51+
- `valid_until` (String) The `valid_until` sets a date and time after which the role's password is no longer valid. Should be a timestamp in "2006-01-02T15:04:05+08:00" format.
5252

5353
<a id="nestedblock--attribute"></a>
5454

examples/environments/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "0.0.6-alpha"
5+
version = "0.0.6-beta"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/instances/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
terraform {
33
required_providers {
44
bytebase = {
5-
version = "0.0.6-alpha"
5+
version = "0.0.6-beta"
66
# For local development, please use "terraform.local/bytebase/bytebase" instead
77
source = "registry.terraform.io/bytebase/bytebase"
88
}

examples/role/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "0.0.6-alpha"
4+
version = "0.0.6-beta"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

examples/setup/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
terraform {
22
required_providers {
33
bytebase = {
4-
version = "0.0.6-alpha"
4+
version = "0.0.6-beta"
55
# For local development, please use "terraform.local/bytebase/bytebase" instead
66
source = "registry.terraform.io/bytebase/bytebase"
77
}

provider/resource_database_role.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"strconv"
77
"strings"
8+
"time"
89

910
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -46,16 +47,18 @@ func resourceDatabaseRole() *schema.Resource {
4647
Description: "The password.",
4748
},
4849
"connection_limit": {
49-
Type: schema.TypeInt,
50-
Optional: true,
51-
Default: -1,
52-
Description: "Connection count limit for role",
50+
Type: schema.TypeInt,
51+
Optional: true,
52+
Default: -1,
53+
ValidateFunc: validation.IntAtLeast(-1),
54+
Description: "Connection count limit for role",
5355
},
5456
"valid_until": {
55-
Type: schema.TypeString,
56-
Optional: true,
57-
Default: "",
58-
Description: "It sets a date and time after which the role's password is no longer valid.",
57+
Type: schema.TypeString,
58+
Optional: true,
59+
Default: "",
60+
ValidateFunc: validateDatetime,
61+
Description: "It sets a date and time after which the role's password is no longer valid. Should be a timestamp in \"2006-01-02T15:04:05+08:00\" format.",
5962
},
6063
"attribute": {
6164
Type: schema.TypeList,
@@ -295,3 +298,18 @@ func parseRoleIdentifier(identifier string) (int, string, error) {
295298

296299
return instanceID, strings.Join(slice[1:], roleIdentifierSeparator), nil
297300
}
301+
302+
func validateDatetime(val interface{}, _ string) (ws []string, es []error) {
303+
raw := val.(string)
304+
if raw == "" {
305+
return nil, nil
306+
}
307+
308+
if _, err := time.Parse(time.RFC3339, raw); err != nil {
309+
if err.Error() == "day out of range" {
310+
return ws, append(es, errors.Errorf("invalid timestamp %s, %s", raw, err.Error()))
311+
}
312+
return ws, append(es, errors.Errorf(`valid_until should in "2006-01-02T15:04:05+08:00" format with timezone`))
313+
}
314+
return nil, nil
315+
}

provider/resource_database_role_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestAccRole(t *testing.T) {
4949
name = "%s"
5050
instance = bytebase_instance.%s.name
5151
connection_limit = 99
52-
valid_until = "2022-12-31"
52+
valid_until = "2022-12-31T15:04:05+08:00"
5353
attribute {}
5454
}
5555
`, mockInstanceResource("instance_2"), roleName, "instance_2"),
@@ -58,7 +58,7 @@ func TestAccRole(t *testing.T) {
5858
resource.TestCheckResourceAttr("bytebase_database_role.test_role_2", "name", roleName),
5959
resource.TestCheckResourceAttr("bytebase_database_role.test_role_2", "instance", "instance_2"),
6060
resource.TestCheckResourceAttr("bytebase_database_role.test_role_2", "connection_limit", "99"),
61-
resource.TestCheckResourceAttr("bytebase_database_role.test_role_2", "valid_until", "2022-12-31"),
61+
resource.TestCheckResourceAttr("bytebase_database_role.test_role_2", "valid_until", "2022-12-31T15:04:05+08:00"),
6262
resource.TestCheckResourceAttr("bytebase_database_role.test_role_2", "attribute.#", "1"),
6363
),
6464
},

0 commit comments

Comments
 (0)