From e30bec06b954139f774f774bd8f719fec0a8d701 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 17 Dec 2024 15:39:07 +0800 Subject: [PATCH 1/3] add --- .../resource_tc_dasb_bind_device_resource.go | 144 +++++++++++++++++- .../resource_tc_dasb_bind_device_resource.md | 12 +- .../services/bh/service_tencentcloud_dasb.go | 45 ++++++ .../r/dasb_bind_device_resource.html.markdown | 17 ++- 4 files changed, 213 insertions(+), 5 deletions(-) diff --git a/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.go b/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.go index 911f9aa145..0afe1f5411 100644 --- a/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.go +++ b/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.go @@ -1,6 +1,7 @@ package bh import ( + "context" "log" tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" @@ -16,12 +17,12 @@ func ResourceTencentCloudDasbBindDeviceResource() *schema.Resource { return &schema.Resource{ Create: resourceTencentCloudDasbBindDeviceResourceCreate, Read: resourceTencentCloudDasbBindDeviceResourceRead, + Update: resourceTencentCloudDasbBindDeviceResourceUpdate, Delete: resourceTencentCloudDasbBindDeviceResourceDelete, Schema: map[string]*schema.Schema{ "device_id_set": { Required: true, - ForceNew: true, Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeInt}, Description: "Asset ID collection.", @@ -32,6 +33,13 @@ func ResourceTencentCloudDasbBindDeviceResource() *schema.Resource { Type: schema.TypeString, Description: "Bastion host service ID.", }, + "domain_id": { + Optional: true, + Computed: true, + ForceNew: true, + Type: schema.TypeString, + Description: "Network Domain ID.", + }, }, } } @@ -59,6 +67,10 @@ func resourceTencentCloudDasbBindDeviceResourceCreate(d *schema.ResourceData, me resourceId = v.(string) } + if v, ok := d.GetOk("domain_id"); ok { + request.DomainId = helper.String(v.(string)) + } + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseDasbClient().BindDeviceResource(request) if e != nil { @@ -83,12 +95,142 @@ func resourceTencentCloudDasbBindDeviceResourceRead(d *schema.ResourceData, meta defer tccommon.LogElapsed("resource.tencentcloud_dasb_bind_device_resource.read")() defer tccommon.InconsistentCheck(d, meta)() + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) + service = DasbService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} + resourceId = d.Id() + ) + + deviceSets, err := service.DescribeDasbDeviceByResourceId(ctx, resourceId) + if err != nil { + return err + } + + if deviceSets == nil { + d.SetId("") + log.Printf("[WARN]%s resource `DeviceResource` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) + return nil + } + + _ = d.Set("resource_id", resourceId) + + tmpList := make([]interface{}, 0, len(deviceSets)) + for _, item := range deviceSets { + if item.Id != nil { + tmpList = append(tmpList, item.Id) + } + + if item.DomainId != nil { + _ = d.Set("domain_id", item.DomainId) + } + } + + _ = d.Set("device_id_set", tmpList) + return nil } +func resourceTencentCloudDasbBindDeviceResourceUpdate(d *schema.ResourceData, meta interface{}) error { + defer tccommon.LogElapsed("resource.tencentcloud_dasb_bind_device_resource.update")() + defer tccommon.InconsistentCheck(d, meta)() + + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + resourceId = d.Id() + ) + + if d.HasChange("device_id_set") { + oldInterface, newInterface := d.GetChange("device_id_set") + olds := oldInterface.(*schema.Set) + news := newInterface.(*schema.Set) + remove := helper.InterfacesIntegers(olds.Difference(news).List()) + add := helper.InterfacesIntegers(news.Difference(olds).List()) + if len(remove) > 0 { + request := dasb.NewBindDeviceResourceRequest() + for _, item := range remove { + request.DeviceIdSet = append(request.DeviceIdSet, helper.IntUint64(item)) + } + + request.ResourceId = helper.String("") + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseDasbClient().BindDeviceResource(request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + return nil + }) + + if err != nil { + log.Printf("[CRITAL]%s operate dasb bindDeviceResource failed, reason:%+v", logId, err) + return err + } + } + + if len(add) > 0 { + request := dasb.NewBindDeviceResourceRequest() + for _, item := range add { + request.DeviceIdSet = append(request.DeviceIdSet, helper.IntUint64(item)) + } + + request.ResourceId = helper.String(resourceId) + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseDasbClient().BindDeviceResource(request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + return nil + }) + + if err != nil { + log.Printf("[CRITAL]%s operate dasb bindDeviceResource failed, reason:%+v", logId, err) + return err + } + } + } + + return resourceTencentCloudDasbBindDeviceResourceRead(d, meta) +} + func resourceTencentCloudDasbBindDeviceResourceDelete(d *schema.ResourceData, meta interface{}) error { defer tccommon.LogElapsed("resource.tencentcloud_dasb_bind_device_resource.delete")() defer tccommon.InconsistentCheck(d, meta)() + var ( + logId = tccommon.GetLogId(tccommon.ContextNil) + request = dasb.NewBindDeviceResourceRequest() + ) + + if v, ok := d.GetOk("device_id_set"); ok { + deviceIdSetSet := v.(*schema.Set).List() + for i := range deviceIdSetSet { + deviceIdSet := deviceIdSetSet[i].(int) + request.DeviceIdSet = append(request.DeviceIdSet, helper.IntUint64(deviceIdSet)) + } + } + + request.ResourceId = helper.String("") + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseDasbClient().BindDeviceResource(request) + if e != nil { + return tccommon.RetryError(e) + } else { + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) + } + + return nil + }) + + if err != nil { + log.Printf("[CRITAL]%s operate dasb bindDeviceResource failed, reason:%+v", logId, err) + return err + } + return nil } diff --git a/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.md b/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.md index 9ade33f175..98e77027da 100644 --- a/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.md +++ b/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.md @@ -1,4 +1,4 @@ -Provides a resource to create a dasb bind_device_resource +Provides a resource to create a dasb bind device resource Example Usage @@ -7,4 +7,14 @@ resource "tencentcloud_dasb_bind_device_resource" "example" { resource_id = "bh-saas-weyosfym" device_id_set = [17, 18] } +``` + +Or custom `domain_id` parameters + +```hcl +resource "tencentcloud_dasb_bind_device_resource" "example" { + resource_id = "bh-saas-lx1pxhli" + domain_id = "net-31nssj3n" + device_id_set = [115, 116] +} ``` \ No newline at end of file diff --git a/tencentcloud/services/bh/service_tencentcloud_dasb.go b/tencentcloud/services/bh/service_tencentcloud_dasb.go index 04e5979426..c0a6bcbe81 100644 --- a/tencentcloud/services/bh/service_tencentcloud_dasb.go +++ b/tencentcloud/services/bh/service_tencentcloud_dasb.go @@ -505,6 +505,51 @@ func (me *DasbService) DescribeDasbDeviceById(ctx context.Context, deviceId stri return } +func (me *DasbService) DescribeDasbDeviceByResourceId(ctx context.Context, resourceId string) (deviceSet []*dasb.Device, errRet error) { + logId := tccommon.GetLogId(ctx) + + request := dasb.NewDescribeDevicesRequest() + request.ResourceIdSet = common.StringPtrs([]string{resourceId}) + + defer func() { + if errRet != nil { + log.Printf("[CRITAL]%s api[%s] fail, request body [%s], reason[%s]\n", logId, request.GetAction(), request.ToJsonString(), errRet.Error()) + } + }() + + ratelimit.Check(request.GetAction()) + + var ( + offset uint64 = 0 + limit uint64 = 100 + ) + + for { + request.Offset = &offset + request.Limit = &limit + response, err := me.client.UseDasbClient().DescribeDevices(request) + if err != nil { + errRet = err + return + } + + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), response.ToJsonString()) + + if response == nil || len(response.Response.DeviceSet) < 1 { + break + } + + deviceSet = append(deviceSet, response.Response.DeviceSet...) + if len(response.Response.DeviceSet) < int(limit) { + break + } + + offset += limit + } + + return +} + func (me *DasbService) DeleteDasbDeviceById(ctx context.Context, deviceId string) (errRet error) { logId := tccommon.GetLogId(ctx) diff --git a/website/docs/r/dasb_bind_device_resource.html.markdown b/website/docs/r/dasb_bind_device_resource.html.markdown index 850b59a2c2..814aaed982 100644 --- a/website/docs/r/dasb_bind_device_resource.html.markdown +++ b/website/docs/r/dasb_bind_device_resource.html.markdown @@ -4,12 +4,12 @@ layout: "tencentcloud" page_title: "TencentCloud: tencentcloud_dasb_bind_device_resource" sidebar_current: "docs-tencentcloud-resource-dasb_bind_device_resource" description: |- - Provides a resource to create a dasb bind_device_resource + Provides a resource to create a dasb bind device resource --- # tencentcloud_dasb_bind_device_resource -Provides a resource to create a dasb bind_device_resource +Provides a resource to create a dasb bind device resource ## Example Usage @@ -20,12 +20,23 @@ resource "tencentcloud_dasb_bind_device_resource" "example" { } ``` +### parameters + +```hcl +resource "tencentcloud_dasb_bind_device_resource" "example" { + resource_id = "bh-saas-lx1pxhli" + domain_id = "net-31nssj3n" + device_id_set = [115, 116] +} +``` + ## Argument Reference The following arguments are supported: -* `device_id_set` - (Required, Set: [`Int`], ForceNew) Asset ID collection. +* `device_id_set` - (Required, Set: [`Int`]) Asset ID collection. * `resource_id` - (Required, String, ForceNew) Bastion host service ID. +* `domain_id` - (Optional, String, ForceNew) Network Domain ID. ## Attributes Reference From 97091f7302daa603615191fa4c429c9605aeca3d Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 17 Dec 2024 15:41:15 +0800 Subject: [PATCH 2/3] add --- .changelog/3022.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/3022.txt diff --git a/.changelog/3022.txt b/.changelog/3022.txt new file mode 100644 index 0000000000..1d93b437d4 --- /dev/null +++ b/.changelog/3022.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/tencentcloud_dasb_bind_device_resource: support `read`, `update` and `delete` +``` \ No newline at end of file From 380e5448c7bf6fa5a207f39bedc1b740584e93d1 Mon Sep 17 00:00:00 2001 From: SevenEarth <391613297@qq.com> Date: Tue, 17 Dec 2024 15:42:24 +0800 Subject: [PATCH 3/3] add --- .../services/bh/resource_tc_dasb_bind_device_resource.md | 2 +- website/docs/r/dasb_bind_device_resource.html.markdown | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.md b/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.md index 98e77027da..7a7ebceff0 100644 --- a/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.md +++ b/tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.md @@ -9,7 +9,7 @@ resource "tencentcloud_dasb_bind_device_resource" "example" { } ``` -Or custom `domain_id` parameters +Or custom domain_id parameters ```hcl resource "tencentcloud_dasb_bind_device_resource" "example" { diff --git a/website/docs/r/dasb_bind_device_resource.html.markdown b/website/docs/r/dasb_bind_device_resource.html.markdown index 814aaed982..7b5f35f654 100644 --- a/website/docs/r/dasb_bind_device_resource.html.markdown +++ b/website/docs/r/dasb_bind_device_resource.html.markdown @@ -20,7 +20,7 @@ resource "tencentcloud_dasb_bind_device_resource" "example" { } ``` -### parameters +### Or custom domain_id parameters ```hcl resource "tencentcloud_dasb_bind_device_resource" "example" {