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
3 changes: 3 additions & 0 deletions .changelog/2901.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_ccn_attachment: fix attachment with ccn_uin
```
46 changes: 5 additions & 41 deletions tencentcloud/services/ccn/resource_tc_ccn_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down
57 changes: 55 additions & 2 deletions tencentcloud/services/ccn/resource_tc_ccn_attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions tencentcloud/services/ccn/service_tencentcloud_ccn.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (info CcnBasicInfo) CreateTime() string {
}

type CcnAttachedInstanceInfo struct {
ccnUin string
ccnId string
instanceType string
instanceRegion string
Expand Down Expand Up @@ -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
Expand Down
Loading