Skip to content

Commit 3c06023

Browse files
committed
refactor: remove some checkNil ensure user won't block on read
remove some checkNil ensure user won't block when terraform refresh optimize eni ipv4s argument, use default set function, it will call schema.HashResource when set elem type is a full resource Signed-off-by: Sherlock Holo <[email protected]>
1 parent 30a7e6e commit 3c06023

File tree

5 files changed

+65
-163
lines changed

5 files changed

+65
-163
lines changed

tencentcloud/data_source_tc_enis.go

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ package tencentcloud
1313

1414
import (
1515
"context"
16-
"errors"
17-
"fmt"
1816
"log"
1917

2018
"github.com/hashicorp/terraform/helper/schema"
@@ -250,34 +248,12 @@ func dataSourceTencentCloudEnisRead(d *schema.ResourceData, m interface{}) error
250248
eniIds := make([]string, 0, len(respEnis))
251249

252250
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-
267251
ipv4s := make([]map[string]interface{}, 0, len(eni.PrivateIpAddressSet))
268252
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-
277253
ipv4s = append(ipv4s, map[string]interface{}{
278-
"ip": *ipv4.PrivateIpAddress,
279-
"primary": *ipv4.Primary,
280-
"description": *eni.NetworkInterfaceDescription,
254+
"ip": ipv4.PrivateIpAddress,
255+
"primary": ipv4.Primary,
256+
"description": eni.NetworkInterfaceDescription,
281257
})
282258
}
283259

@@ -288,38 +264,28 @@ func dataSourceTencentCloudEnisRead(d *schema.ResourceData, m interface{}) error
288264

289265
respTags := make(map[string]string, len(eni.TagSet))
290266
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-
298267
respTags[*tag.Key] = *tag.Value
299268
}
300269

301270
eniIds = append(eniIds, *eni.NetworkInterfaceId)
302271

303272
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,
273+
"id": eni.NetworkInterfaceId,
274+
"name": eni.NetworkInterfaceName,
275+
"description": eni.NetworkInterfaceDescription,
276+
"vpc_id": eni.VpcId,
277+
"subnet_id": eni.SubnetId,
278+
"primary": eni.Primary,
279+
"mac": eni.MacAddress,
280+
"state": eni.State,
281+
"create_time": eni.CreatedTime,
313282
"ipv4s": ipv4s,
314283
"security_groups": sgs,
315284
"tags": respTags,
316285
}
317286

318287
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
288+
m["instance_id"] = eni.Attachment.InstanceId
323289
}
324290

325291
enis = append(enis, m)

tencentcloud/extension_eni.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

tencentcloud/extension_vpc.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,24 @@ const (
3535
EIP_STATUS_OFFLINING = "OFFLINING"
3636
EIP_STATUS_BIND_ENI = "BIND_ENI"
3737
)
38+
39+
// ENI
40+
const (
41+
ENI_DESCRIBE_LIMIT = 100
42+
)
43+
44+
const (
45+
ENI_STATE_PENDING = "PENDING"
46+
ENI_STATE_AVAILABLE = "AVAILABLE"
47+
ENI_STATE_ATTACHING = "ATTACHING"
48+
ENI_STATE_DETACHING = "DETACHING"
49+
ENI_STATE_DELETING = "DELETING"
50+
)
51+
52+
const (
53+
ENI_IP_PENDING = "PENDING"
54+
ENI_IP_AVAILABLE = "AVAILABLE"
55+
ENI_IP_ATTACHING = "ATTACHING"
56+
ENI_IP_DETACHING = "DETACHING"
57+
ENI_IP_DELETING = "DELETING"
58+
)

tencentcloud/resource_tc_eni.go

Lines changed: 28 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,8 @@ import (
4444

4545
"github.com/hashicorp/terraform/helper/schema"
4646
"github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
47-
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
4847
)
4948

