@@ -106,25 +106,29 @@ func resourceInstance() *schema.Resource {
106
106
"password" : {
107
107
Type : schema .TypeString ,
108
108
Optional : true ,
109
+ Sensitive : true ,
109
110
Default : "" ,
110
111
Description : "The connection user password used by Bytebase to perform DDL and DML operations." ,
111
112
},
112
113
"ssl_ca" : {
113
114
Type : schema .TypeString ,
114
115
Optional : true ,
115
116
Default : "" ,
117
+ Sensitive : true ,
116
118
Description : "The CA certificate. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE." ,
117
119
},
118
120
"ssl_cert" : {
119
121
Type : schema .TypeString ,
120
122
Optional : true ,
121
123
Default : "" ,
124
+ Sensitive : true ,
122
125
Description : "The client certificate. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE." ,
123
126
},
124
127
"ssl_key" : {
125
128
Type : schema .TypeString ,
126
129
Optional : true ,
127
130
Default : "" ,
131
+ Sensitive : true ,
128
132
Description : "The client key. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE." ,
129
133
},
130
134
"host" : {
@@ -155,7 +159,7 @@ func resourceInstance() *schema.Resource {
155
159
func resourceInstanceCreate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
156
160
c := m .(api.Client )
157
161
158
- dataSourceList , err := convertDataSourceCreateList (d )
162
+ dataSourceList , err := convertDataSourceCreateList (d , true /* validate */ )
159
163
if err != nil {
160
164
return diag .FromErr (err )
161
165
}
@@ -309,7 +313,7 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
309
313
patch .ExternalLink = & v
310
314
}
311
315
if d .HasChange ("data_sources" ) {
312
- dataSourceList , err := convertDataSourceCreateList (d )
316
+ dataSourceList , err := convertDataSourceCreateList (d , true /* validate */ )
313
317
if err != nil {
314
318
return diag .FromErr (err )
315
319
}
@@ -374,14 +378,28 @@ func setInstanceMessage(d *schema.ResourceData, instance *api.InstanceMessage) d
374
378
if err := d .Set ("external_link" , instance .ExternalLink ); err != nil {
375
379
return diag .Errorf ("cannot set external_link for instance: %s" , err .Error ())
376
380
}
377
- if err := d .Set ("data_sources" , flattenDataSourceList (instance .DataSources )); err != nil {
381
+
382
+ dataSources , err := flattenDataSourceList (d , instance .DataSources )
383
+ if err != nil {
384
+ return diag .FromErr (err )
385
+ }
386
+ if err := d .Set ("data_sources" , dataSources ); err != nil {
378
387
return diag .Errorf ("cannot set data_sources for instance: %s" , err .Error ())
379
388
}
380
389
381
390
return nil
382
391
}
383
392
384
- func flattenDataSourceList (dataSourceList []* api.DataSourceMessage ) []interface {} {
393
+ func flattenDataSourceList (d * schema.ResourceData , dataSourceList []* api.DataSourceMessage ) ([]interface {}, error ) {
394
+ oldDataSourceList , err := convertDataSourceCreateList (d , false )
395
+ if err != nil {
396
+ return nil , err
397
+ }
398
+ oldDataSourceMap := make (map [string ]* api.DataSourceMessage )
399
+ for _ , ds := range oldDataSourceList {
400
+ oldDataSourceMap [ds .ID ] = ds
401
+ }
402
+
385
403
res := []interface {}{}
386
404
for _ , dataSource := range dataSourceList {
387
405
raw := map [string ]interface {}{}
@@ -390,17 +408,21 @@ func flattenDataSourceList(dataSourceList []*api.DataSourceMessage) []interface{
390
408
raw ["username" ] = dataSource .Username
391
409
raw ["host" ] = dataSource .Host
392
410
raw ["port" ] = dataSource .Port
393
- raw ["password" ] = dataSource .Password
394
- raw ["ssl_ca" ] = dataSource .SslCa
395
- raw ["ssl_cert" ] = dataSource .SslCert
396
- raw ["ssl_key" ] = dataSource .SslKey
397
411
raw ["database" ] = dataSource .Database
412
+
413
+ // These sensitive fields won't returned in the API. Propagate state value.
414
+ if ds , ok := oldDataSourceMap [dataSource .ID ]; ok {
415
+ raw ["password" ] = ds .Password
416
+ raw ["ssl_ca" ] = ds .SslCa
417
+ raw ["ssl_cert" ] = ds .SslCert
418
+ raw ["ssl_key" ] = ds .SslKey
419
+ }
398
420
res = append (res , raw )
399
421
}
400
- return res
422
+ return res , nil
401
423
}
402
424
403
- func convertDataSourceCreateList (d * schema.ResourceData ) ([]* api.DataSourceMessage , error ) {
425
+ func convertDataSourceCreateList (d * schema.ResourceData , validate bool ) ([]* api.DataSourceMessage , error ) {
404
426
var dataSourceList []* api.DataSourceMessage
405
427
if rawList , ok := d .Get ("data_sources" ).([]interface {}); ok {
406
428
dataSourceTypeMap := map [api.DataSourceType ]bool {}
@@ -410,9 +432,8 @@ func convertDataSourceCreateList(d *schema.ResourceData) ([]*api.DataSourceMessa
410
432
ID : obj ["id" ].(string ),
411
433
Type : api .DataSourceType (obj ["type" ].(string )),
412
434
}
413
-
414
- if dataSourceTypeMap [dataSource .Type ] {
415
- return nil , errors .Errorf ("duplicate data source type %s" , dataSource .Type )
435
+ if dataSourceTypeMap [dataSource .Type ] && dataSource .Type == api .DataSourceAdmin {
436
+ return nil , errors .Errorf ("duplicate data source type ADMIN" )
416
437
}
417
438
dataSourceTypeMap [dataSource .Type ] = true
418
439
@@ -443,7 +464,7 @@ func convertDataSourceCreateList(d *schema.ResourceData) ([]*api.DataSourceMessa
443
464
dataSourceList = append (dataSourceList , dataSource )
444
465
}
445
466
446
- if ! dataSourceTypeMap [api .DataSourceAdmin ] {
467
+ if ! dataSourceTypeMap [api .DataSourceAdmin ] && validate {
447
468
return nil , errors .Errorf ("data source \" %v\" is required" , api .DataSourceAdmin )
448
469
}
449
470
}
0 commit comments