@@ -63,7 +63,7 @@ func (r *SettingsResource) Schema(ctx context.Context, req resource.SchemaReques
63
63
},
64
64
"database" : schema.StringAttribute {
65
65
CustomType : jsontypes.NormalizedType {},
66
- MarkdownDescription : "Database settings as serialised JSON" ,
66
+ MarkdownDescription : "Database settings as [ serialised JSON](https://api.supabase.com/api/v1#/projects%20config/updateConfig) " ,
67
67
Optional : true ,
68
68
},
69
69
"pooler" : schema.StringAttribute {
@@ -132,6 +132,9 @@ func (r *SettingsResource) Create(ctx context.Context, req resource.CreateReques
132
132
133
133
// Initial settings are always created together with the project resource.
134
134
// We can simply apply partial updates here based on the given TF plan.
135
+ if ! data .Database .IsNull () {
136
+ resp .Diagnostics .Append (updateDatabaseConfig (ctx , & data , r .client )... )
137
+ }
135
138
if ! data .Api .IsNull () {
136
139
resp .Diagnostics .Append (updateApiConfig (ctx , & data , r .client )... )
137
140
}
@@ -164,6 +167,9 @@ func (r *SettingsResource) Read(ctx context.Context, req resource.ReadRequest, r
164
167
165
168
// If an existing state has not been imported or created from a TF plan before,
166
169
// skip loading them because we are not interested in managing them through TF.
170
+ if ! data .Database .IsNull () {
171
+ resp .Diagnostics .Append (readDatabaseConfig (ctx , & data , r .client )... )
172
+ }
167
173
if ! data .Api .IsNull () {
168
174
resp .Diagnostics .Append (readApiConfig (ctx , & data , r .client )... )
169
175
}
@@ -191,6 +197,9 @@ func (r *SettingsResource) Update(ctx context.Context, req resource.UpdateReques
191
197
}
192
198
193
199
// Ignore any states not specified in the TF plan.
200
+ if ! data .Database .IsNull () {
201
+ resp .Diagnostics .Append (updateDatabaseConfig (ctx , & data , r .client )... )
202
+ }
194
203
if ! data .Api .IsNull () {
195
204
resp .Diagnostics .Append (updateApiConfig (ctx , & data , r .client )... )
196
205
}
@@ -223,6 +232,7 @@ func (r *SettingsResource) ImportState(ctx context.Context, req resource.ImportS
223
232
224
233
// Read all configs from API when importing so it's easier to pick
225
234
// individual fields to manage through TF.
235
+ resp .Diagnostics .Append (readDatabaseConfig (ctx , & data , r .client )... )
226
236
resp .Diagnostics .Append (readApiConfig (ctx , & data , r .client )... )
227
237
resp .Diagnostics .Append (readAuthConfig (ctx , & data , r .client )... )
228
238
@@ -269,20 +279,10 @@ func updateApiConfig(ctx context.Context, plan *SettingsResourceModel, client *a
269
279
return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
270
280
}
271
281
272
- partial := make (map [string ]interface {})
273
- if diags := plan .Api .Unmarshal (& partial ); diags .HasError () {
274
- // Unreachable because unmarshalling to request body returned no error
275
- return diags
276
- }
277
- pickConfig (* httpResp .JSON200 , partial )
278
-
279
- value , err := json .Marshal (partial )
280
- if err != nil {
281
- msg := fmt .Sprintf ("Unable to update api settings, got marshal error: %s" , err )
282
+ if plan .Api , err = parseConfig (plan .Api , * httpResp .JSON200 ); err != nil {
283
+ msg := fmt .Sprintf ("Unable to update api settings, got error: %s" , err )
282
284
return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
283
285
}
284
-
285
- plan .Api = jsontypes .NewNormalizedValue (string (value ))
286
286
return nil
287
287
}
288
288
@@ -324,20 +324,55 @@ func updateAuthConfig(ctx context.Context, plan *SettingsResourceModel, client *
324
324
return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
325
325
}
326
326
327
- partial := make (map [string ]interface {})
328
- if diags := plan .Auth .Unmarshal (& partial ); diags .HasError () {
329
- // Unreachable because unmarshalling to request body returned no error
327
+ if plan .Auth , err = parseConfig (plan .Auth , * httpResp .JSON200 ); err != nil {
328
+ msg := fmt .Sprintf ("Unable to update auth settings, got error: %s" , err )
329
+ return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
330
+ }
331
+ return nil
332
+ }
333
+
334
+ func readDatabaseConfig (ctx context.Context , state * SettingsResourceModel , client * api.ClientWithResponses ) diag.Diagnostics {
335
+ httpResp , err := client .GetConfigWithResponse (ctx , state .Id .ValueString ())
336
+ if err != nil {
337
+ msg := fmt .Sprintf ("Unable to read database settings, got error: %s" , err )
338
+ return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
339
+ }
340
+ // Deleted project is an orphan resource, not returning error so it can be destroyed.
341
+ switch httpResp .StatusCode () {
342
+ case http .StatusNotFound , http .StatusNotAcceptable :
343
+ return nil
344
+ }
345
+ if httpResp .JSON200 == nil {
346
+ msg := fmt .Sprintf ("Unable to read database settings, got status %d: %s" , httpResp .StatusCode (), httpResp .Body )
347
+ return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
348
+ }
349
+ if state .Database , err = parseConfig (state .Database , * httpResp .JSON200 ); err != nil {
350
+ msg := fmt .Sprintf ("Unable to read database settings, got error: %s" , err )
351
+ return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
352
+ }
353
+ return nil
354
+ }
355
+
356
+ func updateDatabaseConfig (ctx context.Context , plan * SettingsResourceModel , client * api.ClientWithResponses ) diag.Diagnostics {
357
+ var body api.UpdatePostgresConfigBody
358
+ if diags := plan .Database .Unmarshal (& body ); diags .HasError () {
330
359
return diags
331
360
}
332
- pickConfig (* httpResp .JSON200 , partial )
333
361
334
- value , err := json . Marshal ( partial )
362
+ httpResp , err := client . UpdateConfigWithResponse ( ctx , plan . ProjectRef . ValueString (), body )
335
363
if err != nil {
336
- msg := fmt .Sprintf ("Unable to update auth settings, got marshal error: %s" , err )
364
+ msg := fmt .Sprintf ("Unable to update database settings, got error: %s" , err )
365
+ return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
366
+ }
367
+ if httpResp .JSON200 == nil {
368
+ msg := fmt .Sprintf ("Unable to update database settings, got status %d: %s" , httpResp .StatusCode (), httpResp .Body )
337
369
return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
338
370
}
339
371
340
- plan .Auth = jsontypes .NewNormalizedValue (string (value ))
372
+ if plan .Database , err = parseConfig (plan .Database , * httpResp .JSON200 ); err != nil {
373
+ msg := fmt .Sprintf ("Unable to update database settings, got error: %s" , err )
374
+ return diag.Diagnostics {diag .NewErrorDiagnostic ("Client Error" , msg )}
375
+ }
341
376
return nil
342
377
}
343
378
0 commit comments