50-
func eniIpInputResource() *schema.Resource {
51-
return &schema.Resource{
52-
Schema: map[string]*schema.Schema{
53-
"ip": {
54-
Type: schema.TypeString,
55-
Required: true,
56-
Description: "Intranet IP.",
57-
},
58-
"primary": {
59-
Type: schema.TypeBool,
60-
Required: true,
61-
Description: "Indicates whether the IP is primary.",
62-
},
63-
"description": {
64-
Type: schema.TypeString,
65-
Optional: true,
66-
Default: "",
67-
Description: "Description of the IP, maximum length 25.",
68-
ValidateFunc: validateStringLengthInRange(0, 25),
69-
},
70-
},
71-
}
72-
}
73-
7449
func eniIpOutputResource() *schema.Resource {
7550
return &schema.Resource{
7651
Schema: map[string]*schema.Schema{
@@ -140,10 +115,29 @@ func resourceTencentCloudEni() *schema.Resource {
140115
Type: schema.TypeSet,
141116
Optional: true,
142117
ConflictsWith: []string{"ipv4_count"},
143-
Elem: eniIpInputResource(),
144-
Set: schema.HashResource(eniIpInputResource()),
145-
MaxItems: 30,
146-
Description: "Applying for intranet IPv4s collection, conflict with `ipv4_count`. When there are multiple ipv4s, can only be one primary IP, and the maximum length of the array is 30. Each element contains the following attributes:",
118+
Elem: &schema.Resource{
119+
Schema: map[string]*schema.Schema{
120+
"ip": {
121+
Type: schema.TypeString,
122+
Required: true,
123+
Description: "Intranet IP.",
124+
},
125+
"primary": {
126+
Type: schema.TypeBool,
127+
Required: true,
128+
Description: "Indicates whether the IP is primary.",
129+
},
130+
"description": {
131+
Type: schema.TypeString,
132+
Optional: true,
133+
Default: "",
134+
Description: "Description of the IP, maximum length 25.",
135+
ValidateFunc: validateStringLengthInRange(0, 25),
136+
},
137+
},
138+
},
139+
MaxItems: 30,
140+
Description: "Applying for intranet IPv4s collection, conflict with `ipv4_count`. When there are multiple ipv4s, can only be one primary IP, and the maximum length of the array is 30. Each element contains the following attributes:",
147141
},
148142
"ipv4_count": {
149143
Type: schema.TypeInt,
@@ -364,35 +358,12 @@ func resourceTencentCloudEniRead(d *schema.ResourceData, m interface{}) error {
364358
return err
365359
}
366360

367-
var eni *vpc.NetworkInterface
368-
for _, e := range enis {
369-
if e.NetworkInterfaceId == nil {
370-
return errors.New("eni id is nil")
371-
}
372-
373-
if *e.NetworkInterfaceId == id {
374-
eni = e
375-
break
376-
}
377-
}
378-
379-
if eni == nil {
361+
if len(enis) < 1 {
380362
d.SetId("")
381363
return nil
382364
}
383365

384-
if nilFields := CheckNil(eni, map[string]string{
385-
"NetworkInterfaceName": "name",
386-
"NetworkInterfaceDescription": "description",
387-
"VpcId": "vpc id",
388-
"SubnetId": "subnet id",
389-
"MacAddress": "mac address",
390-
"State": "state",
391-
"CreatedTime": "create time",
392-
"Primary": "primary",
393-
}); len(nilFields) > 0 {
394-
return fmt.Errorf("eni %v are nil", nilFields)
395-
}
366+
eni := enis[0]
396367

397368
d.Set("name", eni.NetworkInterfaceName)
398369
d.Set("vpc_id", eni.VpcId)
@@ -409,24 +380,12 @@ func resourceTencentCloudEniRead(d *schema.ResourceData, m interface{}) error {
409380
}
410381
d.Set("security_groups", sgs)
411382

412-
if len(eni.PrivateIpAddressSet) == 0 {
413-
return errors.New("eni ipv4 is empty")
414-
}
415-
416383
ipv4s := make([]map[string]interface{}, 0, len(eni.PrivateIpAddressSet))
417384
for _, ipv4 := range eni.PrivateIpAddressSet {
418-
if nilFields := CheckNil(ipv4, map[string]string{
419-
"PrivateIpAddress": "ip",
420-
"Primary": "primary",
421-
"Description": "description",
422-
}); len(nilFields) > 0 {
423-
return fmt.Errorf("eni ipv4 %v are nil", nilFields)
424-
}
425-
426385
ipv4s = append(ipv4s, map[string]interface{}{
427-
"ip": *ipv4.PrivateIpAddress,
428-
"primary": *ipv4.Primary,
429-
"description": *ipv4.Description,
386+
"ip": ipv4.PrivateIpAddress,
387+
"primary": ipv4.Primary,
388+
"description": ipv4.Description,
430389
})
431390
}
432391
d.Set("ipv4_info", ipv4s)
@@ -440,13 +399,6 @@ func resourceTencentCloudEniRead(d *schema.ResourceData, m interface{}) error {
440399

441400
tags := make(map[string]string, len(eni.TagSet))
442401
for _, tag := range eni.TagSet {
443-
if tag.Key == nil {
444-
return errors.New("tag key is nil")
445-
}
446-
if tag.Value == nil {
447-
return errors.New("tag value is nil")
448-
}
449-
450402
tags[*tag.Key] = *tag.Value
451403
}
452404
d.Set("tags", tags)

tencentcloud/resource_tc_eni_attachment.go

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,11 @@ package tencentcloud
7272

7373
import (
7474
"context"
75-
"errors"
7675
"fmt"
7776
"log"
7877
"strings"
7978

8079
"github.com/hashicorp/terraform/helper/schema"
81-
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
8280
)
8381

8482
func resourceTencentCloudEniAttachment() *schema.Resource {
@@ -148,32 +146,18 @@ func resourceTencentCloudEniAttachmentRead(d *schema.ResourceData, m interface{}
148146
return err
149147
}
150148

151-
var eni *vpc.NetworkInterface
152-
for _, e := range enis {
153-
if e.NetworkInterfaceId == nil {
154-
return errors.New("eni id is nil")
155-
}
156-
157-
if *e.NetworkInterfaceId == eniId {
158-
eni = e
159-
break
160-
}
161-
}
162-
163-
if eni == nil {
149+
if len(enis) < 1 {
164150
d.SetId("")
165151
return nil
166152
}
167153

154+
eni := enis[0]
155+
168156
if eni.Attachment == nil {
169157
d.SetId("")
170158
return nil
171159
}
172160

173-
if eni.Attachment.InstanceId == nil {
174-
return errors.New("eni attach instance id is nil")
175-
}
176-
177161
d.Set("eni_id", eni.NetworkInterfaceId)
178162
d.Set("instance_id", eni.Attachment.InstanceId)
179163

0 commit comments

Comments
 (0)