Skip to content

Commit fab41d3

Browse files
committed
Add support to create role/user
1 parent 4fb5806 commit fab41d3

8 files changed

Lines changed: 593 additions & 4 deletions

File tree

internal/provider/config.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,12 @@ func NewConfig(dsnTemplate string) *Config {
2020
}
2121

2222
func (c *Config) connectToPostgresqlDb(dbName string) (*sql.DB, error) {
23-
dsn := fmt.Sprintf(c.dsnTemplate, dbName)
23+
dsn := fmt.Sprintf(c.dsnTemplate, "dbname="+dbName)
24+
return c.connectToPostgresql(dsn)
25+
}
26+
27+
func (c *Config) connectToPostgresqlNoDb() (*sql.DB, error) {
28+
dsn := fmt.Sprintf(c.dsnTemplate, "")
2429
return c.connectToPostgresql(dsn)
2530
}
2631

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
"github.com/hashicorp/terraform-plugin-framework/attr/xattr"
10+
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/path"
12+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
13+
"github.com/hashicorp/terraform-plugin-go/tftypes"
14+
)
15+
16+
var (
17+
_ basetypes.StringTypable = CustomTimestampType{}
18+
_ xattr.TypeWithValidate = CustomTimestampType{}
19+
)
20+
21+
type CustomTimestampType struct {
22+
basetypes.StringType
23+
}
24+
25+
func (t CustomTimestampType) Equal(o attr.Type) bool {
26+
other, ok := o.(CustomTimestampType)
27+
28+
if !ok {
29+
return false
30+
}
31+
return t.StringType.Equal(other.StringType)
32+
}
33+
34+
func (t CustomTimestampType) String() string {
35+
return "CustomTimestampType"
36+
}
37+
38+
func (t CustomTimestampType) ValueFromString(ctx context.Context, in basetypes.StringValue) (basetypes.StringValuable, diag.Diagnostics) {
39+
value := CustomTimestampValue{
40+
StringValue: in,
41+
}
42+
43+
return value, nil
44+
}
45+
46+
func (t CustomTimestampType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
47+
attrValue, err := t.StringType.ValueFromTerraform(ctx, in)
48+
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
stringValue, ok := attrValue.(basetypes.StringValue)
54+
55+
if !ok {
56+
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
57+
}
58+
59+
stringValuable, diags := t.ValueFromString(ctx, stringValue)
60+
61+
if diags.HasError() {
62+
return nil, fmt.Errorf("unexpected error converting StringValue to StringValuable: %v", diags)
63+
}
64+
65+
return stringValuable, nil
66+
}
67+
68+
func (t CustomTimestampType) ValueType(ctx context.Context) attr.Value {
69+
return CustomTimestampValue{}
70+
}
71+
72+
func (t CustomTimestampType) Validate(ctx context.Context, value tftypes.Value, valuePath path.Path) diag.Diagnostics {
73+
if value.IsNull() || !value.IsKnown() {
74+
return nil
75+
}
76+
77+
var diags diag.Diagnostics
78+
var valueString string
79+
80+
if err := value.As(&valueString); err != nil {
81+
diags.AddAttributeError(
82+
valuePath,
83+
"Invalid Terraform Value",
84+
"An unexpected error occurred while attempting to convert a Terraform value to a string. "+
85+
"This generally is an issue with the provider schema implementation. "+
86+
"Please contact the provider developers.\n\n"+
87+
"Path: "+valuePath.String()+"\n"+
88+
"Error: "+err.Error(),
89+
)
90+
return diags
91+
}
92+
93+
if valueString == "infinity" {
94+
return diags
95+
}
96+
97+
if _, err := time.Parse(time.DateTime, valueString); err != nil {
98+
diags.AddAttributeError(
99+
valuePath,
100+
"Invalid DateTime String Value",
101+
"An unexpected error occurred while converting a string value that was expected to be a DateTime format. "+
102+
"The string format is 'YYYY-MM-DD HH:MM:SS', such as '2006-01-02 15:04:05' or '2006-01-02 15:04:05'.\n\n"+
103+
"Path: "+valuePath.String()+"\n"+
104+
"Given Value: "+valueString+"\n"+
105+
"Error: "+err.Error(),
106+
)
107+
108+
return diags
109+
}
110+
111+
return diags
112+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-framework/attr"
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
"github.com/hashicorp/terraform-plugin-framework/types"
11+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
12+
)
13+
14+
var (
15+
_ basetypes.StringValuable = CustomTimestampValue{}
16+
_ basetypes.StringValuableWithSemanticEquals = CustomTimestampValue{}
17+
)
18+
19+
type CustomTimestampValue struct {
20+
basetypes.StringValue
21+
}
22+
23+
func (v CustomTimestampValue) Equal(o attr.Value) bool {
24+
other, ok := o.(CustomTimestampValue)
25+
if !ok {
26+
return false
27+
}
28+
return v.StringValue.Equal(other.StringValue)
29+
}
30+
31+
func (v CustomTimestampValue) Type(ctx context.Context) attr.Type {
32+
return CustomTimestampType{}
33+
}
34+
35+
func (v CustomTimestampValue) StringSemanticEquals(ctx context.Context, newValuable basetypes.StringValuable) (bool, diag.Diagnostics) {
36+
var diags diag.Diagnostics
37+
newValue, ok := newValuable.(CustomTimestampValue)
38+
39+
if !ok {
40+
diags.AddError(
41+
"Semantic Equality Check Error",
42+
"An unexpected value type was received while performing semantic equality checks. "+
43+
"Please report this to the provider developers.\n\n"+
44+
"Expected Value Type: "+fmt.Sprintf("%T", v)+"\n"+
45+
"Got Value Type: "+fmt.Sprintf("%T", newValuable),
46+
)
47+
48+
return false, diags
49+
}
50+
51+
if v.StringValue.ValueString() == "infinity" && newValue.ValueString() == "infinity" {
52+
return true, diags
53+
}
54+
55+
priorTime, _ := time.Parse(time.DateTime, v.StringValue.ValueString())
56+
newTime, _ := time.Parse(time.DateTime, newValue.ValueString())
57+
58+
return priorTime.Equal(newTime), diags
59+
}
60+
61+
func (v CustomTimestampValue) ValueTimestamp() string {
62+
return v.StringValue.ValueString()
63+
}
64+
65+
func NewCustomTimestampValue(value string) CustomTimestampValue {
66+
return CustomTimestampValue{
67+
StringValue: types.StringValue(value),
68+
}
69+
}
70+
71+
func NewCustomTimestampNull() CustomTimestampValue {
72+
return CustomTimestampValue{
73+
StringValue: types.StringNull(),
74+
}
75+
}
76+
77+
func NewCustomTimestampUnknown() CustomTimestampValue {
78+
return CustomTimestampValue{
79+
StringValue: types.StringUnknown(),
80+
}
81+
}

