@@ -38,6 +38,20 @@ type BigQueryWarehouseResource struct {
38
38
39
39
// BigQueryWarehouseResourceModel describes the resource data model according to its Schema.
40
40
type BigQueryWarehouseResourceModel struct {
41
+ Uuid types.String `tfsdk:"uuid"`
42
+ Credentials Credentials `tfsdk:"credentials"`
43
+ Name types.String `tfsdk:"name"`
44
+ CollectorUuid types.String `tfsdk:"collector_uuid"`
45
+ DeletionProtection types.Bool `tfsdk:"deletion_protection"`
46
+ }
47
+
48
+ type Credentials struct {
49
+ ConnectionUuid types.String `tfsdk:"connection_uuid"`
50
+ ServiceAccountKey types.String `tfsdk:"service_account_key"`
51
+ UpdatedAt types.String `tfsdk:"updated_at"`
52
+ }
53
+
54
+ type BigQueryWarehouseResourceModelV1 struct {
41
55
Uuid types.String `tfsdk:"uuid"`
42
56
ConnectionUuid types.String `tfsdk:"connection_uuid"`
43
57
Name types.String `tfsdk:"name"`
@@ -70,11 +84,24 @@ func (r *BigQueryWarehouseResource) Schema(ctx context.Context, req resource.Sch
70
84
stringplanmodifier .UseStateForUnknown (),
71
85
},
72
86
},
73
- "connection_uuid" : schema.StringAttribute {
74
- Computed : true ,
75
- Optional : false ,
76
- PlanModifiers : []planmodifier.String {
77
- stringplanmodifier .UseStateForUnknown (),
87
+ "credentials" : schema.SingleNestedAttribute {
88
+ Required : true ,
89
+ Attributes : map [string ]schema.Attribute {
90
+ "connection_uuid" : schema.StringAttribute {
91
+ Computed : true ,
92
+ Optional : false ,
93
+ PlanModifiers : []planmodifier.String {
94
+ stringplanmodifier .UseStateForUnknown (),
95
+ },
96
+ },
97
+ "service_account_key" : schema.StringAttribute {
98
+ Required : true ,
99
+ Sensitive : true ,
100
+ },
101
+ "updated_at" : schema.StringAttribute {
102
+ Computed : true ,
103
+ Optional : false ,
104
+ },
78
105
},
79
106
},
80
107
"name" : schema.StringAttribute {
@@ -87,10 +114,6 @@ func (r *BigQueryWarehouseResource) Schema(ctx context.Context, req resource.Sch
87
114
stringplanmodifier .RequiresReplaceIfConfigured (),
88
115
},
89
116
},
90
- "service_account_key" : schema.StringAttribute {
91
- Required : true ,
92
- Sensitive : true ,
93
- },
94
117
"deletion_protection" : schema.BoolAttribute {
95
118
Optional : true ,
96
119
Computed : true ,
@@ -120,8 +143,8 @@ func (r *BigQueryWarehouseResource) Create(ctx context.Context, req resource.Cre
120
143
}
121
144
122
145
data .Uuid = result .Uuid
123
- data .ConnectionUuid = result .ConnectionUuid
124
- data .Name = result .Name
146
+ data .Credentials . UpdatedAt = result .Credentials . UpdatedAt
147
+ data .Credentials . ConnectionUuid = result .Credentials . ConnectionUuid
125
148
resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
126
149
}
127
150
@@ -167,23 +190,35 @@ func (r *BigQueryWarehouseResource) Read(ctx context.Context, req resource.ReadR
167
190
}
168
191
169
192
readConnectionUuid := types .StringNull ()
170
- readServiceAccountKey := types .StringNull ()
193
+ readConnectionSAKey := types .StringNull ()
194
+ readConnectionUpdatedAt := types .StringNull ()
195
+
171
196
for _ , connection := range getResult .GetWarehouse .Connections {
172
- if connection .Uuid == data .ConnectionUuid .ValueString () {
197
+ if connection .Uuid == data .Credentials . ConnectionUuid .ValueString () {
173
198
if connection .Type != client .BigQueryConnectionTypeResponse {
174
199
resp .Diagnostics .AddError (
175
200
fmt .Sprintf ("Obtained Warehouse [uuid: %s, connection_uuid: %s] but got unexpected connection " +
176
201
"type '%s'." , data .Uuid .ValueString (), connection .Uuid , connection .Type ),
177
202
"Users can manually fix remote state or delete this resource from the Terraform configuration." )
178
203
return
179
204
}
180
- readConnectionUuid = data .ConnectionUuid
181
- readServiceAccountKey = data .ServiceAccountKey
205
+
206
+ readConnectionUuid = data .Credentials .ConnectionUuid
207
+ readConnectionSAKey = data .Credentials .ServiceAccountKey
208
+ readConnectionUpdatedAt = types .StringValue (connection .UpdatedOn )
209
+ if connection .UpdatedOn == "" {
210
+ readConnectionUpdatedAt = types .StringValue (connection .CreatedOn )
211
+ }
182
212
}
183
213
}
184
214
185
- data .ConnectionUuid = readConnectionUuid
186
- data .ServiceAccountKey = readServiceAccountKey
215
+ if ! readConnectionSAKey .IsNull () && ! readConnectionUpdatedAt .Equal (data .Credentials .UpdatedAt ) {
216
+ readConnectionSAKey = types .StringValue ("(unknown external value)" )
217
+ }
218
+
219
+ data .Credentials .UpdatedAt = readConnectionUpdatedAt
220
+ data .Credentials .ConnectionUuid = readConnectionUuid
221
+ data .Credentials .ServiceAccountKey = readConnectionSAKey
187
222
data .Name = types .StringValue (getResult .GetWarehouse .Name )
188
223
resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
189
224
}
@@ -207,10 +242,11 @@ func (r *BigQueryWarehouseResource) Update(ctx context.Context, req resource.Upd
207
242
return
208
243
}
209
244
210
- if data .ConnectionUuid .IsUnknown () || data .ConnectionUuid .IsNull () {
245
+ if data .Credentials . ConnectionUuid .IsUnknown () || data . Credentials .ConnectionUuid .IsNull () {
211
246
if result , diags := r .addConnection (ctx , data ); result != nil {
212
247
resp .Diagnostics .Append (diags ... )
213
- data .ConnectionUuid = result .ConnectionUuid
248
+ data .Credentials .UpdatedAt = result .Credentials .UpdatedAt
249
+ data .Credentials .ConnectionUuid = result .Credentials .ConnectionUuid
214
250
} else {
215
251
resp .Diagnostics .Append (diags ... )
216
252
return
@@ -219,8 +255,8 @@ func (r *BigQueryWarehouseResource) Update(ctx context.Context, req resource.Upd
219
255
220
256
updateResult := client.UpdateCredentials {}
221
257
variables = map [string ]interface {}{
222
- "changes" : client .JSONString (data .ServiceAccountKey .ValueString ()),
223
- "connectionId" : client .UUID (data .ConnectionUuid .ValueString ()),
258
+ "changes" : client .JSONString (data .Credentials . ServiceAccountKey .ValueString ()),
259
+ "connectionId" : client .UUID (data .Credentials . ConnectionUuid .ValueString ()),
224
260
"shouldReplace" : true ,
225
261
"shouldValidate" : true ,
226
262
}
@@ -235,6 +271,8 @@ func (r *BigQueryWarehouseResource) Update(ctx context.Context, req resource.Upd
235
271
resp .Diagnostics .AddError (toPrint , "" )
236
272
return
237
273
}
274
+
275
+ data .Credentials .UpdatedAt = types .StringValue (updateResult .UpdateCredentials .UpdatedAt )
238
276
resp .Diagnostics .Append (resp .State .Set (ctx , & data )... )
239
277
}
240
278
@@ -256,7 +294,7 @@ func (r *BigQueryWarehouseResource) Delete(ctx context.Context, req resource.Del
256
294
}
257
295
258
296
removeResult := client.RemoveConnection {}
259
- variables := map [string ]interface {}{"connectionId" : client .UUID (data .ConnectionUuid .ValueString ())}
297
+ variables := map [string ]interface {}{"connectionId" : client .UUID (data .Credentials . ConnectionUuid .ValueString ())}
260
298
if err := r .client .Mutate (ctx , & removeResult , variables ); err != nil {
261
299
toPrint := fmt .Sprintf ("MC client 'RemoveConnection' mutation result - %s" , err .Error ())
262
300
resp .Diagnostics .AddError (toPrint , "" )
@@ -272,7 +310,7 @@ func (r *BigQueryWarehouseResource) ImportState(ctx context.Context, req resourc
272
310
idsImported := strings .Split (req .ID , "," )
273
311
if len (idsImported ) == 3 && idsImported [0 ] != "" && idsImported [1 ] != "" && idsImported [2 ] != "" {
274
312
resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("uuid" ), idsImported [0 ])... )
275
- resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("connection_uuid" ), idsImported [1 ])... )
313
+ resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("credentials" ). AtName ( " connection_uuid" ), idsImported [1 ])... )
276
314
resp .Diagnostics .Append (resp .State .SetAttribute (ctx , path .Root ("collector_uuid" ), idsImported [2 ])... )
277
315
} else {
278
316
resp .Diagnostics .AddError ("Unexpected Import Identifier" , fmt .Sprintf (
@@ -289,7 +327,7 @@ func (r *BigQueryWarehouseResource) addConnection(ctx context.Context, data BigQ
289
327
"validationName" : "save_credentials" ,
290
328
"connectionDetails" : BqConnectionDetails {
291
329
"serviceJson" : b64 .StdEncoding .EncodeToString (
292
- []byte (data .ServiceAccountKey .ValueString ()),
330
+ []byte (data .Credentials . ServiceAccountKey .ValueString ()),
293
331
),
294
332
},
295
333
}
@@ -334,8 +372,8 @@ func (r *BigQueryWarehouseResource) addConnection(ctx context.Context, data BigQ
334
372
}
335
373
336
374
data .Uuid = types .StringValue (addResult .AddConnection .Connection .Warehouse .Uuid )
337
- data .ConnectionUuid = types .StringValue (addResult .AddConnection .Connection .Uuid )
338
- data .Name = types .StringValue (addResult .AddConnection .Connection .Warehouse . Name )
375
+ data .Credentials . UpdatedAt = types .StringValue (addResult .AddConnection .Connection .CreatedOn )
376
+ data .Credentials . ConnectionUuid = types .StringValue (addResult .AddConnection .Connection .Uuid )
339
377
return & data , diagsResult
340
378
}
341
379
@@ -398,7 +436,7 @@ func (r *BigQueryWarehouseResource) UpgradeState(ctx context.Context) map[int64]
398
436
var priorStateData BigQueryWarehouseResourceModelV0
399
437
resp .Diagnostics .Append (req .State .Get (ctx , & priorStateData )... )
400
438
if ! resp .Diagnostics .HasError () {
401
- upgradedStateData := BigQueryWarehouseResourceModel {
439
+ upgradedStateData := BigQueryWarehouseResourceModelV1 {
402
440
Uuid : priorStateData .Uuid ,
403
441
ConnectionUuid : priorStateData .ConnectionUuid ,
404
442
CollectorUuid : priorStateData .DataCollectorUuid ,
@@ -410,5 +448,62 @@ func (r *BigQueryWarehouseResource) UpgradeState(ctx context.Context) map[int64]
410
448
}
411
449
},
412
450
},
451
+ 1 : {
452
+ PriorSchema : & schema.Schema {
453
+ Attributes : map [string ]schema.Attribute {
454
+ "uuid" : schema.StringAttribute {
455
+ Computed : true ,
456
+ Optional : false ,
457
+ PlanModifiers : []planmodifier.String {
458
+ stringplanmodifier .UseStateForUnknown (),
459
+ },
460
+ },
461
+ "connection_uuid" : schema.StringAttribute {
462
+ Computed : true ,
463
+ Optional : false ,
464
+ PlanModifiers : []planmodifier.String {
465
+ stringplanmodifier .UseStateForUnknown (),
466
+ },
467
+ },
468
+ "name" : schema.StringAttribute {
469
+ Required : true ,
470
+ Validators : []validator.String {stringvalidator .LengthAtLeast (1 )},
471
+ },
472
+ "collector_uuid" : schema.StringAttribute {
473
+ Required : true ,
474
+ PlanModifiers : []planmodifier.String {
475
+ stringplanmodifier .RequiresReplaceIfConfigured (),
476
+ },
477
+ },
478
+ "service_account_key" : schema.StringAttribute {
479
+ Required : true ,
480
+ Sensitive : true ,
481
+ },
482
+ "deletion_protection" : schema.BoolAttribute {
483
+ Optional : true ,
484
+ Computed : true ,
485
+ Default : booldefault .StaticBool (true ),
486
+ },
487
+ },
488
+ },
489
+ StateUpgrader : func (ctx context.Context , req resource.UpgradeStateRequest , resp * resource.UpgradeStateResponse ) {
490
+ var priorStateData BigQueryWarehouseResourceModelV1
491
+ resp .Diagnostics .Append (req .State .Get (ctx , & priorStateData )... )
492
+ if ! resp .Diagnostics .HasError () {
493
+ upgradedStateData := BigQueryWarehouseResourceModel {
494
+ Uuid : priorStateData .Uuid ,
495
+ CollectorUuid : priorStateData .CollectorUuid ,
496
+ Name : priorStateData .Name ,
497
+ DeletionProtection : priorStateData .DeletionProtection ,
498
+ Credentials : Credentials {
499
+ ConnectionUuid : priorStateData .ConnectionUuid ,
500
+ ServiceAccountKey : priorStateData .ServiceAccountKey ,
501
+ UpdatedAt : types .StringNull (),
502
+ },
503
+ }
504
+ resp .Diagnostics .Append (resp .State .Set (ctx , upgradedStateData )... )
505
+ }
506
+ },
507
+ },
413
508
}
414
509
}
0 commit comments