Skip to content

Commit 06ede75

Browse files
tongyimingmikatong
and
mikatong
authored
fix(ccn): [120225384] ccn attachment with ccnuin (#2901)
* fix ccn attachment with ccnuin * add changelog --------- Co-authored-by: mikatong <[email protected]>
1 parent 0ea9330 commit 06ede75

File tree

4 files changed

+65
-43
lines changed

4 files changed

+65
-43
lines changed

.changelog/2901.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
resource/tencentcloud_ccn_attachment: fix attachment with ccn_uin
3+
```

tencentcloud/services/ccn/resource_tc_ccn_attachment.go

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -169,47 +169,6 @@ func resourceTencentCloudCcnAttachmentRead(d *schema.ResourceData, meta interfac
169169
service = VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()}
170170
)
171171

172-
if v, ok := d.GetOk("ccn_uin"); ok {
173-
ccnUin := v.(string)
174-
ccnId := d.Get("ccn_id").(string)
175-
instanceType := d.Get("instance_type").(string)
176-
instanceRegion := d.Get("instance_region").(string)
177-
instanceId := d.Get("instance_id").(string)
178-
179-
err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError {
180-
infos, e := service.DescribeCcnAttachmentsByInstance(ctx, instanceType, instanceId, instanceRegion)
181-
if e != nil {
182-
return tccommon.RetryError(e)
183-
}
184-
185-
if len(infos) == 0 {
186-
d.SetId("")
187-
return nil
188-
}
189-
190-
findFlag := false
191-
for _, info := range infos {
192-
if *info.CcnUin == ccnUin && *info.CcnId == ccnId {
193-
_ = d.Set("state", strings.ToUpper(*info.State))
194-
_ = d.Set("attached_time", info.AttachedTime)
195-
_ = d.Set("cidr_block", info.CidrBlock)
196-
findFlag = true
197-
break
198-
}
199-
}
200-
if !findFlag {
201-
d.SetId("")
202-
return nil
203-
}
204-
return nil
205-
})
206-
207-
if err != nil {
208-
return err
209-
}
210-
return nil
211-
}
212-
213172
var (
214173
ccnId = d.Get("ccn_id").(string)
215174
instanceType = d.Get("instance_type").(string)
@@ -252,6 +211,11 @@ func resourceTencentCloudCcnAttachmentRead(d *schema.ResourceData, meta interfac
252211
return nil
253212
}
254213

214+
if v, ok := d.GetOk("ccn_uin"); ok && v.(string) != info.ccnUin {
215+
d.SetId("")
216+
return nil
217+
}
218+
255219
_ = d.Set("description", info.description)
256220
_ = d.Set("route_table_id", info.routeTableId)
257221
_ = d.Set("state", strings.ToUpper(info.state))

tencentcloud/services/ccn/resource_tc_ccn_attachment_test.go

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1515
)
1616

17-
func TestAccTencentCloudCcnAttachmentResource(t *testing.T) {
18-
t.Parallel()
17+
func TestAccTencentCloudCcnAttachmentResource_basic(t *testing.T) {
1918
keyName := "tencentcloud_ccn_attachment.attachment"
2019
keyNameVpngw := "tencentcloud_ccn_attachment.vpngw_ccn_attachment"
2120
resource.Test(t, resource.TestCase{
@@ -54,6 +53,31 @@ func TestAccTencentCloudCcnAttachmentResource(t *testing.T) {
5453
})
5554
}
5655

56+
func TestAccTencentCloudCcnAttachmentResource_withCcnUin(t *testing.T) {
57+
keyName := "tencentcloud_ccn_attachment.attachment_ccnuin"
58+
resource.Test(t, resource.TestCase{
59+
PreCheck: func() { tcacctest.AccPreCheck(t) },
60+
Providers: tcacctest.AccProviders,
61+
CheckDestroy: testAccCheckCcnAttachmentDestroy,
62+
Steps: []resource.TestStep{
63+
{
64+
Config: testAccCcnAttachmentConfigWithCcnUin,
65+
Check: resource.ComposeTestCheckFunc(
66+
testAccCheckCcnAttachmentExists(keyName),
67+
resource.TestCheckResourceAttrSet(keyName, "ccn_id"),
68+
resource.TestCheckResourceAttrSet(keyName, "instance_type"),
69+
resource.TestCheckResourceAttrSet(keyName, "instance_region"),
70+
resource.TestCheckResourceAttrSet(keyName, "instance_id"),
71+
resource.TestCheckResourceAttrSet(keyName, "state"),
72+
resource.TestCheckResourceAttrSet(keyName, "attached_time"),
73+
resource.TestCheckResourceAttrSet(keyName, "cidr_block.#"),
74+
resource.TestCheckResourceAttrSet(keyName, "route_ids.#"),
75+
),
76+
},
77+
},
78+
})
79+
}
80+
5781
func testAccCheckCcnAttachmentExists(r string) resource.TestCheckFunc {
5882
return func(s *terraform.State) error {
5983
logId := tccommon.GetLogId(tccommon.ContextNil)
@@ -136,6 +160,35 @@ resource tencentcloud_ccn_attachment attachment {
136160
}
137161
`
138162

163+
const testAccCcnAttachmentConfigWithCcnUin = `
164+
variable "region" {
165+
default = "ap-guangzhou"
166+
}
167+
168+
resource tencentcloud_vpc vpc {
169+
name = "ci-temp-test-vpc"
170+
cidr_block = "10.0.0.0/16"
171+
dns_servers = ["119.29.29.29", "8.8.8.8"]
172+
is_multicast = false
173+
}
174+
175+
resource tencentcloud_ccn main {
176+
name = "ci-temp-test-ccn"
177+
description = "ci-temp-test-ccn-des"
178+
qos = "AG"
179+
charge_type = "PREPAID"
180+
bandwidth_limit_type = "INTER_REGION_LIMIT"
181+
}
182+
183+
resource tencentcloud_ccn_attachment attachment_ccnuin {
184+
ccn_id = tencentcloud_ccn.main.id
185+
ccn_uin = "100022770164"
186+
instance_type = "VPC"
187+
instance_id = tencentcloud_vpc.vpc.id
188+
instance_region = var.region
189+
}
190+
`
191+
139192
const testAccCcnAttachmentVpngwConfig = `
140193
variable "region" {
141194
default = "ap-guangzhou"

tencentcloud/services/ccn/service_tencentcloud_ccn.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func (info CcnBasicInfo) CreateTime() string {
7676
}
7777

7878
type CcnAttachedInstanceInfo struct {
79+
ccnUin string
7980
ccnId string
8081
instanceType string
8182
instanceRegion string
@@ -506,6 +507,7 @@ func (me *VpcService) DescribeCcnAttachedInstances(ctx context.Context, ccnId st
506507
info.state = *item.State
507508
info.description = *item.Description
508509
info.routeTableId = *item.RouteTableId
510+
info.ccnUin = *item.CcnUin
509511
infos = append(infos, info)
510512
}
511513
return

0 commit comments

Comments
 (0)