|
| 1 | +/* |
| 2 | +Provides a resource to create a lighthouse snapshot |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_lighthouse_snapshot" "snapshot" { |
| 8 | + instance_id = "lhins-acd1234" |
| 9 | + snapshot_name = "snap_20200903" |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + "log" |
| 18 | + "time" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 22 | + lighthouse "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/lighthouse/v20200324" |
| 23 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 24 | +) |
| 25 | + |
| 26 | +func resourceTencentCloudLighthouseSnapshot() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Create: resourceTencentCloudLighthouseSnapshotCreate, |
| 29 | + Read: resourceTencentCloudLighthouseSnapshotRead, |
| 30 | + Update: resourceTencentCloudLighthouseSnapshotUpdate, |
| 31 | + Delete: resourceTencentCloudLighthouseSnapshotDelete, |
| 32 | + |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "instance_id": { |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + Type: schema.TypeString, |
| 38 | + Description: "ID of the instance for which to create a snapshot.", |
| 39 | + }, |
| 40 | + |
| 41 | + "snapshot_name": { |
| 42 | + Optional: true, |
| 43 | + Type: schema.TypeString, |
| 44 | + Description: "Snapshot name, which can contain up to 60 characters.", |
| 45 | + }, |
| 46 | + }, |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func resourceTencentCloudLighthouseSnapshotCreate(d *schema.ResourceData, meta interface{}) error { |
| 51 | + defer logElapsed("resource.tencentcloud_lighthouse_snapshot.create")() |
| 52 | + defer inconsistentCheck(d, meta)() |
| 53 | + |
| 54 | + logId := getLogId(contextNil) |
| 55 | + |
| 56 | + var ( |
| 57 | + request = lighthouse.NewCreateInstanceSnapshotRequest() |
| 58 | + response = lighthouse.NewCreateInstanceSnapshotResponse() |
| 59 | + snapshotId string |
| 60 | + ) |
| 61 | + request.InstanceId = helper.String(d.Get("instance_id").(string)) |
| 62 | + |
| 63 | + if v, ok := d.GetOk("snapshot_name"); ok { |
| 64 | + request.SnapshotName = helper.String(v.(string)) |
| 65 | + } |
| 66 | + |
| 67 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 68 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().CreateInstanceSnapshot(request) |
| 69 | + if e != nil { |
| 70 | + return retryError(e) |
| 71 | + } else { |
| 72 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 73 | + } |
| 74 | + response = result |
| 75 | + return nil |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + log.Printf("[CRITAL]%s create lighthouse snapshot failed, reason:%+v", logId, err) |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + snapshotId = *response.Response.SnapshotId |
| 83 | + d.SetId(snapshotId) |
| 84 | + |
| 85 | + service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 86 | + |
| 87 | + conf := BuildStateChangeConf([]string{}, []string{"NORMAL"}, 20*readRetryTimeout, time.Second, service.LighthouseSnapshotStateRefreshFunc(d.Id(), []string{})) |
| 88 | + |
| 89 | + if _, e := conf.WaitForState(); e != nil { |
| 90 | + return e |
| 91 | + } |
| 92 | + |
| 93 | + return resourceTencentCloudLighthouseSnapshotRead(d, meta) |
| 94 | +} |
| 95 | + |
| 96 | +func resourceTencentCloudLighthouseSnapshotRead(d *schema.ResourceData, meta interface{}) error { |
| 97 | + defer logElapsed("resource.tencentcloud_lighthouse_snapshot.read")() |
| 98 | + defer inconsistentCheck(d, meta)() |
| 99 | + |
| 100 | + logId := getLogId(contextNil) |
| 101 | + |
| 102 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 103 | + |
| 104 | + service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 105 | + |
| 106 | + snapshotId := d.Id() |
| 107 | + |
| 108 | + snapshot, err := service.DescribeLighthouseSnapshotById(ctx, snapshotId) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + if snapshot == nil { |
| 114 | + d.SetId("") |
| 115 | + log.Printf("[WARN]%s resource `LighthouseSnapshot` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 116 | + return nil |
| 117 | + } |
| 118 | + |
| 119 | + if snapshot.SnapshotName != nil { |
| 120 | + _ = d.Set("snapshot_name", snapshot.SnapshotName) |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func resourceTencentCloudLighthouseSnapshotUpdate(d *schema.ResourceData, meta interface{}) error { |
| 127 | + defer logElapsed("resource.tencentcloud_lighthouse_snapshot.update")() |
| 128 | + defer inconsistentCheck(d, meta)() |
| 129 | + |
| 130 | + logId := getLogId(contextNil) |
| 131 | + |
| 132 | + request := lighthouse.NewModifySnapshotAttributeRequest() |
| 133 | + |
| 134 | + snapshotId := d.Id() |
| 135 | + |
| 136 | + request.SnapshotId = &snapshotId |
| 137 | + |
| 138 | + if d.HasChange("snapshot_name") { |
| 139 | + if v, ok := d.GetOk("snapshot_name"); ok { |
| 140 | + request.SnapshotName = helper.String(v.(string)) |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 145 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseLighthouseClient().ModifySnapshotAttribute(request) |
| 146 | + if e != nil { |
| 147 | + return retryError(e) |
| 148 | + } else { |
| 149 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 150 | + } |
| 151 | + return nil |
| 152 | + }) |
| 153 | + if err != nil { |
| 154 | + log.Printf("[CRITAL]%s update lighthouse snapshot failed, reason:%+v", logId, err) |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + return resourceTencentCloudLighthouseSnapshotRead(d, meta) |
| 159 | +} |
| 160 | + |
| 161 | +func resourceTencentCloudLighthouseSnapshotDelete(d *schema.ResourceData, meta interface{}) error { |
| 162 | + defer logElapsed("resource.tencentcloud_lighthouse_snapshot.delete")() |
| 163 | + defer inconsistentCheck(d, meta)() |
| 164 | + |
| 165 | + logId := getLogId(contextNil) |
| 166 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 167 | + |
| 168 | + service := LightHouseService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 169 | + snapshotId := d.Id() |
| 170 | + |
| 171 | + if err := service.DeleteLighthouseSnapshotById(ctx, snapshotId); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + return nil |
| 176 | +} |
0 commit comments