Skip to content

Commit 30a7e6e

Browse files
committed
refactor(resource eni, eni_attachment): rewrite assign and unassign ipv4
Signed-off-by: Sherlock Holo <[email protected]>
1 parent a0cf4fc commit 30a7e6e

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

tencentcloud/resource_tc_eni.go

Lines changed: 30 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -286,29 +286,22 @@ func resourceTencentCloudEniCreate(d *schema.ResourceData, m interface{}) error
286286
}
287287

288288
// move primary ip to the first
289-
primaryIpv4 := ipv4s[i]
290-
copy(ipv4s[1:], ipv4s[:i])
291-
ipv4s[0] = primaryIpv4
289+
ipv4s[0], ipv4s[i] = ipv4s[i], ipv4s[0]
292290
break
293291
}
294292
}
295293

296-
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, nil, ipv4s[:10])
294+
ipv4ss := chunkEniIP(ipv4s)
295+
withPrimaryIpv4s := ipv4ss[0]
296+
297+
id, err = vpcService.CreateEni(ctx, name, vpcId, subnetId, desc, securityGroups, nil, withPrimaryIpv4s)
297298
if err != nil {
298299
return err
299300
}
300301

301302
d.SetId(id)
302303

303-
ipv4s = ipv4s[10:]
304-
for len(ipv4s) > 10 {
305-
if err = vpcService.AssignIpv4ToEni(ctx, id, ipv4s[:10], nil); err != nil {
306-
return err
307-
}
308-
ipv4s = ipv4s[10:]
309-
}
310-
// assign last ipv4s
311-
if len(ipv4s) > 0 {
304+
for _, ipv4s := range ipv4ss[1:] {
312305
if err = vpcService.AssignIpv4ToEni(ctx, id, ipv4s, nil); err != nil {
313306
return err
314307
}
@@ -595,15 +588,8 @@ func resourceTencentCloudEniUpdate(d *schema.ResourceData, m interface{}) error
595588
return err
596589
}
597590
} else {
598-
for len(removeIpv4) > 10 {
599-
if err := vpcService.UnAssignIpv4FromEni(ctx, id, removeIpv4[:10]); err != nil {
600-
return err
601-
}
602-
removeIpv4 = removeIpv4[10:]
603-
}
604-
// unAssign last ipv4
605-
if len(removeIpv4) > 0 {
606-
if err := vpcService.UnAssignIpv4FromEni(ctx, id, removeIpv4); err != nil {
591+
for _, remove := range chunkRemoveIpv4(removeIpv4) {
592+
if err := vpcService.UnAssignIpv4FromEni(ctx, id, remove); err != nil {
607593
return err
608594
}
609595
}
@@ -616,15 +602,8 @@ func resourceTencentCloudEniUpdate(d *schema.ResourceData, m interface{}) error
616602
return err
617603
}
618604
} else {
619-
for len(addIpv4) > 10 {
620-
if err := vpcService.AssignIpv4ToEni(ctx, id, addIpv4[:10], nil); err != nil {
621-
return err
622-
}
623-
addIpv4 = addIpv4[10:]
624-
}
625-
// assign last ipv4
626-
if len(addIpv4) > 0 {
627-
if err := vpcService.AssignIpv4ToEni(ctx, id, addIpv4, nil); err != nil {
605+
for _, add := range chunkEniIP(addIpv4) {
606+
if err := vpcService.AssignIpv4ToEni(ctx, id, add, nil); err != nil {
628607
return err
629608
}
630609
}
@@ -682,15 +661,8 @@ func resourceTencentCloudEniUpdate(d *schema.ResourceData, m interface{}) error
682661
return err
683662
}
684663
} else {
685-
for len(removeIpv4) > 10 {
686-
if err := vpcService.UnAssignIpv4FromEni(ctx, id, removeIpv4[:10]); err != nil {
687-
return err
688-
}
689-
removeIpv4 = removeIpv4[10:]
690-
}
691-
// unAssign last ipv4
692-
if len(removeIpv4) > 0 {
693-
if err := vpcService.UnAssignIpv4FromEni(ctx, id, removeIpv4); err != nil {
664+
for _, remove := range chunkRemoveIpv4(removeIpv4) {
665+
if err := vpcService.UnAssignIpv4FromEni(ctx, id, remove); err != nil {
694666
return err
695667
}
696668
}
@@ -730,3 +702,21 @@ func resourceTencentCloudEniDelete(d *schema.ResourceData, m interface{}) error
730702

731703
return service.DeleteEni(ctx, id)
732704
}
705+
706+
func chunkEniIP(ipv4s []VpcEniIP) [][]VpcEniIP {
707+
if len(ipv4s) <= 10 {
708+
return [][]VpcEniIP{ipv4s}
709+
}
710+
711+
first := ipv4s[:10]
712+
return append([][]VpcEniIP{first}, chunkEniIP(ipv4s[10:])...)
713+
}
714+
715+
func chunkRemoveIpv4(ss []string) [][]string {
716+
if len(ss) <= 10 {
717+
return [][]string{ss}
718+
}
719+
720+
s := ss[:10]
721+
return append([][]string{s}, chunkRemoveIpv4(ss[10:])...)
722+
}

tencentcloud/resource_tc_eni_attachment.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,6 @@ func resourceTencentCloudEniAttachmentRead(d *schema.ResourceData, m interface{}
165165
return nil
166166
}
167167

168-
if eni.NetworkInterfaceId == nil {
169-
return errors.New("eni id is nil")
170-
}
171-
172168
if eni.Attachment == nil {
173169
d.SetId("")
174170
return nil

0 commit comments

Comments
 (0)