Skip to content

Commit 9c47c74

Browse files
gitmknanonymous
andauthored
Feat/crs datasource (#1660)
* feat: add redis account * feat: support param * feat: support read only * feat: support ssl, maintenance_window * feat: support backup, connection_config, datasource * feat: add crs changelog * fix: modify provider * fix: modify crs * fix: modify test * fix: modify crs test * fix: modify ssl * feat: crs datasource * feat: add changelog --------- Co-authored-by: anonymous <anonymous@mail.org>
1 parent d1042af commit 9c47c74

17 files changed

+1429
-4
lines changed

.changelog/1660.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
```release-note:new-data-source
3+
tencentcloud_redis_param_records
4+
```
5+
6+
7+
```release-note:new-data-source
8+
tencentcloud_redis_instance_shards
9+
```
10+
11+
12+
```release-note:new-data-source
13+
tencentcloud_redis_instance_zone_info
14+
```
15+
16+
17+
```release-note:new-data-source
18+
tencentcloud_redis_instance_task_list
19+
```

tencentcloud/basic_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -961,6 +961,8 @@ variable "cbs_backup_disk_id" {
961961
// CRS
962962
const (
963963
defaultCrsInstanceId = "crs-jf4ico4v"
964+
defaultCrsVpcId = "vpc-4owdpnwr"
965+
defaultCrsSubnetId = "subnet-4o0zd840"
964966
)
965967

966968
// End of CRS
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
/*
2+
Use this data source to query detailed information of redis instance_shards
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_redis_instance_shards" "instance_shards" {
8+
instance_id = "crs-c1nl9rpv"
9+
filter_slave = false
10+
}
11+
```
12+
*/
13+
package tencentcloud
14+
15+
import (
16+
"context"
17+
18+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
19+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
20+
redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412"
21+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
22+
)
23+
24+
func dataSourceTencentCloudRedisInstanceShards() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudRedisInstanceShardsRead,
27+
Schema: map[string]*schema.Schema{
28+
"instance_id": {
29+
Required: true,
30+
Type: schema.TypeString,
31+
Description: "The ID of instance.",
32+
},
33+
34+
"filter_slave": {
35+
Optional: true,
36+
Type: schema.TypeBool,
37+
Description: "Whether to filter out slave information.",
38+
},
39+
40+
"instance_shards": {
41+
Computed: true,
42+
Type: schema.TypeList,
43+
Description: "Instance shard list information.",
44+
Elem: &schema.Resource{
45+
Schema: map[string]*schema.Schema{
46+
"shard_name": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
Description: "Shard node name.",
50+
},
51+
"shard_id": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
Description: "Shard node ID.",
55+
},
56+
"role": {
57+
Type: schema.TypeInt,
58+
Computed: true,
59+
Description: "role.",
60+
},
61+
"keys": {
62+
Type: schema.TypeInt,
63+
Computed: true,
64+
Description: "Number of keys.",
65+
},
66+
"slots": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Slot information.",
70+
},
71+
"storage": {
72+
Type: schema.TypeInt,
73+
Computed: true,
74+
Description: "Used capacity.",
75+
},
76+
"storage_slope": {
77+
Type: schema.TypeFloat,
78+
Computed: true,
79+
Description: "Capacity tilt.",
80+
},
81+
"runid": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "The node ID of the instance runtime.",
85+
},
86+
"connected": {
87+
Type: schema.TypeInt,
88+
Computed: true,
89+
Description: "Service status: 0-down;1-on.",
90+
},
91+
},
92+
},
93+
},
94+
95+
"result_output_file": {
96+
Type: schema.TypeString,
97+
Optional: true,
98+
Description: "Used to save results.",
99+
},
100+
},
101+
}
102+
}
103+
104+
func dataSourceTencentCloudRedisInstanceShardsRead(d *schema.ResourceData, meta interface{}) error {
105+
defer logElapsed("data_source.tencentcloud_redis_instance_shards.read")()
106+
defer inconsistentCheck(d, meta)()
107+
108+
logId := getLogId(contextNil)
109+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
110+
var instanceId string
111+
112+
paramMap := make(map[string]interface{})
113+
if v, ok := d.GetOk("instance_id"); ok {
114+
instanceId = v.(string)
115+
paramMap["InstanceId"] = helper.String(v.(string))
116+
}
117+
118+
if v, _ := d.GetOk("filter_slave"); v != nil {
119+
paramMap["FilterSlave"] = helper.Bool(v.(bool))
120+
}
121+
122+
service := RedisService{client: meta.(*TencentCloudClient).apiV3Conn}
123+
124+
var instanceShards []*redis.InstanceClusterShard
125+
126+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
127+
result, e := service.DescribeRedisInstanceShardsByFilter(ctx, paramMap)
128+
if e != nil {
129+
return retryError(e)
130+
}
131+
instanceShards = result
132+
return nil
133+
})
134+
if err != nil {
135+
return err
136+
}
137+
138+
tmpList := make([]map[string]interface{}, 0, len(instanceShards))
139+
if instanceShards != nil {
140+
for _, instanceClusterShard := range instanceShards {
141+
instanceClusterShardMap := map[string]interface{}{}
142+
143+
if instanceClusterShard.ShardName != nil {
144+
instanceClusterShardMap["shard_name"] = instanceClusterShard.ShardName
145+
}
146+
147+
if instanceClusterShard.ShardId != nil {
148+
instanceClusterShardMap["shard_id"] = instanceClusterShard.ShardId
149+
}
150+
151+
if instanceClusterShard.Role != nil {
152+
instanceClusterShardMap["role"] = instanceClusterShard.Role
153+
}
154+
155+
if instanceClusterShard.Keys != nil {
156+
instanceClusterShardMap["keys"] = instanceClusterShard.Keys
157+
}
158+
159+
if instanceClusterShard.Slots != nil {
160+
instanceClusterShardMap["slots"] = instanceClusterShard.Slots
161+
}
162+
163+
if instanceClusterShard.Storage != nil {
164+
instanceClusterShardMap["storage"] = instanceClusterShard.Storage
165+
}
166+
167+
if instanceClusterShard.StorageSlope != nil {
168+
instanceClusterShardMap["storage_slope"] = instanceClusterShard.StorageSlope
169+
}
170+
171+
if instanceClusterShard.Runid != nil {
172+
instanceClusterShardMap["runid"] = instanceClusterShard.Runid
173+
}
174+
175+
if instanceClusterShard.Connected != nil {
176+
instanceClusterShardMap["connected"] = instanceClusterShard.Connected
177+
}
178+
179+
tmpList = append(tmpList, instanceClusterShardMap)
180+
}
181+
182+
_ = d.Set("instance_shards", tmpList)
183+
}
184+
185+
d.SetId(helper.DataResourceIdsHash([]string{instanceId}))
186+
output, ok := d.GetOk("result_output_file")
187+
if ok && output.(string) != "" {
188+
if e := writeToFile(output.(string), tmpList); e != nil {
189+
return e
190+
}
191+
}
192+
return nil
193+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
// go test -i; go test -test.run TestAccTencentCloudRedisInstanceShardsDataSource_basic -v
10+
func TestAccTencentCloudRedisInstanceShardsDataSource_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: testAccRedisInstanceShardsDataSource,
20+
Check: resource.ComposeTestCheckFunc(
21+
testAccCheckTencentCloudDataSourceID("data.tencentcloud_redis_instance_shards.instance_shards"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.#"),
23+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.connected"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.keys"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.role"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.runid"),
27+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.shard_id"),
28+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.shard_name"),
29+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.slots"),
30+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.storage"),
31+
resource.TestCheckResourceAttrSet("data.tencentcloud_redis_instance_shards.instance_shards", "instance_shards.0.storage_slope"),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
const testAccRedisInstanceShardsDataSource = testAccRedisInstanceCluster + `
39+
40+
data "tencentcloud_redis_instance_shards" "instance_shards" {
41+
instance_id = tencentcloud_redis_instance.redis_cluster.id
42+
filter_slave = true
43+
}
44+
45+
`

0 commit comments

Comments
 (0)