Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 70 additions & 3 deletions tencentcloud/resource_tc_eni_sg_attachment_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package tencentcloud

import (
"context"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccTencentCloudVpcEniSgAttachmentResource_basic(t *testing.T) {
Expand All @@ -12,21 +15,85 @@ func TestAccTencentCloudVpcEniSgAttachmentResource_basic(t *testing.T) {
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Providers: testAccProviders,
CheckDestroy: testAccCheckEniSgAttachmentDestroy,
Steps: []resource.TestStep{
{
Config: testAccVpcEniSgAttachment,
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_vpc_eni_sg_attachment.eni_sg_attachment", "id")),
Check: resource.ComposeTestCheckFunc(testAccCheckEniSgAttachmentExists("tencentcloud_eni_sg_attachment.eni_sg_attachment"),
resource.TestCheckResourceAttrSet("tencentcloud_eni_sg_attachment.eni_sg_attachment", "network_interface_ids.#"),
resource.TestCheckResourceAttrSet("tencentcloud_eni_sg_attachment.eni_sg_attachment", "security_group_ids.#")),
},
{
ResourceName: "tencentcloud_vpc_eni_sg_attachment.eni_sg_attachment",
ResourceName: "tencentcloud_eni_sg_attachment.eni_sg_attachment",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckEniSgAttachmentExists(r string) resource.TestCheckFunc {
return func(s *terraform.State) error {
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)

rs, ok := s.RootModule().Resources[r]
if !ok {
return fmt.Errorf("resource %s is not found", r)
}

service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}

enis, err := service.DescribeEniById(ctx, []string{rs.Primary.ID})
if err != nil {
return err
}
tmpMap := make(map[string]struct{})

for _, v := range enis[0].GroupSet {
tmpMap[*v] = struct{}{}
}
value1, exists1 := rs.Primary.Attributes["security_group_ids.0"]
value2, exists2 := rs.Primary.Attributes["security_group_ids.1"]
if exists1 && exists2 {
_, exists3 := tmpMap[value1]
_, exists4 := tmpMap[value2]
if exists3 && exists4 {
return nil
}
}

return fmt.Errorf("EniSgAttachment %s not found on server", rs.Primary.ID)
}
}

func testAccCheckEniSgAttachmentDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "tencentcloud_eni_sg_attachment" {
continue
}
logId := getLogId(contextNil)
ctx := context.WithValue(context.TODO(), logIdKey, logId)
service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}

enis, err := service.DescribeEniById(ctx, []string{rs.Primary.ID})
if err != nil {
return err
}
if enis == nil || len(enis) < 1 {
return nil
}
for _, v := range enis[0].GroupSet {
if rs.Primary.ID == *v {
return fmt.Errorf("delete EniSgAttachment %s fail, still on server", rs.Primary.ID)
}
}
return nil
}
return nil
}

const testAccVpcEniSgAttachment = `

resource "tencentcloud_eni_sg_attachment" "eni_sg_attachment" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestAccTencentCloudVpcBandwidthPackageAttachment_basic(t *testing.T) {
Config: testAccVpcBandwidthPackageAttachment,
Check: resource.ComposeTestCheckFunc(
testAccCheckBandwidthPackageAttachmentExists("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment"),
resource.TestCheckResourceAttr("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment", "resource_id", "eip-r2l240dq"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment", "resource_id"),
resource.TestCheckResourceAttr("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment", "network_type", "BGP"),
resource.TestCheckResourceAttr("tencentcloud_vpc_bandwidth_package_attachment.bandwidthPackageAttachment", "resource_type", "Address"),
),
Expand Down Expand Up @@ -103,6 +103,10 @@ func testAccCheckBandwidthPackageAttachmentExists(r string) resource.TestCheckFu

const testAccVpcBandwidthPackageAttachment = `

resource "tencentcloud_eip" "foo" {
name = "gateway_eip"
}

