Skip to content

Commit 81063f0

Browse files
authored
Merge branch 'master' into master
2 parents 64a7cc4 + 9fc7dfc commit 81063f0

28 files changed

+5513
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
## 1.37.0 (Unreleased)
22

33
FEATURES:
4-
4+
* **New Resource**: `tencentcloud_postgresql_instance`
5+
* **New Data Source**: `tencentcloud_postgresql_instances`
6+
* **New Data Source**: `tencentcloud_postgresql_speccodes`
57
* **New Data Source**: `tencentcloud_sqlserver_zone_config`
68

79
## 1.36.1 (June 12, 2020)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
resource "tencentcloud_vpc" "foo" {
2+
name = "example"
3+
cidr_block = "10.0.0.0/16"
4+
}
5+
6+
resource "tencentcloud_subnet" "foo" {
7+
name = "example"
8+
availability_zone = var.availability_zone
9+
vpc_id = tencentcloud_vpc.foo.id
10+
cidr_block = "10.0.0.0/24"
11+
is_multicast = false
12+
}
13+
14+
resource "tencentcloud_postgresql_instance" "example" {
15+
name = "tf_postsql_instance_111"
16+
availability_zone = var.availability_zone
17+
charge_type = "POSTPAID_BY_HOUR"
18+
vpc_id = tencentcloud_vpc.foo.id
19+
subnet_id = tencentcloud_subnet.foo.id
20+
engine_version = "9.3.5"
21+
root_password = "1qaA2k1wgvfa3ZZZ"
22+
charset = "UTF8"
23+
project_id = 0
24+
memory = 2
25+
storage = 10
26+
}
27+
28+
data "tencentcloud_postgresql_instances" "id_example" {
29+
id = tencentcloud_postgresql_instance.example.id
30+
}
31+
32+
data "tencentcloud_postgresql_instances" "project_example" {
33+
project_id = 0
34+
}
35+
36+
data "tencentcloud_postgresql_instances" "name_example" {
37+
name = tencentcloud_postgresql_instance.example.name
38+
}
39+
40+
data "tencentcloud_postgresql_specinfos" "example" {
41+
availability_zone = var.availability_zone
42+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
variable "availability_zone" {
2+
default = "ap-guangzhou-2"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = ">= 0.12"
3+
}

examples/tencentcloud-sqlserver/main.tf

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
data "tencentcloud_sqlserver_zone_config" "mysqlserver" {
22
}
33

4+
resource "tencentcloud_vpc" "foo" {
5+
name = "example"
6+
cidr_block = "10.0.0.0/16"
7+
}
8+
9+
resource "tencentcloud_subnet" "foo" {
10+
name = "example"
11+
availability_zone = var.availability_zone
12+
vpc_id = tencentcloud_vpc.foo.id
13+
cidr_block = "10.0.0.0/24"
14+
is_multicast = false
15+
}
16+
17+
resource "tencentcloud_sqlserver_instance" "example" {
18+
name = "example"
19+
availability_zone = var.availability_zone
20+
charge_type = "POSTPAID_BY_HOUR"
21+
vpc_id = tencentcloud_vpc.foo.id
22+
subnet_id = tencentcloud_subnet.foo.id
23+
engine_version = "2008R2"
24+
project_id = 0
25+
memory = 2
26+
storage = 10
27+
}
28+
429
resource "tencentcloud_sqlserver_db" "mysqlserver_db" {
5-
instance_id = "mssql-3cdq7kx5"
30+
instance_id = tencentcloud_sqlserver_instance.example.id
631
name = "db_brickzzhang_update"
732
charset = "Chinese_PRC_BIN"
833
remark = "test-remark-update"
9-
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/mattn/go-colorable v0.1.6 // indirect
1616
github.com/mitchellh/go-homedir v1.1.0
1717
github.com/pkg/errors v0.9.1
18-
github.com/tencentcloud/tencentcloud-sdk-go v3.0.187+incompatible
18+
github.com/tencentcloud/tencentcloud-sdk-go v3.0.189+incompatible
1919
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
2020
github.com/zclconf/go-cty v1.4.2 // indirect
2121
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect

tencentcloud/connectivity/client.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
gaap "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/gaap/v20180529"
2525
mongodb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/mongodb/v20180408"
2626
monitor "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/monitor/v20180724"
27+
postgre "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
2728
redis "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/redis/v20180412"
2829
scf "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/scf/v20180416"
2930
sqlserver "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/sqlserver/v20180328"
@@ -66,6 +67,7 @@ type TencentCloudClient struct {
6667
monitorConn *monitor.Client
6768
esConn *es.Client
6869
sqlserverConn *sqlserver.Client
70+
postgreConn *postgre.Client
6971
}
7072

7173
// NewClientProfile returns a new ClientProfile
@@ -401,6 +403,19 @@ func (me *TencentCloudClient) UseEsClient() *es.Client {
401403
return me.esConn
402404
}
403405

406+
// UsePostgreClient returns postgresql client for service
407+
func (me *TencentCloudClient) UsePostgresqlClient() *postgre.Client {
408+
if me.postgreConn != nil {
409+
return me.postgreConn
410+
}
411+
412+
cpf := me.NewClientProfile(300)
413+
me.postgreConn, _ = postgre.NewClient(me.Credential, me.Region, cpf)
414+
me.postgreConn.WithHttpTransport(&LogRoundTripper{})
415+
416+
return me.postgreConn
417+
}
418+
404419
// UseSqlserverClient returns sqlserver client for service
405420
func (me *TencentCloudClient) UseSqlserverClient() *sqlserver.Client {
406421
if me.sqlserverConn != nil {
Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
/*
2+
Use this data source to query postgresql instances
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_postgresql_instances" "name"{
8+
name = "test"
9+
}
10+
11+
data "tencentcloud_postgresql_instances" "project"{
12+
project_id = 0
13+
}
14+
15+
data "tencentcloud_postgresql_instances" "id"{
16+
id = "postgres-h9t4fde1"
17+
}
18+
```
19+
*/
20+
package tencentcloud
21+
22+
import (
23+
"context"
24+
"log"
25+
26+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
27+
postgresql "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/postgres/v20170312"
28+
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
29+
)
30+
31+
func dataSourceTencentCloudPostgresqlInstances() *schema.Resource {
32+
return &schema.Resource{
33+
Read: dataSourceTencentCloudPostgresqlInstanceRead,
34+
Schema: map[string]*schema.Schema{
35+
"name": {
36+
Type: schema.TypeString,
37+
Optional: true,
38+
Description: "Name of the postgresql instance to be query.",
39+
},
40+
"id": {
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Description: "ID of the postgresql instance to be query.",
44+
},
45+
"project_id": {
46+
Type: schema.TypeInt,
47+
Optional: true,
48+
Description: "Project id of the postgresql instance to be query.",
49+
},
50+
"result_output_file": {
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Description: "Used to save results.",
54+
},
55+
"instance_list": {
56+
Type: schema.TypeList,
57+
Computed: true,
58+
Description: "A list of postgresql instances. Each element contains the following attributes.",
59+
Elem: &schema.Resource{
60+
Schema: map[string]*schema.Schema{
61+
"id": {
62+
Type: schema.TypeString,
63+
Computed: true,
64+
Description: "ID of the postgresql instance.",
65+
},
66+
"name": {
67+
Type: schema.TypeString,
68+
Computed: true,
69+
Description: "Name of the postgresql instance.",
70+
},
71+
"charge_type": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
Description: "Pay type of the postgresql instance.",
75+
},
76+
"auto_renew_flag": {
77+
Type: schema.TypeInt,
78+
Computed: true,
79+
Description: "Auto renew flag.",
80+
},
81+
"engine_version": {
82+
Type: schema.TypeString,
83+
Computed: true,
84+
Description: "Version of the postgresql database engine.",
85+
},
86+
"vpc_id": {
87+
Type: schema.TypeString,
88+
Computed: true,
89+
Description: "ID of VPC.",
90+
},
91+
"subnet_id": {
92+
Type: schema.TypeString,
93+
Computed: true,
94+
Description: "ID of subnet.",
95+
},
96+
"storage": {
97+
Type: schema.TypeInt,
98+
Computed: true,
99+
Description: "Disk size (in GB).",
100+
},
101+
"memory": {
102+
Type: schema.TypeInt,
103+
Computed: true,
104+
Description: "Memory size (in MB).",
105+
},
106+
"project_id": {
107+
Type: schema.TypeInt,
108+
Computed: true,
109+
Description: "Project ID, default value is 0.",
110+
},
111+
"availability_zone": {
112+
Type: schema.TypeString,
113+
Computed: true,
114+
Description: "Availability zone.",
115+
},
116+
"public_access_switch": {
117+
Type: schema.TypeBool,
118+
Computed: true,
119+
Description: "Indicates whether to enable the access to an instance from public network or not.",
120+
},
121+
"public_access_host": {
122+
Type: schema.TypeString,
123+
Computed: true,
124+
Description: "Host for public access.",
125+
},
126+
"public_access_port": {
127+
Type: schema.TypeInt,
128+
Computed: true,
129+
Description: "Port for public access.",
130+
},
131+
"private_access_ip": {
132+
Type: schema.TypeString,
133+
Computed: true,
134+
Description: "Ip address for private access.",
135+
},
136+
"private_access_port": {
137+
Type: schema.TypeInt,
138+
Computed: true,
139+
Description: "Port for private access.",
140+
},
141+
"charset": {
142+
Type: schema.TypeString,
143+
Computed: true,
144+
Description: "Charset of the postgresql instance.",
145+
},
146+
"create_time": {
147+
Type: schema.TypeString,
148+
Computed: true,
149+
Description: "Create time of the postgresql instance.",
150+
},
151+
},
152+
},
153+
},
154+
},
155+
}
156+
}
157+
158+
func dataSourceTencentCloudPostgresqlInstanceRead(d *schema.ResourceData, meta interface{}) error {
159+
defer logElapsed("data_source.tencentcloud_postgresql_instances.read")()
160+
161+
logId := getLogId(contextNil)
162+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
163+
164+
service := PostgresqlService{client: meta.(*TencentCloudClient).apiV3Conn}
165+
166+
filter := make([]*postgresql.Filter, 0)
167+
if v, ok := d.GetOk("name"); ok {
168+
filter = append(filter, &postgresql.Filter{Name: helper.String("db-instance-name"), Values: []*string{helper.String(v.(string))}})
169+
}
170+
if v, ok := d.GetOk("id"); ok {
171+
filter = append(filter, &postgresql.Filter{Name: helper.String("db-instance-id"), Values: []*string{helper.String(v.(string))}})
172+
}
173+
if v, ok := d.GetOk("project_id"); ok {
174+
filter = append(filter, &postgresql.Filter{Name: helper.String("db-project-id"), Values: []*string{helper.String(v.(string))}})
175+
}
176+
177+
instanceList, err := service.DescribePostgresqlInstances(ctx, filter)
178+
179+
if err != nil {
180+
instanceList, err = service.DescribePostgresqlInstances(ctx, filter)
181+
}
182+
183+
if err != nil {
184+
return err
185+
}
186+
187+
ids := make([]string, 0, len(instanceList))
188+
list := make([]map[string]interface{}, 0, len(instanceList))
189+
190+
for _, v := range instanceList {
191+
listItem := make(map[string]interface{})
192+
listItem["id"] = v.DBInstanceId
193+
listItem["name"] = v.DBInstanceName
194+
listItem["auto_renew_flag"] = v.AutoRenew
195+
listItem["project_id"] = v.ProjectId
196+
listItem["storage"] = v.DBInstanceStorage
197+
listItem["memory"] = v.DBInstanceMemory
198+
listItem["availability_zone"] = v.Zone
199+
listItem["create_time"] = v.CreateTime
200+
listItem["vpc_id"] = v.VpcId
201+
listItem["subnet_id"] = v.SubnetId
202+
listItem["engine_version"] = v.DBVersion
203+
listItem["public_access_switch"] = false
204+
listItem["charset"] = v.DBCharset
205+
listItem["public_access_host"] = ""
206+
207+
for _, netInfo := range v.DBInstanceNetInfo {
208+
if *netInfo.NetType == "public" {
209+
if *netInfo.Status == "opened" || *netInfo.Status == "1" {
210+
listItem["public_access_switch"] = true
211+
}
212+
listItem["public_access_host"] = netInfo.Address
213+
listItem["public_access_port"] = netInfo.Port
214+
}
215+
if (*netInfo.NetType == "private" || *netInfo.NetType == "inner") && *netInfo.Ip != "" {
216+
listItem["private_access_ip"] = netInfo.Ip
217+
listItem["private_access_port"] = netInfo.Port
218+
}
219+
}
220+
221+
if *v.PayType == POSTGRESQL_PAYTYPE_PREPAID || *v.PayType == COMMON_PAYTYPE_PREPAID {
222+
listItem["charge_type"] = COMMON_PAYTYPE_PREPAID
223+
} else {
224+
listItem["charge_type"] = COMMON_PAYTYPE_POSTPAID
225+
}
226+
list = append(list, listItem)
227+
ids = append(ids, *v.DBInstanceId)
228+
}
229+
230+
d.SetId(helper.DataResourceIdsHash(ids))
231+
if e := d.Set("instance_list", list); e != nil {
232+
log.Printf("[CRITAL]%s provider set list fail, reason:%s\n", logId, e.Error())
233+
return e
234+
}
235+
output, ok := d.GetOk("result_output_file")
236+
if ok && output.(string) != "" {
237+
return writeToFile(output.(string), list)
238+
}
239+
return nil
240+
}

0 commit comments

Comments
 (0)