/
resource_connection_postgres.go
197 lines (167 loc) · 5.54 KB
/
resource_connection_postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package resources
import (
"context"
"log"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/materialize"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
var connectionPostgresSchema = map[string]*schema.Schema{
"name": ObjectNameSchema("connection", true, false),
"schema_name": SchemaNameSchema("connection", false),
"database_name": DatabaseNameSchema("connection", false),
"qualified_sql_name": QualifiedNameSchema("connection"),
"comment": CommentSchema(false),
"database": {
Description: "The target Postgres database.",
Type: schema.TypeString,
Required: true,
},
"host": {
Description: "The Postgres database hostname.",
Type: schema.TypeString,
Required: true,
},
"port": {
Description: "The Postgres database port.",
Type: schema.TypeInt,
Optional: true,
Default: 5432,
},
"user": ValueSecretSchema("user", "The Postgres database username.", true, false),
"password": IdentifierSchema(IdentifierSchemaParams{
Elem: "password",
Description: "The Postgres database password.",
Required: false,
ForceNew: false,
}),
"ssh_tunnel": IdentifierSchema(IdentifierSchemaParams{
Elem: "ssh_tunnel",
Description: "The SSH tunnel configuration for the Postgres database.",
Required: false,
ForceNew: false,
}),
"ssl_certificate_authority": ValueSecretSchema("ssl_certificate_authority", "The CA certificate for the Postgres database.", false, false),
"ssl_certificate": ValueSecretSchema("ssl_certificate", "The client certificate for the Postgres database.", false, false),
"ssl_key": IdentifierSchema(IdentifierSchemaParams{
Elem: "ssl_key",
Description: "The client key for the Postgres database.",
Required: false,
ForceNew: false,
}),
"ssl_mode": {
Description: "The SSL mode for the Postgres database.",
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"aws_privatelink": IdentifierSchema(IdentifierSchemaParams{
Elem: "aws_privatelink",
Description: "The AWS PrivateLink configuration for the Postgres database.",
Required: false,
ForceNew: false,
}),
"validate": ValidateConnectionSchema(),
"ownership_role": OwnershipRoleSchema(),
"region": RegionSchema(),
}
func ConnectionPostgres() *schema.Resource {
return &schema.Resource{
Description: "A Postgres connection establishes a link to a single database of a PostgreSQL server.",
CreateContext: connectionPostgresCreate,
ReadContext: connectionRead,
UpdateContext: connectionUpdate,
DeleteContext: connectionDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: connectionPostgresSchema,
}
}
func connectionPostgresCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
connectionName := d.Get("name").(string)
schemaName := d.Get("schema_name").(string)
databaseName := d.Get("database_name").(string)
metaDb, region, err := utils.GetDBClientFromMeta(meta, d)
if err != nil {
return diag.FromErr(err)
}
o := materialize.MaterializeObject{ObjectType: "CONNECTION", Name: connectionName, SchemaName: schemaName, DatabaseName: databaseName}
b := materialize.NewConnectionPostgresBuilder(metaDb, o)
if v, ok := d.GetOk("connection_type"); ok {
b.ConnectionType(v.(string))
}
if v, ok := d.GetOk("host"); ok {
b.PostgresHost(v.(string))
}
if v, ok := d.GetOk("port"); ok {
b.PostgresPort(v.(int))
}
if v, ok := d.GetOk("user"); ok {
user := materialize.GetValueSecretStruct(v)
b.PostgresUser(user)
}
if v, ok := d.GetOk("password"); ok {
pass := materialize.GetIdentifierSchemaStruct(v)
b.PostgresPassword(pass)
}
if v, ok := d.GetOk("database"); ok {
b.PostgresDatabase(v.(string))
}
if v, ok := d.GetOk("ssl_mode"); ok {
b.PostgresSSLMode(v.(string))
}
if v, ok := d.GetOk("ssl_certificate_authority"); ok {
ssl_ca := materialize.GetValueSecretStruct(v)
b.PostgresSSLCa(ssl_ca)
}
if v, ok := d.GetOk("ssl_certificate"); ok {
ssl_cert := materialize.GetValueSecretStruct(v)
b.PostgresSSLCert(ssl_cert)
}
if v, ok := d.GetOk("ssl_key"); ok {
k := materialize.GetIdentifierSchemaStruct(v)
b.PostgresSSLKey(k)
}
if v, ok := d.GetOk("aws_privatelink"); ok {
conn := materialize.GetIdentifierSchemaStruct(v)
b.PostgresAWSPrivateLink(conn)
}
if v, ok := d.GetOk("ssh_tunnel"); ok {
conn := materialize.GetIdentifierSchemaStruct(v)
b.PostgresSSHTunnel(conn)
}
if v, ok := d.GetOk("validate"); ok {
b.Validate(v.(bool))
}
// create resource
if err := b.Create(); err != nil {
return diag.FromErr(err)
}
// ownership
if v, ok := d.GetOk("ownership_role"); ok {
ownership := materialize.NewOwnershipBuilder(metaDb, o)
if err := ownership.Alter(v.(string)); err != nil {
log.Printf("[DEBUG] resource failed ownership, dropping object: %s", o.Name)
b.Drop()
return diag.FromErr(err)
}
}
// object comment
if v, ok := d.GetOk("comment"); ok {
comment := materialize.NewCommentBuilder(metaDb, o)
if err := comment.Object(v.(string)); err != nil {
log.Printf("[DEBUG] resource failed comment, dropping object: %s", o.Name)
b.Drop()
return diag.FromErr(err)
}
}
// set id
i, err := materialize.ConnectionId(metaDb, o)
if err != nil {
return diag.FromErr(err)
}
d.SetId(utils.TransformIdWithRegion(string(region), i))
return connectionRead(ctx, d, meta)
}