@@ -106,25 +106,29 @@ func resourceInstance() *schema.Resource {
106106 "password" : {
107107 Type : schema .TypeString ,
108108 Optional : true ,
109+ Sensitive : true ,
109110 Default : "" ,
110111 Description : "The connection user password used by Bytebase to perform DDL and DML operations." ,
111112 },
112113 "ssl_ca" : {
113114 Type : schema .TypeString ,
114115 Optional : true ,
115116 Default : "" ,
117+ Sensitive : true ,
116118 Description : "The CA certificate. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE." ,
117119 },
118120 "ssl_cert" : {
119121 Type : schema .TypeString ,
120122 Optional : true ,
121123 Default : "" ,
124+ Sensitive : true ,
122125 Description : "The client certificate. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE." ,
123126 },
124127 "ssl_key" : {
125128 Type : schema .TypeString ,
126129 Optional : true ,
127130 Default : "" ,
131+ Sensitive : true ,
128132 Description : "The client key. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE." ,
129133 },
130134 "host" : {
@@ -155,7 +159,7 @@ func resourceInstance() *schema.Resource {
155159func resourceInstanceCreate (ctx context.Context , d * schema.ResourceData , m interface {}) diag.Diagnostics {
156160 c := m .(api.Client )
157161
158- dataSourceList , err := convertDataSourceCreateList (d )
162+ dataSourceList , err := convertDataSourceCreateList (d , true /* validate */ )
159163 if err != nil {
160164 return diag .FromErr (err )
161165 }
@@ -309,7 +313,7 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
309313 patch .ExternalLink = & v
310314 }
311315 if d .HasChange ("data_sources" ) {
312- dataSourceList , err := convertDataSourceCreateList (d )
316+ dataSourceList , err := convertDataSourceCreateList (d , true /* validate */ )
313317 if err != nil {
314318 return diag .FromErr (err )
315319 }
@@ -374,14 +378,28 @@ func setInstanceMessage(d *schema.ResourceData, instance *api.InstanceMessage) d
374378 if err := d .Set ("external_link" , instance .ExternalLink ); err != nil {
375379 return diag .Errorf ("cannot set external_link for instance: %s" , err .Error ())
376380 }
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 {
378387 return diag .Errorf ("cannot set data_sources for instance: %s" , err .Error ())
379388 }
380389
381390 return nil
382391}
383392
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+
385403 res := []interface {}{}
386404 for _ , dataSource := range dataSourceList {
387405 raw := map [string ]interface {}{}
@@ -390,17 +408,21 @@ func flattenDataSourceList(dataSourceList []*api.DataSourceMessage) []interface{
390408 raw ["username" ] = dataSource .Username
391409 raw ["host" ] = dataSource .Host
392410 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
397411 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+ }
398420 res = append (res , raw )
399421 }
400- return res
422+ return res , nil
401423}
402424
403- func convertDataSourceCreateList (d * schema.ResourceData ) ([]* api.DataSourceMessage , error ) {
425+ func convertDataSourceCreateList (d * schema.ResourceData , validate bool ) ([]* api.DataSourceMessage , error ) {
404426 var dataSourceList []* api.DataSourceMessage
405427 if rawList , ok := d .Get ("data_sources" ).([]interface {}); ok {
406428 dataSourceTypeMap := map [api.DataSourceType ]bool {}
@@ -410,9 +432,8 @@ func convertDataSourceCreateList(d *schema.ResourceData) ([]*api.DataSourceMessa
410432 ID : obj ["id" ].(string ),
411433 Type : api .DataSourceType (obj ["type" ].(string )),
412434 }
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" )
416437 }
417438 dataSourceTypeMap [dataSource .Type ] = true
418439
@@ -443,7 +464,7 @@ func convertDataSourceCreateList(d *schema.ResourceData) ([]*api.DataSourceMessa
443464 dataSourceList = append (dataSourceList , dataSource )
444465 }
445466
446- if ! dataSourceTypeMap [api .DataSourceAdmin ] {
467+ if ! dataSourceTypeMap [api .DataSourceAdmin ] && validate {
447468 return nil , errors .Errorf ("data source \" %v\" is required" , api .DataSourceAdmin )
448469 }
449470 }
0 commit comments