Skip to content

Commit 23cd687

Browse files
authored
fix(crs): [122437999] support wan_address fileds (#3241)
* fix(crs): [122437999] support wan_address : command not found fileds * feat: add changelog
1 parent 63d2d45 commit 23cd687

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

.changelog/3241.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_redis_instance: support `wan_address` fileds.
3+
```

tencentcloud/services/crs/resource_tc_redis_instance.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,17 @@ func ResourceTencentCloudRedisInstance() *schema.Resource {
271271
},
272272
},
273273
},
274+
"wan_address_switch": {
275+
Type: schema.TypeString,
276+
Optional: true,
277+
Computed: true,
278+
Description: "Wan address switch, default `close`, values: `open`, `close`.",
279+
},
280+
"wan_address": {
281+
Type: schema.TypeString,
282+
Computed: true,
283+
Description: "Allocate Wan Address.",
284+
},
274285
},
275286
}
276287
}
@@ -522,6 +533,13 @@ func resourceTencentCloudRedisInstanceCreate(d *schema.ResourceData, meta interf
522533
}
523534
//internal version: replace null end, please do not modify this annotation and refrain from inserting any code between the beginning and end lines of the annotation.
524535

536+
if v, ok := d.GetOk("wan_address_switch"); ok {
537+
err := resourceRedisWanAddressModify(ctx, &redisService, meta, d.Id(), v.(string))
538+
if err != nil {
539+
return err
540+
}
541+
}
542+
525543
return resourceTencentCloudRedisInstanceRead(d, meta)
526544
}
527545

@@ -684,6 +702,14 @@ func resourceTencentCloudRedisInstanceRead(d *schema.ResourceData, meta interfac
684702
_ = d.Set("tags", tags)
685703

686704
_ = d.Set("charge_type", REDIS_CHARGE_TYPE_NAME[*info.BillingMode])
705+
706+
if info.WanAddress != nil && *info.WanAddress != "" {
707+
_ = d.Set("wan_address", info.WanAddress)
708+
_ = d.Set("wan_address_switch", "open")
709+
} else {
710+
_ = d.Set("wan_address_switch", "close")
711+
}
712+
687713
return nil
688714
}
689715

@@ -1018,6 +1044,13 @@ func resourceTencentCloudRedisInstanceUpdate(d *schema.ResourceData, meta interf
10181044
_ = d.Set("operation_network", operation)
10191045
}
10201046

1047+
if d.HasChange("wan_address_switch") {
1048+
err := resourceRedisWanAddressModify(ctx, &redisService, meta, d.Id(), d.Get("wan_address_switch").(string))
1049+
if err != nil {
1050+
return err
1051+
}
1052+
}
1053+
10211054
if d.HasChange("tags") {
10221055
oldTags, newTags := d.GetChange("tags")
10231056
replaceTags, deleteTags := svctag.DiffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{}))
@@ -1292,3 +1325,67 @@ func TencentCloudRedisGetRemoveNodesByIds(ids []int, nodes []*redis.RedisNodeInf
12921325
}
12931326
return
12941327
}
1328+
1329+
func resourceRedisWanAddressModify(ctx context.Context, service *RedisService, meta interface{}, instanceId, addressSwitch string) error {
1330+
instance, err := service.DescribeRedisInstanceById(ctx, instanceId)
1331+
if err != nil {
1332+
return err
1333+
}
1334+
1335+
if addressSwitch == "close" {
1336+
if instance.WanAddress != nil && *instance.WanAddress != "" {
1337+
request := redis.NewReleaseWanAddressRequest()
1338+
request.InstanceId = helper.String(instanceId)
1339+
1340+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1341+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseRedisClient().ReleaseWanAddressWithContext(ctx, request)
1342+
if e != nil {
1343+
return tccommon.RetryError(e)
1344+
} else {
1345+
log.Printf("[DEBUG] api[%s] success, request body [%s], response body [%s]\n", request.GetAction(), request.ToJsonString(), result.ToJsonString())
1346+
}
1347+
return nil
1348+
})
1349+
if reqErr != nil {
1350+
log.Printf("[CRITAL] delete redis wan address failed, reason:%+v", reqErr)
1351+
return reqErr
1352+
}
1353+
1354+
_, _, _, err := service.CheckRedisOnlineOk(ctx, instanceId, 20*tccommon.ReadRetryTimeout)
1355+
if err != nil {
1356+
log.Printf("[CRITAL] redis networkConfig fail, reason:%s\n", err.Error())
1357+
return err
1358+
}
1359+
}
1360+
} else if addressSwitch == "open" {
1361+
if instance.WanAddress == nil || *instance.WanAddress == "" {
1362+
request := redis.NewAllocateWanAddressRequest()
1363+
request.InstanceId = helper.String(instanceId)
1364+
1365+
reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
1366+
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseRedisClient().AllocateWanAddressWithContext(ctx, request)
1367+
if e != nil {
1368+
return tccommon.RetryError(e)
1369+
} else {
1370+
log.Printf("[DEBUG] api[%s] success, request body [%s], response body [%s]\n", request.GetAction(), request.ToJsonString(), result.ToJsonString())
1371+
}
1372+
return nil
1373+
})
1374+
if reqErr != nil {
1375+
log.Printf("[CRITAL] create redis wan address failed, reason:%+v", reqErr)
1376+
return reqErr
1377+
}
1378+
1379+
service := RedisService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
1380+
_, _, _, err := service.CheckRedisOnlineOk(ctx, instanceId, 20*tccommon.ReadRetryTimeout)
1381+
if err != nil {
1382+
log.Printf("[CRITAL] redis networkConfig fail, reason:%s\n", err.Error())
1383+
return err
1384+
}
1385+
}
1386+
} else {
1387+
return fmt.Errorf("invalid address_switch %s", addressSwitch)
1388+
}
1389+
1390+
return nil
1391+
}

website/docs/r/redis_instance.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ The following arguments are supported:
314314
* `type` - (Optional, String, ForceNew, **Deprecated**) It has been deprecated from version 1.33.1. Please use 'type_id' instead. Instance type. Available values: `cluster_ckv`,`cluster_redis5.0`,`cluster_redis`,`master_slave_ckv`,`master_slave_redis4.0`,`master_slave_redis5.0`,`master_slave_redis`,`standalone_redis`, specific region support specific types, need to refer data `tencentcloud_redis_zone_config`.
315315
* `vpc_id` - (Optional, String) ID of the vpc with which the instance is to be associated. When the `operation_network` is `changeVpc` or `changeBaseToVpc`, this parameter needs to be configured.
316316
* `wait_switch` - (Optional, Int) Switching mode: `1`-maintenance time window switching, `2`-immediate switching, default value `2`.
317+
* `wan_address_switch` - (Optional, String) Wan address switch, default `close`, values: `open`, `close`.
317318

318319
## Attributes Reference
319320

@@ -327,6 +328,7 @@ In addition to all arguments above, the following attributes are exported:
327328
* `master` - Indicates whether the node is master.
328329
* `zone_id` - ID of the availability zone of the master or replica node.
329330
* `status` - Current status of an instance, maybe: init, processing, online, isolate and todelete.
331+
* `wan_address` - Allocate Wan Address.
330332

331333

332334
## Import

0 commit comments

Comments
 (0)