Skip to content

Commit a793625

Browse files
committed
feat: add eni resources
Signed-off-by: Sherlock Holo <[email protected]>
1 parent 5aafc2a commit a793625

13 files changed

+3310
-0
lines changed

tencentcloud/data_source_tc_enis.go

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
/*
2+
Use this data source to query query ENIs.
3+
4+
Example Usage
5+
6+
```hcl
7+
data "tencentcloud_enis" "name" {
8+
name = "test eni"
9+
}
10+
```
11+
*/
12+
package tencentcloud
13+
14+
import (
15+
"context"
16+
"errors"
17+
"fmt"
18+
"log"
19+
20+
"github.com/hashicorp/terraform/helper/schema"
21+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
22+
)
23+
24+
func dataSourceTencentCloudEnis() *schema.Resource {
25+
return &schema.Resource{
26+
Read: dataSourceTencentCloudEnisRead,
27+
Schema: map[string]*schema.Schema{
28+
"ids": {
29+
Type: schema.TypeSet,
30+
Optional: true,
31+
Elem: &schema.Schema{Type: schema.TypeString},
32+
Set: schema.HashString,
33+
ConflictsWith: []string{"vpc_id", "subnet_id", "instance_id", "security_group", "name", "description", "ipv4", "tags"},
34+
Description: "ID of the ENIs to be queried. Conflict with `vpc_id`,`subnet_id`,`instance_id`,`security_group`,`name`,`ipv4` and `tags`.",
35+
},
36+
"vpc_id": {
37+
Type: schema.TypeString,
38+
Optional: true,
39+
ConflictsWith: []string{"ids"},
40+
Description: "ID of the vpc to be queried. Conflict with `ids`.",
41+
},
42+
"subnet_id": {
43+
Type: schema.TypeString,
44+
Optional: true,
45+
ConflictsWith: []string{"ids"},
46+
Description: "ID of the subnet within this vpc to be queried. Conflict with `ids`.",
47+
},
48+
"instance_id": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
ConflictsWith: []string{"ids"},
52+
Description: "ID of the instance which bind the ENI. Conflict with `ids`.",
53+
},
54+
"security_group": {
55+
Type: schema.TypeString,
56+
Optional: true,
57+
ConflictsWith: []string{"ids"},
58+
Description: "A set of security group IDs which bind the ENI. Conflict with `ids`.",
59+
},
60+
"name": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
ConflictsWith: []string{"ids"},
64+
Description: "Name of the ENI to be queried. Conflict with `ids`.",
65+
},
66+
"description": {
67+
Type: schema.TypeString,
68+
Optional: true,
69+
ConflictsWith: []string{"ids"},
70+
Description: "Description of the ENI. Conflict with `ids`.",
71+
},
72+
"ipv4": {
73+
Type: schema.TypeString,
74+
Optional: true,
75+
ConflictsWith: []string{"ids"},
76+
Description: "Intranet IP of the ENI. Conflict with `ids`.",
77+
},
78+
"tags": {
79+
Type: schema.TypeMap,
80+
Optional: true,
81+
ConflictsWith: []string{"ids"},
82+
Description: "Tags of the ENI. Conflict with `ids`.",
83+
},
84+
"result_output_file": {
85+
Type: schema.TypeString,
86+
Optional: true,
87+
Description: "Used to save results.",
88+
},
89+
90+
// computed
91+
"enis": {
92+
Type: schema.TypeList,
93+
Computed: true,
94+
Description: "An information list of ENIs. Each element contains the following attributes:",
95+
Elem: &schema.Resource{
96+
Schema: map[string]*schema.Schema{
97+
"id": {
98+
Type: schema.TypeString,
99+
Computed: true,
100+
Description: "ID of the ENI.",
101+
},
102+
"name": {
103+
Type: schema.TypeString,
104+
Computed: true,
105+
Description: "Name of the ENI.",
106+
},
107+
"description": {
108+
Type: schema.TypeString,
109+
Computed: true,
110+
Description: "Description of the ENI.",
111+
},
112+
"vpc_id": {
113+
Type: schema.TypeString,
114+
Computed: true,
115+
Description: "ID of the vpc.",
116+
},
117+
"subnet_id": {
118+
Type: schema.TypeString,
119+
Computed: true,
120+
Description: "ID of the subnet within this vpc.",
121+
},
122+
"security_groups": {
123+
Type: schema.TypeList,
124+
Elem: &schema.Schema{Type: schema.TypeString},
125+
Computed: true,
126+
Description: "A set of security group IDs which bind the ENI.",
127+
},
128+
"primary": {
129+
Type: schema.TypeBool,
130+
Computed: true,
131+
Description: "Indicates whether the IP is primary.",
132+
},
133+
"mac": {
134+
Type: schema.TypeString,
135+
Computed: true,
136+
Description: "MAC address.",
137+
},
138+
"state": {
139+
Type: schema.TypeString,
140+
Computed: true,
141+
Description: "States of the ENI.",
142+
},
143+
"ipv4s": {
144+
Type: schema.TypeList,
145+
Computed: true,
146+
Description: "A set of intranet IPv4s.",
147+
Elem: &schema.Resource{
148+
Schema: map[string]*schema.Schema{
149+
"ip": {
150+
Type: schema.TypeString,
151+
Computed: true,
152+
Description: "Intranet IP.",
153+
},
154+
"primary": {
155+
Type: schema.TypeBool,
156+
Computed: true,
157+
Description: "Indicates whether the IP is primary.",
158+
},
159+
"description": {
160+
Type: schema.TypeString,
161+
Computed: true,
162+
Description: "Description of the IP.",
163+
},
164+
},
165+
},
166+
},
167+
"instance_id": {
168+
Type: schema.TypeString,
169+
Computed: true,
170+
Description: "ID of the instance which bind the ENI.",
171+
},
172+
"tags": {
173+
Type: schema.TypeMap,
174+
Computed: true,
175+
Description: "Tags of the ENI.",
176+
},
177+
"create_time": {
178+
Type: schema.TypeString,
179+
Computed: true,
180+
Description: "Creation time of the ENI.",
181+
},
182+
},
183+
},
184+
},
185+
},
186+
}
187+
}
188+
189+
func dataSourceTencentCloudEnisRead(d *schema.ResourceData, m interface{}) error {
190+
defer logElapsed("data_source.tencentcloud_enis.read")()
191+
logId := getLogId(contextNil)
192+
ctx := context.WithValue(context.TODO(), "logId", logId)
193+
194+
service := VpcService{client: m.(*TencentCloudClient).apiV3Conn}
195+
196+
var (
197+
ids []string
198+
vpcId *string
199+
subnetId *string
200+
cvmId *string
201+
sgId *string
202+
name *string
203+
desc *string
204+
ipv4 *string
205+
)
206+
207+
if raw, ok := d.GetOk("ids"); ok {
208+
ids = expandStringList(raw.(*schema.Set).List())
209+
}
210+
211+
if raw, ok := d.GetOk("vpc_id"); ok {
212+
vpcId = stringToPointer(raw.(string))
213+
}
214+
if raw, ok := d.GetOk("subnet_id"); ok {
215+
subnetId = stringToPointer(raw.(string))
216+
}
217+
if raw, ok := d.GetOk("instance_id"); ok {
218+
cvmId = stringToPointer(raw.(string))
219+
}
220+
if raw, ok := d.GetOk("security_group"); ok {
221+
sgId = stringToPointer(raw.(string))
222+
}
223+
if raw, ok := d.GetOk("name"); ok {
224+
name = stringToPointer(raw.(string))
225+
}
226+
if raw, ok := d.GetOk("description"); ok {
227+
desc = stringToPointer(raw.(string))
228+
}
229+
if raw, ok := d.GetOk("ipv4"); ok {
230+
ipv4 = stringToPointer(raw.(string))
231+
}
232+
tags := getTags(d, "tags")
233+
234+
var (
235+
respEnis []*vpc.NetworkInterface
236+
err error
237+
)
238+
239+
if len(ids) > 0 {
240+
respEnis, err = service.DescribeEniById(ctx, ids)
241+
} else {
242+
respEnis, err = service.DescribeEniByFilters(ctx, vpcId, subnetId, cvmId, sgId, name, desc, ipv4, tags)
243+
}
244+
245+
if err != nil {
246+
return err
247+
}
248+
249+
enis := make([]map[string]interface{}, 0, len(respEnis))
250+
eniIds := make([]string, 0, len(respEnis))
251+
252+
for _, eni := range respEnis {
253+
if nilFields := CheckNil(eni, map[string]string{
254+
"NetworkInterfaceId": "id",
255+
"NetworkInterfaceName": "name",
256+
"NetworkInterfaceDescription": "description",
257+
"VpcId": "vpc id",
258+
"SubnetId": "subnet id",
259+
"MacAddress": "mac address",
260+
"State": "state",
261+
"CreatedTime": "create time",
262+
"Primary": "primary",
263+
}); len(nilFields) > 0 {
264+
return fmt.Errorf("eni %v are nil", nilFields)
265+
}
266+
267+
ipv4s := make([]map[string]interface{}, 0, len(eni.PrivateIpAddressSet))
268+
for _, ipv4 := range eni.PrivateIpAddressSet {
269+
if nilFields := CheckNil(ipv4, map[string]string{
270+
"PrivateIpAddress": "ip",
271+
"Primary": "primary",
272+
"Description": "description",
273+
}); len(nilFields) > 0 {
274+
return fmt.Errorf("eni ipv4 %v are nil", nilFields)
275+
}
276+
277+
ipv4s = append(ipv4s, map[string]interface{}{
278+
"ip": *ipv4.PrivateIpAddress,
279+
"primary": *ipv4.Primary,
280+
"description": *eni.NetworkInterfaceDescription,
281+
})
282+
}
283+
284+
sgs := make([]string, 0, len(eni.GroupSet))
285+
for _, sg := range eni.GroupSet {
286+
sgs = append(sgs, *sg)
287+
}
288+
289+
respTags := make(map[string]string, len(eni.TagSet))
290+
for _, tag := range eni.TagSet {
291+
if tag.Key == nil {
292+
return errors.New("eni tag key is nil")
293+
}
294+
if tag.Value == nil {
295+
return errors.New("eni tag value is nil")
296+
}
297+
298+
respTags[*tag.Key] = *tag.Value
299+
}
300+
301+
eniIds = append(eniIds, *eni.NetworkInterfaceId)
302+
303+
m := map[string]interface{}{
304+
"id": *eni.NetworkInterfaceId,
305+
"name": *eni.NetworkInterfaceName,
306+
"description": *eni.NetworkInterfaceDescription,
307+
"vpc_id": *eni.VpcId,
308+
"subnet_id": *eni.SubnetId,
309+
"primary": *eni.Primary,
310+
"mac": *eni.MacAddress,
311+
"state": *eni.State,
312+
"create_time": *eni.CreatedTime,
313+
"ipv4s": ipv4s,
314+
"security_groups": sgs,
315+
"tags": respTags,
316+
}
317+
318+
if eni.Attachment != nil {
319+
if eni.Attachment.InstanceId == nil {
320+
return errors.New("eni attach instance id is nil")
321+
}
322+
m["instance_id"] = *eni.Attachment.InstanceId
323+
}
324+
325+
enis = append(enis, m)
326+
}
327+
328+
d.Set("enis", enis)
329+
d.SetId(dataResourceIdsHash(eniIds))
330+
331+
if output, ok := d.GetOk("result_output_file"); ok && output.(string) != "" {
332+
if err := writeToFile(output.(string), enis); err != nil {
333+
log.Printf("[CRITAL]%s output file[%s] fail, reason[%v]",
334+
logId, output.(string), err)
335+
return err
336+
}
337+
}
338+
339+
return nil
340+
}

0 commit comments

Comments
 (0)