internal/provider/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (p *CloudSqlPostgresqlProvider) Configure(ctx context.Context, req provider
216216
)
217217
}
218218

219-
dsnTemplate := fmt.Sprintf("host=%s dbname=%%s user=%s password=%s sslmode=%s", connectionName, username, password, sslMode)
219+
dsnTemplate := fmt.Sprintf("host=%s %%s user=%s password=%s sslmode=%s", connectionName, username, password, sslMode)
220220

221221
dbConfig := NewConfig(dsnTemplate)
222222

@@ -229,6 +229,7 @@ func (p *CloudSqlPostgresqlProvider) Resources(ctx context.Context) []func() res
229229
newDatabaseGrantResource,
230230
newSchemaGrantResource,
231231
newTableGrantResource,
232+
newRoleResource,
232233
}
233234
}
234235

internal/provider/resource_grant_schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (r *schemaGrantResource) Metadata(_ context.Context, req resource.MetadataR
5050
func (r *schemaGrantResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
5151
resp.Schema = schema.Schema{
5252
Description: "The `cloudsqlpostgresql_grant_schema` resource creates and manages privileges given to a user or role on a schema",
53-
MarkdownDescription: "The cloudsqlpostgresql_grant_schema resource creates and manages privileges given to a user or role on a schema",
53+
MarkdownDescription: "The `cloudsqlpostgresql_grant_schema` resource creates and manages privileges given to a user or role on a schema",
5454
Attributes: map[string]schema.Attribute{
5555
"role": schema.StringAttribute{
5656
Description: "The name of the role to grant privileges on the schema. Can be username or role.",

internal/provider/resource_grant_table.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,6 @@ func (r *tableGrantResource) Create(ctx context.Context, req resource.CreateRequ
204204
}
205205

206206
func (r *tableGrantResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
207-
208207
var state tableGrantResourceModel
209208

210209
diags := req.State.Get(ctx, &state)

0 commit comments

Comments
 (0)