Skip to content

Commit 8077998

Browse files
WeiMengXSWeiMengXS
andauthored
fix: fix vpc eni sg attachment (#2012)
* fix: fix vpc eni sg attachment * fix: fix vpc eni sg attachment * fix: delete change log * fix: fix vpc bandwidth test * fix: fix vpc ipv6 test --------- Co-authored-by: WeiMengXS <nickcchen@tencent.com>
1 parent 65a1c27 commit 8077998

File tree

3 files changed

+157
-6
lines changed

3 files changed

+157
-6
lines changed

tencentcloud/resource_tc_eni_sg_attachment_test.go

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package tencentcloud
22

33
import (
4+
"context"
5+
"fmt"
46
"testing"
57

68
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
710
)
811

912
func TestAccTencentCloudVpcEniSgAttachmentResource_basic(t *testing.T) {
@@ -12,21 +15,85 @@ func TestAccTencentCloudVpcEniSgAttachmentResource_basic(t *testing.T) {
1215
PreCheck: func() {
1316
testAccPreCheck(t)
1417
},
15-
Providers: testAccProviders,
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCheckEniSgAttachmentDestroy,
1620
Steps: []resource.TestStep{
1721
{
1822
Config: testAccVpcEniSgAttachment,
19-
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_vpc_eni_sg_attachment.eni_sg_attachment", "id")),
23+
Check: resource.ComposeTestCheckFunc(testAccCheckEniSgAttachmentExists("tencentcloud_eni_sg_attachment.eni_sg_attachment"),
24+
resource.TestCheckResourceAttrSet("tencentcloud_eni_sg_attachment.eni_sg_attachment", "network_interface_ids.#"),
25+
resource.TestCheckResourceAttrSet("tencentcloud_eni_sg_attachment.eni_sg_attachment", "security_group_ids.#")),
2026
},
2127
{
22-
ResourceName: "tencentcloud_vpc_eni_sg_attachment.eni_sg_attachment",
28+
ResourceName: "tencentcloud_eni_sg_attachment.eni_sg_attachment",
2329
ImportState: true,
2430
ImportStateVerify: true,
2531
},
2632
},
2733
})
2834
}
2935

36+
func testAccCheckEniSgAttachmentExists(r string) resource.TestCheckFunc {
37+
return func(s *terraform.State) error {
38+
logId := getLogId(contextNil)
39+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
40+
41+
rs, ok := s.RootModule().Resources[r]
42+
if !ok {
43+
return fmt.Errorf("resource %s is not found", r)
44+
}
45+
46+
service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
47+
48+
enis, err := service.DescribeEniById(ctx, []string{rs.Primary.ID})
49+
if err != nil {
50+
return err
51+
}
52+
tmpMap := make(map[string]struct{})
53+
54+
for _, v := range enis[0].GroupSet {
55+
tmpMap[*v] = struct{}{}
56+
}
57+
value1, exists1 := rs.Primary.Attributes["security_group_ids.0"]
58+
value2, exists2 := rs.Primary.Attributes["security_group_ids.1"]
59+
if exists1 && exists2 {
60+
_, exists3 := tmpMap[value1]
61+
_, exists4 := tmpMap[value2]
62+
if exists3 && exists4 {
63+
return nil
64+
}
65+
}
66+
67+
return fmt.Errorf("EniSgAttachment %s not found on server", rs.Primary.ID)
68+
}
69+
}
70+
71+
func testAccCheckEniSgAttachmentDestroy(s *terraform.State) error {
72+
for _, rs := range s.RootModule().Resources {
73+
if rs.Type != "tencentcloud_eni_sg_attachment" {
74+
continue
75+
}
76+
logId := getLogId(contextNil)
77+
ctx := context.WithValue(context.TODO(), logIdKey, logId)
78+
service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
79+
80+
enis, err := service.DescribeEniById(ctx, []string{rs.Primary.ID})
81+
if err != nil {
82+
return err
83+
}
84+
if enis == nil || len(enis) < 1 {
85+
return nil
86+
}
87+
for _, v := range enis[0].GroupSet {
88+
if rs.Primary.ID == *v {
89+
return fmt.Errorf("delete EniSgAttachment %s fail, still on server", rs.Primary.ID)
90+
}
91+
}
92+
return nil
93+
}
94+
return nil
95+
}
96+
3097
const testAccVpcEniSgAttachment = `
3198
3299
resource "tencentcloud_eni_sg_attachment" "eni_sg_attachment" {

tencentcloud/resource_tc_vpc_bandwidth_package_attachment_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestAccTencentCloudVpcBandwidthPackageAttachment_basic(t *testing.T) {
2525
Config: testAccVpcBandwidthPackageAttachment,
2626
Check: resource.ComposeTestCheckFunc(
2727
testAccCheckBandwidthPackageAttachmentExists("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment"),
28-
resource.TestCheckResourceAttr("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment", "resource_id", "eip-r2l240dq"),
28+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment", "resource_id"),
2929
resource.TestCheckResourceAttr("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment", "network_type", "BGP"),
3030
resource.TestCheckResourceAttr("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment", "resource_type", "Address"),
3131
),
@@ -103,6 +103,10 @@ func testAccCheckBandwidthPackageAttachmentExists(r string) resource.TestCheckFu
103103

104104
const testAccVpcBandwidthPackageAttachment = `
105105
106+
resource "tencentcloud_eip" "foo" {
107+
name = "gateway_eip"
108+
}
109+
106110
resource "tencentcloud_vpc_bandwidth_package" "bandwidth_package" {
107111
network_type = "BGP"
108112
charge_type = "TOP5_POSTPAID_BY_MONTH"
@@ -113,7 +117,7 @@ resource "tencentcloud_vpc_bandwidth_package" "bandwidth_package" {
113117
}
114118
115119
resource "tencentcloud_vpc_bandwidth_package_attachment" "bandwidthPackageAttachment" {
116-
resource_id = "eip-r2l240dq"
120+
resource_id = tencentcloud_eip.foo.id
117121
bandwidth_package_id = tencentcloud_vpc_bandwidth_package.bandwidth_package.id
118122
network_type = "BGP"
119123
resource_type = "Address"

tencentcloud/resource_tc_vpc_ipv6_eni_address_test.go

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,23 @@ func TestAccTencentCloudVpcIpv6EniAddressResource_basic(t *testing.T) {
1212
PreCheck: func() {
1313
testAccPreCheck(t)
1414
},
15+
//CheckDestroy: testAccCheckVpcIpv6EniAddressDestroy,
1516
Providers: testAccProviders,
1617
Steps: []resource.TestStep{
1718
{
1819
Config: testAccVpcIpv6EniAddress,
19-
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "id")),
20+
Check: resource.ComposeTestCheckFunc(
21+
//testAccCheckBandwidthPackageAttachmentExists("tencentcloud_vpc_ipv6_eni_address"),
22+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "vpc_id"),
23+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "network_interface_id"),
24+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses"),
25+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.address"),
26+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.primary"),
27+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.address_id"),
28+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.description"),
29+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.is_wan_ip_blocked"),
30+
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.state"),
31+
),
2032
},
2133
{
2234
ResourceName: "tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address",
@@ -27,6 +39,74 @@ func TestAccTencentCloudVpcIpv6EniAddressResource_basic(t *testing.T) {
2739
})
2840
}
2941

