Skip to content

Commit 63887e3

Browse files
authored
Merge pull request #1674 from tencentcloudstack/feat/ccn_attachment_support_update
ccn attachment support des
2 parents 7cf50ef + c443488 commit 63887e3

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

.changelog/1674.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: support set `description`
3+
```

tencentcloud/resource_tc_ccn_attachment.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,12 @@ import (
5151
"context"
5252
"crypto/md5"
5353
"fmt"
54+
"log"
5455
"strings"
5556

57+
vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312"
58+
"github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/ratelimit"
59+
5660
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
5761
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5862
)
@@ -61,6 +65,7 @@ func resourceTencentCloudCcnAttachment() *schema.Resource {
6165
return &schema.Resource{
6266
Create: resourceTencentCloudCcnAttachmentCreate,
6367
Read: resourceTencentCloudCcnAttachmentRead,
68+
Update: resourceTencentCloudCcnAttachmentUpdate,
6469
Delete: resourceTencentCloudCcnAttachmentDelete,
6570

6671
Schema: map[string]*schema.Schema{
@@ -89,6 +94,11 @@ func resourceTencentCloudCcnAttachment() *schema.Resource {
8994
ForceNew: true,
9095
Description: "ID of instance is attached.",
9196
},
97+
"description": {
98+
Type: schema.TypeString,
99+
Optional: true,
100+
Description: "Remark of attachment.",
101+
},
92102
"ccn_uin": {
93103
Type: schema.TypeString,
94104
Optional: true,
@@ -132,13 +142,18 @@ func resourceTencentCloudCcnAttachmentCreate(d *schema.ResourceData, meta interf
132142
instanceType = d.Get("instance_type").(string)
133143
instanceRegion = d.Get("instance_region").(string)
134144
instanceId = d.Get("instance_id").(string)
145+
description = ""
135146
ccnUin = ""
136147
)
137148

138149
if len(ccnId) < 4 || len(instanceRegion) < 3 || len(instanceId) < 3 {
139150
return fmt.Errorf("param ccn_id or instance_region or instance_id error")
140151
}
141152

153+
if v, ok := d.GetOk("description"); ok {
154+
description = v.(string)
155+
}
156+
142157
if v, ok := d.GetOk("ccn_uin"); ok {
143158
ccnUin = v.(string)
144159
if ccnUin != "" && instanceType != CNN_INSTANCE_TYPE_VPC {
@@ -154,7 +169,7 @@ func resourceTencentCloudCcnAttachmentCreate(d *schema.ResourceData, meta interf
154169
}
155170
}
156171

157-
if err := service.AttachCcnInstances(ctx, ccnId, instanceRegion, instanceType, instanceId, ccnUin); err != nil {
172+
if err := service.AttachCcnInstances(ctx, ccnId, instanceRegion, instanceType, instanceId, ccnUin, description); err != nil {
158173
return err
159174
}
160175

@@ -253,6 +268,7 @@ func resourceTencentCloudCcnAttachmentRead(d *schema.ResourceData, meta interfac
253268
return nil
254269
}
255270

271+
_ = d.Set("description", info.description)
256272
_ = d.Set("state", strings.ToUpper(info.state))
257273
_ = d.Set("attached_time", info.attachedTime)
258274
_ = d.Set("cidr_block", info.cidrBlock)
@@ -264,6 +280,49 @@ func resourceTencentCloudCcnAttachmentRead(d *schema.ResourceData, meta interfac
264280
return nil
265281
}
266282

283+
func resourceTencentCloudCcnAttachmentUpdate(d *schema.ResourceData, meta interface{}) error {
284+
defer logElapsed("resource.tencentcloud_ccn_attachment.create")()
285+
logId := getLogId(contextNil)
286+
287+
if d.HasChange("description") {
288+
var (
289+
ccnId = d.Get("ccn_id").(string)
290+
instanceType = d.Get("instance_type").(string)
291+
instanceRegion = d.Get("instance_region").(string)
292+
instanceId = d.Get("instance_id").(string)
293+
description = d.Get("description").(string)
294+
)
295+
296+
request := vpc.NewModifyCcnAttachedInstancesAttributeRequest()
297+
request.CcnId = &ccnId
298+
299+
var ccnInstance vpc.CcnInstance
300+
ccnInstance.InstanceId = &instanceId
301+
ccnInstance.InstanceRegion = &instanceRegion
302+
ccnInstance.InstanceType = &instanceType
303+
ccnInstance.Description = &description
304+
305+
request.Instances = []*vpc.CcnInstance{&ccnInstance}
306+
307+
err := resource.Retry(writeRetryTimeout, func() *resource.RetryError {
308+
ratelimit.Check(request.GetAction())
309+
response, err := meta.(*TencentCloudClient).apiV3Conn.UseVpcClient().ModifyCcnAttachedInstancesAttribute(request)
310+
if err != nil {
311+
log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n",
312+
logId, request.GetAction(), request.ToJsonString(), err.Error())
313+
return retryError(err)
314+
}
315+
log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n",
316+
logId, request.GetAction(), request.ToJsonString(), response.ToJsonString())
317+
return nil
318+
})
319+
if err != nil {
320+
return err
321+
}
322+
}
323+
return resourceTencentCloudCcnAttachmentRead(d, meta)
324+
}
325+
267326
func resourceTencentCloudCcnAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
268327
defer logElapsed("resource.tencentcloud_ccn_attachment.delete")()
269328

tencentcloud/service_tencentcloud_ccn.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type CcnAttachedInstanceInfo struct {
3232
state string
3333
attachedTime string
3434
cidrBlock []string
35+
description string
3536
}
3637

3738
type CcnBandwidthLimit struct {
@@ -398,6 +399,7 @@ func (me *VpcService) DescribeCcnAttachedInstances(ctx context.Context, ccnId st
398399
info.instanceRegion = *item.InstanceRegion
399400
info.instanceType = *item.InstanceType
400401
info.state = *item.State
402+
info.description = *item.Description
401403
infos = append(infos, info)
402404
}
403405
return
@@ -448,7 +450,7 @@ func (me *VpcService) DescribeCcnAttachmentsByInstance(ctx context.Context, inst
448450
return
449451
}
450452

451-
func (me *VpcService) AttachCcnInstances(ctx context.Context, ccnId, instanceRegion, instanceType, instanceId string, ccnUin string) (errRet error) {
453+
func (me *VpcService) AttachCcnInstances(ctx context.Context, ccnId, instanceRegion, instanceType, instanceId string, ccnUin string, description string) (errRet error) {
452454

453455
logId := getLogId(ctx)
454456
request := vpc.NewAttachCcnInstancesRequest()
@@ -457,10 +459,14 @@ func (me *VpcService) AttachCcnInstances(ctx context.Context, ccnId, instanceReg
457459
if ccnUin != "" {
458460
request.CcnUin = &ccnUin
459461
}
462+
460463
var ccnInstance vpc.CcnInstance
461464
ccnInstance.InstanceId = &instanceId
462465
ccnInstance.InstanceRegion = &instanceRegion
463466
ccnInstance.InstanceType = &instanceType
467+
if description != "" {
468+
ccnInstance.Description = &description
469+
}
464470

465471
request.Instances = []*vpc.CcnInstance{&ccnInstance}
466472
ratelimit.Check(request.GetAction())

website/docs/r/ccn_attachment.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The following arguments are supported:
6464
* `instance_region` - (Required, String, ForceNew) The region that the instance locates at.
6565
* `instance_type` - (Required, String, ForceNew) Type of attached instance network, and available values include `VPC`, `DIRECTCONNECT`, `BMVPC` and `VPNGW`. Note: `VPNGW` type is only for whitelist customer now.
6666
* `ccn_uin` - (Optional, String, ForceNew) Uin of the ccn attached. Default is ``, which means the uin of this account. This parameter is used with case when attaching ccn of other account to the instance of this account. For now only support instance type `VPC`.
67+
* `description` - (Optional, String) Remark of attachment.
6768

6869
## Attributes Reference
6970

0 commit comments

Comments
 (0)