Skip to content

Commit ca1cca6

Browse files
committed
say byebye to api v2
1 parent 9c165e8 commit ca1cca6

27 files changed

+671
-687
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.13
44

55
require (
66
github.com/athom/goset v0.0.0-20151103071336-f07be69aa101
7-
github.com/aws/aws-sdk-go v1.25.18
7+
github.com/aws/aws-sdk-go v1.25.21
88
github.com/bflad/tfproviderlint v0.5.0
99
github.com/client9/misspell v0.3.4
1010
github.com/golangci/golangci-lint v1.21.0
@@ -14,7 +14,6 @@ require (
1414
github.com/mitchellh/go-homedir v1.1.0
1515
github.com/tencentcloud/tencentcloud-sdk-go v3.0.94+incompatible
1616
github.com/yangwenmai/ratelimit v0.0.0-20180104140304-44221c2292e1
17-
github.com/zqfan/tencentcloud-sdk-go v0.1.2
1817
labix.org/v2/mgo v0.0.0-20140701140051-000000000287 // indirect
1918
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
2019
)

go.sum

Lines changed: 61 additions & 4 deletions
Large diffs are not rendered by default.

tencentcloud/config.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tencentcloud
22

33
import (
44
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/connectivity"
5-
"github.com/zqfan/tencentcloud-sdk-go/client"
65
)
76

87
type Config struct {
@@ -13,14 +12,11 @@ type Config struct {
1312
}
1413

1514
type TencentCloudClient struct {
16-
commonConn *client.Client
17-
apiV3Conn *connectivity.TencentCloudClient
15+
apiV3Conn *connectivity.TencentCloudClient
1816
}
1917

2018
func (c *Config) Client() (interface{}, error) {
2119
var tcClient TencentCloudClient
22-
23-
tcClient.commonConn = client.NewClient(c.SecretId, c.SecretKey, c.Region)
2420
tcClient.apiV3Conn = connectivity.NewTencentCloudClient(c.SecretId, c.SecretKey, c.SecurityToken, c.Region)
2521

2622
return &tcClient, nil

tencentcloud/data_source_tc_route_table.go

Lines changed: 46 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package tencentcloud
22

33
import (
4-
"encoding/json"
4+
"context"
55
"fmt"
6-
"log"
76
"strings"
87

98
"github.com/hashicorp/terraform/helper/schema"
@@ -19,10 +18,6 @@ func dataSourceTencentCloudRouteTable() *schema.Resource {
1918
Type: schema.TypeString,
2019
Required: true,
2120
},
22-
"vpc_id": {
23-
Type: schema.TypeString,
24-
Computed: true,
25-
},
2621
"name": {
2722
Type: schema.TypeString,
2823
Optional: true,
@@ -34,6 +29,10 @@ func dataSourceTencentCloudRouteTable() *schema.Resource {
3429
return
3530
},
3631
},
32+
"vpc_id": {
33+
Type: schema.TypeString,
34+
Computed: true,
35+
},
3736
"subnet_num": {
3837
Type: schema.TypeInt,
3938
Computed: true,
@@ -70,85 +69,68 @@ func dataSourceTencentCloudRouteTable() *schema.Resource {
7069
}
7170
}
7271

73-
func dataSourceTencentCloudRouteTableRead(d *schema.ResourceData, m interface{}) error {
74-
client := m.(*TencentCloudClient).commonConn
75-
params := map[string]string{
76-
"Action": "DescribeRouteTable",
77-
"routeTableId": d.Get("route_table_id").(string),
78-
}
79-
if _, ok := d.GetOk("name"); ok {
80-
params["routeTableName"] = d.Get("name").(string)
81-
}
72+
func dataSourceTencentCloudRouteTableRead(d *schema.ResourceData, meta interface{}) error {
73+
defer logElapsed("data_source.tencentcloud_route_table.read")()
8274

83-
log.Printf("[DEBUG] data_source_tc_route_table read params:%v", params)
75+
logId := getLogId(contextNil)
76+
ctx := context.WithValue(context.TODO(), "logId", logId)
77+
service := VpcService{client: meta.(*TencentCloudClient).apiV3Conn}
8478

85-
response, err := client.SendRequest("vpc", params)
86-
if err != nil {
87-
log.Printf("[DEBUG] data_source_tc_route_table read client.SendRequest error:%v", err)
88-
return err
79+
var (
80+
routeTableId string
81+
name string
82+
)
83+
if temp, ok := d.GetOk("route_table_id"); ok {
84+
tempStr := temp.(string)
85+
if tempStr != "" {
86+
routeTableId = tempStr
87+
}
8988
}
90-
91-
var jsonresp struct {
92-
Code int `json:"code"`
93-
Message string `json:"message"`
94-
CodeDesc string `json:"codeDesc"`
95-
TotalCount int `json:"totalCount"`
96-
Data []struct {
97-
UnVpcId string `json:"UnVpcId"`
98-
UnRouteTableId string `json:"unRouteTableId"`
99-
RouteTableName string `json:"routeTableName"`
100-
RouteTableCreateTime string `json:"routeTableCreateTime"`
101-
SubnetNum int `json:"subnetNum"`
102-
RouteTableSet []struct {
103-
DestinationCidrBlock string `json:"destinationCidrBlock"`
104-
NextType int `json:"nextType"`
105-
UnNextHub string `json:"unNextHub"`
106-
Description string `json:"description"`
107-
} `json:"routeSet"`
89+
if temp, ok := d.GetOk("name"); ok {
90+
tempStr := temp.(string)
91+
if tempStr != "" {
92+
name = tempStr
10893
}
10994
}
110-
err = json.Unmarshal([]byte(response), &jsonresp)
95+
96+
var infos, err = service.DescribeRouteTables(ctx, routeTableId, name, "", map[string]string{})
11197
if err != nil {
112-
log.Printf("[DEBUG] data_source_tc_route_table read json.Unmarshal error:%v", err)
11398
return err
11499
}
115-
if jsonresp.Code != 0 {
116-
return fmt.Errorf("data_source_tc_route_table read error, code:%v, message:%v, CodeDesc:%v", jsonresp.Code, jsonresp.Message, jsonresp.CodeDesc)
117-
} else if jsonresp.TotalCount <= 0 || len(jsonresp.Data) <= 0 {
118-
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
119-
}
120100

121-
rt := jsonresp.Data[0]
122-
123-
log.Printf("[DEBUG] data_source_tc_route_table read result route_table_id:%v", rt.UnRouteTableId)
101+
if len(infos) == 0 {
102+
return fmt.Errorf("route table route_table_id=%s, name=%s not found", routeTableId, name)
103+
}
124104

125-
d.SetId(rt.UnRouteTableId)
126-
d.Set("route_table_id", rt.UnRouteTableId)
127-
d.Set("vpc_id", rt.UnVpcId)
128-
d.Set("name", rt.RouteTableName)
129-
d.Set("subnet_num", rt.SubnetNum)
130-
d.Set("create_time", rt.RouteTableCreateTime)
105+
routetable := infos[0]
106+
d.SetId(routetable.routeTableId)
107+
d.Set("route_table_id", routetable.routeTableId)
108+
d.Set("vpc_id", routetable.vpcId)
109+
d.Set("name", routetable.name)
110+
d.Set("subnet_num", len(routetable.entryInfos))
111+
d.Set("create_time", routetable.createTime)
131112

132-
routes := make([]map[string]interface{}, 0, len(rt.RouteTableSet))
133-
for _, r := range rt.RouteTableSet {
134-
if strings.ToUpper(r.UnNextHub) == "LOCAL" {
113+
routes := make([]map[string]interface{}, 0, len(routetable.entryInfos))
114+
for _, r := range routetable.entryInfos {
115+
if strings.ToUpper(r.nextBub) == "LOCAL" {
135116
continue
136117
}
137118
m := make(map[string]interface{})
138-
for vgwKey, vgwType := range nextTypes {
139-
if vgwType == r.NextType {
140-
m["next_type"] = vgwKey
119+
nextType := ""
120+
for k, v := range routeTypeNewMap {
121+
if v == r.nextType {
122+
nextType = k
141123
break
142124
}
143125
}
144-
m["next_hub"] = r.UnNextHub
145-
m["cidr_block"] = r.DestinationCidrBlock
146-
m["description"] = r.Description
126+
m["next_type"] = nextType
127+
m["next_hub"] = r.nextBub
128+
m["cidr_block"] = r.destinationCidr
129+
m["description"] = r.description
147130
routes = append(routes, m)
148131
}
149132

150133
if err := d.Set("routes", routes); err != nil {
151-
log.Printf("[DEBUG] data_source_tc_route_table read d.Set routes error:%v", err)
152134
return err
153135
}
154136

tencentcloud/data_source_tc_route_table_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestAccDataSourceTencentCloudRouteTable_basic(t *testing.T) {
1515
Config: testAccDataSourceTencentCloudRouteTableConfig,
1616
Check: resource.ComposeTestCheckFunc(
1717
testAccCheckTencentCloudDataSourceID("data.tencentcloud_route_table.foo"),
18-
resource.TestCheckResourceAttr("data.tencentcloud_route_table.foo", "name", "ci-temp-test-rt"),
18+
resource.TestCheckResourceAttr("data.tencentcloud_route_table.foo", "name", "tf-ci-test"),
1919
),
2020
},
2121
},
@@ -24,20 +24,20 @@ func TestAccDataSourceTencentCloudRouteTable_basic(t *testing.T) {
2424

2525
const testAccDataSourceTencentCloudRouteTableConfig = `
2626
variable "availability_zone" {
27-
default = "ap-guangzhou-3"
27+
default = "ap-guangzhou-3"
2828
}
2929
3030
resource "tencentcloud_vpc" "foo" {
31-
name = "guagua-ci-temp-test"
32-
cidr_block = "10.0.0.0/16"
31+
name = "tf-ci-test"
32+
cidr_block = "10.0.0.0/16"
3333
}
3434
3535
resource "tencentcloud_route_table" "route_table" {
36-
vpc_id = "${tencentcloud_vpc.foo.id}"
37-
name = "ci-temp-test-rt"
36+
vpc_id = "${tencentcloud_vpc.foo.id}"
37+
name = "tf-ci-test"
3838
}
3939
4040
data "tencentcloud_route_table" "foo" {
41-
route_table_id = "${tencentcloud_route_table.route_table.id}"
41+
route_table_id = "${tencentcloud_route_table.route_table.id}"
4242
}
4343
`

tencentcloud/data_source_tc_security_group.go

Lines changed: 49 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@ data "tencentcloud_security_group" "sglab" {
1414
package tencentcloud
1515

1616
import (
17-
"encoding/json"
18-
"errors"
17+
"context"
1918
"fmt"
20-
"log"
21-
"reflect"
22-
"strconv"
2319

2420
"github.com/hashicorp/terraform/helper/schema"
21+
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
2522
)
2623

2724
func dataSourceTencentCloudSecurityGroup() *schema.Resource {
@@ -30,23 +27,22 @@ func dataSourceTencentCloudSecurityGroup() *schema.Resource {
3027
Read: dataSourceTencentCloudSecurityGroupRead,
3128
Schema: map[string]*schema.Schema{
3229
"security_group_id": {
33-
Type: schema.TypeString,
34-
Required: true,
35-
Description: "ID of the security group to be queried.",
30+
Type: schema.TypeString,
31+
Optional: true,
32+
ConflictsWith: []string{"name"},
33+
Description: "ID of the security group to be queried. Conflict with `name`.",
3634
},
3735
"name": {
38-
Type: schema.TypeString,
39-
Optional: true,
40-
Computed: true,
41-
ValidateFunc: validateStringLengthInRange(2, 60),
42-
Description: "Name of the security group to be queried.",
36+
Type: schema.TypeString,
37+
Optional: true,
38+
ValidateFunc: validateStringLengthInRange(1, 60),
39+
ConflictsWith: []string{"security_group_id"},
40+
Description: "Name of the security group to be queried. Conflict with `security_group_id`.",
4341
},
4442
"description": {
45-
Type: schema.TypeString,
46-
Optional: true,
47-
Computed: true,
48-
ValidateFunc: validateStringLengthInRange(2, 100),
49-
Description: "Description of the security group.",
43+
Type: schema.TypeString,
44+
Computed: true,
45+
Description: "Description of the security group.",
5046
},
5147
"create_time": {
5248
Type: schema.TypeString,
@@ -59,73 +55,58 @@ func dataSourceTencentCloudSecurityGroup() *schema.Resource {
5955
Description: "Number of security group binding resources.",
6056
},
6157
"project_id": {
62-
Type: schema.TypeInt,
63-
Computed: true,
58+
Type: schema.TypeInt,
59+
Computed: true,
60+
Description: "Project ID of the security group.",
6461
},
6562
},
6663
}
6764
}
6865

69-
func dataSourceTencentCloudSecurityGroupRead(d *schema.ResourceData, m interface{}) error {
70-
client := m.(*TencentCloudClient).commonConn
71-
params := map[string]string{
72-
"Action": "DescribeSecurityGroupEx",
73-
"sgId": d.Get("security_group_id").(string),
66+
func dataSourceTencentCloudSecurityGroupRead(d *schema.ResourceData, meta interface{}) error {
67+
defer logElapsed("data_source.tencentcloud_security_group.read")()
68+
69+
logId := getLogId(contextNil)
70+
ctx := context.WithValue(context.TODO(), "logId", logId)
71+
72+
client := meta.(*TencentCloudClient).apiV3Conn
73+
vpcService := VpcService{client: client}
74+
75+
var (
76+
sgId *string
77+
sgName *string
78+
)
79+
80+
if raw, ok := d.GetOk("security_group_id"); ok {
81+
sgId = common.StringPtr(raw.(string))
7482
}
7583

76-
log.Printf("[DEBUG] resource_tc_security_group read params:%v", params)
84+
if raw, ok := d.GetOk("name"); ok {
85+
sgName = common.StringPtr(raw.(string))
86+
}
7787

78-
response, err := client.SendRequest("dfw", params)
88+
sgs, err := vpcService.DescribeSecurityGroups(ctx, sgId, sgName, nil, map[string]string{})
7989
if err != nil {
80-
log.Printf("[ERROR] resource_tc_security_group read client.SendRequest error:%v", err)
8190
return err
8291
}
8392

84-
var jsonresp struct {
85-
Code int `json:"code"`
86-
Message string `json:"message"`
87-
Data struct {
88-
TotalNum int `json:"totalNum"`
89-
Detail []struct {
90-
SgId string `json:"sgId"`
91-
SgName string `json:"sgName"`
92-
SgRemark string `json:"sgRemark"`
93-
BeAssociateCount int `json:"beAssociateCount"`
94-
CreateTime string `json:"createTime"`
95-
ProjectId interface{} `json:"projectId"`
96-
}
97-
}
93+
if len(sgs) == 0 {
94+
return fmt.Errorf("security group security_group_id=%s, name=%s not found", *sgId, *sgName)
9895
}
99-
err = json.Unmarshal([]byte(response), &jsonresp)
96+
97+
sg := sgs[0]
98+
in, out, _, err := vpcService.DescribeSecurityGroupPolices(ctx, *sg.SecurityGroupId)
10099
if err != nil {
101-
log.Printf("[ERROR] resource_tc_security_group read json.Unmarshal error:%v", err)
102100
return err
103101
}
104-
if jsonresp.Code != 0 {
105-
log.Printf("[ERROR] resource_tc_security_group read error, code:%v, message:%v", jsonresp.Code, jsonresp.Message)
106-
return errors.New(jsonresp.Message)
107-
} else if jsonresp.Data.TotalNum <= 0 || len(jsonresp.Data.Detail) <= 0 {
108-
return errors.New("Security group not found")
109-
}
110102

111-
sg := jsonresp.Data.Detail[0]
103+
d.SetId(*sg.SecurityGroupId)
104+
d.Set("security_group_id", sg.SecurityGroupId)
105+
d.Set("name", sg.SecurityGroupName)
106+
d.Set("description", sg.SecurityGroupDesc)
107+
d.Set("create_time", sg.CreatedTime)
108+
d.Set("be_associate_count", len(in)+len(out))
109+
d.Set("project_id", sg.ProjectId)
112110

113-
d.SetId(sg.SgId)
114-
d.Set("security_group_id", sg.SgId)
115-
d.Set("name", sg.SgName)
116-
d.Set("description", sg.SgRemark)
117-
d.Set("create_time", sg.CreateTime)
118-
d.Set("be_associate_count", sg.BeAssociateCount)
119-
120-
if "string" == reflect.TypeOf(sg.ProjectId).String() {
121-
if intVal, err := strconv.ParseInt(sg.ProjectId.(string), 10, 64); err != nil {
122-
return fmt.Errorf("create security_group project ParseInt error ,%s", err.Error())
123-
} else {
124-
d.Set("project_id", int(intVal))
125-
}
126-
127-
} else {
128-
d.Set("project_id", sg.ProjectId.(int))
129-
}
130111
return nil
131112
}

0 commit comments

Comments
 (0)