diff --git a/alicloud/provider.go b/alicloud/provider.go index 8d0cd553b417..f60e97248457 100644 --- a/alicloud/provider.go +++ b/alicloud/provider.go @@ -1058,7 +1058,7 @@ func Provider() terraform.ResourceProvider { "alicloud_ess_notification": resourceAlicloudEssNotification(), "alicloud_ess_alarm": resourceAlicloudEssAlarm(), "alicloud_ess_scalinggroup_vserver_groups": resourceAlicloudEssScalingGroupVserverGroups(), - "alicloud_ess_alb_server_group_attachment": resourceAlicloudEssAlbServerGroupAttachment(), + "alicloud_ess_server_group_attachment": resourceAlicloudEssServerGroupAttachment(), "alicloud_vpc": resourceAliCloudVpcVpc(), "alicloud_nat_gateway": resourceAlicloudNatGateway(), "alicloud_nas_file_system": resourceAlicloudNasFileSystem(), diff --git a/alicloud/resource_alicloud_ess_alb_server_group_attachment.go b/alicloud/resource_alicloud_ess_server_group_attachment.go similarity index 59% rename from alicloud/resource_alicloud_ess_alb_server_group_attachment.go rename to alicloud/resource_alicloud_ess_server_group_attachment.go index 501081209c02..d4794d74a4b7 100644 --- a/alicloud/resource_alicloud_ess_alb_server_group_attachment.go +++ b/alicloud/resource_alicloud_ess_server_group_attachment.go @@ -14,12 +14,12 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) -func resourceAlicloudEssAlbServerGroupAttachment() *schema.Resource { +func resourceAlicloudEssServerGroupAttachment() *schema.Resource { return &schema.Resource{ - Create: resourceAliyunEssAlbServerGroupAttachmentCreate, - Read: resourceAliyunEssAlbServerGroupAttachmentRead, - Update: resourceAliyunEssAlbServerGroupAttachmentUpdate, - Delete: resourceAliyunEssAlbServerGroupAttachmentDelete, + Create: resourceAliyunEssServerGroupAttachmentCreate, + Read: resourceAliyunEssServerGroupAttachmentRead, + Update: resourceAliyunEssServerGroupAttachmentUpdate, + Delete: resourceAliyunEssServerGroupAttachmentDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -29,7 +29,7 @@ func resourceAlicloudEssAlbServerGroupAttachment() *schema.Resource { ForceNew: true, Required: true, }, - "alb_server_group_id": { + "server_group_id": { Type: schema.TypeString, ForceNew: true, Required: true, @@ -39,6 +39,11 @@ func resourceAlicloudEssAlbServerGroupAttachment() *schema.Resource { ForceNew: true, Required: true, }, + "type": { + Type: schema.TypeString, + ForceNew: true, + Required: true, + }, "weight": { Type: schema.TypeInt, ForceNew: true, @@ -52,30 +57,32 @@ func resourceAlicloudEssAlbServerGroupAttachment() *schema.Resource { } } -func resourceAliyunEssAlbServerGroupAttachmentCreate(d *schema.ResourceData, meta interface{}) error { +func resourceAliyunEssServerGroupAttachmentCreate(d *schema.ResourceData, meta interface{}) error { scalingGroupId := d.Get("scaling_group_id").(string) - albServerGroupId := d.Get("alb_server_group_id").(string) + serverGroupId := d.Get("server_group_id").(string) + typeAttribute := d.Get("type").(string) port := strconv.Itoa(formatInt(d.Get("port"))) client := meta.(*connectivity.AliyunClient) - request := ess.CreateAttachAlbServerGroupsRequest() + request := ess.CreateAttachServerGroupsRequest() request.RegionId = client.RegionId request.ScalingGroupId = scalingGroupId request.ForceAttach = requests.NewBoolean(d.Get("force_attach").(bool)) - attachScalingGroupAlbServerGroups := make([]ess.AttachAlbServerGroupsAlbServerGroup, 0) - attachScalingGroupAlbServerGroups = append(attachScalingGroupAlbServerGroups, ess.AttachAlbServerGroupsAlbServerGroup{ - AlbServerGroupId: albServerGroupId, - Port: port, - Weight: strconv.Itoa(formatInt(d.Get("weight"))), + attachScalingGroupServerGroups := make([]ess.AttachServerGroupsServerGroup, 0) + attachScalingGroupServerGroups = append(attachScalingGroupServerGroups, ess.AttachServerGroupsServerGroup{ + ServerGroupId: serverGroupId, + Port: port, + Weight: strconv.Itoa(formatInt(d.Get("weight"))), + Type: typeAttribute, }) - request.AlbServerGroup = &attachScalingGroupAlbServerGroups + request.ServerGroup = &attachScalingGroupServerGroups wait := incrementalWait(1*time.Second, 2*time.Second) var raw interface{} var err error err = resource.Retry(5*time.Minute, func() *resource.RetryError { raw, err = client.WithEssClient(func(essClient *ess.Client) (interface{}, error) { - return essClient.AttachAlbServerGroups(request) + return essClient.AttachServerGroups(request) }) if err != nil { if IsExpectedErrors(err, []string{"IncorrectScalingGroupStatus"}) || NeedRetry(err) { @@ -92,67 +99,69 @@ func resourceAliyunEssAlbServerGroupAttachmentCreate(d *schema.ResourceData, met } addDebug(request.GetActionName(), raw, request.RpcRequest, request) - response, _ := raw.(*ess.AttachAlbServerGroupsResponse) + response, _ := raw.(*ess.AttachServerGroupsResponse) - d.SetId(fmt.Sprint(scalingGroupId, ":", albServerGroupId, ":", port)) + d.SetId(fmt.Sprint(scalingGroupId, ":", serverGroupId, ":", typeAttribute, ":", port)) if len(response.ScalingActivityId) == 0 { - return resourceAliyunEssAlbServerGroupAttachmentRead(d, meta) + return resourceAliyunEssServerGroupAttachmentRead(d, meta) } essService := EssService{client} stateConf := BuildStateConf([]string{}, []string{"Successful"}, d.Timeout(schema.TimeoutCreate), 1*time.Minute, essService.ActivityStateRefreshFunc(response.ScalingActivityId, []string{"Failed", "Rejected"})) if _, err := stateConf.WaitForState(); err != nil { return WrapErrorf(err, IdMsg, d.Id()) } - return resourceAliyunEssAlbServerGroupAttachmentRead(d, meta) + return resourceAliyunEssServerGroupAttachmentRead(d, meta) } -func resourceAliyunEssAlbServerGroupAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { - return WrapErrorf(Error("alb_server_group_attachment not support modify operation"), DefaultErrorMsg, "alicloud_ess_alb_server_groups", "Modify", AlibabaCloudSdkGoERROR) +func resourceAliyunEssServerGroupAttachmentUpdate(d *schema.ResourceData, meta interface{}) error { + return WrapErrorf(Error("server_group_attachment not support modify operation"), DefaultErrorMsg, "alicloud_ess_server_groups", "Modify", AlibabaCloudSdkGoERROR) } -func resourceAliyunEssAlbServerGroupAttachmentRead(d *schema.ResourceData, meta interface{}) error { +func resourceAliyunEssServerGroupAttachmentRead(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) essService := EssService{client} - strs, _ := ParseResourceId(d.Id(), 3) - scalingGroupId, albServerGroupId, port := strs[0], strs[1], strs[2] + strs, _ := ParseResourceId(d.Id(), 4) + scalingGroupId, serverGroupId, typeAttribute, port := strs[0], strs[1], strs[2], strs[3] object, err := essService.DescribeEssScalingGroup(scalingGroupId) if err != nil { return WrapError(err) } - for _, v := range object.AlbServerGroups.AlbServerGroup { - if v.AlbServerGroupId == albServerGroupId && v.Port == formatInt(port) { + for _, v := range object.ServerGroups.ServerGroup { + if v.ServerGroupId == serverGroupId && v.Port == formatInt(port) && v.Type == typeAttribute { d.Set("scaling_group_id", object.ScalingGroupId) - d.Set("alb_server_group_id", v.AlbServerGroupId) + d.Set("type", v.Type) + d.Set("server_group_id", v.ServerGroupId) d.Set("weight", v.Weight) d.Set("port", v.Port) return nil } } - return WrapErrorf(Error(GetNotFoundMessage("AlbServerGroup", d.Id())), NotFoundMsg, ProviderERROR) + return WrapErrorf(Error(GetNotFoundMessage("ServerGroup", d.Id())), NotFoundMsg, ProviderERROR) } -func resourceAliyunEssAlbServerGroupAttachmentDelete(d *schema.ResourceData, meta interface{}) error { +func resourceAliyunEssServerGroupAttachmentDelete(d *schema.ResourceData, meta interface{}) error { client := meta.(*connectivity.AliyunClient) - request := ess.CreateDetachAlbServerGroupsRequest() + request := ess.CreateDetachServerGroupsRequest() request.RegionId = client.RegionId strs, _ := ParseResourceId(d.Id(), 3) - scalingGroupId, albServerGroupId, port := strs[0], strs[1], strs[2] + scalingGroupId, serverGroupId, typeAttribute, port := strs[0], strs[1], strs[2], strs[3] request.ScalingGroupId = scalingGroupId request.ForceDetach = requests.NewBoolean(d.Get("force_attach").(bool)) - detachScalingGroupAlbServerGroups := make([]ess.DetachAlbServerGroupsAlbServerGroup, 0) - detachScalingGroupAlbServerGroups = append(detachScalingGroupAlbServerGroups, ess.DetachAlbServerGroupsAlbServerGroup{ - AlbServerGroupId: albServerGroupId, - Port: port, + detachScalingGroupServerGroups := make([]ess.DetachServerGroupsServerGroup, 0) + detachScalingGroupServerGroups = append(detachScalingGroupServerGroups, ess.DetachServerGroupsServerGroup{ + ServerGroupId: serverGroupId, + Port: port, + Type: typeAttribute, }) - request.AlbServerGroup = &detachScalingGroupAlbServerGroups + request.ServerGroup = &detachScalingGroupServerGroups activityId := "" err := resource.Retry(client.GetRetryTimeout(d.Timeout(schema.TimeoutDelete)), func() *resource.RetryError { raw, err := client.WithEssClient(func(essClient *ess.Client) (interface{}, error) { - return essClient.DetachAlbServerGroups(request) + return essClient.DetachServerGroups(request) }) if err != nil { if IsExpectedErrors(err, []string{"IncorrectScalingGroupStatus"}) { @@ -160,7 +169,7 @@ func resourceAliyunEssAlbServerGroupAttachmentDelete(d *schema.ResourceData, met } return resource.NonRetryableError(err) } - response, _ := raw.(*ess.DetachAlbServerGroupsResponse) + response, _ := raw.(*ess.DetachServerGroupsResponse) activityId = response.ScalingActivityId if len(response.ScalingActivityId) == 0 { return nil diff --git a/alicloud/resource_alicloud_ess_alb_server_group_attachment_test.go b/alicloud/resource_alicloud_ess_server_group_attachment_test.go similarity index 67% rename from alicloud/resource_alicloud_ess_alb_server_group_attachment_test.go rename to alicloud/resource_alicloud_ess_server_group_attachment_test.go index 2237a658ffa4..63302eb04691 100644 --- a/alicloud/resource_alicloud_ess_alb_server_group_attachment_test.go +++ b/alicloud/resource_alicloud_ess_server_group_attachment_test.go @@ -10,15 +10,15 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/terraform" ) -func TestAccAliCloudEssAlbServerGroupAttachment_basic(t *testing.T) { +func TestAccAliCloudEssServerGroupAttachment_basic(t *testing.T) { rand := acctest.RandIntRange(1000, 999999) - resourceId := "alicloud_ess_alb_server_group_attachment.default" + resourceId := "alicloud_ess_server_group_attachment.default" basicMap := map[string]string{ "scaling_group_id": CHECKSET, } ra := resourceAttrInit(resourceId, basicMap) testAccCheck := ra.resourceAttrMapUpdateSet() - name := fmt.Sprintf("tf-testAccEssScalingGroupAlbServerGroup-%d", rand) + name := fmt.Sprintf("tf-testAccEssScalingGroupServerGroup-%d", rand) testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScalingGroupAlbServerGroup) resource.Test(t, resource.TestCase{ PreCheck: func() { @@ -28,16 +28,17 @@ func TestAccAliCloudEssAlbServerGroupAttachment_basic(t *testing.T) { IDRefreshName: resourceId, Providers: testAccProviders, - CheckDestroy: testAccCheckEssAlbServerGroupsDestroy, + CheckDestroy: testAccCheckEssServerGroupsDestroy, Steps: []resource.TestStep{ { Config: testAccConfig(map[string]interface{}{ - "depends_on": []string{"alicloud_ess_scaling_configuration.default"}, - "force_attach": true, - "weight": "100", - "port": "80", - "alb_server_group_id": "${alicloud_alb_server_group.default.id}", - "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", + "depends_on": []string{"alicloud_ess_scaling_configuration.default"}, + "force_attach": true, + "weight": "100", + "port": "80", + "type": "ALB", + "server_group_id": "${alicloud_alb_server_group.default.id}", + "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -55,16 +56,16 @@ func TestAccAliCloudEssAlbServerGroupAttachment_basic(t *testing.T) { }) } -func TestAccAliCloudEssAlbServerGroupAttachment_nonForceAttach(t *testing.T) { +func TestAccAliCloudEssServerGroupAttachment_nonForceAttach(t *testing.T) { rand := acctest.RandIntRange(1000, 999999) - resourceId := "alicloud_ess_alb_server_group_attachment.default" + resourceId := "alicloud_ess_server_group_attachment.default" basicMap := map[string]string{ "scaling_group_id": CHECKSET, } ra := resourceAttrInit(resourceId, basicMap) testAccCheck := ra.resourceAttrMapUpdateSet() name := fmt.Sprintf("tf-testAccEssScalingGroupAlbServerGroup-%d", rand) - testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScalingGroupAlbServerGroupNotForceAttach) + testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScalingGroupServerGroupNotForceAttach) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) @@ -73,16 +74,17 @@ func TestAccAliCloudEssAlbServerGroupAttachment_nonForceAttach(t *testing.T) { IDRefreshName: resourceId, Providers: testAccProviders, - CheckDestroy: testAccCheckEssAlbServerGroupsDestroy, + CheckDestroy: testAccCheckEssServerGroupsDestroy, Steps: []resource.TestStep{ { Config: testAccConfig(map[string]interface{}{ - "depends_on": []string{"alicloud_ess_scaling_configuration.default"}, - "force_attach": false, - "weight": "11", - "port": "22", - "alb_server_group_id": "${alicloud_alb_server_group.default.id}", - "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", + "depends_on": []string{"alicloud_ess_scaling_configuration.default"}, + "force_attach": false, + "weight": "11", + "port": "22", + "type": "ALB", + "server_group_id": "${alicloud_alb_server_group.default.id}", + "scaling_group_id": "${alicloud_ess_scaling_group.default.id}", }), Check: resource.ComposeTestCheckFunc( testAccCheck(map[string]string{ @@ -100,7 +102,7 @@ func TestAccAliCloudEssAlbServerGroupAttachment_nonForceAttach(t *testing.T) { }) } -func testAccCheckEssAlbServerGroupsDestroy(s *terraform.State) error { +func testAccCheckEssServerGroupsDestroy(s *terraform.State) error { client := testAccProvider.Meta().(*connectivity.AliyunClient) essService := EssService{client} for _, rs := range s.RootModule().Resources { @@ -116,13 +118,13 @@ func testAccCheckEssAlbServerGroupsDestroy(s *terraform.State) error { return WrapError(err) } - if len(scalingGroup.AlbServerGroups.AlbServerGroup) > 0 { + if len(scalingGroup.ServerGroups.ServerGroup) > 0 { return WrapError(fmt.Errorf("There are still attached alb server groups.")) } } return nil } -func testAccEssScalingGroupAlbServerGroupNotForceAttach(name string) string { +func testAccEssScalingGroupServerGroupNotForceAttach(name string) string { return fmt.Sprintf(` %s variable "name" { @@ -137,11 +139,19 @@ func testAccEssScalingGroupAlbServerGroupNotForceAttach(name string) string { removal_policies = ["OldestInstance"] vswitch_ids = ["${alicloud_vswitch.default.id}"] } - + data "alicloud_images" "default2" { + name_regex = "^aliyun" + most_recent = true + owners = "system" + } + data "alicloud_instance_types" "c6" { + instance_type_family = "ecs.c6" + availability_zone = "${data.alicloud_zones.default.zones.0.id}" + } 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 = "ecs.f1-c8f1.2xlarge" + image_id = data.alicloud_images.default2.images[0].id + instance_type = data.alicloud_instance_types.c6.instance_types[0].id security_group_id = alicloud_security_group.default.id force_delete = true active = true @@ -179,10 +189,20 @@ func testAccEssScalingGroupAlbServerGroup(name string) string { vswitch_ids = ["${alicloud_vswitch.default.id}"] } + data "alicloud_images" "default2" { + name_regex = "^aliyun" + most_recent = true + owners = "system" + } + data "alicloud_instance_types" "c6" { + instance_type_family = "ecs.c6" + availability_zone = "${data.alicloud_zones.default.zones.0.id}" + } + 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 = "ecs.f1-c8f1.2xlarge" + image_id = data.alicloud_images.default2.images[0].id + instance_type = data.alicloud_instance_types.c6.instance_types[0].id security_group_id = alicloud_security_group.default.id force_delete = true active = true diff --git a/website/docs/r/ess_alb_server_group_attachment.html.markdown b/website/docs/r/ess_server_group_attachment.html.markdown similarity index 76% rename from website/docs/r/ess_alb_server_group_attachment.html.markdown rename to website/docs/r/ess_server_group_attachment.html.markdown index c75371e96d08..91feee8fa46c 100644 --- a/website/docs/r/ess_alb_server_group_attachment.html.markdown +++ b/website/docs/r/ess_server_group_attachment.html.markdown @@ -7,24 +7,24 @@ description: |- Provides a ESS Attachment resource to attach or remove alb server group. --- -# alicloud_ess_alb_server_group_attachment +# alicloud_ess_server_group_attachment -Attaches/Detaches alb server group to a specified scaling group. +Attaches/Detaches server group to a specified scaling group. -For information about alb server group attachment, see [AttachAlbServerGroups](https://www.alibabacloud.com/help/en/doc-detail/266800.html). +For information about server group attachment, see [AttachServerGroups](https://www.alibabacloud.com/help/en/auto-scaling/developer-reference/api-attachservergroups). -> **NOTE:** If scaling group's network type is `VPC`, the alb server groups must be in the same `VPC`. --> **NOTE:** Alb server group attachment is defined uniquely by `scaling_group_id`, `alb_server_group_id`, `port`. +-> **NOTE:** Alb server group attachment is defined uniquely by `scaling_group_id`, `server_group_id`,`type`, `port`. --> **NOTE:** Resource `alicloud_ess_alb_server_group_attachment` don't support modification. +-> **NOTE:** Resource `alicloud_ess_server_group_attachment` don't support modification. --> **NOTE:** Available since v1.158.0. +-> **NOTE:** Available since v1.231.0. ## Example Usage