42+
//
43+
//func testAccCheckVpcIpv6EniAddressDestroy(s *terraform.State) error {
44+
// logId := getLogId(contextNil)
45+
// ctx := context.WithValue(context.TODO(), logIdKey, logId)
46+
// service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
47+
// for _, rs := range s.RootModule().Resources {
48+
// if rs.Type != "tencentcloud_vpc_bandwidth_package_attachment" {
49+
// continue
50+
// }
51+
// idSplit := strings.Split(rs.Primary.ID, FILED_SP)
52+
// if len(idSplit) != 2 {
53+
// return fmt.Errorf("id is broken,%s", rs.Primary.ID)
54+
// }
55+
// bandwidthPackageId := idSplit[0]
56+
// resourceId := idSplit[1]
57+
//
58+
// bandwidthPackageResources, err := service.DescribeVpcBandwidthPackageAttachment(ctx, bandwidthPackageId, resourceId)
59+
// if err != nil {
60+
// log.Printf("[CRITAL]%s read VPN connection failed, reason:%s\n", logId, err.Error())
61+
// ee, ok := err.(*errors.TencentCloudSDKError)
62+
// if !ok {
63+
// return err
64+
// }
65+
// fmt.Print(ee)
66+
// if ee.Code == "InvalidParameterValue.BandwidthPackageNotFound" {
67+
// return nil
68+
// } else {
69+
// return err
70+
// }
71+
// } else {
72+
// if bandwidthPackageResources != nil {
73+
// return fmt.Errorf("vpc bandwidthPackageResources %s still exists", rs.Primary.ID)
74+
// }
75+
// }
76+
// }
77+
// return nil
78+
//}
79+
//
80+
//func testAccCheckVpcIpv6EniAddressExists(r string) resource.TestCheckFunc {
81+
// return func(s *terraform.State) error {
82+
// logId := getLogId(contextNil)
83+
// ctx := context.WithValue(context.TODO(), logIdKey, logId)
84+
//
85+
// rs, ok := s.RootModule().Resources[r]
86+
// if !ok {
87+
// return fmt.Errorf("resource %s is not found", r)
88+
// }
89+
//
90+
// idSplit := strings.Split(rs.Primary.ID, FILED_SP)
91+
// if len(idSplit) != 2 {
92+
// return fmt.Errorf("id is broken,%s", rs.Primary.ID)
93+
// }
94+
// bandwidthPackageId := idSplit[0]
95+
// resourceId := idSplit[1]
96+
//
97+
// service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
98+
// bandwidthPackageResources, err := service.DescribeVpcBandwidthPackageAttachment(ctx, bandwidthPackageId, resourceId)
99+
// if bandwidthPackageResources == nil {
100+
// return fmt.Errorf("vpc bandwidthPackageResources %s is not found", rs.Primary.ID)
101+
// }
102+
// if err != nil {
103+
// return err
104+
// }
105+
//
106+
// return nil
107+
// }
108+
//}
109+
30110
const testAccVpcIpv6EniAddress = `
31111
32112
resource "tencentcloud_vpc_ipv6_eni_address" "ipv6_eni_address" {

0 commit comments

Comments
 (0)