diff --git a/alicloud/resource_alicloud_ess_scheduled_task.go b/alicloud/resource_alicloud_ess_scheduled_task.go index 60ceda5a7fdc..3b24b990b5cd 100644 --- a/alicloud/resource_alicloud_ess_scheduled_task.go +++ b/alicloud/resource_alicloud_ess_scheduled_task.go @@ -47,7 +47,7 @@ func resourceAlicloudEssScheduledTask() *schema.Resource { Type: schema.TypeInt, Default: 600, Optional: true, - ValidateFunc: validation.IntBetween(0, 21600), + ValidateFunc: IntBetween(0, 1800), }, "min_value": { Type: schema.TypeInt, @@ -119,19 +119,25 @@ func resourceAliyunEssScheduledTaskRead(d *schema.ResourceData, meta interface{} return WrapError(err) } - d.Set("scheduled_action", object.ScheduledAction) - d.Set("launch_time", object.LaunchTime) - d.Set("scheduled_task_name", object.ScheduledTaskName) - d.Set("description", object.Description) - d.Set("launch_expiration_time", object.LaunchExpirationTime) - d.Set("recurrence_type", object.RecurrenceType) - d.Set("recurrence_value", object.RecurrenceValue) - d.Set("recurrence_end_time", object.RecurrenceEndTime) - d.Set("task_enabled", object.TaskEnabled) - d.Set("min_value", object.MinValue) - d.Set("max_value", object.MaxValue) - d.Set("desired_capacity", object.DesiredCapacity) - d.Set("scaling_group_id", object.ScalingGroupId) + d.Set("scheduled_action", object["ScheduledAction"]) + d.Set("launch_time", object["LaunchTime"]) + d.Set("scheduled_task_name", object["ScheduledTaskName"]) + d.Set("description", object["Description"]) + d.Set("launch_expiration_time", object["LaunchExpirationTime"]) + d.Set("recurrence_type", object["RecurrenceType"]) + d.Set("recurrence_value", object["RecurrenceValue"]) + d.Set("recurrence_end_time", object["RecurrenceEndTime"]) + d.Set("task_enabled", object["TaskEnabled"]) + if object["MinValue"] != nil { + d.Set("min_value", object["MinValue"]) + } + if object["MaxValue"] != nil { + d.Set("max_value", object["MaxValue"]) + } + if object["DesiredCapacity"] != nil { + d.Set("desired_capacity", object["DesiredCapacity"]) + } + d.Set("scaling_group_id", object["ScalingGroupId"]) return nil } diff --git a/alicloud/resource_alicloud_ess_scheduled_task_test.go b/alicloud/resource_alicloud_ess_scheduled_task_test.go index 371d64379b1b..2a0a67d63717 100644 --- a/alicloud/resource_alicloud_ess_scheduled_task_test.go +++ b/alicloud/resource_alicloud_ess_scheduled_task_test.go @@ -92,7 +92,7 @@ func testSweepEssSchedules(region string) error { return nil } -func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { +func TestAccAliCloudEssScheduledTask_basic(t *testing.T) { var v ess.ScheduledTask resourceId := "alicloud_ess_scheduled_task.default" ra := resourceAttrInit(resourceId, nil) @@ -105,6 +105,8 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { // Setting schedule time to more than one day oneDay, _ := time.ParseDuration("24h") rand := acctest.RandIntRange(1000, 999999) + name := fmt.Sprintf("tf-testAccEssScheduleConfig-%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScheduleConfig) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -117,9 +119,11 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { CheckDestroy: testAccCheckEssScheduledTaskDestroy, Steps: []resource.TestStep{ { - Config: testAccEssScheduleConfig(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), - + Config: testAccConfig(map[string]interface{}{ + "scheduled_action": "${alicloud_ess_scaling_rule.default.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ "scheduled_action": CHECKSET, @@ -136,8 +140,11 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccEssScheduleUpdateScheduledTaskName(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + Config: testAccConfig(map[string]interface{}{ + "scheduled_action": "${alicloud_ess_scaling_rule.default.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": fmt.Sprintf("tf-testAccEssSchedule-%d", rand), + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -145,10 +152,13 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { }), ), }, - { - Config: testAccEssScheduleUpdateDescription(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), - + { //description = "terraform test + Config: testAccConfig(map[string]interface{}{ + "scheduled_action": "${alicloud_ess_scaling_rule.default.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": fmt.Sprintf("tf-testAccEssSchedule-%d", rand), + "description": "terraform test", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ "description": "terraform test", @@ -156,8 +166,13 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { ), }, { - Config: testAccEssScheduleUpdateLaunchExpirationTime(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + Config: testAccConfig(map[string]interface{}{ + "scheduled_action": "${alicloud_ess_scaling_rule.default.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": fmt.Sprintf("tf-testAccEssSchedule-%d", rand), + "description": "terraform test", + "launch_expiration_time": "500", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -166,8 +181,16 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { ), }, { - Config: testAccEssScheduleUpdateRecurrenceType(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + Config: testAccConfig(map[string]interface{}{ + "scheduled_action": "${alicloud_ess_scaling_rule.default.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": fmt.Sprintf("tf-testAccEssSchedule-%d", rand), + "description": "terraform test", + "launch_expiration_time": "500", + "recurrence_type": "Weekly", + "recurrence_value": "0,1,2", + "recurrence_end_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -178,8 +201,17 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { ), }, { - Config: testAccEssScheduleUpdateTaskEnabled(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + Config: testAccConfig(map[string]interface{}{ + "scheduled_action": "${alicloud_ess_scaling_rule.default.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": fmt.Sprintf("tf-testAccEssSchedule-%d", rand), + "description": "terraform test", + "launch_expiration_time": "500", + "recurrence_type": "Weekly", + "recurrence_value": "0,1,2", + "recurrence_end_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "task_enabled": "false", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -187,15 +219,105 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { }), ), }, + }, + }) +} + +func TestAccAliCloudEssScheduledTask_basic_2(t *testing.T) { + var v ess.ScheduledTask + resourceId := "alicloud_ess_scheduled_task.default" + ra := resourceAttrInit(resourceId, nil) + rc := resourceCheckInit(resourceId, &v, func() interface{} { + return &EssService{testAccProvider.Meta().(*connectivity.AliyunClient)} + }) + rac := resourceAttrCheckInit(rc, ra) + + testAccCheck := rac.resourceAttrMapUpdateSet() + // Setting schedule time to more than one day + oneDay, _ := time.ParseDuration("24h") + rand := acctest.RandIntRange(1000, 999999) + name := fmt.Sprintf("tf-testAccEssScheduleConfig-%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScheduleConfig) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + + // module name + IDRefreshName: resourceId, + + Providers: testAccProviders, + CheckDestroy: testAccCheckEssScheduledTaskDestroy, + Steps: []resource.TestStep{ { - Config: testAccEssScheduleConfig(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + Config: testAccConfig(map[string]interface{}{ + "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}", + "min_value": "1", + "max_value": "5", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ + "scaling_group_id": CHECKSET, + "launch_time": CHECKSET, "scheduled_task_name": fmt.Sprintf("tf-testAccEssScheduleConfig-%d", rand), "launch_expiration_time": "600", "task_enabled": "true", + "min_value": "1", + "max_value": "5", + }), + ), + }, + { + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccConfig(map[string]interface{}{ + "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}", + "min_value": "2", + "max_value": "5", + }), + + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "min_value": "2", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}", + "min_value": "2", + "max_value": "4", + }), + + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "max_value": "4", + }), + ), + }, + { + Config: testAccConfig(map[string]interface{}{ + "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}", + "min_value": "3", + "max_value": "6", + }), + + Check: resource.ComposeTestCheckFunc( + testAccCheck(map[string]string{ + "min_value": "3", + "max_value": "6", }), ), }, @@ -203,7 +325,7 @@ func TestAccAlicloudEssScheduledTask_basic(t *testing.T) { }) } -func TestAccAlicloudEssScheduledTask_basic_2(t *testing.T) { +func TestAccAliCloudEssScheduledTask_basic_3(t *testing.T) { var v ess.ScheduledTask resourceId := "alicloud_ess_scheduled_task.default" ra := resourceAttrInit(resourceId, nil) @@ -216,6 +338,8 @@ func TestAccAlicloudEssScheduledTask_basic_2(t *testing.T) { // Setting schedule time to more than one day oneDay, _ := time.ParseDuration("24h") rand := acctest.RandIntRange(1000, 999999) + name := fmt.Sprintf("tf-testAccEssScheduleConfig-%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScheduleConfig1) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -228,8 +352,14 @@ func TestAccAlicloudEssScheduledTask_basic_2(t *testing.T) { CheckDestroy: testAccCheckEssScheduledTaskDestroy, Steps: []resource.TestStep{ { - Config: testAccEssScheduleConfig_2(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + Config: testAccConfig(map[string]interface{}{ + "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}", + "min_value": "1", + "max_value": "5", + "desired_capacity": "2", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -240,6 +370,7 @@ func TestAccAlicloudEssScheduledTask_basic_2(t *testing.T) { "task_enabled": "true", "min_value": "1", "max_value": "5", + "desired_capacity": "2", }), ), }, @@ -249,33 +380,87 @@ func TestAccAlicloudEssScheduledTask_basic_2(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccEssScheduleUpdateMin(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + Config: testAccConfig(map[string]interface{}{ + "scaling_group_id": "${alicloud_ess_scaling_group.default1.id}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:06Z"), + "scheduled_task_name": "${var.name}", + "min_value": "2", + "max_value": "4", + "desired_capacity": "3", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ - "min_value": "2", + "min_value": "2", + "launch_time": CHECKSET, + "desired_capacity": "3", + "max_value": "4", + "scaling_group_id": CHECKSET, }), ), }, - { - Config: testAccEssScheduleUpdateMax(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + }, + }) +} +func TestAccAliCloudEssScheduledTask_basic4(t *testing.T) { + var v ess.ScheduledTask + resourceId := "alicloud_ess_scheduled_task.default" + ra := resourceAttrInit(resourceId, nil) + rc := resourceCheckInit(resourceId, &v, func() interface{} { + return &EssService{testAccProvider.Meta().(*connectivity.AliyunClient)} + }) + rac := resourceAttrCheckInit(rc, ra) + + testAccCheck := rac.resourceAttrMapUpdateSet() + // Setting schedule time to more than one day + oneDay, _ := time.ParseDuration("24h") + rand := acctest.RandIntRange(1000, 999999) + name := fmt.Sprintf("tf-testAccEssScheduleConfig-%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScheduleConfig1) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + testAccPreCheck(t) + }, + + // module name + IDRefreshName: resourceId, + + Providers: testAccProviders, + CheckDestroy: testAccCheckEssScheduledTaskDestroy, + Steps: []resource.TestStep{ + { + Config: testAccConfig(map[string]interface{}{ + "scheduled_action": "${alicloud_ess_scaling_rule.default.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ - "max_value": "4", + "scheduled_action": CHECKSET, + "launch_time": CHECKSET, + "scheduled_task_name": fmt.Sprintf("tf-testAccEssScheduleConfig-%d", rand), + "launch_expiration_time": "600", + "task_enabled": "true", }), ), }, { - Config: testAccEssScheduleUpdateMinMax(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + ResourceName: resourceId, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccConfig(map[string]interface{}{ + "scheduled_action": "${alicloud_ess_scaling_rule.default1.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": fmt.Sprintf("tf-testAccEssSchedule-%d", rand), + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ - "min_value": "3", - "max_value": "6", + "scheduled_action": CHECKSET, + "scheduled_task_name": fmt.Sprintf("tf-testAccEssSchedule-%d", rand), }), ), }, @@ -283,7 +468,7 @@ func TestAccAlicloudEssScheduledTask_basic_2(t *testing.T) { }) } -func TestAccAlicloudEssScheduledTask_multi(t *testing.T) { +func TestAccAliCloudEssScheduledTask_multi(t *testing.T) { var v ess.ScheduledTask resourceId := "alicloud_ess_scheduled_task.default.9" ra := resourceAttrInit(resourceId, nil) @@ -296,6 +481,8 @@ func TestAccAlicloudEssScheduledTask_multi(t *testing.T) { // Setting schedule time to more than one day oneDay, _ := time.ParseDuration("24h") rand := acctest.RandIntRange(1000, 999999) + name := fmt.Sprintf("tf-testAccEssScheduleConfig-%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScheduleConfig) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -308,9 +495,12 @@ func TestAccAlicloudEssScheduledTask_multi(t *testing.T) { CheckDestroy: testAccCheckEssScheduledTaskDestroy, Steps: []resource.TestStep{ { - Config: testAccEssScheduleConfigMulti(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), - + Config: testAccConfig(map[string]interface{}{ + "count": "10", + "scheduled_action": "${alicloud_ess_scaling_rule.default.ari}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}-${count.index}", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ "scheduled_action": CHECKSET, @@ -325,7 +515,7 @@ func TestAccAlicloudEssScheduledTask_multi(t *testing.T) { }) } -func TestAccAlicloudEssScheduledTask_max_min_supportZero(t *testing.T) { +func TestAccAliCloudEssScheduledTask_max_min_supportZero(t *testing.T) { var v ess.ScheduledTask resourceId := "alicloud_ess_scheduled_task.default.9" ra := resourceAttrInit(resourceId, nil) @@ -336,6 +526,8 @@ func TestAccAlicloudEssScheduledTask_max_min_supportZero(t *testing.T) { testAccCheck := rac.resourceAttrMapUpdateSet() oneDay, _ := time.ParseDuration("24h") rand := acctest.RandIntRange(1000, 999999) + name := fmt.Sprintf("tf-testAccEssScheduleConfig-%d", rand) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScheduleConfig) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -348,8 +540,15 @@ func TestAccAlicloudEssScheduledTask_max_min_supportZero(t *testing.T) { CheckDestroy: testAccCheckEssScheduledTaskDestroy, Steps: []resource.TestStep{ { - Config: testAccEssScheduleConfigZero(EcsInstanceCommonTestCase, - time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), rand), + + Config: testAccConfig(map[string]interface{}{ + "count": "10", + "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", + "launch_time": time.Now().Add(oneDay).Format("2006-01-02T15:04Z"), + "scheduled_task_name": "${var.name}-${count.index}", + "min_value": "0", + "max_value": "0", + }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -386,11 +585,11 @@ func testAccCheckEssScheduledTaskDestroy(s *terraform.State) error { return nil } -func testAccEssScheduleConfig(common, scheduleTime string, rand int) string { +func testAccEssScheduleConfig(common string) string { return fmt.Sprintf(` %s variable "name" { - default = "tf-testAccEssScheduleConfig-%d" + default = "%s" } resource "alicloud_ess_scaling_group" "default" { @@ -415,60 +614,15 @@ func testAccEssScheduleConfig(common, scheduleTime string, rand int) string { adjustment_value = 2 cooldown = 60 } - - resource "alicloud_ess_scheduled_task" "default" { - scheduled_action = "${alicloud_ess_scaling_rule.default.ari}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - } - `, common, rand, scheduleTime) -} -func testAccEssScheduleConfig_2(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssScheduleConfig-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - min_value = 1 - max_value = 5 - } - `, common, rand, scheduleTime) + `, EcsInstanceCommonTestCase, common) } -func testAccEssScheduleUpdateScheduledTaskName(common, scheduleTime string, rand int) string { +func testAccEssScheduleConfig1(common string) string { return fmt.Sprintf(` %s variable "name" { - default = "tf-testAccEssSchedule-%d" + default = "%s" } resource "alicloud_ess_scaling_group" "default" { @@ -477,6 +631,7 @@ func testAccEssScheduleUpdateScheduledTaskName(common, scheduleTime string, rand scaling_group_name = "${var.name}" vswitch_ids = ["${alicloud_vswitch.default.id}"] removal_policies = ["OldestInstance", "NewestInstance"] + desired_capacity = 1 } resource "alicloud_ess_scaling_configuration" "default" { @@ -487,381 +642,35 @@ func testAccEssScheduleUpdateScheduledTaskName(common, scheduleTime string, rand force_delete = "true" } - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - scheduled_action = "${alicloud_ess_scaling_rule.default.ari}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - } - `, common, rand, scheduleTime) -} - -func testAccEssScheduleUpdateDescription(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssSchedule-%d" - } - - resource "alicloud_ess_scaling_group" "default" { + resource "alicloud_ess_scaling_group" "default1" { min_size = 1 max_size = 1 - scaling_group_name = "${var.name}" + scaling_group_name = "${var.name}-1" vswitch_ids = ["${alicloud_vswitch.default.id}"] removal_policies = ["OldestInstance", "NewestInstance"] + desired_capacity = 1 } - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" + resource "alicloud_ess_scaling_configuration" "default1" { + scaling_group_id = "${alicloud_ess_scaling_group.default1.id}" image_id = "${data.alicloud_images.default.images.0.id}" instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" security_group_id = "${alicloud_security_group.default.id}" force_delete = "true" } - resource "alicloud_ess_scaling_rule" "default" { scaling_group_id = "${alicloud_ess_scaling_group.default.id}" adjustment_type = "TotalCapacity" adjustment_value = 2 cooldown = 60 } - - resource "alicloud_ess_scheduled_task" "default" { - scheduled_action = "${alicloud_ess_scaling_rule.default.ari}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - description = "terraform test" - } - `, common, rand, scheduleTime) -} -func testAccEssScheduleUpdateMin(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssScheduleConfig-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" + resource "alicloud_ess_scaling_rule" "default1" { + scaling_group_id = "${alicloud_ess_scaling_group.default1.id}" adjustment_type = "TotalCapacity" - adjustment_value = 2 + adjustment_value = 3 cooldown = 60 } - - resource "alicloud_ess_scheduled_task" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - min_value = 2 - max_value = 5 - } - `, common, rand, scheduleTime) -} -func testAccEssScheduleUpdateMax(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssScheduleConfig-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - min_value = 2 - max_value = 4 - } - `, common, rand, scheduleTime) -} - -func testAccEssScheduleUpdateMinMax(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssScheduleConfig-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - min_value = 3 - max_value = 6 - } - `, common, rand, scheduleTime) -} - -func testAccEssScheduleUpdateLaunchExpirationTime(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssSchedule-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - scheduled_action = "${alicloud_ess_scaling_rule.default.ari}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - description = "terraform test" - launch_expiration_time = 500 - } - `, common, rand, scheduleTime) -} -func testAccEssScheduleUpdateRecurrenceType(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssSchedule-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - scheduled_action = "${alicloud_ess_scaling_rule.default.ari}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - description = "terraform test" - launch_expiration_time = 500 - recurrence_type = "Weekly" - recurrence_value = "0,1,2" - recurrence_end_time = "%s" - } - `, common, rand, scheduleTime, scheduleTime) -} - -func testAccEssScheduleUpdateTaskEnabled(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssSchedule-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - scheduled_action = "${alicloud_ess_scaling_rule.default.ari}" - launch_time = "%s" - scheduled_task_name = "${var.name}" - description = "terraform test" - launch_expiration_time = 500 - recurrence_type = "Weekly" - recurrence_value = "0,1,2" - recurrence_end_time = "%s" - task_enabled = false - } - `, common, rand, scheduleTime, scheduleTime) -} -func testAccEssScheduleConfigMulti(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssScheduleConfig-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - count = 10 - scheduled_action = "${alicloud_ess_scaling_rule.default.ari}" - launch_time = "%s" - scheduled_task_name = "${var.name}-${count.index}" - } - `, common, rand, scheduleTime) -} - -func testAccEssScheduleConfigZero(common, scheduleTime string, rand int) string { - return fmt.Sprintf(` - %s - variable "name" { - default = "tf-testAccEssScheduleConfig-%d" - } - - resource "alicloud_ess_scaling_group" "default" { - min_size = 1 - max_size = 1 - scaling_group_name = "${var.name}" - vswitch_ids = ["${alicloud_vswitch.default.id}"] - removal_policies = ["OldestInstance", "NewestInstance"] - } - - resource "alicloud_ess_scaling_configuration" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - image_id = "${data.alicloud_images.default.images.0.id}" - instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}" - security_group_id = "${alicloud_security_group.default.id}" - force_delete = "true" - } - - resource "alicloud_ess_scaling_rule" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - adjustment_type = "TotalCapacity" - adjustment_value = 2 - cooldown = 60 - } - - resource "alicloud_ess_scheduled_task" "default" { - scaling_group_id = "${alicloud_ess_scaling_group.default.id}" - count = 10 - launch_time = "%s" - scheduled_task_name = "${var.name}-${count.index}" - min_value = 0 - max_value = 0 - } - `, common, rand, scheduleTime) + `, EcsInstanceCommonTestCase, common) } diff --git a/alicloud/service_alicloud_ess.go b/alicloud/service_alicloud_ess.go index 8f3474b9e3a4..c6c32e38f85c 100644 --- a/alicloud/service_alicloud_ess.go +++ b/alicloud/service_alicloud_ess.go @@ -586,27 +586,43 @@ func (s *EssService) WaitForEssScalingRule(id string, status Status, timeout int } } -func (s *EssService) DescribeEssScheduledTask(id string) (task ess.ScheduledTask, err error) { - request := ess.CreateDescribeScheduledTasksRequest() - request.ScheduledTaskId = &[]string{id} - request.RegionId = s.client.RegionId - raw, err := s.client.WithEssClient(func(essClient *ess.Client) (interface{}, error) { - return essClient.DescribeScheduledTasks(request) - }) +func (s *EssService) DescribeEssScheduledTask(id string) (object map[string]interface{}, err error) { + + var response map[string]interface{} + conn, err := s.client.NewEssClient() if err != nil { - return task, WrapErrorf(err, DefaultErrorMsg, id, request.GetActionName(), AlibabaCloudSdkGoERROR) + return nil, WrapError(err) + } + request := map[string]interface{}{ + "ScheduledTaskId.1": id, + "RegionId": s.client.RegionId, + } + runtime := util.RuntimeOptions{} + runtime.SetAutoretry(true) + response, err = conn.DoRequest(StringPointer("DescribeScheduledTasks"), nil, StringPointer("POST"), StringPointer("2014-08-28"), StringPointer("AK"), nil, request, &runtime) + if err != nil { + return object, WrapErrorf(err, DefaultErrorMsg, id, "DescribeScheduledTasks", AlibabaCloudSdkGoERROR) + } + + addDebug("DescribeScheduledTasks", response, request, request) + + v, err := jsonpath.Get("$.ScheduledTasks.ScheduledTask", response) + if err != nil { + return object, WrapErrorf(err, FailedGetAttributeMsg, id, "$.ScheduledTasks.ScheduledTask", response) + } + if len(v.([]interface{})) == 0 { + return object, WrapErrorf(Error(GetNotFoundMessage("ScheduledTasks", id)), NotFoundMsg, ProviderERROR) } - addDebug(request.GetActionName(), raw, request.RpcRequest, request) - response, _ := raw.(*ess.DescribeScheduledTasksResponse) - for _, v := range response.ScheduledTasks.ScheduledTask { - if v.ScheduledTaskId == id { - task = v - return + for _, w := range v.([]interface{}) { + m := w.(map[string]interface{}) + if m["ScheduledTaskId"] == id { + return m, nil } } - err = WrapErrorf(Error(GetNotFoundMessage("EssSchedule", id)), NotFoundMsg, ProviderERROR) + err = WrapErrorf(Error(GetNotFoundMessage("EssScheduledTask", id)), NotFoundMsg, ProviderERROR) return + } func (s *EssService) WaitForEssScheduledTask(id string, status Status, timeout int) error { @@ -623,13 +639,13 @@ func (s *EssService) WaitForEssScheduledTask(id string, status Status, timeout i } } - if object.TaskEnabled { + if object["TaskEnabled"] == "true" { return nil } time.Sleep(DefaultIntervalShort * time.Second) if time.Now().After(deadline) { - return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, object.ScheduledTaskId, id, ProviderERROR) + return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, object["ScheduledTaskId"], id, ProviderERROR) } } } diff --git a/website/docs/r/ess_scheduled_task.html.markdown b/website/docs/r/ess_scheduled_task.html.markdown index 593d7b50357a..c8bc49d4006d 100644 --- a/website/docs/r/ess_scheduled_task.html.markdown +++ b/website/docs/r/ess_scheduled_task.html.markdown @@ -133,7 +133,7 @@ The following arguments are supported: The time must be in UTC. You cannot enter a time point later than 90 days from the date of scheduled task creation. If the `recurrence_type` parameter is specified, the task is executed repeatedly at the time specified by LaunchTime. Otherwise, the task is only executed once at the date and time specified by LaunchTime. -* `launch_expiration_time` - (Optional) The time period during which a failed scheduled task is retried. Unit: seconds. Valid values: 0 to 21600. Default value: 600 +* `launch_expiration_time` - (Optional) The time period during which a failed scheduled task is retried. Unit: seconds. Valid values: 0 to 1800, Available in 1.239.0. Default value: 600 * `recurrence_type` - (Optional) Specifies the recurrence type of the scheduled task. **NOTE:** You must specify `RecurrenceType`, `RecurrenceValue`, and `RecurrenceEndTime` at the same time. Valid values: - Daily: The scheduled task is executed once every specified number of days. - Weekly: The scheduled task is executed on each specified day of a week.