diff --git a/tencentcloud/resource_tc_eni_sg_attachment_test.go b/tencentcloud/resource_tc_eni_sg_attachment_test.go index a0ee1920a8..264fb55918 100644 --- a/tencentcloud/resource_tc_eni_sg_attachment_test.go +++ b/tencentcloud/resource_tc_eni_sg_attachment_test.go @@ -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) { @@ -12,14 +15,17 @@ 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, }, @@ -27,6 +33,67 @@ func TestAccTencentCloudVpcEniSgAttachmentResource_basic(t *testing.T) { }) } +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" { diff --git a/tencentcloud/resource_tc_vpc_bandwidth_package_attachment_test.go b/tencentcloud/resource_tc_vpc_bandwidth_package_attachment_test.go index c70abbcb7e..4bd0f076c2 100644 --- a/tencentcloud/resource_tc_vpc_bandwidth_package_attachment_test.go +++ b/tencentcloud/resource_tc_vpc_bandwidth_package_attachment_test.go @@ -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"), ), @@ -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" @@ -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" diff --git a/tencentcloud/resource_tc_vpc_ipv6_eni_address_test.go b/tencentcloud/resource_tc_vpc_ipv6_eni_address_test.go index 33a7c94f48..5a52109f03 100644 --- a/tencentcloud/resource_tc_vpc_ipv6_eni_address_test.go +++ b/tencentcloud/resource_tc_vpc_ipv6_eni_address_test.go @@ -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", @@ -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" {