Skip to content

Commit d81cc7a

Browse files
gitmknanonymous
andauthored
feat: support switck master (#1785)
* feat: support switck master * feat: add changelog * feat: add doc * fix: update misspell * fix: update misspell --------- Co-authored-by: anonymous <anonymous@mail.org>
1 parent d429792 commit d81cc7a

File tree

7 files changed

+308
-0
lines changed

7 files changed

+308
-0
lines changed

.changelog/1785.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:new-resource
2+
tencentcloud_redis_switch_master
3+
```

tencentcloud/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ TencentDB for Redis(crs)
576576
tencentcloud_redis_upgrade_proxy_version_operation
577577
tencentcloud_redis_maintenance_window
578578
tencentcloud_redis_replica_readonly
579+
tencentcloud_redis_switch_master
579580
580581
Serverless Cloud Function(SCF)
581582
Data Source
@@ -1748,6 +1749,7 @@ func Provider() *schema.Provider {
17481749
"tencentcloud_redis_upgrade_proxy_version_operation": resourceTencentCloudRedisUpgradeProxyVersionOperation(),
17491750
"tencentcloud_redis_maintenance_window": resourceTencentCloudRedisMaintenanceWindow(),
17501751
"tencentcloud_redis_replica_readonly": resourceTencentCloudRedisReplicaReadonly(),
1752+
"tencentcloud_redis_switch_master": resourceTencentCloudRedisSwitchMaster(),
17511753
"tencentcloud_as_scaling_config": resourceTencentCloudAsScalingConfig(),
17521754
"tencentcloud_as_scaling_group": resourceTencentCloudAsScalingGroup(),
17531755
"tencentcloud_as_attachment": resourceTencentCloudAsAttachment(),
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
/*
2+
Provides a resource to create a redis switch_master
3+
4+
Example Usage
5+
6+
```hcl
7+
resource "tencentcloud_redis_switch_master" "switch_master" {
8+
instance_id = "crs-kfdkirid"
9+
group_id = 29369
10+
}
11+
```
12+
13+
*/
14+
package tencentcloud
15+
16+
import (
17+
"context"
18+
"fmt"
19+
"log"
20+
21+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
23+
sdkErrors "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common/errors"
24+
redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412"
25+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
26+
)
27+
28+
func resourceTencentCloudRedisSwitchMaster() *schema.Resource {
29+
return &schema.Resource{
30+
Create: resourceTencentCloudRedisSwitchMasterCreate,
31+
Read: resourceTencentCloudRedisSwitchMasterRead,
32+
Update: resourceTencentCloudRedisSwitchMasterUpdate,
33+
Delete: resourceTencentCloudRedisSwitchMasterDelete,
34+
35+
Schema: map[string]*schema.Schema{
36+
"instance_id": {
37+
Required: true,
38+
Type: schema.TypeString,
39+
Description: "The ID of instance.",
40+
},
41+
42+
"group_id": {
43+
Optional: true,
44+
Type: schema.TypeInt,
45+
Description: "Replication group ID, required for multi-AZ instances.",
46+
},
47+
},
48+
}
49+
}
50+
51+
func resourceTencentCloudRedisSwitchMasterCreate(d *schema.ResourceData, meta interface{}) error {
52+
defer logElapsed("resource.tencentcloud_redis_switch_master.create")()
53+
defer inconsistentCheck(d, meta)()
54+
55+
var (
56+
instanceId string
57+
)
58+
if v, ok := d.GetOk("instance_id"); ok {
59+
instanceId = v.(string)
60+
}
61+
62+
d.SetId(instanceId)
63+
64+
return resourceTencentCloudRedisSwitchMasterUpdate(d, meta)
65+
}
66+
67+
func resourceTencentCloudRedisSwitchMasterRead(d *schema.ResourceData, meta interface{}) error {
68+
defer logElapsed("resource.tencentcloud_redis_switch_master.read")()
69+
defer inconsistentCheck(d, meta)()
70+
71+
logId := getLogId(contextNil)
72+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
73+
74+
service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn}
75+
76+
instanceId := d.Id()
77+
paramMap := make(map[string]interface{})
78+
paramMap["InstanceId"] = &instanceId
79+
80+
switchMaster, err := service.DescribeRedisInstanceZoneInfoByFilter(ctx, paramMap)
81+
if err != nil {
82+
return err
83+
}
84+
85+
if switchMaster == nil {
86+
d.SetId("")
87+
log.Printf("[WARN]%s resource `RedisSwitchMaster` [%s] not found, please check if it has been deleted.\n", logId, d.Id())
88+
return nil
89+
}
90+
91+
_ = d.Set("instance_id", instanceId)
92+
93+
if len(switchMaster) > 1 {
94+
for _, v := range switchMaster {
95+
if *v.Role == "master" {
96+
_ = d.Set("group_id", v.GroupId)
97+
break
98+
}
99+
}
100+
}
101+
102+
return nil
103+
}
104+
105+
func resourceTencentCloudRedisSwitchMasterUpdate(d *schema.ResourceData, meta interface{}) error {
106+
defer logElapsed("resource.tencentcloud_redis_switch_master.update")()
107+
defer inconsistentCheck(d, meta)()
108+
109+
logId := getLogId(contextNil)
110+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
111+
112+
request := redis.NewChangeReplicaToMasterRequest()
113+
response := redis.NewChangeReplicaToMasterResponse()
114+
115+
instanceId := d.Id()
116+
request.InstanceId = &instanceId
117+
118+
if v, ok := d.GetOk("group_id"); ok {
119+
request.GroupId = helper.IntInt64(v.(int))
120+
}
121+
122+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
123+
result, e := meta.(*TencentCloudClient).apiV3Conn.UseRedisClient().ChangeReplicaToMaster(request)
124+
if e != nil {
125+
if _, ok := e.(*sdkErrors.TencentCloudSDKError); !ok {
126+
return resource.RetryableError(e)
127+
} else {
128+
return resource.NonRetryableError(e)
129+
}
130+
} else {
131+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString())
132+
}
133+
response = result
134+
return nil
135+
})
136+
if err != nil {
137+
log.Printf("[CRITAL]%s update redis switchMaster failed, reason:%+v", logId, err)
138+
return err
139+
}
140+
141+
service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn}
142+
taskId := *response.Response.TaskId
143+
err = resource.Retry(6*readRetryTimeout, func() *resource.RetryError {
144+
ok, err := service.DescribeTaskInfo(ctx, instanceId, taskId)
145+
if err != nil {
146+
if _, ok := err.(*sdkErrors.TencentCloudSDKError); !ok {
147+
return resource.RetryableError(err)
148+
} else {
149+
return resource.NonRetryableError(err)
150+
}
151+
}
152+
if ok {
153+
return nil
154+
} else {
155+
return resource.RetryableError(fmt.Errorf("update redis switchMaster is processing"))
156+
}
157+
})
158+
159+
if err != nil {
160+
log.Printf("[CRITAL]%s update redis switchMaster fail, reason:%s\n", logId, err.Error())
161+
return err
162+
}
163+
164+
return resourceTencentCloudRedisSwitchMasterRead(d, meta)
165+
}
166+
167+
func resourceTencentCloudRedisSwitchMasterDelete(d *schema.ResourceData, meta interface{}) error {
168+
defer logElapsed("resource.tencentcloud_redis_switch_master.delete")()
169+
defer inconsistentCheck(d, meta)()
170+
171+
return nil
172+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudRedisSwitchMasterResource_basic -v
10+
func TestAccTencentCloudRedisSwitchMasterResource_basic(t *testing.T) {
11+
t.Parallel()
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() {
14+
testAccPreCheck(t)
15+
},
16+
Providers: testAccProviders,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccRedisSwitchMaster,
20+
Check: resource.ComposeTestCheckFunc(
21+
resource.TestCheckResourceAttrSet("tencentcloud_redis_switch_master.switch_master", "id"),
22+
resource.TestCheckResourceAttr("tencentcloud_redis_switch_master.switch_master", "instance_id", "crs-2yypjrnv"),
23+
resource.TestCheckResourceAttr("tencentcloud_redis_switch_master.switch_master", "group_id", "8925"),
24+
),
25+
},
26+
{
27+
Config: testAccRedisSwitchMasterUp,
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttrSet("tencentcloud_redis_switch_master.switch_master", "id"),
30+
resource.TestCheckResourceAttr("tencentcloud_redis_switch_master.switch_master", "instance_id", "crs-2yypjrnv"),
31+
resource.TestCheckResourceAttr("tencentcloud_redis_switch_master.switch_master", "group_id", "8924"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
const testAccRedisSwitchMaster = `
39+
40+
resource "tencentcloud_redis_switch_master" "switch_master" {
41+
instance_id = "crs-2yypjrnv"
42+
group_id = 8925
43+
}
44+
45+
`
46+
47+
const testAccRedisSwitchMasterUp = `
48+
49+
resource "tencentcloud_redis_switch_master" "switch_master" {
50+
instance_id = "crs-2yypjrnv"
51+
group_id = 8924
52+
}
53+
54+
`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
subcategory: "TencentDB for Redis(crs)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_redis_switch_master"
5+
sidebar_current: "docs-tencentcloud-resource-redis_switch_master"
6+
description: |-
7+
Provides a resource to create a redis switch_master
8+
---
9+
10+
# tencentcloud_redis_switch_master
11+
12+
Provides a resource to create a redis switch_master
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_redis_switch_master" "switch_master" {
18+
instance_id = "crs-kfdkirid"
19+
group_id = 29369
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `instance_id` - (Required, String) The ID of instance.
28+
* `group_id` - (Optional, Int) Replication group ID, required for multi-AZ instances.
29+
30+
## Attributes Reference
31+
32+
In addition to all arguments above, the following attributes are exported:
33+
34+
* `id` - ID of the resource.
35+
36+
37+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
subcategory: "TencentDB for Redis(crs)"
3+
layout: "tencentcloud"
4+
page_title: "TencentCloud: tencentcloud_redis_switch_master"
5+
sidebar_current: "docs-tencentcloud-resource-redis_switch_master"
6+
description: |-
7+
Provides a resource to create a redis switch_master
8+
---
9+
10+
# tencentcloud_redis_switch_master
11+
12+
Provides a resource to create a redis switch_master
13+
14+
## Example Usage
15+
16+
```hcl
17+
resource "tencentcloud_redis_switch_master" "switch_master" {
18+
instance_id = "crs-kfdkirid"
19+
group_id = 29369
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
The following arguments are supported:
26+
27+
* `instance_id` - (Required, String) The ID of instance.
28+
* `group_id` - (Optional, Int) Replication group ID, required for multi-AZ instances.
29+
30+
## Attributes Reference
31+
32+
In addition to all arguments above, the following attributes are exported:
33+
34+
* `id` - ID of the resource.
35+
36+
37+

website/tencentcloud.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,6 +3202,9 @@
32023202
<li>
32033203
<a href="/docs/providers/tencentcloud/r/redis_startup_instance_operation.html">tencentcloud_redis_startup_instance_operation</a>
32043204
</li>
3205+
<li>
3206+
<a href="/docs/providers/tencentcloud/r/redis_switch_master.html">tencentcloud_redis_switch_master</a>
3207+
</li>
32053208
<li>
32063209
<a href="/docs/providers/tencentcloud/r/redis_upgrade_cache_version_operation.html">tencentcloud_redis_upgrade_cache_version_operation</a>
32073210
</li>

0 commit comments

Comments
 (0)