@@ -834,28 +834,41 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
834
834
835
835
_ = d .Set ("project_id" , int (* instance .ProjectId ))
836
836
_ = d .Set ("availability_zone" , instance .Zone )
837
- _ = d .Set ("vpc_id" , instance .VpcId )
838
- _ = d .Set ("subnet_id" , instance .SubnetId )
839
837
_ = d .Set ("engine_version" , instance .DBVersion )
840
838
_ = d .Set ("db_kernel_version" , instance .DBKernelVersion )
841
839
_ = d .Set ("db_major_vesion" , instance .DBMajorVersion )
842
840
_ = d .Set ("db_major_version" , instance .DBMajorVersion )
843
841
_ = d .Set ("name" , instance .DBInstanceName )
844
842
_ = d .Set ("charset" , instance .DBCharset )
845
843
846
- if rootUser != "" {
847
- _ = d .Set ("root_user" , & rootUser )
848
- }
844
+ // check net num
845
+ if len (instance .DBInstanceNetInfo ) == 3 {
846
+ _ = d .Set ("vpc_id" , instance .DBInstanceNetInfo [0 ].VpcId )
847
+ _ = d .Set ("subnet_id" , instance .DBInstanceNetInfo [0 ].SubnetId )
848
+ _ = d .Set ("private_access_ip" , instance .DBInstanceNetInfo [0 ].Ip )
849
+ _ = d .Set ("private_access_port" , instance .DBInstanceNetInfo [0 ].Port )
849
850
850
- if * instance .PayType == POSTGRESQL_PAYTYPE_PREPAID || * instance .PayType == COMMON_PAYTYPE_PREPAID {
851
- _ = d .Set ("charge_type" , COMMON_PAYTYPE_PREPAID )
852
- } else {
853
- _ = d .Set ("charge_type" , COMMON_PAYTYPE_POSTPAID )
854
- }
851
+ // net status
852
+ public_access_switch := false
853
+ for _ , v := range instance .DBInstanceNetInfo {
854
+ if * v .NetType == "public" {
855
+ // both 1 and opened used in SDK
856
+ if * v .Status == "opened" || * v .Status == "1" {
857
+ public_access_switch = true
858
+ }
855
859
856
- // net status
857
- public_access_switch := false
858
- if len (instance .DBInstanceNetInfo ) > 0 {
860
+ _ = d .Set ("public_access_host" , v .Address )
861
+ _ = d .Set ("public_access_port" , v .Port )
862
+ }
863
+ }
864
+
865
+ _ = d .Set ("public_access_switch" , public_access_switch )
866
+ } else if len (instance .DBInstanceNetInfo ) == 2 {
867
+ _ = d .Set ("vpc_id" , instance .VpcId )
868
+ _ = d .Set ("subnet_id" , instance .SubnetId )
869
+
870
+ // net status
871
+ public_access_switch := false
859
872
for _ , v := range instance .DBInstanceNetInfo {
860
873
if * v .NetType == "public" {
861
874
// both 1 and opened used in SDK
@@ -873,9 +886,21 @@ func resourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta int
873
886
_ = d .Set ("private_access_port" , v .Port )
874
887
}
875
888
}
889
+
890
+ _ = d .Set ("public_access_switch" , public_access_switch )
891
+ } else {
892
+ return fmt .Errorf ("DBInstanceNetInfo returned incorrect information." )
893
+ }
894
+
895
+ if rootUser != "" {
896
+ _ = d .Set ("root_user" , & rootUser )
876
897
}
877
898
878
- _ = d .Set ("public_access_switch" , public_access_switch )
899
+ if * instance .PayType == POSTGRESQL_PAYTYPE_PREPAID || * instance .PayType == COMMON_PAYTYPE_PREPAID {
900
+ _ = d .Set ("charge_type" , COMMON_PAYTYPE_PREPAID )
901
+ } else {
902
+ _ = d .Set ("charge_type" , COMMON_PAYTYPE_POSTPAID )
903
+ }
879
904
880
905
// security groups
881
906
sg , err := postgresqlService .DescribeDBInstanceSecurityGroupsById (ctx , d .Id ())
@@ -1227,95 +1252,130 @@ func resourceTencentCloudPostgresqlInstanceUpdate(d *schema.ResourceData, meta i
1227
1252
// update vpc and subnet
1228
1253
if d .HasChange ("vpc_id" ) || d .HasChange ("subnet_id" ) {
1229
1254
var (
1230
- vpcOld string
1231
- vpcNew string
1232
- subnetOld string
1233
- subnetNew string
1234
- vipOld string
1235
- vipNew string
1255
+ postgresqlService = PostgresqlService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
1256
+ instance * postgresql.DBInstance
1257
+ has bool
1258
+ vpcOld string
1259
+ vpcNew string
1260
+ subnetOld string
1261
+ subnetNew string
1262
+ vipOld string
1263
+ vipNew string
1236
1264
)
1237
1265
1238
- old , new := d .GetChange ("vpc_id" )
1239
- if old != nil {
1240
- vpcOld = old .(string )
1241
- }
1242
-
1243
- if new != nil {
1244
- vpcNew = new .(string )
1245
- }
1246
-
1247
- old , new = d .GetChange ("subnet_id" )
1248
- if old != nil {
1249
- subnetOld = old .(string )
1250
- }
1266
+ // check net first
1267
+ outErr = resource .Retry (tccommon .ReadRetryTimeout , func () * resource.RetryError {
1268
+ instance , has , inErr = postgresqlService .DescribePostgresqlInstanceById (ctx , d .Id ())
1269
+ if inErr != nil {
1270
+ ee , ok := inErr .(* sdkErrors.TencentCloudSDKError )
1271
+ if ok && (ee .GetCode () == "ResourceNotFound.InstanceNotFoundError" || ee .GetCode () == "InvalidParameter" ) {
1272
+ return nil
1273
+ }
1251
1274
1252
- if new != nil {
1253
- subnetNew = new .(string )
1254
- }
1275
+ return tccommon .RetryError (inErr )
1276
+ }
1255
1277
1256
- // Create new network first, then delete the old one
1257
- request := postgresql .NewCreateDBInstanceNetworkAccessRequest ()
1258
- request .DBInstanceId = helper .String (instanceId )
1259
- request .VpcId = helper .String (vpcNew )
1260
- request .SubnetId = helper .String (subnetNew )
1261
- // ip assigned by system
1262
- request .IsAssignVip = helper .Bool (false )
1263
- err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
1264
- result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlClient ().CreateDBInstanceNetworkAccess (request )
1265
- if e != nil {
1266
- return tccommon .RetryError (e )
1267
- } else {
1268
- log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
1278
+ if instance != nil && tccommon .IsContains (POSTGRESQL_RETRYABLE_STATUS , * instance .DBInstanceStatus ) {
1279
+ return resource .RetryableError (fmt .Errorf ("instance %s is %s, retrying" , * instance .DBInstanceId , * instance .DBInstanceStatus ))
1269
1280
}
1270
1281
1271
1282
return nil
1272
1283
})
1273
1284
1274
- if err != nil {
1275
- log .Printf ("[CRITAL]%s create postgresql Instance NetworkAccess failed, reason:%+v" , logId , err )
1276
- return err
1285
+ if outErr != nil {
1286
+ return outErr
1277
1287
}
1278
1288
1279
- service := PostgresqlService {client : meta .(tccommon.ProviderMeta ).GetAPIV3Conn ()}
1280
- // wait for new network enabled
1281
- conf := tccommon .BuildStateChangeConf ([]string {}, []string {"opened" }, 3 * tccommon .ReadRetryTimeout , time .Second , service .PostgresqlDBInstanceNetworkAccessStateRefreshFunc (instanceId , vpcNew , subnetNew , vipOld , "" , []string {}))
1282
- if object , e := conf .WaitForState (); e != nil {
1283
- return e
1284
- } else {
1285
- // find the vip assiged by system
1286
- ret := object .(* postgresql.DBInstanceNetInfo )
1287
- vipNew = * ret .Ip
1289
+ if ! has {
1290
+ d .SetId ("" )
1291
+ return nil
1288
1292
}
1289
1293
1290
- // wait unit network changing operation of instance done
1291
- conf = tccommon .BuildStateChangeConf ([]string {}, []string {"running" }, 3 * tccommon .ReadRetryTimeout , time .Second , service .PostgresqlDBInstanceStateRefreshFunc (instanceId , []string {}))
1292
- if _ , e := conf .WaitForState (); e != nil {
1293
- return e
1294
- }
1294
+ // check net num
1295
+ if instance .DBInstanceNetInfo != nil && len (instance .DBInstanceNetInfo ) > 2 {
1296
+ return fmt .Errorf ("There are already %d network information for the current PostgreSQL instance %s. Please remove one before modifying the instance network information." , len (instance .DBInstanceNetInfo )- 1 , d .Id ())
1297
+ } else {
1298
+ old , new := d .GetChange ("vpc_id" )
1299
+ if old != nil {
1300
+ vpcOld = old .(string )
1301
+ }
1295
1302
1296
- // delete the old one
1297
- if v , ok := d .GetOk ("private_access_ip" ); ok {
1298
- vipOld = v .(string )
1299
- }
1303
+ if new != nil {
1304
+ vpcNew = new .(string )
1305
+ }
1300
1306
1301
- if err := service .DeletePostgresqlDBInstanceNetworkAccessById (ctx , instanceId , vpcOld , subnetOld , vipOld ); err != nil {
1302
- return err
1303
- }
1307
+ old , new = d .GetChange ("subnet_id" )
1308
+ if old != nil {
1309
+ subnetOld = old .(string )
1310
+ }
1304
1311
1305
- // wait for old network removed
1306
- conf = tccommon .BuildStateChangeConf ([]string {}, []string {"closed" }, 3 * tccommon .ReadRetryTimeout , time .Second , service .PostgresqlDBInstanceNetworkAccessStateRefreshFunc (instanceId , vpcOld , subnetOld , vipNew , vipOld , []string {}))
1307
- if _ , e := conf .WaitForState (); e != nil {
1308
- return e
1309
- }
1312
+ if new != nil {
1313
+ subnetNew = new .(string )
1314
+ }
1310
1315
1311
- // wait unit network changing operation of instance done
1312
- conf = tccommon .BuildStateChangeConf ([]string {}, []string {"running" }, 3 * tccommon .ReadRetryTimeout , time .Second , service .PostgresqlDBInstanceStateRefreshFunc (instanceId , []string {}))
1313
- if _ , e := conf .WaitForState (); e != nil {
1314
- return e
1315
- }
1316
+ // Create new network first, then delete the old one
1317
+ request := postgresql .NewCreateDBInstanceNetworkAccessRequest ()
1318
+ request .DBInstanceId = helper .String (instanceId )
1319
+ request .VpcId = helper .String (vpcNew )
1320
+ request .SubnetId = helper .String (subnetNew )
1321
+ // ip assigned by system
1322
+ request .IsAssignVip = helper .Bool (false )
1323
+ err := resource .Retry (tccommon .WriteRetryTimeout , func () * resource.RetryError {
1324
+ result , e := meta .(tccommon.ProviderMeta ).GetAPIV3Conn ().UsePostgresqlClient ().CreateDBInstanceNetworkAccess (request )
1325
+ if e != nil {
1326
+ return tccommon .RetryError (e )
1327
+ } else {
1328
+ log .Printf ("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n " , logId , request .GetAction (), request .ToJsonString (), result .ToJsonString ())
1329
+ }
1316
1330
1317
- // refresh the private ip with new one
1318
- _ = d .Set ("private_access_ip" , vipNew )
1331
+ return nil
1332
+ })
1333
+
1334
+ if err != nil {
1335
+ log .Printf ("[CRITAL]%s create postgresql Instance NetworkAccess failed, reason:%+v" , logId , err )
1336
+ return err
1337
+ }
1338
+
1339
+ // wait for new network enabled
1340
+ conf := tccommon .BuildStateChangeConf ([]string {}, []string {"opened" }, 3 * tccommon .ReadRetryTimeout , time .Second , postgresqlService .PostgresqlDBInstanceNetworkAccessStateRefreshFunc (instanceId , vpcNew , subnetNew , vipOld , "" , []string {}))
1341
+ if object , e := conf .WaitForState (); e != nil {
1342
+ return e
1343
+ } else {
1344
+ // find the vip assiged by system
1345
+ ret := object .(* postgresql.DBInstanceNetInfo )
1346
+ vipNew = * ret .Ip
1347
+ }
1348
+
1349
+ // wait unit network changing operation of instance done
1350
+ conf = tccommon .BuildStateChangeConf ([]string {}, []string {"running" }, 3 * tccommon .ReadRetryTimeout , time .Second , postgresqlService .PostgresqlDBInstanceStateRefreshFunc (instanceId , []string {}))
1351
+ if _ , e := conf .WaitForState (); e != nil {
1352
+ return e
1353
+ }
1354
+
1355
+ // delete the old one
1356
+ if v , ok := d .GetOk ("private_access_ip" ); ok {
1357
+ vipOld = v .(string )
1358
+ }
1359
+
1360
+ if err := postgresqlService .DeletePostgresqlDBInstanceNetworkAccessById (ctx , instanceId , vpcOld , subnetOld , vipOld ); err != nil {
1361
+ return err
1362
+ }
1363
+
1364
+ // wait for old network removed
1365
+ conf = tccommon .BuildStateChangeConf ([]string {}, []string {"closed" }, 3 * tccommon .ReadRetryTimeout , time .Second , postgresqlService .PostgresqlDBInstanceNetworkAccessStateRefreshFunc (instanceId , vpcOld , subnetOld , vipNew , vipOld , []string {}))
1366
+ if _ , e := conf .WaitForState (); e != nil {
1367
+ return e
1368
+ }
1369
+
1370
+ // wait unit network changing operation of instance done
1371
+ conf = tccommon .BuildStateChangeConf ([]string {}, []string {"running" }, 3 * tccommon .ReadRetryTimeout , time .Second , postgresqlService .PostgresqlDBInstanceStateRefreshFunc (instanceId , []string {}))
1372
+ if _ , e := conf .WaitForState (); e != nil {
1373
+ return e
1374
+ }
1375
+
1376
+ // refresh the private ip with new one
1377
+ _ = d .Set ("private_access_ip" , vipNew )
1378
+ }
1319
1379
}
1320
1380
1321
1381
// update name
0 commit comments