Skip to content

Commit

Permalink
resource/alicloud_ess_alb_server_group_attachment: rename alicloud_es…
Browse files Browse the repository at this point in the history
…s_server_group_attachment and add attribute of type.
  • Loading branch information
fuliu-zln committed Sep 23, 2024
1 parent 56dc53e commit 1b69c2e
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 81 deletions.
2 changes: 1 addition & 1 deletion alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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) {
Expand All @@ -92,75 +99,77 @@ 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"}) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
response, _ := raw.(*ess.DetachAlbServerGroupsResponse)
response, _ := raw.(*ess.DetachServerGroupsResponse)
activityId = response.ScalingActivityId
if len(response.ScalingActivityId) == 0 {
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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{
Expand All @@ -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)
Expand All @@ -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{
Expand All @@ -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 {
Expand All @@ -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" {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 1b69c2e

Please sign in to comment.