-
Notifications
You must be signed in to change notification settings - Fork 134
/
data_source_tc_clb_cross_targets.go
200 lines (171 loc) · 5.83 KB
/
data_source_tc_clb_cross_targets.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
package clb
import (
"context"
tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
clb "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/clb/v20180317"
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper"
)
func DataSourceTencentCloudClbCrossTargets() *schema.Resource {
return &schema.Resource{
Read: dataSourceTencentCloudClbCrossTargetsRead,
Schema: map[string]*schema.Schema{
"filters": {
Optional: true,
Type: schema.TypeList,
Description: "Filter conditions to query CVMs and ENIs: vpc-id - String - Required: No - (Filter condition) Filter by VPC ID, such as vpc-12345678. ip - String - Required: No - (Filter condition) Filter by real server IP, such as 192.168.0.1. listener-id - String - Required: No - (Filter condition) Filter by listener ID, such as lbl-12345678. location-id - String - Required: No - (Filter condition) Filter by forwarding rule ID of the layer-7 listener, such as loc-12345678.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Filter name.",
},
"values": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Required: true,
Description: "Filter values.",
},
},
},
},
"cross_target_set": {
Computed: true,
Type: schema.TypeList,
Description: "Cross target set.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"local_vpc_id": {
Type: schema.TypeString,
Computed: true,
Description: "VPC ID of the CLB instance.",
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
Description: "VPC ID of the CVM or ENI instance.",
},
"ip": {
Type: schema.TypeString,
Computed: true,
Description: "IP address of the CVM or ENI instance.",
},
"vpc_name": {
Type: schema.TypeString,
Computed: true,
Description: "VPC name of the CVM or ENI instance.",
},
"eni_id": {
Type: schema.TypeString,
Computed: true,
Description: "ENI ID of the CVM instance.",
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the CVM instance.Note: This field may return null, indicating that no valid value was found.",
},
"instance_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the CVM instance. Note: This field may return null, indicating that no valid value was found.",
},
"region": {
Type: schema.TypeString,
Computed: true,
Description: "Region of the CVM or ENI instance.",
},
},
},
},
"result_output_file": {
Type: schema.TypeString,
Optional: true,
Description: "Used to save results.",
},
},
}
}
func dataSourceTencentCloudClbCrossTargetsRead(d *schema.ResourceData, meta interface{}) error {
defer tccommon.LogElapsed("data_source.tencentcloud_clb_cross_targets.read")()
defer tccommon.InconsistentCheck(d, meta)()
logId := tccommon.GetLogId(tccommon.ContextNil)
ctx := context.WithValue(context.TODO(), tccommon.LogIdKey, logId)
paramMap := make(map[string]interface{})
if v, ok := d.GetOk("filters"); ok {
filtersSet := v.([]interface{})
tmpSet := make([]*clb.Filter, 0, len(filtersSet))
for _, item := range filtersSet {
filter := clb.Filter{}
filterMap := item.(map[string]interface{})
if v, ok := filterMap["name"]; ok {
filter.Name = helper.String(v.(string))
}
if v, ok := filterMap["values"]; ok {
valuesSet := v.(*schema.Set).List()
filter.Values = helper.InterfacesStringsPoint(valuesSet)
}
tmpSet = append(tmpSet, &filter)
}
paramMap["Filters"] = tmpSet
}
service := ClbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
var crossTargetSet []*clb.CrossTargets
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
result, e := service.DescribeClbCrossTargetsByFilter(ctx, paramMap)
if e != nil {
return tccommon.RetryError(e)
}
crossTargetSet = result
return nil
})
if err != nil {
return err
}
ids := make([]string, 0, len(crossTargetSet))
tmpList := make([]map[string]interface{}, 0, len(crossTargetSet))
if crossTargetSet != nil {
for _, crossTargets := range crossTargetSet {
crossTargetsMap := map[string]interface{}{}
if crossTargets.LocalVpcId != nil {
crossTargetsMap["local_vpc_id"] = crossTargets.LocalVpcId
}
if crossTargets.VpcId != nil {
crossTargetsMap["vpc_id"] = crossTargets.VpcId
}
if crossTargets.IP != nil {
crossTargetsMap["ip"] = crossTargets.IP
}
if crossTargets.VpcName != nil {
crossTargetsMap["vpc_name"] = crossTargets.VpcName
}
if crossTargets.EniId != nil {
crossTargetsMap["eni_id"] = crossTargets.EniId
}
if crossTargets.InstanceId != nil {
crossTargetsMap["instance_id"] = crossTargets.InstanceId
}
if crossTargets.InstanceName != nil {
crossTargetsMap["instance_name"] = crossTargets.InstanceName
}
if crossTargets.Region != nil {
crossTargetsMap["region"] = crossTargets.Region
}
ids = append(ids, *crossTargets.VpcId)
tmpList = append(tmpList, crossTargetsMap)
}
_ = d.Set("cross_target_set", tmpList)
}
d.SetId(helper.DataResourceIdsHash(ids))
output, ok := d.GetOk("result_output_file")
if ok && output.(string) != "" {
if e := tccommon.WriteToFile(output.(string), tmpList); e != nil {
return e
}
}
return nil
}