|
| 1 | +package duplocloud |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/duplocloud/terraform-provider-duplocloud/duplosdk" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | +) |
| 13 | + |
| 14 | +func asgInstanceRefresh() map[string]*schema.Schema { |
| 15 | + return map[string]*schema.Schema{ |
| 16 | + "tenant_id": { |
| 17 | + Description: "The GUID of the tenant that the asg will be created in.", |
| 18 | + Type: schema.TypeString, |
| 19 | + Required: true, |
| 20 | + ForceNew: true, |
| 21 | + ValidateFunc: validation.IsUUID, |
| 22 | + }, |
| 23 | + "asg_name": { |
| 24 | + Description: "The fullname of the asg", |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + ForceNew: true, |
| 28 | + }, |
| 29 | + "refresh_identifier": { |
| 30 | + Description: "To identify refresh or invoke a refresh", |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + Computed: true, |
| 34 | + }, |
| 35 | + "auto_rollback": { |
| 36 | + Description: "Automatically rollback if instance refresh fails", |
| 37 | + Type: schema.TypeBool, |
| 38 | + Optional: true, |
| 39 | + Computed: true, |
| 40 | + }, |
| 41 | + "instance_warmup": { |
| 42 | + Description: "Number of seconds until a newly launched instance is configured and ready to use. Default behavior is to use the Auto Scaling Group's health check grace period.", |
| 43 | + Type: schema.TypeInt, |
| 44 | + Optional: true, |
| 45 | + Computed: true, |
| 46 | + }, |
| 47 | + "max_healthy_percentage": { |
| 48 | + Description: "Amount of capacity in the Auto Scaling group that can be in service and healthy, or pending, to support your workload when an instance refresh is in place, as a percentage of the desired capacity of the Auto Scaling group.", |
| 49 | + Type: schema.TypeInt, |
| 50 | + Default: 100, |
| 51 | + Optional: true, |
| 52 | + ValidateFunc: validation.IntBetween(100, 200), |
| 53 | + }, |
| 54 | + "min_healthy_percentage": { |
| 55 | + Description: "Amount of capacity in the Auto Scaling group that must remain healthy during an instance refresh to allow the operation to continue, as a percentage of the desired capacity of the Auto Scaling group.", |
| 56 | + Type: schema.TypeInt, |
| 57 | + Default: 90, |
| 58 | + Optional: true, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | +func resourceAsgInstanceRefresh() *schema.Resource { |
| 63 | + return &schema.Resource{ |
| 64 | + Description: "duplocloud_asg_instance_refresh triggers the instance refresh of asg in duplo", |
| 65 | + ReadContext: resourceASGInstanceRefreshRead, |
| 66 | + CreateContext: resourceASGInstanceRefreshCreate, |
| 67 | + UpdateContext: resourceASGInstanceRefreshCreate, |
| 68 | + DeleteContext: resourceASGInstanceRefreshDelete, |
| 69 | + Importer: &schema.ResourceImporter{ |
| 70 | + StateContext: schema.ImportStatePassthroughContext, |
| 71 | + }, |
| 72 | + Timeouts: &schema.ResourceTimeout{ |
| 73 | + Create: schema.DefaultTimeout(45 * time.Minute), |
| 74 | + Update: schema.DefaultTimeout(20 * time.Minute), |
| 75 | + Delete: schema.DefaultTimeout(20 * time.Minute), |
| 76 | + }, |
| 77 | + |
| 78 | + Schema: asgInstanceRefresh(), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func resourceASGInstanceRefreshRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 83 | + return nil |
| 84 | +} |
| 85 | +func resourceASGInstanceRefreshCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 86 | + tenantId := d.Get("tenant_id").(string) |
| 87 | + rq := expandInstanceRefresh(d) |
| 88 | + c := m.(*duplosdk.Client) |
| 89 | + err := c.AsgInstanceRefresh(tenantId, &rq) |
| 90 | + if err != nil { |
| 91 | + return diag.Errorf("%s", err.Error()) |
| 92 | + } |
| 93 | + d.SetId(tenantId + "/asg-refresh/" + rq.AutoScalingGroupName) |
| 94 | + diag := resourceASGInstanceRefreshRead(ctx, d, m) |
| 95 | + return diag |
| 96 | + |
| 97 | +} |
| 98 | + |
| 99 | +func resourceASGInstanceRefreshDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 100 | + return nil |
| 101 | + |
| 102 | +} |
| 103 | + |
| 104 | +func expandInstanceRefresh(d *schema.ResourceData) duplosdk.DuploAsgInstanceRefresh { |
| 105 | + |
| 106 | + return duplosdk.DuploAsgInstanceRefresh{ |
| 107 | + AutoScalingGroupName: d.Get("asg_name").(string), |
| 108 | + InstanceWarmup: d.Get("instance_warmup").(int), |
| 109 | + MaxHealthyPercentage: d.Get("max_healthy_percentage").(int), |
| 110 | + MinHealthyPercentage: d.Get("min_healthy_percentage").(int), |
| 111 | + AutoRollback: d.Get("auto_rollback").(bool), |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments