github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/alicloud/resource_alicloud_ess_schedule_test.go (about) 1 package alicloud 2 3 import ( 4 "fmt" 5 "github.com/denverdino/aliyungo/common" 6 "github.com/denverdino/aliyungo/ess" 7 "github.com/hashicorp/terraform/helper/resource" 8 "github.com/hashicorp/terraform/terraform" 9 "log" 10 "testing" 11 ) 12 13 func TestAccAlicloudEssSchedule_basic(t *testing.T) { 14 var sc ess.ScheduledTaskItemType 15 16 resource.Test(t, resource.TestCase{ 17 PreCheck: func() { 18 testAccPreCheck(t) 19 }, 20 21 // module name 22 IDRefreshName: "alicloud_ess_schedule.foo", 23 24 Providers: testAccProviders, 25 CheckDestroy: testAccCheckEssScheduleDestroy, 26 Steps: []resource.TestStep{ 27 resource.TestStep{ 28 Config: testAccEssScheduleConfig, 29 Check: resource.ComposeTestCheckFunc( 30 testAccCheckEssScheduleExists( 31 "alicloud_ess_schedule.foo", &sc), 32 resource.TestCheckResourceAttr( 33 "alicloud_ess_schedule.foo", 34 "launch_time", 35 "2017-04-29T07:30Z"), 36 resource.TestCheckResourceAttr( 37 "alicloud_ess_schedule.foo", 38 "task_enabled", 39 "true"), 40 ), 41 }, 42 }, 43 }) 44 } 45 46 func testAccCheckEssScheduleExists(n string, d *ess.ScheduledTaskItemType) resource.TestCheckFunc { 47 return func(s *terraform.State) error { 48 rs, ok := s.RootModule().Resources[n] 49 if !ok { 50 return fmt.Errorf("Not found: %s", n) 51 } 52 53 if rs.Primary.ID == "" { 54 return fmt.Errorf("No ESS Schedule ID is set") 55 } 56 57 client := testAccProvider.Meta().(*AliyunClient) 58 attr, err := client.DescribeScheduleById(rs.Primary.ID) 59 log.Printf("[DEBUG] check schedule %s attribute %#v", rs.Primary.ID, attr) 60 61 if err != nil { 62 return err 63 } 64 65 if attr == nil { 66 return fmt.Errorf("Ess schedule not found") 67 } 68 69 *d = *attr 70 return nil 71 } 72 } 73 74 func testAccCheckEssScheduleDestroy(s *terraform.State) error { 75 client := testAccProvider.Meta().(*AliyunClient) 76 77 for _, rs := range s.RootModule().Resources { 78 if rs.Type != "alicloud_ess_schedule" { 79 continue 80 } 81 ins, err := client.DescribeScheduleById(rs.Primary.ID) 82 83 if ins != nil { 84 return fmt.Errorf("Error ESS schedule still exist") 85 } 86 87 // Verify the error is what we want 88 if err != nil { 89 // Verify the error is what we want 90 e, _ := err.(*common.Error) 91 if e.ErrorResponse.Code == InstanceNotfound { 92 continue 93 } 94 return err 95 } 96 } 97 98 return nil 99 } 100 101 const testAccEssScheduleConfig = ` 102 data "alicloud_images" "ecs_image" { 103 most_recent = true 104 name_regex = "^centos_6\\w{1,5}[64].*" 105 } 106 107 resource "alicloud_security_group" "tf_test_foo" { 108 name = "tf_test_foo" 109 description = "foo" 110 } 111 112 resource "alicloud_security_group_rule" "ssh-in" { 113 type = "ingress" 114 ip_protocol = "tcp" 115 nic_type = "internet" 116 policy = "accept" 117 port_range = "22/22" 118 priority = 1 119 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 120 cidr_ip = "0.0.0.0/0" 121 } 122 123 resource "alicloud_ess_scaling_group" "bar" { 124 min_size = 1 125 max_size = 1 126 scaling_group_name = "bar" 127 removal_policies = ["OldestInstance", "NewestInstance"] 128 } 129 130 resource "alicloud_ess_scaling_configuration" "foo" { 131 scaling_group_id = "${alicloud_ess_scaling_group.bar.id}" 132 133 image_id = "${data.alicloud_images.ecs_image.images.0.id}" 134 instance_type = "ecs.s2.large" 135 io_optimized = "optimized" 136 security_group_id = "${alicloud_security_group.tf_test_foo.id}" 137 } 138 139 resource "alicloud_ess_scaling_rule" "foo" { 140 scaling_group_id = "${alicloud_ess_scaling_group.bar.id}" 141 adjustment_type = "TotalCapacity" 142 adjustment_value = 2 143 cooldown = 60 144 } 145 146 resource "alicloud_ess_schedule" "foo" { 147 scheduled_action = "${alicloud_ess_scaling_rule.foo.ari}" 148 launch_time = "2017-04-29T07:30Z" 149 scheduled_task_name = "tf-foo" 150 } 151 `