resource "tencentcloud_vpc_bandwidth_package" "bandwidth_package" {
network_type = "BGP"
charge_type = "TOP5_POSTPAID_BY_MONTH"
Expand All @@ -113,7 +117,7 @@ resource "tencentcloud_vpc_bandwidth_package" "bandwidth_package" {
}

resource "tencentcloud_vpc_bandwidth_package_attachment" "bandwidthPackageAttachment" {
resource_id = "eip-r2l240dq"
resource_id = tencentcloud_eip.foo.id
bandwidth_package_id = tencentcloud_vpc_bandwidth_package.bandwidth_package.id
network_type = "BGP"
resource_type = "Address"
Expand Down
82 changes: 81 additions & 1 deletion tencentcloud/resource_tc_vpc_ipv6_eni_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ func TestAccTencentCloudVpcIpv6EniAddressResource_basic(t *testing.T) {
PreCheck: func() {
testAccPreCheck(t)
},
//CheckDestroy: testAccCheckVpcIpv6EniAddressDestroy,
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccVpcIpv6EniAddress,
Check: resource.ComposeTestCheckFunc(resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "id")),
Check: resource.ComposeTestCheckFunc(
//testAccCheckBandwidthPackageAttachmentExists("tencentcloud_vpc_ipv6_eni_address"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "vpc_id"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "network_interface_id"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.address"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.primary"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.address_id"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.description"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.is_wan_ip_blocked"),
resource.TestCheckResourceAttrSet("tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address", "ipv6_addresses.0.state"),
),
},
{
ResourceName: "tencentcloud_vpc_ipv6_eni_address.ipv6_eni_address",
Expand All @@ -27,6 +39,74 @@ func TestAccTencentCloudVpcIpv6EniAddressResource_basic(t *testing.T) {
})
}

//
//func testAccCheckVpcIpv6EniAddressDestroy(s *terraform.State) error {
// logId := getLogId(contextNil)
// ctx := context.WithValue(context.TODO(), logIdKey, logId)
// service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
// for _, rs := range s.RootModule().Resources {
// if rs.Type != "tencentcloud_vpc_bandwidth_package_attachment" {
// continue
// }
// idSplit := strings.Split(rs.Primary.ID, FILED_SP)
// if len(idSplit) != 2 {
// return fmt.Errorf("id is broken,%s", rs.Primary.ID)
// }
// bandwidthPackageId := idSplit[0]
// resourceId := idSplit[1]
//
// bandwidthPackageResources, err := service.DescribeVpcBandwidthPackageAttachment(ctx, bandwidthPackageId, resourceId)
// if err != nil {
// log.Printf("[CRITAL]%s read VPN connection failed, reason:%s\n", logId, err.Error())
// ee, ok := err.(*errors.TencentCloudSDKError)
// if !ok {
// return err
// }
// fmt.Print(ee)
// if ee.Code == "InvalidParameterValue.BandwidthPackageNotFound" {
// return nil
// } else {
// return err
// }
// } else {
// if bandwidthPackageResources != nil {
// return fmt.Errorf("vpc bandwidthPackageResources %s still exists", rs.Primary.ID)
// }
// }
// }
// return nil
//}
//
//func testAccCheckVpcIpv6EniAddressExists(r string) resource.TestCheckFunc {
// return func(s *terraform.State) error {
// logId := getLogId(contextNil)
// ctx := context.WithValue(context.TODO(), logIdKey, logId)
//
// rs, ok := s.RootModule().Resources[r]
// if !ok {
// return fmt.Errorf("resource %s is not found", r)
// }
//
// idSplit := strings.Split(rs.Primary.ID, FILED_SP)
// if len(idSplit) != 2 {
// return fmt.Errorf("id is broken,%s", rs.Primary.ID)
// }
// bandwidthPackageId := idSplit[0]
// resourceId := idSplit[1]
//
// service := VpcService{client: testAccProvider.Meta().(*TencentCloudClient).apiV3Conn}
// bandwidthPackageResources, err := service.DescribeVpcBandwidthPackageAttachment(ctx, bandwidthPackageId, resourceId)
// if bandwidthPackageResources == nil {
// return fmt.Errorf("vpc bandwidthPackageResources %s is not found", rs.Primary.ID)
// }
// if err != nil {
// return err
// }
//
// return nil
// }
//}

const testAccVpcIpv6EniAddress = `

resource "tencentcloud_vpc_ipv6_eni_address" "ipv6_eni_address" {
Expand Down