github.com/shvar/terraform@v0.6.9-0.20151215234924-3365cd2231df/builtin/providers/aws/resource_aws_autoscaling_schedule_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/autoscaling"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  )
    12  
    13  func TestAccAWSAutoscalingSchedule_basic(t *testing.T) {
    14  	var schedule autoscaling.ScheduledUpdateGroupAction
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckAWSAutoscalingScheduleDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccAWSAutoscalingScheduleConfig,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckScalingScheduleExists("aws_autoscaling_schedule.foobar", &schedule),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckScalingScheduleExists(n string, policy *autoscaling.ScheduledUpdateGroupAction) resource.TestCheckFunc {
    32  	return func(s *terraform.State) error {
    33  		rs, ok := s.RootModule().Resources[n]
    34  		if !ok {
    35  			return fmt.Errorf("Not found: %s", n)
    36  		}
    37  
    38  		autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"]
    39  		conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    40  		params := &autoscaling.DescribeScheduledActionsInput{
    41  			AutoScalingGroupName: aws.String(autoScalingGroup),
    42  			ScheduledActionNames: []*string{aws.String(rs.Primary.ID)},
    43  		}
    44  
    45  		resp, err := conn.DescribeScheduledActions(params)
    46  		if err != nil {
    47  			return err
    48  		}
    49  		if len(resp.ScheduledUpdateGroupActions) == 0 {
    50  			return fmt.Errorf("Scaling Schedule not found")
    51  		}
    52  
    53  		return nil
    54  	}
    55  }
    56  
    57  func testAccCheckAWSAutoscalingScheduleDestroy(s *terraform.State) error {
    58  	conn := testAccProvider.Meta().(*AWSClient).autoscalingconn
    59  
    60  	for _, rs := range s.RootModule().Resources {
    61  		if rs.Type != "aws_autoscaling_schedule" {
    62  			continue
    63  		}
    64  
    65  		autoScalingGroup, _ := rs.Primary.Attributes["autoscaling_group_name"]
    66  		params := &autoscaling.DescribeScheduledActionsInput{
    67  			AutoScalingGroupName: aws.String(autoScalingGroup),
    68  			ScheduledActionNames: []*string{aws.String(rs.Primary.ID)},
    69  		}
    70  
    71  		resp, err := conn.DescribeScheduledActions(params)
    72  
    73  		if err == nil {
    74  			if len(resp.ScheduledUpdateGroupActions) != 0 &&
    75  				*resp.ScheduledUpdateGroupActions[0].ScheduledActionName == rs.Primary.ID {
    76  				return fmt.Errorf("Scaling Schedule Still Exists: %s", rs.Primary.ID)
    77  			}
    78  		}
    79  	}
    80  
    81  	return nil
    82  }
    83  
    84  var testAccAWSAutoscalingScheduleConfig = fmt.Sprintf(`
    85  resource "aws_launch_configuration" "foobar" {
    86      name = "terraform-test-foobar5"
    87      image_id = "ami-21f78e11"
    88      instance_type = "t1.micro"
    89  }
    90  
    91  resource "aws_autoscaling_group" "foobar" {
    92      availability_zones = ["us-west-2a"]
    93      name = "terraform-test-foobar5"
    94      max_size = 1
    95      min_size = 1
    96      health_check_grace_period = 300
    97      health_check_type = "ELB"
    98      force_delete = true
    99      termination_policies = ["OldestInstance"]
   100      launch_configuration = "${aws_launch_configuration.foobar.name}"
   101      tag {
   102          key = "Foo"
   103          value = "foo-bar"
   104          propagate_at_launch = true
   105      }
   106  }
   107  
   108  resource "aws_autoscaling_schedule" "foobar" {
   109      scheduled_action_name = "foobar"
   110      min_size = 0
   111      max_size = 1
   112      desired_capacity = 0
   113      start_time = "2016-12-11T18:00:00Z"
   114      end_time = "2016-12-12T06:00:00Z"
   115      autoscaling_group_name = "${aws_autoscaling_group.foobar.name}"
   116  }
   117  `)