Skip to content

fix(crs): [122437999] support wan_address fileds #3241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/3241.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_redis_instance: support `wan_address` fileds.
```
97 changes: 97 additions & 0 deletions tencentcloud/services/crs/resource_tc_redis_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,17 @@ func ResourceTencentCloudRedisInstance() *schema.Resource {
},
},
},
"wan_address_switch": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Wan address switch, default `close`, values: `open`, `close`.",
},
"wan_address": {
Type: schema.TypeString,
Computed: true,
Description: "Allocate Wan Address.",
},
},
}
}
Expand Down Expand Up @@ -522,6 +533,13 @@ func resourceTencentCloudRedisInstanceCreate(d *schema.ResourceData, meta interf
}
//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.

if v, ok := d.GetOk("wan_address_switch"); ok {
err := resourceRedisWanAddressModify(ctx, &redisService, meta, d.Id(), v.(string))
if err != nil {
return err
}
}

return resourceTencentCloudRedisInstanceRead(d, meta)
}

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

_ = d.Set("charge_type", REDIS_CHARGE_TYPE_NAME[*info.BillingMode])

if info.WanAddress != nil && *info.WanAddress != "" {
_ = d.Set("wan_address", info.WanAddress)
_ = d.Set("wan_address_switch", "open")
} else {
_ = d.Set("wan_address_switch", "close")
}

return nil
}

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

if d.HasChange("wan_address_switch") {
err := resourceRedisWanAddressModify(ctx, &redisService, meta, d.Id(), d.Get("wan_address_switch").(string))
if err != nil {
return err
}
}

if d.HasChange("tags") {
oldTags, newTags := d.GetChange("tags")
replaceTags, deleteTags := svctag.DiffTags(oldTags.(map[string]interface{}), newTags.(map[string]interface{}))
Expand Down Expand Up @@ -1292,3 +1325,67 @@ func TencentCloudRedisGetRemoveNodesByIds(ids []int, nodes []*redis.RedisNodeInf
}
return
}

func resourceRedisWanAddressModify(ctx context.Context, service *RedisService, meta interface{}, instanceId, addressSwitch string) error {
instance, err := service.DescribeRedisInstanceById(ctx, instanceId)
if err != nil {
return err
}

if addressSwitch == "close" {
if instance.WanAddress != nil && *instance.WanAddress != "" {
request := redis.NewReleaseWanAddressRequest()
request.InstanceId = helper.String(instanceId)

reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseRedisClient().ReleaseWanAddressWithContext(ctx, request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG] api[%s] success, request body [%s], response body [%s]\n", request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if reqErr != nil {
log.Printf("[CRITAL] delete redis wan address failed, reason:%+v", reqErr)
return reqErr
}

_, _, _, err := service.CheckRedisOnlineOk(ctx, instanceId, 20*tccommon.ReadRetryTimeout)
if err != nil {
log.Printf("[CRITAL] redis networkConfig fail, reason:%s\n", err.Error())
return err
}
}
} else if addressSwitch == "open" {
if instance.WanAddress == nil || *instance.WanAddress == "" {
request := redis.NewAllocateWanAddressRequest()
request.InstanceId = helper.String(instanceId)

reqErr := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError {
result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseRedisClient().AllocateWanAddressWithContext(ctx, request)
if e != nil {
return tccommon.RetryError(e)
} else {
log.Printf("[DEBUG] api[%s] success, request body [%s], response body [%s]\n", request.GetAction(), request.ToJsonString(), result.ToJsonString())
}
return nil
})
if reqErr != nil {
log.Printf("[CRITAL] create redis wan address failed, reason:%+v", reqErr)
return reqErr
}

service := RedisService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
_, _, _, err := service.CheckRedisOnlineOk(ctx, instanceId, 20*tccommon.ReadRetryTimeout)
if err != nil {
log.Printf("[CRITAL] redis networkConfig fail, reason:%s\n", err.Error())
return err
}
}
} else {
return fmt.Errorf("invalid address_switch %s", addressSwitch)
}

return nil
}
2 changes: 2 additions & 0 deletions website/docs/r/redis_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ The following arguments are supported:
* `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`.
* `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.
* `wait_switch` - (Optional, Int) Switching mode: `1`-maintenance time window switching, `2`-immediate switching, default value `2`.
* `wan_address_switch` - (Optional, String) Wan address switch, default `close`, values: `open`, `close`.

## Attributes Reference

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


## Import
Expand Down
Loading