|
| 1 | +package cvm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + cvm "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cvm/v20170312" |
| 9 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 10 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 11 | + "log" |
| 12 | +) |
| 13 | + |
| 14 | +func ResourceTencentCloudCvmActionTimer() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceTencentCloudCvmActionTimerCreate, |
| 17 | + Read: resourceTencentCloudCvmActionTimerRead, |
| 18 | + //Update: resourceTencentCloudCvmActionTimerUpdate, |
| 19 | + Delete: resourceTencentCloudCvmActionTimerDelete, |
| 20 | + //Importer: &schema.ResourceImporter{ |
| 21 | + // State: schema.ImportStatePassthrough, |
| 22 | + //}, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "instance_id": { |
| 25 | + Required: true, |
| 26 | + ForceNew: true, |
| 27 | + Type: schema.TypeString, |
| 28 | + Description: "Instance ID.", |
| 29 | + }, |
| 30 | + "action_timer": { |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + Type: schema.TypeList, |
| 34 | + MaxItems: 1, |
| 35 | + Description: "Scheduled tasks. This parameter can be used to specify scheduled tasks for instances, and currently only supports scheduled destruction.", |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "timer_action": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Description: "Timer action, currently only supports destroying one value: TerminateInstances.", |
| 42 | + }, |
| 43 | + "action_time": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + 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.", |
| 47 | + }, |
| 48 | + "externals": { |
| 49 | + Type: schema.TypeList, |
| 50 | + MaxItems: 1, |
| 51 | + Optional: true, |
| 52 | + Description: "Extended data.", |
| 53 | + Elem: &schema.Resource{ |
| 54 | + Schema: map[string]*schema.Schema{ |
| 55 | + "release_address": { |
| 56 | + Type: schema.TypeBool, |
| 57 | + Optional: true, |
| 58 | + Description: "Whether to release address of this instance.", |
| 59 | + }, |
| 60 | + "unsupport_networks": { |
| 61 | + Type: schema.TypeSet, |
| 62 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 63 | + Optional: true, |
| 64 | + Description: "Unsupported network type, Value range: BASIC - Basic Network; VPC1.0 - Private Network VPC1.0.", |
| 65 | + }, |
| 66 | + "storage_block_attr": { |
| 67 | + Type: schema.TypeList, |
| 68 | + MaxItems: 1, |
| 69 | + Optional: true, |
| 70 | + Description: "HDD local storage properties.", |
| 71 | + Elem: &schema.Resource{ |
| 72 | + Schema: map[string]*schema.Schema{ |
| 73 | + "type": { |
| 74 | + Type: schema.TypeString, |
| 75 | + Optional: true, |
| 76 | + Description: "HDD local storage type, Value is: LOCAL_PRO.", |
| 77 | + }, |
| 78 | + "min_size": { |
| 79 | + Type: schema.TypeInt, |
| 80 | + Optional: true, |
| 81 | + Description: "Minimum capacity for HDD local storage.", |
| 82 | + }, |
| 83 | + "max_size": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Optional: true, |
| 86 | + Description: "Maximum capacity of HDD local storage.", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func resourceTencentCloudCvmActionTimerCreate(d *schema.ResourceData, meta interface{}) error { |
| 102 | + defer tccommon.LogElapsed("resource.tencentcloud_cvm_action_timer.create")() |
| 103 | + |
| 104 | + var ( |
| 105 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 106 | + request = cvm.NewImportInstancesActionTimerRequest() |
| 107 | + response = cvm.NewImportInstancesActionTimerResponse() |
| 108 | + actionTimerId string |
| 109 | + ) |
| 110 | + |
| 111 | + if v, ok := d.GetOk("instance_id"); ok { |
| 112 | + request.InstanceIds = []*string{helper.String(v.(string))} |
| 113 | + } |
| 114 | + |
| 115 | + if dMap, ok := helper.InterfacesHeadMap(d, "action_timer"); ok { |
| 116 | + actionTimer := cvm.ActionTimer{} |
| 117 | + if v, ok := dMap["timer_action"]; ok { |
| 118 | + actionTimer.TimerAction = helper.String(v.(string)) |
| 119 | + } |
| 120 | + |
| 121 | + if v, ok := dMap["action_time"]; ok { |
| 122 | + actionTimer.ActionTime = helper.String(v.(string)) |
| 123 | + } |
| 124 | + |
| 125 | + if externalsMap, ok := helper.InterfaceToMap(dMap, "externals"); ok { |
| 126 | + externals := cvm.Externals{} |
| 127 | + if v, ok := externalsMap["release_address"]; ok { |
| 128 | + externals.ReleaseAddress = helper.Bool(v.(bool)) |
| 129 | + } |
| 130 | + |
| 131 | + if v, ok := externalsMap["unsupport_networks"]; ok { |
| 132 | + unsupportNetworksSet := v.(*schema.Set).List() |
| 133 | + for i := range unsupportNetworksSet { |
| 134 | + if unsupportNetworksSet[i] != nil { |
| 135 | + unsupportNetworks := unsupportNetworksSet[i].(string) |
| 136 | + externals.UnsupportNetworks = append(externals.UnsupportNetworks, &unsupportNetworks) |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if storageBlockAttrMap, ok := helper.InterfaceToMap(externalsMap, "storage_block_attr"); ok { |
| 142 | + storageBlock := cvm.StorageBlock{} |
| 143 | + if v, ok := storageBlockAttrMap["type"]; ok { |
| 144 | + storageBlock.Type = helper.String(v.(string)) |
| 145 | + } |
| 146 | + |
| 147 | + if v, ok := storageBlockAttrMap["min_size"]; ok { |
| 148 | + storageBlock.MinSize = helper.IntInt64(v.(int)) |
| 149 | + } |
| 150 | + |
| 151 | + if v, ok := storageBlockAttrMap["max_size"]; ok { |
| 152 | + storageBlock.MaxSize = helper.IntInt64(v.(int)) |
| 153 | + } |
| 154 | + |
| 155 | + externals.StorageBlockAttr = &storageBlock |
| 156 | + } |
| 157 | + |
| 158 | + actionTimer.Externals = &externals |
| 159 | + } |
| 160 | + |
| 161 | + request.ActionTimer = &actionTimer |
| 162 | + } |
| 163 | + |
| 164 | + err := resource.Retry(tccommon.WriteRetryTimeout, func() *resource.RetryError { |
| 165 | + result, e := meta.(tccommon.ProviderMeta).GetAPIV3Conn().UseCvmClient().ImportInstancesActionTimer(request) |
| 166 | + if e != nil { |
| 167 | + return resource.RetryableError(e) |
| 168 | + } else { |
| 169 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 170 | + } |
| 171 | + |
| 172 | + if result == nil || result.Response == nil || len(result.Response.ActionTimerIds) != 1 { |
| 173 | + e = fmt.Errorf("create cvm InstanceActionTimer failed") |
| 174 | + return resource.NonRetryableError(e) |
| 175 | + } |
| 176 | + |
| 177 | + response = result |
| 178 | + return nil |
| 179 | + }) |
| 180 | + |
| 181 | + if err != nil { |
| 182 | + log.Printf("[CRITAL]%s create cvm InstanceActionTimer failed, reason:%+v", logId, err) |
| 183 | + return err |
| 184 | + } |
| 185 | + |
| 186 | + actionTimerId = *response.Response.ActionTimerIds[0] |
| 187 | + d.SetId(actionTimerId) |
| 188 | + |
| 189 | + return resourceTencentCloudCvmActionTimerRead(d, meta) |
| 190 | +} |
| 191 | + |
| 192 | +func resourceTencentCloudCvmActionTimerRead(d *schema.ResourceData, meta interface{}) error { |
| 193 | + defer tccommon.LogElapsed("resource.tencentcloud_cvm_action_timer.read")() |
| 194 | + |
| 195 | + var ( |
| 196 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 197 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 198 | + service = CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 199 | + actionTimerId = d.Id() |
| 200 | + ) |
| 201 | + |
| 202 | + InstanceActionTimer, err := service.DescribeCvmInstanceActionTimerById(ctx, actionTimerId) |
| 203 | + if err != nil { |
| 204 | + return err |
| 205 | + } |
| 206 | + |
| 207 | + if InstanceActionTimer == nil { |
| 208 | + d.SetId("") |
| 209 | + log.Printf("[WARN]%s resource `CvmInstanceActionTimer` [%s] not found, please check if it has been deleted.\n", logId, d.Id()) |
| 210 | + return nil |
| 211 | + } |
| 212 | + |
| 213 | + actionTimerMap := map[string]interface{}{} |
| 214 | + if InstanceActionTimer.TimerAction != nil { |
| 215 | + actionTimerMap["timer_action"] = InstanceActionTimer.TimerAction |
| 216 | + } |
| 217 | + |
| 218 | + if InstanceActionTimer.ActionTime != nil { |
| 219 | + actionTimerMap["action_time"] = InstanceActionTimer.ActionTime |
| 220 | + } |
| 221 | + |
| 222 | + actionTimerMap["externals"] = []interface{}{} |
| 223 | + if InstanceActionTimer.Externals != nil { |
| 224 | + externalsMap := map[string]interface{}{} |
| 225 | + if InstanceActionTimer.Externals.ReleaseAddress != nil { |
| 226 | + externalsMap["release_address"] = InstanceActionTimer.Externals.ReleaseAddress |
| 227 | + } |
| 228 | + |
| 229 | + if InstanceActionTimer.Externals.UnsupportNetworks != nil { |
| 230 | + externalsMap["unsupport_networks"] = InstanceActionTimer.Externals.UnsupportNetworks |
| 231 | + } |
| 232 | + |
| 233 | + if InstanceActionTimer.Externals.StorageBlockAttr != nil { |
| 234 | + storageBlockAttrMap := map[string]interface{}{} |
| 235 | + if InstanceActionTimer.Externals.StorageBlockAttr.Type != nil { |
| 236 | + storageBlockAttrMap["type"] = InstanceActionTimer.Externals.StorageBlockAttr.Type |
| 237 | + } |
| 238 | + |
| 239 | + if InstanceActionTimer.Externals.StorageBlockAttr.MinSize != nil { |
| 240 | + storageBlockAttrMap["min_size"] = InstanceActionTimer.Externals.StorageBlockAttr.MinSize |
| 241 | + } |
| 242 | + |
| 243 | + if InstanceActionTimer.Externals.StorageBlockAttr.MaxSize != nil { |
| 244 | + storageBlockAttrMap["max_size"] = InstanceActionTimer.Externals.StorageBlockAttr.MaxSize |
| 245 | + } |
| 246 | + |
| 247 | + externalsMap["storage_block_attr"] = []interface{}{storageBlockAttrMap} |
| 248 | + } |
| 249 | + |
| 250 | + actionTimerMap["externals"] = []interface{}{externalsMap} |
| 251 | + } |
| 252 | + |
| 253 | + _ = d.Set("action_timer", []interface{}{actionTimerMap}) |
| 254 | + |
| 255 | + return nil |
| 256 | +} |
| 257 | + |
| 258 | +func resourceTencentCloudCvmActionTimerUpdate(d *schema.ResourceData, meta interface{}) error { |
| 259 | + defer tccommon.LogElapsed("resource.tencentcloud_cvm_action_timer.update")() |
| 260 | + defer tccommon.InconsistentCheck(d, meta)() |
| 261 | + |
| 262 | + return resourceTencentCloudCvmActionTimerRead(d, meta) |
| 263 | +} |
| 264 | + |
| 265 | +func resourceTencentCloudCvmActionTimerDelete(d *schema.ResourceData, meta interface{}) error { |
| 266 | + defer tccommon.LogElapsed("resource.tencentcloud_cvm_action_timer.delete")() |
| 267 | + |
| 268 | + var ( |
| 269 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 270 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 271 | + service = CvmService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 272 | + actionTimerId = d.Id() |
| 273 | + ) |
| 274 | + |
| 275 | + if err := service.DeleteCvmInstanceActionTimerById(ctx, actionTimerId); err != nil { |
| 276 | + return err |
| 277 | + } |
| 278 | + |
| 279 | + return nil |
| 280 | +} |
0 commit comments