Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(dasb): [121226566] tencentcloud_dasb_bind_device_resource resource support read, update and delete #3022

Merged
merged 3 commits into from
Dec 18, 2024
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/3022.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/tencentcloud_dasb_bind_device_resource: support `read`, `update` and `delete`
```
144 changes: 143 additions & 1 deletion tencentcloud/services/bh/resource_tc_dasb_bind_device_resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bh

import (
"context"
"log"

tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common"
Expand All @@ -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.",
Expand All @@ -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.",
},
},
}
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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]
}
```
45 changes: 45 additions & 0 deletions tencentcloud/services/bh/service_tencentcloud_dasb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
17 changes: 14 additions & 3 deletions website/docs/r/dasb_bind_device_resource.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -20,12 +20,23 @@ resource "tencentcloud_dasb_bind_device_resource" "example" {
}
```

### 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]
}
```

## 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

Expand Down
Loading