Skip to content

Commit 686ef9d

Browse files
authored
Merge pull request #1008 from duplocloud/DUPLO-30294
DUPLO-30294 TF: No way to set/trigger Instance Refresh
2 parents 44881dd + 1f288d4 commit 686ef9d

File tree

5 files changed

+206
-0
lines changed

5 files changed

+206
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "duplocloud_asg_instance_refresh Resource - terraform-provider-duplocloud"
4+
subcategory: ""
5+
description: |-
6+
duplocloud_asg_instance_refresh triggers the instance refresh of asg in duplo
7+
---
8+
9+
# duplocloud_asg_instance_refresh (Resource)
10+
11+
duplocloud_asg_instance_refresh triggers the instance refresh of asg in duplo
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "duplocloud_tenant" "myapp" {
17+
account_name = "myapp"
18+
plan_id = "default"
19+
}
20+
21+
22+
resource "duplocloud_asg_instance_refresh" "name" {
23+
tenant_id = duplocloud_tenant.myapp.tenant_id
24+
asg_name = "asg-name"
25+
refresh_identifier = "1" #any identifier values can be used
26+
max_healthy_percentage = 100
27+
min_healthy_percentage = 90
28+
}
29+
```
30+
31+
<!-- schema generated by tfplugindocs -->
32+
## Schema
33+
34+
### Required
35+
36+
- `asg_name` (String) The fullname of the asg
37+
- `tenant_id` (String) The GUID of the tenant that the asg will be created in.
38+
39+
### Optional
40+
41+
- `auto_rollback` (Boolean) Automatically rollback if instance refresh fails
42+
- `instance_warmup` (Number) 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+
- `max_healthy_percentage` (Number) 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. Defaults to `100`.
44+
- `min_healthy_percentage` (Number) 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. Defaults to `90`.
45+
- `refresh_identifier` (String) To identify refresh or invoke a refresh
46+
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))
47+
48+
### Read-Only
49+
50+
- `id` (String) The ID of this resource.
51+
52+
<a id="nestedblock--timeouts"></a>
53+
### Nested Schema for `timeouts`
54+
55+
Optional:
56+
57+
- `create` (String)
58+
- `delete` (String)
59+
- `update` (String)

duplocloud/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ func Provider() *schema.Provider {
189189
"duplocloud_azure_datafactory": resourceAzureDataFactory(),
190190
"duplocloud_azure_availability_set": resourceAzureAvailabilitySet(),
191191
"duplocloud_aws_launch_template_default_version": resourceAwsLaunchTemplateDefaultVersion(),
192+
"duplocloud_asg_instance_refresh": resourceAsgInstanceRefresh(),
192193
},
193194
DataSourcesMap: map[string]*schema.Resource{
194195
"duplocloud_admin_aws_credentials": dataSourceAdminAwsCredentials(),
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
}

duplosdk/aws_launch_templates.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,22 @@ type DuploLaunchTemplateOperatorResponse struct {
231231
Managed bool `json:"Managed,omitempty"`
232232
Principal string `json:"Principal,omitempty"`
233233
}
234+
235+
type DuploAsgInstanceRefresh struct {
236+
AutoScalingGroupName string `json:"AutoScalingGroupName"`
237+
AutoRollback bool `json:"AutoRollback"`
238+
InstanceWarmup int `json:"InstanceWarmup"`
239+
MaxHealthyPercentage int `json:"MaxHealthyPercentage"`
240+
MinHealthyPercentage int `json:"MinHealthyPercentage"`
241+
}
242+
243+
func (c *Client) AsgInstanceRefresh(tenantId string, rq *DuploAsgInstanceRefresh) ClientError {
244+
var rp interface{}
245+
err := c.postAPI(
246+
fmt.Sprintf("AsgInstanceRefresh(%s, %s)", tenantId, rq.AutoScalingGroupName),
247+
fmt.Sprintf("v3/subscriptions/%s/aws/asg/refreshinstances", tenantId),
248+
rq,
249+
&rp,
250+
)
251+
return err
252+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
resource "duplocloud_tenant" "myapp" {
2+
account_name = "myapp"
3+
plan_id = "default"
4+
}
5+
6+
7+
resource "duplocloud_asg_instance_refresh" "name" {
8+
tenant_id = duplocloud_tenant.myapp.tenant_id
9+
asg_name = "asg-name"
10+
refresh_identifier = "1" #any identifier values can be used
11+
max_healthy_percentage = 100
12+
min_healthy_percentage = 90
13+
}

0 commit comments

Comments
 (0)