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