mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-20 11:48:24 -06:00
Fixes #9654 Before the fix, I created an ASG with a schedule on it. Went to the AWS console and deleted the schedule. A terraform plan looked as follows: ``` % terraform plan 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: terraform-test-foobar5) aws_autoscaling_group.foobar: Refreshing state... (ID: terraform-test-foobar5) aws_autoscaling_schedule.foobar: Refreshing state... (ID: foobar) Error refreshing state: 1 error(s) occurred: * aws_autoscaling_schedule.foobar: Unable to find Autoscaling * Scheduled Action: []*autoscaling.ScheduledUpdateGroupAction(nil) ``` After the fix: ``` terraform plan 1 ↵ 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: terraform-test-foobar5) aws_autoscaling_group.foobar: Refreshing state... (ID: terraform-test-foobar5) 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_schedule.foobar arn: "<computed>" autoscaling_group_name: "terraform-test-foobar5" desired_capacity: "0" end_time: "2018-01-16T13:00:00Z" max_size: "0" min_size: "0" recurrence: "<computed>" scheduled_action_name: "foobar" start_time: "2018-01-16T07:00:00Z" Plan: 1 to add, 0 to change, 0 to destroy. ``` Tests run as expected: ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSAutoscalingSchedule_' 2 ↵ ✹ ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/10/27 17:45:19 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSAutoscalingSchedule_ -timeout 120m === RUN TestAccAWSAutoscalingSchedule_basic --- PASS: TestAccAWSAutoscalingSchedule_basic (140.94s) === RUN TestAccAWSAutoscalingSchedule_disappears --- PASS: TestAccAWSAutoscalingSchedule_disappears (179.17s) === RUN TestAccAWSAutoscalingSchedule_recurrence --- PASS: TestAccAWSAutoscalingSchedule_recurrence (186.72s) === RUN TestAccAWSAutoscalingSchedule_zeroValues --- PASS: TestAccAWSAutoscalingSchedule_zeroValues (167.73s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws 674.530s ```
183 lines
5.3 KiB
Go
183 lines
5.3 KiB
Go
package aws
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
"github.com/aws/aws-sdk-go/service/autoscaling"
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
)
|
|
|
|
const awsAutoscalingScheduleTimeLayout = "2006-01-02T15:04:05Z"
|
|
|
|
func resourceAwsAutoscalingSchedule() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: resourceAwsAutoscalingScheduleCreate,
|
|
Read: resourceAwsAutoscalingScheduleRead,
|
|
Update: resourceAwsAutoscalingScheduleCreate,
|
|
Delete: resourceAwsAutoscalingScheduleDelete,
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"arn": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
"scheduled_action_name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
"autoscaling_group_name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
"start_time": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
ValidateFunc: validateASGScheduleTimestamp,
|
|
},
|
|
"end_time": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
ValidateFunc: validateASGScheduleTimestamp,
|
|
},
|
|
"recurrence": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
},
|
|
"min_size": &schema.Schema{
|
|
Type: schema.TypeInt,
|
|
Optional: true,
|
|
Computed: true,
|
|
},
|
|
"max_size": &schema.Schema{
|
|
Type: schema.TypeInt,
|
|
Optional: true,
|
|
Computed: true,
|
|
},
|
|
"desired_capacity": &schema.Schema{
|
|
Type: schema.TypeInt,
|
|
Optional: true,
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceAwsAutoscalingScheduleCreate(d *schema.ResourceData, meta interface{}) error {
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
|
params := &autoscaling.PutScheduledUpdateGroupActionInput{
|
|
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 {
|
|
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)
|
|
}
|
|
|
|
if attr, ok := d.GetOk("recurrence"); ok {
|
|
params.Recurrence = aws.String(attr.(string))
|
|
}
|
|
|
|
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)))
|
|
|
|
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 {
|
|
sa, err, exists := resourceAwsASGScheduledActionRetrieve(d, meta)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !exists {
|
|
log.Printf("Error retrieving Autoscaling Scheduled Actions. Removing from state")
|
|
d.SetId("")
|
|
return nil
|
|
}
|
|
|
|
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)
|
|
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))
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceAwsAutoscalingScheduleDelete(d *schema.ResourceData, meta interface{}) error {
|
|
autoscalingconn := meta.(*AWSClient).autoscalingconn
|
|
|
|
params := &autoscaling.DeleteScheduledActionInput{
|
|
AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
|
|
ScheduledActionName: aws.String(d.Id()),
|
|
}
|
|
|
|
log.Printf("[INFO] Deleting Autoscaling Scheduled Action: %s", d.Id())
|
|
_, err := autoscalingconn.DeleteScheduledAction(params)
|
|
if err != nil {
|
|
return fmt.Errorf("Error deleting Autoscaling Scheduled Action: %s", err.Error())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interface{}) (*autoscaling.ScheduledUpdateGroupAction, error, bool) {
|
|
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 {
|
|
return nil, fmt.Errorf("Error retrieving Autoscaling Scheduled Actions: %s", err), false
|
|
}
|
|
|
|
if len(actions.ScheduledUpdateGroupActions) != 1 ||
|
|
*actions.ScheduledUpdateGroupActions[0].ScheduledActionName != d.Id() {
|
|
return nil, nil, false
|
|
}
|
|
|
|
return actions.ScheduledUpdateGroupActions[0], nil, true
|
|
}
|