|
| 1 | +/* |
| 2 | +Provides a resource to create a as start_instances |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +resource "tencentcloud_as_start_instances" "start_instances" { |
| 8 | + auto_scaling_group_id = tencentcloud_as_scaling_group.scaling_group.id |
| 9 | + instance_ids = ["ins-xxxxx"] |
| 10 | +} |
| 11 | +``` |
| 12 | +
|
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "log" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 23 | + as "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/as/v20180419" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | +) |
| 26 | + |
| 27 | +func resourceTencentCloudAsStartInstances() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Create: resourceTencentCloudAsStartInstancesCreate, |
| 30 | + Read: resourceTencentCloudAsStartInstancesRead, |
| 31 | + Delete: resourceTencentCloudAsStartInstancesDelete, |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "auto_scaling_group_id": { |
| 34 | + Required: true, |
| 35 | + ForceNew: true, |
| 36 | + Type: schema.TypeString, |
| 37 | + Description: "Launch configuration ID.", |
| 38 | + }, |
| 39 | + |
| 40 | + "instance_ids": { |
| 41 | + Required: true, |
| 42 | + ForceNew: true, |
| 43 | + Type: schema.TypeSet, |
| 44 | + Elem: &schema.Schema{ |
| 45 | + Type: schema.TypeString, |
| 46 | + }, |
| 47 | + Description: "List of cvm instances to start.", |
| 48 | + }, |
| 49 | + }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func resourceTencentCloudAsStartInstancesCreate(d *schema.ResourceData, meta interface{}) error { |
| 54 | + defer logElapsed("data_source.tencentcloud_as_start_instances.create")() |
| 55 | + defer inconsistentCheck(d, meta)() |
| 56 | + |
| 57 | + logId := getLogId(contextNil) |
| 58 | + |
| 59 | + var ( |
| 60 | + request = as.NewStartAutoScalingInstancesRequest() |
| 61 | + response = as.NewStartAutoScalingInstancesResponse() |
| 62 | + activityId string |
| 63 | + ) |
| 64 | + if v, ok := d.GetOk("auto_scaling_group_id"); ok { |
| 65 | + request.AutoScalingGroupId = helper.String(v.(string)) |
| 66 | + } |
| 67 | + |
| 68 | + if v, ok := d.GetOk("instance_ids"); ok { |
| 69 | + instanceIdsSet := v.(*schema.Set).List() |
| 70 | + for i := range instanceIdsSet { |
| 71 | + instanceIds := instanceIdsSet[i].(string) |
| 72 | + request.InstanceIds = append(request.InstanceIds, &instanceIds) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + err := resource.Retry(writeRetryTimeout, func() *resource.RetryError { |
| 77 | + result, e := meta.(*TencentCloudClient).apiV3Conn.UseAsClient().StartAutoScalingInstances(request) |
| 78 | + if e != nil { |
| 79 | + return retryError(e) |
| 80 | + } else { |
| 81 | + log.Printf("[DEBUG]%s api[%s] success, request body [%s], response body [%s]\n", logId, request.GetAction(), request.ToJsonString(), result.ToJsonString()) |
| 82 | + } |
| 83 | + response = result |
| 84 | + return nil |
| 85 | + }) |
| 86 | + if err != nil { |
| 87 | + log.Printf("[CRITAL]%s operate as startInstances failed, reason:%+v", logId, err) |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + activityId = *response.Response.ActivityId |
| 92 | + |
| 93 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 94 | + service := AsService{ |
| 95 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 96 | + } |
| 97 | + |
| 98 | + err = resource.Retry(4*readRetryTimeout, func() *resource.RetryError { |
| 99 | + status, err := service.DescribeActivityById(ctx, activityId) |
| 100 | + if err != nil { |
| 101 | + return resource.NonRetryableError(err) |
| 102 | + } |
| 103 | + if status == SCALING_GROUP_ACTIVITY_STATUS_INIT || status == SCALING_GROUP_ACTIVITY_STATUS_RUNNING { |
| 104 | + return resource.RetryableError(fmt.Errorf("remove status is running(%s)", status)) |
| 105 | + } |
| 106 | + if status == SCALING_GROUP_ACTIVITY_STATUS_SUCCESSFUL { |
| 107 | + return nil |
| 108 | + } |
| 109 | + return resource.NonRetryableError(fmt.Errorf("remove status is failed(%s)", status)) |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + d.SetId(activityId) |
| 116 | + |
| 117 | + return resourceTencentCloudAsStartInstancesRead(d, meta) |
| 118 | +} |
| 119 | + |
| 120 | +func resourceTencentCloudAsStartInstancesRead(d *schema.ResourceData, meta interface{}) error { |
| 121 | + defer logElapsed("resource.tencentcloud_as_start_instances.read")() |
| 122 | + defer inconsistentCheck(d, meta)() |
| 123 | + |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func resourceTencentCloudAsStartInstancesDelete(d *schema.ResourceData, meta interface{}) error { |
| 128 | + defer logElapsed("resource.tencentcloud_as_start_instances.delete")() |
| 129 | + defer inconsistentCheck(d, meta)() |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
0 commit comments