|
| 1 | +/* |
| 2 | +Provides a resource to create a cfs auto_snapshot_policy_attachment |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_cfs_auto_snapshot_policy_attachment" "auto_snapshot_policy_attachment" { |
| 8 | + auto_snapshot_policy_id = "asp-basic" |
| 9 | + file_system_ids = "cfs-4xzkct19,cfs-iobiaxtj" |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +Import |
| 14 | +
|
| 15 | +cfs auto_snapshot_policy_attachment can be imported using the id, e.g. |
| 16 | +
|
| 17 | +``` |
| 18 | +terraform import tencentcloud_cfs_auto_snapshot_policy_attachment.auto_snapshot_policy_attachment auto_snapshot_policy_id#file_system_ids |
| 19 | +``` |
| 20 | +*/ |
| 21 | +package tencentcloud |
| 22 | + |
| 23 | +import ( |
| 24 | + "context" |
| 25 | + "fmt" |
| 26 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 27 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 28 | + cfs "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cfs/v20190719" |
| 29 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 30 | + "log" |
| 31 | + "strings" |
| 32 | +) |
| 33 | + |
| 34 | +func resourceTencentCloudCfsAutoSnapshotPolicyAttachment() *schema.Resource { |
| 35 | + return &schema.Resource{ |
| 36 | + Create: resourceTencentCloudCfsAutoSnapshotPolicyAttachmentCreate, |
| 37 | + Read: resourceTencentCloudCfsAutoSnapshotPolicyAttachmentRead, |
| 38 | + Delete: resourceTencentCloudCfsAutoSnapshotPolicyAttachmentDelete, |
| 39 | + Importer: &schema.ResourceImporter{ |
| 40 | + State: schema.ImportStatePassthrough, |
| 41 | + }, |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "auto_snapshot_policy_id": { |
| 44 | + Required: true, |
| 45 | + ForceNew: true, |
| 46 | + Type: schema.TypeString, |
| 47 | + Description: "ID of the snapshot to be unbound.", |
| 48 | + }, |
| 49 | + |
| 50 | + "file_system_ids": { |
| 51 | + Required: true, |
| 52 | + ForceNew: true, |
| 53 | + Type: schema.TypeString, |
| 54 | + Description: "List of IDs of the file systems to be unbound, separated by comma.", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func resourceTencentCloudCfsAutoSnapshotPolicyAttachmentCreate(d *schema.ResourceData, meta interface{}) error { |
| 61 | + defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy_attachment.create")() |
| 62 | + defer inconsistentCheck(d, meta)() |
| 63 | + |
| 64 | + logId := getLogId(contextNil) |
| 65 | + |
| 66 | + var ( |
| 67 | + request = cfs.NewBindAutoSnapshotPolicyRequest() |
| 68 | + autoSnapshotPolicyId string |
| 69 | + fileSystemIds string |
| 70 | + ) |
| 71 | + if v, ok := d.GetOk("auto_snapshot_policy_id"); ok { |
| 72 | + autoSnapshotPolicyId = v.(string) |
| 73 | + request.AutoSnapshotPolicyId = helper.String(v.(string)) |
| 74 | + } |
| 75 | + |
| 76 | + if v, ok := d.GetOk("file_system_ids"); ok { |
| 77 | + fileSystemIds = v.(string) |
| 78 | + request.FileSystemIds = helper.String(v.(string)) |
| 79 | + } |
| 80 | + |
| 81 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 82 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseCfsClient().BindAutoSnapshotPolicy(request) |
| 83 | + if e != nil { |
| 84 | + return retryError(e) |
| 85 | + } else { |
| 86 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 87 | + } |
| 88 | + return nil |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + log.Printf("[CRITAL]%s create cfs autoSnapshotPolicyAttachment failed, reason:%+v", logId, err) |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + d.SetId(autoSnapshotPolicyId + FILED_SP + fileSystemIds) |
| 96 | + |
| 97 | + return resourceTencentCloudCfsAutoSnapshotPolicyAttachmentRead(d, meta) |
| 98 | +} |
| 99 | + |
| 100 | +func resourceTencentCloudCfsAutoSnapshotPolicyAttachmentRead(d *schema.ResourceData, meta interface{}) error { |
| 101 | + defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy_attachment.read")() |
| 102 | + defer inconsistentCheck(d, meta)() |
| 103 | + |
| 104 | + logId := getLogId(contextNil) |
| 105 | + |
| 106 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 107 | + |
| 108 | + service := CfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 109 | + |
| 110 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 111 | + if len(idSplit) != 2 { |
| 112 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 113 | + } |
| 114 | + autoSnapshotPolicyId := idSplit[0] |
| 115 | + fileSystemIds := idSplit[1] |
| 116 | + |
| 117 | + autoSnapshotPolicyAttachment, err := service.DescribeCfsAutoSnapshotPolicyAttachmentById(ctx, autoSnapshotPolicyId, fileSystemIds) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + if autoSnapshotPolicyAttachment == nil { |
| 123 | + d.SetId("") |
| 124 | + return fmt.Errorf("resource `tencentcloud_cfs_auto_snapshot_policy_attachment` %s does not exist", d.Id()) |
| 125 | + } |
| 126 | + |
| 127 | + if autoSnapshotPolicyAttachment.AutoSnapshotPolicyId != nil { |
| 128 | + _ = d.Set("auto_snapshot_policy_id", autoSnapshotPolicyId) |
| 129 | + } |
| 130 | + |
| 131 | + if autoSnapshotPolicyAttachment.FileSystems != nil { |
| 132 | + _ = d.Set("file_system_ids", fileSystemIds) |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func resourceTencentCloudCfsAutoSnapshotPolicyAttachmentDelete(d *schema.ResourceData, meta interface{}) error { |
| 139 | + defer logElapsed("resource.tencentcloud_cfs_auto_snapshot_policy_attachment.delete")() |
| 140 | + defer inconsistentCheck(d, meta)() |
| 141 | + |
| 142 | + logId := getLogId(contextNil) |
| 143 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 144 | + |
| 145 | + service := CfsService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 146 | + idSplit := strings.Split(d.Id(), FILED_SP) |
| 147 | + if len(idSplit) != 2 { |
| 148 | + return fmt.Errorf("id is broken,%s", d.Id()) |
| 149 | + } |
| 150 | + autoSnapshotPolicyId := idSplit[0] |
| 151 | + fileSystemIds := idSplit[1] |
| 152 | + |
| 153 | + if err := service.DeleteCfsAutoSnapshotPolicyAttachmentById(ctx, autoSnapshotPolicyId, fileSystemIds); err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + return nil |
| 158 | +} |
0 commit comments