|
| 1 | +/* |
| 2 | +Provides a resource to create a chdfs mount_point_attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_chdfs_mount_point_attachment" "mount_point_attachment" { |
| 8 | + access_group_ids = [ |
| 9 | + "ag-bvmzrbsm", |
| 10 | + "ag-lairqrgr", |
| 11 | + ] |
| 12 | + mount_point_id = "f14mpfy5lh4e-KuiL" |
| 13 | +} |
| 14 | +``` |
| 15 | +
|
| 16 | +Import |
| 17 | +
|
| 18 | +chdfs mount_point_attachment can be imported using the id, e.g. |
| 19 | +
|
| 20 | +``` |
| 21 | +terraform import tencentcloud_chdfs_mount_point_attachment.mount_point_attachment mount_point_id |
| 22 | +``` |
| 23 | +*/ |
| 24 | +package tencentcloud |
| 25 | + |
| 26 | +import ( |
| 27 | + "context" |
| 28 | + "log" |
| 29 | + |
| 30 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 31 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 32 | + chdfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs/v20201112" |
| 33 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 34 | +) |
| 35 | + |
| 36 | +func resourceTencentCloudChdfsMountPointAttachment() *schema.Resource { |
| 37 | + return &schema.Resource{ |
| 38 | + Create: resourceTencentCloudChdfsMountPointAttachmentCreate, |
| 39 | + Read: resourceTencentCloudChdfsMountPointAttachmentRead, |
| 40 | + Delete: resourceTencentCloudChdfsMountPointAttachmentDelete, |
| 41 | + Importer: &schema.ResourceImporter{ |
| 42 | + State: schema.ImportStatePassthrough, |
| 43 | + }, |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "mount_point_id": { |
| 46 | + Required: true, |
| 47 | + ForceNew: true, |
| 48 | + Type: schema.TypeString, |
| 49 | + Description: "associate mount point.", |
| 50 | + }, |
| 51 | + |
| 52 | + "access_group_ids": { |
| 53 | + Required: true, |
| 54 | + ForceNew: true, |
| 55 | + Type: schema.TypeSet, |
| 56 | + Elem: &schema.Schema{ |
| 57 | + Type: schema.TypeString, |
| 58 | + }, |
| 59 | + Description: "associate access group id.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func resourceTencentCloudChdfsMountPointAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 66 | + defer logElapsed("resource.tencentcloud_chdfs_mount_point_attachment.create")() |
| 67 | + defer inconsistentCheck(d, meta)() |
| 68 | + |
| 69 | + logId := getLogId(contextNil) |
| 70 | + |
| 71 | + var ( |
| 72 | + request = chdfs.NewAssociateAccessGroupsRequest() |
| 73 | + mountPointId string |
| 74 | + ) |
| 75 | + if v, ok := d.GetOk("mount_point_id"); ok { |
| 76 | + mountPointId = v.(string) |
| 77 | + request.MountPointId = helper.String(v.(string)) |
| 78 | + } |
| 79 | + |
| 80 | + if v, ok := d.GetOk("access_group_ids"); ok { |
| 81 | + accessGroupIdsSet := v.(*schema.Set).List() |
| 82 | + for i := range accessGroupIdsSet { |
| 83 | + accessGroupIds := accessGroupIdsSet[i].(string) |
| 84 | + request.AccessGroupIds = append(request.AccessGroupIds, &accessGroupIds) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 89 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseChdfsClient().AssociateAccessGroups(request) |
| 90 | + if e != nil { |
| 91 | + return retryError(e) |
| 92 | + } else { |
| 93 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 94 | + } |
| 95 | + return nil |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + log.Printf("[CRITAL]%s create chdfs mountPointAttachment failed, reason:%+v", logId, err) |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + d.SetId(mountPointId) |
| 103 | + |
| 104 | + return resourceTencentCloudChdfsMountPointAttachmentRead(d, meta) |
| 105 | +} |
| 106 | + |
| 107 | +func resourceTencentCloudChdfsMountPointAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 108 | + defer logElapsed("resource.tencentcloud_chdfs_mount_point_attachment.read")() |
| 109 | + defer inconsistentCheck(d, meta)() |
| 110 | + |
| 111 | + logId := getLogId(contextNil) |
| 112 | + |
| 113 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 114 | + |
| 115 | + service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 116 | + |
| 117 | + mountPointId := d.Id() |
| 118 | + |
| 119 | + mountPointAttachment, err := service.DescribeChdfsMountPointById(ctx, mountPointId) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + if mountPointAttachment == nil { |
| 125 | + d.SetId("") |
| 126 | + log.Printf("[WARN]%s resource `ChdfsMountPointAttachment` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 127 | + return nil |
| 128 | + } |
| 129 | + |
| 130 | + if mountPointAttachment.MountPointId != nil { |
| 131 | + _ = d.Set("mount_point_id", mountPointAttachment.MountPointId) |
| 132 | + } |
| 133 | + |
| 134 | + if mountPointAttachment.AccessGroupIds != nil { |
| 135 | + _ = d.Set("access_group_ids", mountPointAttachment.AccessGroupIds) |
| 136 | + } |
| 137 | + |
| 138 | + return nil |
| 139 | +} |
| 140 | + |
| 141 | +func resourceTencentCloudChdfsMountPointAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 142 | + defer logElapsed("resource.tencentcloud_chdfs_mount_point_attachment.delete")() |
| 143 | + defer inconsistentCheck(d, meta)() |
| 144 | + |
| 145 | + logId := getLogId(contextNil) |
| 146 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 147 | + |
| 148 | + service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 149 | + |
| 150 | + mountPointId := d.Id() |
| 151 | + |
| 152 | + mountPoint, err := service.DescribeChdfsMountPointById(ctx, mountPointId) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + if err := service.DeleteChdfsMountPointAttachmentById(ctx, mountPointId, mountPoint.AccessGroupIds); err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
0 commit comments