|
| 1 | +package cvm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" |
| 11 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 12 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 13 | +) |
| 14 | + |
| 15 | +func ResourceTencentCloudCvmActionTimer() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceTencentCloudCvmActionTimerCreate, |
| 18 | + Read: resourceTencentCloudCvmActionTimerRead, |
| 19 | + //Update: resourceTencentCloudCvmActionTimerUpdate, |
| 20 | + Delete: resourceTencentCloudCvmActionTimerDelete, |
| 21 | + //Importer: &schema.ResourceImporter{ |
| 22 | + // State: schema.ImportStatePassthrough, |
| 23 | + //}, |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "instance_id": { |
| 26 | + Required: true, |
| 27 | + ForceNew: true, |
| 28 | + Type: schema.TypeString, |
| 29 | + Description: "Instance ID.", |
| 30 | + }, |
| 31 | + "action_timer": { |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + Type: schema.TypeList, |
| 35 | + MaxItems: 1, |
| 36 | + Description: "Scheduled tasks. This parameter can be used to specify scheduled tasks for instances, and currently only supports scheduled destruction.", |
| 37 | + Elem: &schema.Resource{ |
| 38 | + Schema: map[string]*schema.Schema{ |
| 39 | + "timer_action": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + ForceNew: true, |
| 43 | + Description: "Timer action, currently only supports destroying one value: TerminateInstances.", |
| 44 | + }, |
| 45 | + "action_time": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + ForceNew: true, |
| 49 | + Description: "Execution time, expressed according to ISO8601 standard and using UTC time. The format is YYYY-MM-DDThh:mm:ssZ. For example, 2018-05-29T11:26:40Z, the execution time must be 5 minutes longer than the current time.", |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func resourceTencentCloudCvmActionTimerCreate(d *schema.ResourceData, meta interface{}) error { |
| 59 | + defer tccommon.LogElapsed("resource.tencentcloud_cvm_action_timer.create")() |
| 60 | + |
| 61 | + var ( |
| 62 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 63 | + request = cvm.NewImportInstancesActionTimerRequest() |
| 64 | + response = cvm.NewImportInstancesActionTimerResponse() |
| 65 | + actionTimerId string |
| 66 | + ) |
| 67 | + |
| 68 | + if v, ok := d.GetOk("instance_id"); ok { |
| 69 | + request.InstanceIds = []*string{helper.String(v.(string))} |
| 70 | + } |
| 71 | + |
| 72 | + if dMap, ok := helper.InterfacesHeadMap(d, "action_timer"); ok { |
| 73 | + actionTimer := cvm.ActionTimer{} |
| 74 | + if v, ok := dMap["timer_action"]; ok { |
| 75 | + actionTimer.TimerAction = helper.String(v.(string)) |
| 76 | + } |
| 77 | + |
| 78 | + if v, ok := dMap["action_time"]; ok { |
| 79 | + actionTimer.ActionTime = helper.String(v.(string)) |
| 80 | + } |
| 81 | + |
| 82 | + request.ActionTimer = &actionTimer |
| 83 | + } |
| 84 | + |
| 85 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 86 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCvmClient().ImportInstancesActionTimer(request) |
| 87 | + if e != nil { |
| 88 | + return resource.RetryableError(e) |
| 89 | + } else { |
| 90 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 91 | + } |
| 92 | + |
| 93 | + if result == nil || result.Response == nil || len(result.Response.ActionTimerIds) != 1 { |
| 94 | + e = fmt.Errorf("create cvm InstanceActionTimer failed") |
| 95 | + return resource.NonRetryableError(e) |
| 96 | + } |
| 97 | + |
| 98 | + response = result |
| 99 | + return nil |
| 100 | + }) |
| 101 | + |
| 102 | + if err != nil { |
| 103 | + log.Printf("[CRITAL]%s create cvm InstanceActionTimer failed, reason:%+v", logId, err) |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + actionTimerId = *response.Response.ActionTimerIds[0] |
| 108 | + d.SetId(actionTimerId) |
| 109 | + |
| 110 | + return resourceTencentCloudCvmActionTimerRead(d, meta) |
| 111 | +} |
| 112 | + |
| 113 | +func resourceTencentCloudCvmActionTimerRead(d *schema.ResourceData, meta interface{}) error { |
| 114 | + defer tccommon.LogElapsed("resource.tencentcloud_cvm_action_timer.read")() |
| 115 | + |
| 116 | + var ( |
| 117 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 118 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 119 | + service = CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 120 | + actionTimerId = d.Id() |
| 121 | + ) |
| 122 | + |
| 123 | + InstanceActionTimer, err := service.DescribeCvmInstanceActionTimerById(ctx, actionTimerId) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + if InstanceActionTimer == nil { |
| 129 | + d.SetId("") |
| 130 | + log.Printf("[WARN]%s resource `CvmInstanceActionTimer` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + actionTimerMap := map[string]interface{}{} |
| 135 | + if InstanceActionTimer.TimerAction != nil { |
| 136 | + actionTimerMap["timer_action"] = InstanceActionTimer.TimerAction |
| 137 | + } |
| 138 | + |
| 139 | + if InstanceActionTimer.ActionTime != nil { |
| 140 | + actionTimerMap["action_time"] = InstanceActionTimer.ActionTime |
| 141 | + } |
| 142 | + |
| 143 | + _ = d.Set("action_timer", []interface{}{actionTimerMap}) |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// func resourceTencentCloudCvmActionTimerUpdate(d *schema.ResourceData, meta interface{}) error { |
| 149 | +// defer tccommon.LogElapsed("resource.tencentcloud_cvm_action_timer.update")() |
| 150 | +// defer tccommon.InconsistentCheck(d, meta)() |
| 151 | + |
| 152 | +// return resourceTencentCloudCvmActionTimerRead(d, meta) |
| 153 | +// } |
| 154 | + |
| 155 | +func resourceTencentCloudCvmActionTimerDelete(d *schema.ResourceData, meta interface{}) error { |
| 156 | + defer tccommon.LogElapsed("resource.tencentcloud_cvm_action_timer.delete")() |
| 157 | + |
| 158 | + var ( |
| 159 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 160 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 161 | + service = CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 162 | + actionTimerId = d.Id() |
| 163 | + ) |
| 164 | + |
| 165 | + if err := service.DeleteCvmInstanceActionTimerById(ctx, actionTimerId); err != nil { |
| 166 | + return err |
| 167 | + } |
| 168 | + |
| 169 | + return nil |
| 170 | +} |
0 commit comments