/
data_source_tc_subnet.go
149 lines (135 loc) · 3.66 KB
/
data_source_tc_subnet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package tencentcloud
import (
"encoding/json"
"errors"
"fmt"
"log"
"strconv"
"github.com/hashicorp/terraform/helper/schema"
)
func dataSourceTencentCloudSubnet() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudSubnetRead,
Schema: map[string]*schema.Schema{
"vpc_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"subnet_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"cidr_block": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"availability_zone": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"route_table_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceTencentCloudSubnetRead(d *schema.ResourceData, m interface{}) error {
client := m.(*TencentCloudClient).commonConn
subnetParams := map[string]string{
"Action": "DescribeSubnet",
"vpcId": d.Get("vpc_id").(string),
"subnetId": d.Get("subnet_id").(string),
}
response, err := client.SendRequest("vpc", subnetParams)
if err != nil {
return err
}
var jsonresp struct {
Code int `json:"code"`
Message string `json:"message"`
CodeDesc string `json:"codeDesc"`
SubnetName string `json:"subnetName"`
CidrBlock string `json:"cidrBlock"`
RouteTableId string `json:"routeTableId"`
ZoneId int `json:"zoneId"`
AvailabilityZone string
}
err = json.Unmarshal([]byte(response), &jsonresp)
if err != nil {
return err
}
if jsonresp.CodeDesc == "InvalidSubnet.NotFound" {
return fmt.Errorf("Subnet Not Found")
} else if jsonresp.ZoneId == 0 {
return fmt.Errorf("data_source_tc_subnet got error, ZoneId is empty")
} else if jsonresp.Code != 0 {
return fmt.Errorf("data_source_tc_subnet got error, code:%v, message:%v", jsonresp.Code, jsonresp.Message)
}
subnet := jsonresp
subnetZoneId := strconv.Itoa(subnet.ZoneId)
//[zone] transform [availability_zone], eg: 100002 --> ap-guangzhou-2
params := map[string]string{
"Version": "2017-03-12",
"Action": "DescribeZones",
}
response, err = client.SendRequest("cvm", params)
if err != nil {
return err
}
type Zone struct {
Zone string `json:"Zone"`
ZoneName string `json:"ZoneName"`
ZoneId string `json:"ZoneId"`
ZoneState string `json:"ZoneState"`
}
var ZoneJsonresp struct {
Response struct {
Error struct {
Code string `json:"Code"`
Message string `json:"Message"`
}
RequestId string `json:"RequestId"`
ZoneSet []Zone
}
}
err = json.Unmarshal([]byte(response), &ZoneJsonresp)
if err != nil {
return err
}
if ZoneJsonresp.Response.Error.Code != "" {
return fmt.Errorf(
"tencentcloud_availability_zones got error, code:%v, message:%v",
ZoneJsonresp.Response.Error.Code,
ZoneJsonresp.Response.Error.Message,
)
}
if len(ZoneJsonresp.Response.ZoneSet) == 0 {
return errors.New("No avalability zones found")
}
for _, zone := range ZoneJsonresp.Response.ZoneSet {
log.Printf(
"[DEBUG] tencentcloud_availability_zones - Zone found id: %v, name:% v, description: %v, state: %v",
zone.ZoneId,
zone.Zone,
zone.ZoneName,
zone.ZoneState,
)
if zone.ZoneId == subnetZoneId {
subnet.AvailabilityZone = zone.Zone
}
}
if subnet.AvailabilityZone == "" {
return errors.New("No avalability zones found")
}
d.SetId(subnetParams["subnetId"])
d.Set("cidr_block", subnet.CidrBlock)
d.Set("name", subnet.SubnetName)
d.Set("route_table_id", subnet.RouteTableId)
d.Set("availability_zone", subnet.AvailabilityZone)
return nil
}