Skip to content

Commit

Permalink
New Resource: alicloud_ess_server_group_attachment.
Browse files Browse the repository at this point in the history
  • Loading branch information
fuliu-zln committed Sep 24, 2024
1 parent aed7d02 commit ef5fcf0
Show file tree
Hide file tree
Showing 4 changed files with 726 additions and 0 deletions.
1 change: 1 addition & 0 deletions alicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,7 @@ func Provider() terraform.ResourceProvider {
"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
204 changes: 204 additions & 0 deletions alicloud/resource_alicloud_ess_server_group_attachment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package alicloud

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"

"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ess"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceAlicloudEssServerGroupAttachment() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunEssServerGroupAttachmentCreate,
Read: resourceAliyunEssServerGroupAttachmentRead,
Update: resourceAliyunEssServerGroupAttachmentUpdate,
Delete: resourceAliyunEssServerGroupAttachmentDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Delete: schema.DefaultTimeout(5 * time.Minute),
},
Schema: map[string]*schema.Schema{
"scaling_group_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"server_group_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"port": {
Type: schema.TypeInt,
ForceNew: true,
Required: true,
},
"type": {
Type: schema.TypeString,
ForceNew: true,
ValidateFunc: StringInSlice([]string{"ALB", "NLB"}, false),
Required: true,
},
"weight": {
Type: schema.TypeInt,
ForceNew: true,
Required: true,
},
"force_attach": {
Type: schema.TypeBool,
Optional: true,
},
},
}
}

func resourceAliyunEssServerGroupAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
scalingGroupId := d.Get("scaling_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.CreateAttachServerGroupsRequest()
request.RegionId = client.RegionId
request.ScalingGroupId = scalingGroupId
request.ForceAttach = requests.NewBoolean(d.Get("force_attach").(bool))
attachScalingGroupServerGroups := make([]ess.AttachServerGroupsServerGroup, 0)
attachScalingGroupServerGroups = append(attachScalingGroupServerGroups, ess.AttachServerGroupsServerGroup{
ServerGroupId: serverGroupId,
Port: port,
Weight: strconv.Itoa(formatInt(d.Get("weight"))),
Type: typeAttribute,
})
request.ServerGroup = &attachScalingGroupServerGroups
wait := incrementalWait(1*time.Second, 2*time.Second)

var raw interface{}
var err error
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
raw, err = client.WithEssClient(func(essClient *ess.Client) (interface{}, error) {
return essClient.AttachServerGroups(request)
})
if err != nil {
if IsExpectedErrors(err, []string{"IncorrectScalingGroupStatus"}) || NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})

if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)

response, _ := raw.(*ess.AttachServerGroupsResponse)

d.SetId(fmt.Sprint(scalingGroupId, ":", serverGroupId, ":", typeAttribute, ":", port))
if len(response.ScalingActivityId) == 0 {
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 resourceAliyunEssServerGroupAttachmentRead(d, meta)
}

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 resourceAliyunEssServerGroupAttachmentRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
essService := EssService{client}
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.ServerGroups.ServerGroup {
if v.ServerGroupId == serverGroupId && v.Port == formatInt(port) && v.Type == typeAttribute {
d.Set("scaling_group_id", object.ScalingGroupId)
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("ServerGroup", d.Id())), NotFoundMsg, ProviderERROR)
}

func resourceAliyunEssServerGroupAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
request := ess.CreateDetachServerGroupsRequest()
request.RegionId = client.RegionId
strs, _ := ParseResourceId(d.Id(), 3)
scalingGroupId, serverGroupId, typeAttribute, port := strs[0], strs[1], strs[2], strs[3]

request.ScalingGroupId = scalingGroupId
request.ForceDetach = requests.NewBoolean(d.Get("force_attach").(bool))
detachScalingGroupServerGroups := make([]ess.DetachServerGroupsServerGroup, 0)
detachScalingGroupServerGroups = append(detachScalingGroupServerGroups, ess.DetachServerGroupsServerGroup{
ServerGroupId: serverGroupId,
Port: port,
Type: typeAttribute,
})
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.DetachServerGroups(request)
})
if err != nil {
if IsExpectedErrors(err, []string{"IncorrectScalingGroupStatus"}) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
response, _ := raw.(*ess.DetachServerGroupsResponse)
activityId = response.ScalingActivityId
if len(response.ScalingActivityId) == 0 {
return nil
}

addDebug(request.GetActionName(), raw, request.RpcRequest, request)
return nil
})
if err != nil {
if strings.Contains(err.Error(), "The specified value of parameter \"ScalingGroupId\" is not valid") {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
essService := EssService{client}
if activityId == "" {
return nil
}
stateConf := BuildStateConf([]string{}, []string{"Successful"}, d.Timeout(schema.TimeoutCreate), 1*time.Minute, essService.ActivityStateRefreshFunc(activityId, []string{"Failed", "Rejected"}))
if _, err := stateConf.WaitForState(); err != nil {
if strings.Contains(err.Error(), "activity not found") {
return nil
}
return WrapErrorf(err, IdMsg, d.Id())
}
return nil
}
Loading

0 comments on commit ef5fcf0

Please sign in to comment.