This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
data_source_tc_ha_vips.go
227 lines (213 loc) · 6.15 KB
/
data_source_tc_ha_vips.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
Use this data source to query detailed information of HA VIPs.
Example Usage
```hcl
data "tencentcloud_ha_vips" "havips" {
id = "havip-kjqwe4ba"
name = "test"
vpc_id = "vpc-gzea3dd7"
subnet_id = "subnet-4d4m4cd4"
address_ip = "10.0.4.16"
}
```
*/
package tencentcloud
import (
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/pkg/errors"
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
"github.com/terraform-providers/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func dataSourceTencentCloudHaVips() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudHaVipsRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateStringLengthInRange(1, 60),
Description: "Name of the HA VIP. The length of character is limited to 1-60.",
},
"id": {
Type: schema.TypeString,
Optional: true,
Description: "Id of the HA VIP to be queried.",
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
Description: "VPC id of the HA VIP to be queried.",
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
Description: "Subnet id of the HA VIP to be queried.",
},
"address_ip": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateIp,
Description: "EIP of the HA VIP to be queried.",
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
// Computed values
"ha_vip_list": {
Type: schema.TypeList,
Computed: true,
Description: "Information list of the dedicated HA VIPs.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Description: "Id of the HA VIP.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the HA VIP.",
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Description: "VPC id.",
},
"subnet_id": {
Type: schema.TypeString,
Computed: true,
Description: "Subnet id.",
},
"vip": {
Type: schema.TypeString,
Computed: true,
Description: "Virtual IP address, it must not be occupied and in this VPC network segment. If not set, it will be assigned after resource created automatically.",
},
"state": {
Type: schema.TypeString,
Computed: true,
Description: "State of the HA VIP, values are `AVAILABLE`, `UNBIND`.",
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "Instance id that is associated.",
},
"network_interface_id": {
Type: schema.TypeString,
Computed: true,
Description: "Network interface id that is associated.",
},
"address_ip": {
Type: schema.TypeString,
Computed: true,
Description: "EIP that is associated.",
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Description: "Create time of the HA VIP.",
},
},
},
},
},
}
}
func dataSourceTencentCloudHaVipsRead(d *schema.ResourceData, meta interface{}) error {
defer logElapsed("data_source.tencentcloud_ha_vips.read")()
logId := getLogId(contextNil)
request := vpc.NewDescribeHaVipsRequest()
params := make(map[string]string)
if v, ok := d.GetOk("id"); ok {
params["havip-id"] = v.(string)
}
if v, ok := d.GetOk("name"); ok {
params["havip-name"] = v.(string)
}
if v, ok := d.GetOk("address_ip"); ok {
params["address-ip"] = v.(string)
}
if v, ok := d.GetOk("subnet_id"); ok {
params["subnet-id"] = v.(string)
}
if v, ok := d.GetOk("vpc_id"); ok {
params["vpc-ip"] = v.(string)
}
request.Filters = make([]*vpc.Filter, 0, len(params))
for k, v := range params {
filter := &vpc.Filter{
Name: helper.String(k),
Values: []*string{helper.String(v)},
}
request.Filters = append(request.Filters, filter)
}
offset := uint64(0)
request.Offset = &offset
result := make([]*vpc.HaVip, 0)
limit := uint64(HAVIP_DESCRIBE_LIMIT)
request.Limit = &limit
for {
var response *vpc.DescribeHaVipsResponse
err := resource.Retry(readRetryTimeout, func() *resource.RetryError {
result, e := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().DescribeHaVips(request)
if e != nil {
return retryError(errors.WithStack(e))
}
response = result
return nil
})
if err != nil {
log.Printf("[CRITAL]%s read HA VIP failed, reason:%+v", logId, err)
return err
} else {
result = append(result, response.Response.HaVipSet...)
if len(response.Response.HaVipSet) < HAVIP_DESCRIBE_LIMIT {
break
} else {
offset = offset + limit
}
}
}
ids := make([]string, 0, len(result))
haVipList := make([]map[string]interface{}, 0, len(result))
for _, haVip := range result {
mapping := map[string]interface{}{
"id": *haVip.HaVipId,
"vip": *haVip.Vip,
"name": *haVip.HaVipName,
"state": *haVip.State,
"vpc_id": *haVip.VpcId,
"subnet_id": *haVip.SubnetId,
"create_time": *haVip.CreatedTime,
}
if haVip.NetworkInterfaceId != nil {
mapping["network_interface_id"] = *haVip.NetworkInterfaceId
}
if haVip.AddressIp != nil {
mapping["address_ip"] = *haVip.AddressIp
}
if haVip.InstanceId != nil {
mapping["instance_id"] = *haVip.InstanceId
}
haVipList = append(haVipList, mapping)
ids = append(ids, *haVip.HaVipId)
}
d.SetId(helper.DataResourceIdsHash(ids))
if e := d.Set("ha_vip_list", haVipList); e != nil {
log.Printf("[CRITAL]%s provider set haVip list fail, reason:%s\n", logId, e)
return e
}
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := writeToFile(output.(string), haVipList); e != nil {
return e
}
}
return nil
}