Skip to content

Commit 485e902

Browse files
authored
Merge pull request #1 from Sesede/master
add cynosdb data sources
2 parents 2fea210 + 0c2440b commit 485e902

11 files changed

+642
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
FEATURES:
44

5-
* **New Resource**: `tencentcloud_cynosdb_cluster`, `tencentcloud_cynosdb_readonly_instance`.
5+
* **New Resource**: `tencentcloud_cynosdb_cluster`
6+
* **New Resource**: `tencentcloud_cynosdb_readonly_instance`.
7+
* **New Data Source**: `tencentcloud_cynosdb_clusters`
8+
* **New Data Source**: `tencentcloud_cynosdb_readonly_instances`.
69

710
## 1.42.2 (September 14, 2020)
811

examples/tencentcloud-cynosdb/main.tf

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ resource "tencentcloud_cynosdb_cluster" "foo" {
1313
password = "cynos@123"
1414
instance_maintain_duration = 7200
1515
instance_maintain_start_time = 10800
16-
instance_maintain_weekdays = [
16+
instance_maintain_weekdays = [
1717
"Fri",
1818
"Mon",
1919
"Sat",
@@ -49,7 +49,7 @@ resource "tencentcloud_cynosdb_readonly_instance" "foo" {
4949

5050
instance_maintain_duration = 3600
5151
instance_maintain_start_time = 10800
52-
instance_maintain_weekdays = [
52+
instance_maintain_weekdays = [
5353
"Fri",
5454
"Mon",
5555
"Sat",
@@ -59,3 +59,18 @@ resource "tencentcloud_cynosdb_readonly_instance" "foo" {
5959
"Tue",
6060
]
6161
}
62+
63+
data "tencentcloud_cynosdb_clusters" "foo" {
64+
cluster_id = "cynosdbmysql-dzj5l8gz"
65+
project_id = 0
66+
db_type = "MYSQL"
67+
cluster_name = "test"
68+
}
69+
70+
data "tencentcloud_cynosdb_instances" "foo" {
71+
instance_id = "cynosdbmysql-ins-0wln9u6w"
72+
project_id = 0
73+
db_type = "MYSQL"
74+
instance_name = "test"
75+
}
76+
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/*
2+
Use this data source to query detailed information of Cynosdb clusters.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_cynosdb_clusters" "foo" {
8+
cluster_id = "cynosdbmysql-dzj5l8gz"
9+
project_id = 0
10+
db_type = "MYSQL"
11+
cluster_name = "test"
12+
}
13+
```
14+
*/
15+
package tencentcloud
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"log"
21+
22+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
23+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
24+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
25+
)
26+
27+
func dataSourceTencentCloudCynosdbClusters() *schema.Resource {
28+
return &schema.Resource{
29+
Read: dataSourceTencentCloudCynosdbClustersRead,
30+
31+
Schema: map[string]*schema.Schema{
32+
"db_type": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
Description: "Type of CynosDB, and available values include `MYSQL`, `POSTGRESQL`.",
36+
},
37+
"cluster_id": {
38+
Type: schema.TypeString,
39+
Optional: true,
40+
Description: "ID of the cluster to be queried.",
41+
},
42+
"project_id": {
43+
Type: schema.TypeInt,
44+
Optional: true,
45+
Description: "ID of the project to be queried.",
46+
},
47+
"cluster_name": {
48+
Type: schema.TypeString,
49+
Optional: true,
50+
Description: "Name of the cluster to be queried.",
51+
},
52+
"result_output_file": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
Description: "Used to save results.",
56+
},
57+
"cluster_list": {
58+
Type: schema.TypeList,
59+
Computed: true,
60+
Description: "A list of clusters. Each element contains the following attributes:",
61+
Elem: &schema.Resource{
62+
Schema: map[string]*schema.Schema{
63+
"project_id": {
64+
Type: schema.TypeInt,
65+
Computed: true,
66+
Description: "ID of the project.",
67+
},
68+
"available_zone": {
69+
Type: schema.TypeString,
70+
Computed: true,
71+
Description: "The available zone of the CynosDB Cluster.",
72+
},
73+
"vpc_id": {
74+
Type: schema.TypeString,
75+
Computed: true,
76+
Description: "ID of the VPC.",
77+
},
78+
"subnet_id": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
Description: "ID of the subnet within this VPC.",
82+
},
83+
"port": {
84+
Type: schema.TypeInt,
85+
Computed: true,
86+
Description: "Port of CynosDB cluster.",
87+
},
88+
"db_type": {
89+
Type: schema.TypeString,
90+
Computed: true,
91+
Description: "Type of CynosDB, and available values include `MYSQL`.",
92+
},
93+
"db_version": {
94+
Type: schema.TypeString,
95+
Computed: true,
96+
Description: "Version of CynosDB, which is related to `db_type`. For `MYSQL`, available value is `5.7`.",
97+
},
98+
"cluster_limit": {
99+
Type: schema.TypeInt,
100+
Computed: true,
101+
Description: "Storage limit of CynosDB cluster instance, unit in GB.",
102+
},
103+
"cluster_name": {
104+
Type: schema.TypeString,
105+
Computed: true,
106+
Description: "Name of CynosDB cluster.",
107+
},
108+
"cluster_id": {
109+
Type: schema.TypeString,
110+
Computed: true,
111+
Description: "ID of CynosDB cluster.",
112+
},
113+
// payment
114+
"charge_type": {
115+
Type: schema.TypeString,
116+
Computed: true,
117+
Description: "The charge type of instance. Valid values are `PREPAID` and `POSTPAID_BY_HOUR`. Default value is `POSTPAID_BY_HOUR`.",
118+
},
119+
"auto_renew_flag": {
120+
Type: schema.TypeInt,
121+
Computed: true,
122+
Description: "Auto renew flag. Valid values are `0`(MANUAL_RENEW), `1`(AUTO_RENEW). Only works for PREPAID cluster.",
123+
},
124+
"cluster_status": {
125+
Type: schema.TypeString,
126+
Computed: true,
127+
Description: "Status of the Cynosdb cluster.",
128+
},
129+
"create_time": {
130+
Type: schema.TypeString,
131+
Computed: true,
132+
Description: "Creation time of the CynosDB cluster.",
133+
},
134+
},
135+
},
136+
},
137+
},
138+
}
139+
}
140+
141+
func dataSourceTencentCloudCynosdbClustersRead(d *schema.ResourceData, meta interface{}) error {
142+
defer logElapsed("data_source.tencentcloud_cynosdb_clusters.read")()
143+
144+
logId := getLogId(contextNil)
145+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
146+
147+
params := make(map[string]string)
148+
if v, ok := d.GetOk("cluster_id"); ok {
149+
params["ClusterId"] = v.(string)
150+
}
151+
if v, ok := d.GetOk("cluster_name"); ok {
152+
params["ClusterName"] = v.(string)
153+
}
154+
if v, ok := d.GetOkExists("project_id"); ok {
155+
params["ProjectId"] = fmt.Sprintf("%d", v.(int))
156+
}
157+
clusterType := ""
158+
if v, ok := d.GetOk("cluster_type"); ok {
159+
clusterType = v.(string)
160+
}
161+
162+
cynosdbService := CynosdbService{
163+
client: meta.(*TencentCloudClient).apiV3Conn,
164+
}
165+
166+
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
167+
clusters, e := cynosdbService.DescribeClusters(ctx, params)
168+
if e != nil {
169+
return retryError(e)
170+
}
171+
ids := make([]string, 0, len(clusters))
172+
clusterList := make([]map[string]interface{}, 0, len(clusters))
173+
for _, cluster := range clusters {
174+
if clusterType != "" && clusterType != *cluster.DbType {
175+
continue
176+
}
177+
mapping := map[string]interface{}{
178+
"cluster_id": cluster.ClusterId,
179+
"cluster_name": cluster.ClusterName,
180+
"cluster_limit": cluster.StorageLimit,
181+
"db_type": cluster.DbType,
182+
"available_zone": cluster.Zone,
183+
"project_id": cluster.ProjectID,
184+
"create_time": cluster.CreateTime,
185+
"cluster_status": cluster.Status,
186+
"auto_renew_flag": cluster.RenewFlag,
187+
"port": cluster.Vport,
188+
"vpc_id": cluster.VpcId,
189+
"subnet_id": cluster.SubnetId,
190+
"db_version": cluster.DbVersion,
191+
"charge_type": CYNOSDB_CHARGE_TYPE[*cluster.PayMode],
192+
}
193+
194+
clusterList = append(clusterList, mapping)
195+
ids = append(ids, *cluster.ClusterId)
196+
}
197+
198+
d.SetId(helper.DataResourceIdsHash(ids))
199+
if e = d.Set("cluster_list", clusterList); e != nil {
200+
log.Printf("[CRITAL]%s provider set cluster list fail, reason:%s\n ", logId, e.Error())
201+
return resource.NonRetryableError(e)
202+
}
203+
204+
output, ok := d.GetOk("result_output_file")
205+
if ok && output.(string) != "" {
206+
if e := writeToFile(output.(string), clusterList); e != nil {
207+
return resource.NonRetryableError(e)
208+
}
209+
}
210+
211+
return nil
212+
})
213+
if err != nil {
214+
log.Printf("[CRITAL]%s read cynosdb clusters failed, reason:%s\n ", logId, err.Error())
215+
return err
216+
}
217+
218+
return nil
219+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package tencentcloud
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccTencentCloudCynosdbClustersDataSource_full(t *testing.T) {
10+
t.Parallel()
11+
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
Providers: testAccProviders,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccCynosdbClusterstDataSource_full(),
18+
Check: resource.ComposeAggregateTestCheckFunc(
19+
testAccCheckTencentCloudDataSourceID("tencentcloud_cynosdb_cluster.foo"),
20+
resource.TestCheckResourceAttr("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.#", "1"),
21+
resource.TestCheckResourceAttr("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.cluster_name", "tf-cynosdb"),
22+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.cluster_id"),
23+
resource.TestCheckResourceAttr("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.available_zone", "ap-guangzhou-4"),
24+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.vpc_id"),
25+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.subnet_id"),
26+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.create_time"),
27+
resource.TestCheckResourceAttr("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.db_type", "MYSQL"),
28+
resource.TestCheckResourceAttr("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.db_version", "5.7"),
29+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.cluster_status"),
30+
resource.TestCheckResourceAttr("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.charge_type", "POSTPAID_BY_HOUR"),
31+
resource.TestCheckResourceAttrSet("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.port"),
32+
resource.TestCheckResourceAttr("data.tencentcloud_cynosdb_clusters.cluster_full", "cluster_list.0.auto_renew_flag", "0"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func testAccCynosdbClusterstDataSource_full() string {
40+
return testAccCynosdbCluster + `
41+
data "tencentcloud_cynosdb_clusters" "cluster_full" {
42+
cluster_id = tencentcloud_cynosdb_cluster.foo.id
43+
project_id = 0
44+
db_type = "MYSQL"
45+
cluster_name = "tf-cynosdb"
46+
}`
47+
}

0 commit comments

Comments
 (0)