diff --git a/.changelog/2901.txt b/.changelog/2901.txt new file mode 100644 index 0000000000..a72a17420a --- /dev/null +++ b/.changelog/2901.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_ccn_attachment: fix attachment with ccn_uin +``` \ No newline at end of file diff --git a/tencentcloud/services/ccn/resource_tc_ccn_attachment.go b/tencentcloud/services/ccn/resource_tc_ccn_attachment.go index 32b90d2979..d8fce3feed 100644 --- a/tencentcloud/services/ccn/resource_tc_ccn_attachment.go +++ b/tencentcloud/services/ccn/resource_tc_ccn_attachment.go @@ -169,47 +169,6 @@ func resourceTencentCloudCcnAttachmentRead(d *schema.ResourceData, meta interfac service = VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} ) - if v, ok := d.GetOk("ccn_uin"); ok { - ccnUin := v.(string) - ccnId := d.Get("ccn_id").(string) - instanceType := d.Get("instance_type").(string) - instanceRegion := d.Get("instance_region").(string) - instanceId := d.Get("instance_id").(string) - - err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { - infos, e := service.DescribeCcnAttachmentsByInstance(ctx, instanceType, instanceId, instanceRegion) - if e != nil { - return tccommon.RetryError(e) - } - - if len(infos) == 0 { - d.SetId("") - return nil - } - - findFlag := false - for _, info := range infos { - if *info.CcnUin == ccnUin && *info.CcnId == ccnId { - _ = d.Set("state", strings.ToUpper(*info.State)) - _ = d.Set("attached_time", info.AttachedTime) - _ = d.Set("cidr_block", info.CidrBlock) - findFlag = true - break - } - } - if !findFlag { - d.SetId("") - return nil - } - return nil - }) - - if err != nil { - return err - } - return nil - } - var ( ccnId = d.Get("ccn_id").(string) instanceType = d.Get("instance_type").(string) @@ -252,6 +211,11 @@ func resourceTencentCloudCcnAttachmentRead(d *schema.ResourceData, meta interfac return nil } + if v, ok := d.GetOk("ccn_uin"); ok && v.(string) != info.ccnUin { + d.SetId("") + return nil + } + _ = d.Set("description", info.description) _ = d.Set("route_table_id", info.routeTableId) _ = d.Set("state", strings.ToUpper(info.state)) diff --git a/tencentcloud/services/ccn/resource_tc_ccn_attachment_test.go b/tencentcloud/services/ccn/resource_tc_ccn_attachment_test.go index 677c210687..1030b3b3e5 100644 --- a/tencentcloud/services/ccn/resource_tc_ccn_attachment_test.go +++ b/tencentcloud/services/ccn/resource_tc_ccn_attachment_test.go @@ -14,8 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) -func TestAccTencentCloudCcnAttachmentResource(t *testing.T) { - t.Parallel() +func TestAccTencentCloudCcnAttachmentResource_basic(t *testing.T) { keyName := "tencentcloud_ccn_attachment.attachment" keyNameVpngw := "tencentcloud_ccn_attachment.vpngw_ccn_attachment" resource.Test(t, resource.TestCase{ @@ -54,6 +53,31 @@ func TestAccTencentCloudCcnAttachmentResource(t *testing.T) { }) } +func TestAccTencentCloudCcnAttachmentResource_withCcnUin(t *testing.T) { + keyName := "tencentcloud_ccn_attachment.attachment_ccnuin" + resource.Test(t, resource.TestCase{ + PreCheck: func() { tcacctest.AccPreCheck(t) }, + Providers: tcacctest.AccProviders, + CheckDestroy: testAccCheckCcnAttachmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCcnAttachmentConfigWithCcnUin, + Check: resource.ComposeTestCheckFunc( + testAccCheckCcnAttachmentExists(keyName), + resource.TestCheckResourceAttrSet(keyName, "ccn_id"), + resource.TestCheckResourceAttrSet(keyName, "instance_type"), + resource.TestCheckResourceAttrSet(keyName, "instance_region"), + resource.TestCheckResourceAttrSet(keyName, "instance_id"), + resource.TestCheckResourceAttrSet(keyName, "state"), + resource.TestCheckResourceAttrSet(keyName, "attached_time"), + resource.TestCheckResourceAttrSet(keyName, "cidr_block.#"), + resource.TestCheckResourceAttrSet(keyName, "route_ids.#"), + ), + }, + }, + }) +} + func testAccCheckCcnAttachmentExists(r string) resource.TestCheckFunc { return func(s *terraform.State) error { logId := tccommon.GetLogId(tccommon.ContextNil) @@ -136,6 +160,35 @@ resource tencentcloud_ccn_attachment attachment { } ` +const testAccCcnAttachmentConfigWithCcnUin = ` +variable "region" { + default = "ap-guangzhou" +} + +resource tencentcloud_vpc vpc { + name = "ci-temp-test-vpc" + cidr_block = "10.0.0.0/16" + dns_servers = ["119.29.29.29", "8.8.8.8"] + is_multicast = false +} + +resource tencentcloud_ccn main { + name = "ci-temp-test-ccn" + description = "ci-temp-test-ccn-des" + qos = "AG" + charge_type = "PREPAID" + bandwidth_limit_type = "INTER_REGION_LIMIT" +} + +resource tencentcloud_ccn_attachment attachment_ccnuin { + ccn_id = tencentcloud_ccn.main.id + ccn_uin = "100022770164" + instance_type = "VPC" + instance_id = tencentcloud_vpc.vpc.id + instance_region = var.region +} +` + const testAccCcnAttachmentVpngwConfig = ` variable "region" { default = "ap-guangzhou" diff --git a/tencentcloud/services/ccn/service_tencentcloud_ccn.go b/tencentcloud/services/ccn/service_tencentcloud_ccn.go index 7573dd769b..3d983c8d22 100644 --- a/tencentcloud/services/ccn/service_tencentcloud_ccn.go +++ b/tencentcloud/services/ccn/service_tencentcloud_ccn.go @@ -76,6 +76,7 @@ func (info CcnBasicInfo) CreateTime() string { } type CcnAttachedInstanceInfo struct { + ccnUin string ccnId string instanceType string instanceRegion string @@ -506,6 +507,7 @@ func (me *VpcService) DescribeCcnAttachedInstances(ctx context.Context, ccnId st info.state = *item.State info.description = *item.Description info.routeTableId = *item.RouteTableId + info.ccnUin = *item.CcnUin infos = append(infos, info) } return