2015-12-11 01:18:59 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-12-11 19:13:52 +01:00
|
|
|
"time"
|
2015-12-11 01:18:59 +01:00
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
provider/aws: Refresh aws_autoscaling_schedule from state when autoscaling_group (#12312)
not found
Fixes: #12279
When manually deleting an autoscaling_group from the console, a
terraform plan would look as follows:
```
% terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_launch_configuration.foobar: Refreshing state... (ID: test-0096cf26c7eebdc9fcb5bd1837)
aws_autoscaling_group.foobar: Refreshing state... (ID: test)
aws_autoscaling_schedule.foobar: Refreshing state... (ID: foobar)
Error refreshing state: 1 error(s) occurred:
* aws_autoscaling_schedule.foobar: aws_autoscaling_schedule.foobar: Error retrieving Autoscaling Scheduled Actions: ValidationError: Group test not found
status code: 400, request id: 093e9ed5-fe01-11e6-b990-1f64334b3a10
```
After this patch:
```
% terraform plan ✹ ✭
[WARN] /Users/stacko/Code/go/bin/terraform-provider-aws overrides an internal plugin for aws-provider.
If you did not expect to see this message you will need to remove the old plugin.
See https://www.terraform.io/docs/internals/internal-plugins.html
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_launch_configuration.foobar: Refreshing state... (ID: test-0096cf26c7eebdc9fcb5bd1837)
aws_autoscaling_group.foobar: Refreshing state... (ID: test)
aws_autoscaling_schedule.foobar: Refreshing state... (ID: foobar)
The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed. Cyan entries are data sources to be read.
Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.
+ aws_autoscaling_group.foobar
arn: "<computed>"
availability_zones.#: "1"
availability_zones.2487133097: "us-west-2a"
default_cooldown: "<computed>"
desired_capacity: "<computed>"
force_delete: "true"
health_check_grace_period: "300"
health_check_type: "ELB"
launch_configuration: "test-0096cf26c7eebdc9fcb5bd1837"
load_balancers.#: "<computed>"
max_size: "1"
metrics_granularity: "1Minute"
min_size: "1"
name: "test"
protect_from_scale_in: "false"
tag.#: "1"
tag.157008572.key: "Foo"
tag.157008572.propagate_at_launch: "true"
tag.157008572.value: "foo-bar"
termination_policies.#: "1"
termination_policies.0: "OldestInstance"
vpc_zone_identifier.#: "<computed>"
wait_for_capacity_timeout: "10m"
+ aws_autoscaling_schedule.foobar
arn: "<computed>"
autoscaling_group_name: "test"
desired_capacity: "0"
end_time: "2017-12-12T06:00:00Z"
max_size: "1"
min_size: "0"
recurrence: "<computed>"
scheduled_action_name: "foobar"
start_time: "2017-12-11T18:00:00Z"
Plan: 2 to add, 0 to change, 0 to destroy.
```
2017-02-28 23:08:22 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-12-11 01:18:59 +01:00
|
|
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2015-12-11 19:13:52 +01:00
|
|
|
const awsAutoscalingScheduleTimeLayout = "2006-01-02T15:04:05Z"
|
|
|
|
|
2015-12-11 01:18:59 +01:00
|
|
|
func resourceAwsAutoscalingSchedule() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceAwsAutoscalingScheduleCreate,
|
|
|
|
Read: resourceAwsAutoscalingScheduleRead,
|
|
|
|
Update: resourceAwsAutoscalingScheduleCreate,
|
|
|
|
Delete: resourceAwsAutoscalingScheduleDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-01-30 21:07:10 +01:00
|
|
|
"arn": {
|
2015-12-11 19:13:52 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-01-30 21:07:10 +01:00
|
|
|
"scheduled_action_name": {
|
2015-12-11 01:18:59 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-01-30 21:07:10 +01:00
|
|
|
"autoscaling_group_name": {
|
2015-12-11 01:18:59 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-01-30 21:07:10 +01:00
|
|
|
"start_time": {
|
2015-12-11 19:13:52 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ValidateFunc: validateASGScheduleTimestamp,
|
2015-12-11 01:18:59 +01:00
|
|
|
},
|
2017-01-30 21:07:10 +01:00
|
|
|
"end_time": {
|
2015-12-11 19:13:52 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ValidateFunc: validateASGScheduleTimestamp,
|
2015-12-11 01:18:59 +01:00
|
|
|
},
|
2017-01-30 21:07:10 +01:00
|
|
|
"recurrence": {
|
2015-12-11 01:18:59 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-01-30 21:07:10 +01:00
|
|
|
"min_size": {
|
2015-12-11 01:18:59 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-01-30 21:07:10 +01:00
|
|
|
"max_size": {
|
2015-12-11 01:18:59 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
2017-01-30 21:07:10 +01:00
|
|
|
"desired_capacity": {
|
2015-12-11 01:18:59 +01:00
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsAutoscalingScheduleCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
2015-12-11 19:13:52 +01:00
|
|
|
params := &autoscaling.PutScheduledUpdateGroupActionInput{
|
2015-12-11 01:18:59 +01:00
|
|
|
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
|
|
|
ScheduledActionName: aws.String(d.Get("scheduled_action_name").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("start_time"); ok {
|
2015-12-11 19:13:52 +01:00
|
|
|
t, err := time.Parse(awsAutoscalingScheduleTimeLayout, attr.(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Parsing AWS Autoscaling Group Schedule Start Time: %s", err.Error())
|
|
|
|
}
|
|
|
|
params.StartTime = aws.Time(t)
|
|
|
|
}
|
|
|
|
|
|
|
|
if attr, ok := d.GetOk("end_time"); ok {
|
|
|
|
t, err := time.Parse(awsAutoscalingScheduleTimeLayout, attr.(string))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Parsing AWS Autoscaling Group Schedule End Time: %s", err.Error())
|
|
|
|
}
|
|
|
|
params.EndTime = aws.Time(t)
|
|
|
|
}
|
|
|
|
|
2016-01-08 20:41:20 +01:00
|
|
|
if attr, ok := d.GetOk("recurrence"); ok {
|
2015-12-11 19:13:52 +01:00
|
|
|
params.Recurrence = aws.String(attr.(string))
|
2015-12-11 01:18:59 +01:00
|
|
|
}
|
|
|
|
|
2016-01-21 23:02:46 +01:00
|
|
|
params.MinSize = aws.Int64(int64(d.Get("min_size").(int)))
|
|
|
|
params.MaxSize = aws.Int64(int64(d.Get("max_size").(int)))
|
|
|
|
params.DesiredCapacity = aws.Int64(int64(d.Get("desired_capacity").(int)))
|
2015-12-11 01:18:59 +01:00
|
|
|
|
|
|
|
log.Printf("[INFO] Creating Autoscaling Scheduled Action: %s", d.Get("scheduled_action_name").(string))
|
|
|
|
_, err := autoscalingconn.PutScheduledUpdateGroupAction(params)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error Creating Autoscaling Scheduled Action: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(d.Get("scheduled_action_name").(string))
|
|
|
|
|
|
|
|
return resourceAwsAutoscalingScheduleRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsAutoscalingScheduleRead(d *schema.ResourceData, meta interface{}) error {
|
2016-10-27 19:39:15 +02:00
|
|
|
sa, err, exists := resourceAwsASGScheduledActionRetrieve(d, meta)
|
2015-12-11 19:13:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-27 19:39:15 +02:00
|
|
|
if !exists {
|
|
|
|
log.Printf("Error retrieving Autoscaling Scheduled Actions. Removing from state")
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-11 19:13:52 +01:00
|
|
|
d.Set("autoscaling_group_name", sa.AutoScalingGroupName)
|
|
|
|
d.Set("arn", sa.ScheduledActionARN)
|
|
|
|
d.Set("desired_capacity", sa.DesiredCapacity)
|
|
|
|
d.Set("min_size", sa.MinSize)
|
|
|
|
d.Set("max_size", sa.MaxSize)
|
2016-01-08 20:41:20 +01:00
|
|
|
d.Set("recurrence", sa.Recurrence)
|
|
|
|
|
|
|
|
if sa.StartTime != nil {
|
|
|
|
d.Set("start_time", sa.StartTime.Format(awsAutoscalingScheduleTimeLayout))
|
|
|
|
}
|
|
|
|
|
|
|
|
if sa.EndTime != nil {
|
|
|
|
d.Set("end_time", sa.EndTime.Format(awsAutoscalingScheduleTimeLayout))
|
|
|
|
}
|
2015-12-11 19:13:52 +01:00
|
|
|
|
2015-12-11 01:18:59 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAwsAutoscalingScheduleDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
|
|
|
|
2015-12-11 19:13:52 +01:00
|
|
|
params := &autoscaling.DeleteScheduledActionInput{
|
2015-12-11 01:18:59 +01:00
|
|
|
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
2015-12-11 19:13:52 +01:00
|
|
|
ScheduledActionName: aws.String(d.Id()),
|
2015-12-11 01:18:59 +01:00
|
|
|
}
|
|
|
|
|
2015-12-11 19:13:52 +01:00
|
|
|
log.Printf("[INFO] Deleting Autoscaling Scheduled Action: %s", d.Id())
|
2015-12-11 01:18:59 +01:00
|
|
|
_, err := autoscalingconn.DeleteScheduledAction(params)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting Autoscaling Scheduled Action: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-11 19:13:52 +01:00
|
|
|
|
2016-10-27 19:39:15 +02:00
|
|
|
func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interface{}) (*autoscaling.ScheduledUpdateGroupAction, error, bool) {
|
2015-12-11 19:13:52 +01:00
|
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
|
|
|
|
|
|
|
params := &autoscaling.DescribeScheduledActionsInput{
|
|
|
|
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
|
|
|
ScheduledActionNames: []*string{aws.String(d.Id())},
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Describing Autoscaling Scheduled Action: %+v", params)
|
|
|
|
actions, err := autoscalingconn.DescribeScheduledActions(params)
|
|
|
|
if err != nil {
|
provider/aws: Refresh aws_autoscaling_schedule from state when autoscaling_group (#12312)
not found
Fixes: #12279
When manually deleting an autoscaling_group from the console, a
terraform plan would look as follows:
```
% terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_launch_configuration.foobar: Refreshing state... (ID: test-0096cf26c7eebdc9fcb5bd1837)
aws_autoscaling_group.foobar: Refreshing state... (ID: test)
aws_autoscaling_schedule.foobar: Refreshing state... (ID: foobar)
Error refreshing state: 1 error(s) occurred:
* aws_autoscaling_schedule.foobar: aws_autoscaling_schedule.foobar: Error retrieving Autoscaling Scheduled Actions: ValidationError: Group test not found
status code: 400, request id: 093e9ed5-fe01-11e6-b990-1f64334b3a10
```
After this patch:
```
% terraform plan ✹ ✭
[WARN] /Users/stacko/Code/go/bin/terraform-provider-aws overrides an internal plugin for aws-provider.
If you did not expect to see this message you will need to remove the old plugin.
See https://www.terraform.io/docs/internals/internal-plugins.html
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
aws_launch_configuration.foobar: Refreshing state... (ID: test-0096cf26c7eebdc9fcb5bd1837)
aws_autoscaling_group.foobar: Refreshing state... (ID: test)
aws_autoscaling_schedule.foobar: Refreshing state... (ID: foobar)
The Terraform execution plan has been generated and is shown below.
Resources are shown in alphabetical order for quick scanning. Green resources
will be created (or destroyed and then created if an existing resource
exists), yellow resources are being changed in-place, and red resources
will be destroyed. Cyan entries are data sources to be read.
Note: You didn't specify an "-out" parameter to save this plan, so when
"apply" is called, Terraform can't guarantee this is what will execute.
+ aws_autoscaling_group.foobar
arn: "<computed>"
availability_zones.#: "1"
availability_zones.2487133097: "us-west-2a"
default_cooldown: "<computed>"
desired_capacity: "<computed>"
force_delete: "true"
health_check_grace_period: "300"
health_check_type: "ELB"
launch_configuration: "test-0096cf26c7eebdc9fcb5bd1837"
load_balancers.#: "<computed>"
max_size: "1"
metrics_granularity: "1Minute"
min_size: "1"
name: "test"
protect_from_scale_in: "false"
tag.#: "1"
tag.157008572.key: "Foo"
tag.157008572.propagate_at_launch: "true"
tag.157008572.value: "foo-bar"
termination_policies.#: "1"
termination_policies.0: "OldestInstance"
vpc_zone_identifier.#: "<computed>"
wait_for_capacity_timeout: "10m"
+ aws_autoscaling_schedule.foobar
arn: "<computed>"
autoscaling_group_name: "test"
desired_capacity: "0"
end_time: "2017-12-12T06:00:00Z"
max_size: "1"
min_size: "0"
recurrence: "<computed>"
scheduled_action_name: "foobar"
start_time: "2017-12-11T18:00:00Z"
Plan: 2 to add, 0 to change, 0 to destroy.
```
2017-02-28 23:08:22 +01:00
|
|
|
//A ValidationError here can mean that either the Schedule is missing OR the Autoscaling Group is missing
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "ValidationError" {
|
|
|
|
log.Printf("[WARNING] %s not found, removing from state", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil, nil, false
|
|
|
|
}
|
2016-10-27 19:39:15 +02:00
|
|
|
return nil, fmt.Errorf("Error retrieving Autoscaling Scheduled Actions: %s", err), false
|
2015-12-11 19:13:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(actions.ScheduledUpdateGroupActions) != 1 ||
|
|
|
|
*actions.ScheduledUpdateGroupActions[0].ScheduledActionName != d.Id() {
|
2016-10-27 19:39:15 +02:00
|
|
|
return nil, nil, false
|
2015-12-11 19:13:52 +01:00
|
|
|
}
|
|
|
|
|
2016-10-27 19:39:15 +02:00
|
|
|
return actions.ScheduledUpdateGroupActions[0], nil, true
|
2015-12-11 19:13:52 +01:00
|
|
|
}
|