github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_autoscaling_schedule.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/autoscaling"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  const awsAutoscalingScheduleTimeLayout = "2006-01-02T15:04:05Z"
    15  
    16  func resourceAwsAutoscalingSchedule() *schema.Resource {
    17  	return &schema.Resource{
    18  		Create: resourceAwsAutoscalingScheduleCreate,
    19  		Read:   resourceAwsAutoscalingScheduleRead,
    20  		Update: resourceAwsAutoscalingScheduleCreate,
    21  		Delete: resourceAwsAutoscalingScheduleDelete,
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"arn": {
    25  				Type:     schema.TypeString,
    26  				Computed: true,
    27  			},
    28  			"scheduled_action_name": {
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  			"autoscaling_group_name": {
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  			"start_time": {
    39  				Type:         schema.TypeString,
    40  				Optional:     true,
    41  				Computed:     true,
    42  				ValidateFunc: validateASGScheduleTimestamp,
    43  			},
    44  			"end_time": {
    45  				Type:         schema.TypeString,
    46  				Optional:     true,
    47  				Computed:     true,
    48  				ValidateFunc: validateASGScheduleTimestamp,
    49  			},
    50  			"recurrence": {
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				Computed: true,
    54  			},
    55  			"min_size": {
    56  				Type:     schema.TypeInt,
    57  				Optional: true,
    58  				Computed: true,
    59  			},
    60  			"max_size": {
    61  				Type:     schema.TypeInt,
    62  				Optional: true,
    63  				Computed: true,
    64  			},
    65  			"desired_capacity": {
    66  				Type:     schema.TypeInt,
    67  				Optional: true,
    68  				Computed: true,
    69  			},
    70  		},
    71  	}
    72  }
    73  
    74  func resourceAwsAutoscalingScheduleCreate(d *schema.ResourceData, meta interface{}) error {
    75  	autoscalingconn := meta.(*AWSClient).autoscalingconn
    76  	params := &autoscaling.PutScheduledUpdateGroupActionInput{
    77  		AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
    78  		ScheduledActionName:  aws.String(d.Get("scheduled_action_name").(string)),
    79  	}
    80  
    81  	if attr, ok := d.GetOk("start_time"); ok {
    82  		t, err := time.Parse(awsAutoscalingScheduleTimeLayout, attr.(string))
    83  		if err != nil {
    84  			return fmt.Errorf("Error Parsing AWS Autoscaling Group Schedule Start Time: %s", err.Error())
    85  		}
    86  		params.StartTime = aws.Time(t)
    87  	}
    88  
    89  	if attr, ok := d.GetOk("end_time"); ok {
    90  		t, err := time.Parse(awsAutoscalingScheduleTimeLayout, attr.(string))
    91  		if err != nil {
    92  			return fmt.Errorf("Error Parsing AWS Autoscaling Group Schedule End Time: %s", err.Error())
    93  		}
    94  		params.EndTime = aws.Time(t)
    95  	}
    96  
    97  	if attr, ok := d.GetOk("recurrence"); ok {
    98  		params.Recurrence = aws.String(attr.(string))
    99  	}
   100  
   101  	params.MinSize = aws.Int64(int64(d.Get("min_size").(int)))
   102  	params.MaxSize = aws.Int64(int64(d.Get("max_size").(int)))
   103  	params.DesiredCapacity = aws.Int64(int64(d.Get("desired_capacity").(int)))
   104  
   105  	log.Printf("[INFO] Creating Autoscaling Scheduled Action: %s", d.Get("scheduled_action_name").(string))
   106  	_, err := autoscalingconn.PutScheduledUpdateGroupAction(params)
   107  	if err != nil {
   108  		return fmt.Errorf("Error Creating Autoscaling Scheduled Action: %s", err.Error())
   109  	}
   110  
   111  	d.SetId(d.Get("scheduled_action_name").(string))
   112  
   113  	return resourceAwsAutoscalingScheduleRead(d, meta)
   114  }
   115  
   116  func resourceAwsAutoscalingScheduleRead(d *schema.ResourceData, meta interface{}) error {
   117  	sa, err, exists := resourceAwsASGScheduledActionRetrieve(d, meta)
   118  	if err != nil {
   119  		return err
   120  	}
   121  
   122  	if !exists {
   123  		log.Printf("Error retrieving Autoscaling Scheduled Actions. Removing from state")
   124  		d.SetId("")
   125  		return nil
   126  	}
   127  
   128  	d.Set("autoscaling_group_name", sa.AutoScalingGroupName)
   129  	d.Set("arn", sa.ScheduledActionARN)
   130  	d.Set("desired_capacity", sa.DesiredCapacity)
   131  	d.Set("min_size", sa.MinSize)
   132  	d.Set("max_size", sa.MaxSize)
   133  	d.Set("recurrence", sa.Recurrence)
   134  
   135  	if sa.StartTime != nil {
   136  		d.Set("start_time", sa.StartTime.Format(awsAutoscalingScheduleTimeLayout))
   137  	}
   138  
   139  	if sa.EndTime != nil {
   140  		d.Set("end_time", sa.EndTime.Format(awsAutoscalingScheduleTimeLayout))
   141  	}
   142  
   143  	return nil
   144  }
   145  
   146  func resourceAwsAutoscalingScheduleDelete(d *schema.ResourceData, meta interface{}) error {
   147  	autoscalingconn := meta.(*AWSClient).autoscalingconn
   148  
   149  	params := &autoscaling.DeleteScheduledActionInput{
   150  		AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
   151  		ScheduledActionName:  aws.String(d.Id()),
   152  	}
   153  
   154  	log.Printf("[INFO] Deleting Autoscaling Scheduled Action: %s", d.Id())
   155  	_, err := autoscalingconn.DeleteScheduledAction(params)
   156  	if err != nil {
   157  		return fmt.Errorf("Error deleting Autoscaling Scheduled Action: %s", err.Error())
   158  	}
   159  
   160  	return nil
   161  }
   162  
   163  func resourceAwsASGScheduledActionRetrieve(d *schema.ResourceData, meta interface{}) (*autoscaling.ScheduledUpdateGroupAction, error, bool) {
   164  	autoscalingconn := meta.(*AWSClient).autoscalingconn
   165  
   166  	params := &autoscaling.DescribeScheduledActionsInput{
   167  		AutoScalingGroupName: aws.String(d.Get("autoscaling_group_name").(string)),
   168  		ScheduledActionNames: []*string{aws.String(d.Id())},
   169  	}
   170  
   171  	log.Printf("[INFO] Describing Autoscaling Scheduled Action: %+v", params)
   172  	actions, err := autoscalingconn.DescribeScheduledActions(params)
   173  	if err != nil {
   174  		//A ValidationError here can mean that either the Schedule is missing OR the Autoscaling Group is missing
   175  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "ValidationError" {
   176  			log.Printf("[WARNING] %s not found, removing from state", d.Id())
   177  			d.SetId("")
   178  
   179  			return nil, nil, false
   180  		}
   181  		return nil, fmt.Errorf("Error retrieving Autoscaling Scheduled Actions: %s", err), false
   182  	}
   183  
   184  	if len(actions.ScheduledUpdateGroupActions) != 1 ||
   185  		*actions.ScheduledUpdateGroupActions[0].ScheduledActionName != d.Id() {
   186  		return nil, nil, false
   187  	}
   188  
   189  	return actions.ScheduledUpdateGroupActions[0], nil, true
   190  }