|
5 | 5 | "fmt" |
6 | 6 | "strconv" |
7 | 7 | "strings" |
| 8 | + "time" |
8 | 9 |
|
9 | 10 | "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
10 | 11 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
@@ -46,16 +47,18 @@ func resourceDatabaseRole() *schema.Resource { |
46 | 47 | Description: "The password.", |
47 | 48 | }, |
48 | 49 | "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", |
53 | 55 | }, |
54 | 56 | "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.", |
59 | 62 | }, |
60 | 63 | "attribute": { |
61 | 64 | Type: schema.TypeList, |
@@ -295,3 +298,18 @@ func parseRoleIdentifier(identifier string) (int, string, error) { |
295 | 298 |
|
296 | 299 | return instanceID, strings.Join(slice[1:], roleIdentifierSeparator), nil |
297 | 300 | } |
| 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 | +} |
0 commit comments