|
| 1 | +/* |
| 2 | +Provides a resource to create a chdfs mount_point |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_chdfs_mount_point" "mount_point" { |
| 8 | + file_system_id = "f14mpfy5lh4e" |
| 9 | + mount_point_name = "terraform-test" |
| 10 | + mount_point_status = 1 |
| 11 | +} |
| 12 | +``` |
| 13 | +
|
| 14 | +Import |
| 15 | +
|
| 16 | +chdfs mount_point can be imported using the id, e.g. |
| 17 | +
|
| 18 | +``` |
| 19 | +terraform import tencentcloud_chdfs_mount_point.mount_point mount_point_id |
| 20 | +``` |
| 21 | +*/ |
| 22 | +package tencentcloud |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "log" |
| 27 | + |
| 28 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 29 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 30 | + chdfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/chdfs/v20201112" |
| 31 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 32 | +) |
| 33 | + |
| 34 | +func resourceTencentCloudChdfsMountPoint() *schema.Resource { |
| 35 | + return &schema.Resource{ |
| 36 | + Create: resourceTencentCloudChdfsMountPointCreate, |
| 37 | + Read: resourceTencentCloudChdfsMountPointRead, |
| 38 | + Update: resourceTencentCloudChdfsMountPointUpdate, |
| 39 | + Delete: resourceTencentCloudChdfsMountPointDelete, |
| 40 | + Importer: &schema.ResourceImporter{ |
| 41 | + State: schema.ImportStatePassthrough, |
| 42 | + }, |
| 43 | + Schema: map[string]*schema.Schema{ |
| 44 | + "mount_point_name": { |
| 45 | + Required: true, |
| 46 | + Type: schema.TypeString, |
| 47 | + Description: "mount point name.", |
| 48 | + }, |
| 49 | + |
| 50 | + "file_system_id": { |
| 51 | + Required: true, |
| 52 | + ForceNew: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "file system id you want to mount.", |
| 55 | + }, |
| 56 | + |
| 57 | + "mount_point_status": { |
| 58 | + Required: true, |
| 59 | + Type: schema.TypeInt, |
| 60 | + Description: "mount status 1:open, 2:close.", |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceTencentCloudChdfsMountPointCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + defer logElapsed("resource.tencentcloud_chdfs_mount_point.create")() |
| 68 | + defer inconsistentCheck(d, meta)() |
| 69 | + |
| 70 | + logId := getLogId(contextNil) |
| 71 | + |
| 72 | + var ( |
| 73 | + request = chdfs.NewCreateMountPointRequest() |
| 74 | + response = chdfs.NewCreateMountPointResponse() |
| 75 | + mountPointId string |
| 76 | + ) |
| 77 | + if v, ok := d.GetOk("mount_point_name"); ok { |
| 78 | + request.MountPointName = helper.String(v.(string)) |
| 79 | + } |
| 80 | + |
| 81 | + if v, ok := d.GetOk("file_system_id"); ok { |
| 82 | + request.FileSystemId = helper.String(v.(string)) |
| 83 | + } |
| 84 | + |
| 85 | + if v, _ := d.GetOk("mount_point_status"); v != nil { |
| 86 | + request.MountPointStatus = helper.IntUint64(v.(int)) |
| 87 | + } |
| 88 | + |
| 89 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 90 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseChdfsClient().CreateMountPoint(request) |
| 91 | + if e != nil { |
| 92 | + return retryError(e) |
| 93 | + } else { |
| 94 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 95 | + } |
| 96 | + response = result |
| 97 | + return nil |
| 98 | + }) |
| 99 | + if err != nil { |
| 100 | + log.Printf("[CRITAL]%s create chdfs mountPoint failed, reason:%+v", logId, err) |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + mountPointId = *response.Response.MountPoint.MountPointId |
| 105 | + d.SetId(mountPointId) |
| 106 | + |
| 107 | + return resourceTencentCloudChdfsMountPointRead(d, meta) |
| 108 | +} |
| 109 | + |
| 110 | +func resourceTencentCloudChdfsMountPointRead(d *schema.ResourceData, meta interface{}) error { |
| 111 | + defer logElapsed("resource.tencentcloud_chdfs_mount_point.read")() |
| 112 | + defer inconsistentCheck(d, meta)() |
| 113 | + |
| 114 | + logId := getLogId(contextNil) |
| 115 | + |
| 116 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 117 | + |
| 118 | + service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 119 | + |
| 120 | + mountPointId := d.Id() |
| 121 | + |
| 122 | + mountPoint, err := service.DescribeChdfsMountPointById(ctx, mountPointId) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + |
| 127 | + if mountPoint == nil { |
| 128 | + d.SetId("") |
| 129 | + log.Printf("[WARN]%s resource `ChdfsMountPoint` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + if mountPoint.MountPointName != nil { |
| 134 | + _ = d.Set("mount_point_name", mountPoint.MountPointName) |
| 135 | + } |
| 136 | + |
| 137 | + if mountPoint.FileSystemId != nil { |
| 138 | + _ = d.Set("file_system_id", mountPoint.FileSystemId) |
| 139 | + } |
| 140 | + |
| 141 | + if mountPoint.Status != nil { |
| 142 | + _ = d.Set("mount_point_status", mountPoint.Status) |
| 143 | + } |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func resourceTencentCloudChdfsMountPointUpdate(d *schema.ResourceData, meta interface{}) error { |
| 149 | + defer logElapsed("resource.tencentcloud_chdfs_mount_point.update")() |
| 150 | + defer inconsistentCheck(d, meta)() |
| 151 | + |
| 152 | + logId := getLogId(contextNil) |
| 153 | + |
| 154 | + request := chdfs.NewModifyMountPointRequest() |
| 155 | + |
| 156 | + mountPointId := d.Id() |
| 157 | + |
| 158 | + request.MountPointId = &mountPointId |
| 159 | + |
| 160 | + if d.HasChange("mount_point_name") { |
| 161 | + if v, ok := d.GetOk("mount_point_name"); ok { |
| 162 | + request.MountPointName = helper.String(v.(string)) |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + if d.HasChange("mount_point_status") { |
| 167 | + if v, _ := d.GetOk("mount_point_status"); v != nil { |
| 168 | + request.MountPointStatus = helper.IntUint64(v.(int)) |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 173 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseChdfsClient().ModifyMountPoint(request) |
| 174 | + if e != nil { |
| 175 | + return retryError(e) |
| 176 | + } else { |
| 177 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 178 | + } |
| 179 | + return nil |
| 180 | + }) |
| 181 | + if err != nil { |
| 182 | + log.Printf("[CRITAL]%s update chdfs mountPoint failed, reason:%+v", logId, err) |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + return resourceTencentCloudChdfsMountPointRead(d, meta) |
| 187 | +} |
| 188 | + |
| 189 | +func resourceTencentCloudChdfsMountPointDelete(d *schema.ResourceData, meta interface{}) error { |
| 190 | + defer logElapsed("resource.tencentcloud_chdfs_mount_point.delete")() |
| 191 | + defer inconsistentCheck(d, meta)() |
| 192 | + |
| 193 | + logId := getLogId(contextNil) |
| 194 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 195 | + |
| 196 | + service := ChdfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 197 | + mountPointId := d.Id() |
| 198 | + |
| 199 | + if err := service.DeleteChdfsMountPointById(ctx, mountPointId); err != nil { |
| 200 | + return err |
| 201 | + } |
| 202 | + |
| 203 | + return nil |
| 204 | +} |
0 commit comments