Skip to content

Commit d2abb35

Browse files
committed
resource/alicloud_ess_server_group_attachment: support GWLB
1 parent 3dd5a0d commit d2abb35

3 files changed

Lines changed: 157 additions & 24 deletions

File tree

alicloud/resource_alicloud_ess_server_group_attachment.go

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ func resourceAliCloudEssServerGroupAttachment() *schema.Resource {
4040
"port": {
4141
Type: schema.TypeInt,
4242
ForceNew: true,
43-
Required: true,
43+
Optional: true,
4444
},
4545
"type": {
4646
Type: schema.TypeString,
4747
ForceNew: true,
48-
ValidateFunc: StringInSlice([]string{"ALB", "NLB"}, false),
48+
ValidateFunc: StringInSlice([]string{"ALB", "NLB", "GWLB"}, false),
4949
Required: true,
5050
},
5151
"weight": {
5252
Type: schema.TypeInt,
5353
ForceNew: true,
54-
Required: true,
54+
Optional: true,
5555
},
5656
"force_attach": {
5757
Type: schema.TypeBool,
@@ -67,18 +67,35 @@ func resourceAliyunEssServerGroupAttachmentCreate(d *schema.ResourceData, meta i
6767
typeAttribute := d.Get("type").(string)
6868
port := strconv.Itoa(formatInt(d.Get("port")))
6969

70+
if typeAttribute == "GWLB" {
71+
if _, ok := d.GetOkExists("weight"); ok {
72+
return WrapError(Error("When the type is set to GWLB, both weight and port are prohibited. Please remove the configurations for weight and port!"))
73+
}
74+
if _, ok := d.GetOkExists("port"); ok {
75+
return WrapError(Error("When the type is set to GWLB, both weight and port are prohibited. Please remove the configurations for weight and port!"))
76+
}
77+
}
78+
7079
client := meta.(*connectivity.AliyunClient)
7180
request := ess.CreateAttachServerGroupsRequest()
7281
request.RegionId = client.RegionId
7382
request.ScalingGroupId = scalingGroupId
7483
request.ForceAttach = requests.NewBoolean(d.Get("force_attach").(bool))
7584
attachScalingGroupServerGroups := make([]ess.AttachServerGroupsServerGroup, 0)
76-
attachScalingGroupServerGroups = append(attachScalingGroupServerGroups, ess.AttachServerGroupsServerGroup{
77-
ServerGroupId: serverGroupId,
78-
Port: port,
79-
Weight: strconv.Itoa(formatInt(d.Get("weight"))),
80-
Type: typeAttribute,
81-
})
85+
if typeAttribute == "GWLB" {
86+
attachScalingGroupServerGroups = append(attachScalingGroupServerGroups, ess.AttachServerGroupsServerGroup{
87+
ServerGroupId: serverGroupId,
88+
Type: typeAttribute,
89+
})
90+
} else {
91+
attachScalingGroupServerGroups = append(attachScalingGroupServerGroups, ess.AttachServerGroupsServerGroup{
92+
ServerGroupId: serverGroupId,
93+
Port: port,
94+
Weight: strconv.Itoa(formatInt(d.Get("weight"))),
95+
Type: typeAttribute,
96+
})
97+
}
98+
8299
request.ServerGroup = &attachScalingGroupServerGroups
83100
wait := incrementalWait(1*time.Second, 2*time.Second)
84101

@@ -133,14 +150,24 @@ func resourceAliyunEssServerGroupAttachmentRead(d *schema.ResourceData, meta int
133150
}
134151

135152
for _, v := range object.ServerGroups.ServerGroup {
136-
if v.ServerGroupId == serverGroupId && v.Port == formatInt(port) && v.Type == typeAttribute {
137-
d.Set("scaling_group_id", object.ScalingGroupId)
138-
d.Set("type", v.Type)
139-
d.Set("server_group_id", v.ServerGroupId)
140-
d.Set("weight", v.Weight)
141-
d.Set("port", v.Port)
142-
return nil
153+
if v.Type == "GWLB" {
154+
if v.ServerGroupId == serverGroupId && v.Type == typeAttribute {
155+
d.Set("scaling_group_id", object.ScalingGroupId)
156+
d.Set("type", v.Type)
157+
d.Set("server_group_id", v.ServerGroupId)
158+
return nil
159+
}
160+
} else {
161+
if v.ServerGroupId == serverGroupId && v.Port == formatInt(port) && v.Type == typeAttribute {
162+
d.Set("scaling_group_id", object.ScalingGroupId)
163+
d.Set("type", v.Type)
164+
d.Set("server_group_id", v.ServerGroupId)
165+
d.Set("weight", v.Weight)
166+
d.Set("port", v.Port)
167+
return nil
168+
}
143169
}
170+
144171
}
145172
return WrapErrorf(NotFoundErr("ServerGroup", d.Id()), NotFoundMsg, ProviderERROR)
146173
}
@@ -155,11 +182,18 @@ func resourceAliyunEssServerGroupAttachmentDelete(d *schema.ResourceData, meta i
155182
request.ScalingGroupId = scalingGroupId
156183
request.ForceDetach = requests.NewBoolean(d.Get("force_attach").(bool))
157184
detachScalingGroupServerGroups := make([]ess.DetachServerGroupsServerGroup, 0)
158-
detachScalingGroupServerGroups = append(detachScalingGroupServerGroups, ess.DetachServerGroupsServerGroup{
159-
ServerGroupId: serverGroupId,
160-
Port: port,
161-
Type: typeAttribute,
162-
})
185+
if typeAttribute == "GWLB" {
186+
detachScalingGroupServerGroups = append(detachScalingGroupServerGroups, ess.DetachServerGroupsServerGroup{
187+
ServerGroupId: serverGroupId,
188+
Type: typeAttribute,
189+
})
190+
} else {
191+
detachScalingGroupServerGroups = append(detachScalingGroupServerGroups, ess.DetachServerGroupsServerGroup{
192+
ServerGroupId: serverGroupId,
193+
Port: port,
194+
Type: typeAttribute,
195+
})
196+
}
163197
request.ServerGroup = &detachScalingGroupServerGroups
164198

165199
activityId := ""

alicloud/resource_alicloud_ess_server_group_attachment_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,55 @@ func TestAccAliCloudEssServerGroupAttachment_basic_nlb(t *testing.T) {
168168
})
169169
}
170170

171+
func TestAccAliCloudEssServerGroupAttachment_basic_gwlb(t *testing.T) {
172+
rand := acctest.RandIntRange(1000, 999999)
173+
resourceId := "alicloud_ess_server_group_attachment.default"
174+
checkoutSupportedRegions(t, true, connectivity.MetaTagSupportRegions)
175+
176+
basicMap := map[string]string{
177+
"scaling_group_id": CHECKSET,
178+
}
179+
ra := resourceAttrInit(resourceId, basicMap)
180+
testAccCheck := ra.resourceAttrMapUpdateSet()
181+
name := fmt.Sprintf("tf-testAccEssScalingGroupServerGroup-%d", rand)
182+
testAccConfig := resourceTestAccConfigFunc(resourceId, name, testAccEssScalingGroupServerGroupForGwlb)
183+
resource.Test(t, resource.TestCase{
184+
PreCheck: func() {
185+
testAccPreCheck(t)
186+
},
187+
// module name
188+
IDRefreshName: resourceId,
189+
190+
Providers: testAccProviders,
191+
CheckDestroy: testAccCheckEssServerGroupsDestroy,
192+
Steps: []resource.TestStep{
193+
{
194+
Config: testAccConfig(map[string]interface{}{
195+
"depends_on": []string{"alicloud_ess_scaling_configuration.default"},
196+
"force_attach": true,
197+
"type": "GWLB",
198+
"server_group_id": "${alicloud_gwlb_server_group.default.id}",
199+
"scaling_group_id": "${alicloud_ess_scaling_group.default.id}",
200+
}),
201+
Check: resource.ComposeTestCheckFunc(
202+
testAccCheck(map[string]string{
203+
"id": CHECKSET,
204+
"type": "GWLB",
205+
"server_group_id": CHECKSET,
206+
"scaling_group_id": CHECKSET,
207+
}),
208+
),
209+
},
210+
{
211+
ResourceName: resourceId,
212+
ImportState: true,
213+
ImportStateVerify: true,
214+
ImportStateVerifyIgnore: []string{"force_attach"},
215+
},
216+
},
217+
})
218+
}
219+
171220
func TestAccAliCloudEssServerGroupAttachment_nonForceAttach_nlb(t *testing.T) {
172221
rand := acctest.RandIntRange(1000, 999999)
173222
resourceId := "alicloud_ess_server_group_attachment.default"
@@ -524,3 +573,53 @@ func testAccEssScalingGroupServerGroup(name string) string {
524573
}
525574
`, EcsInstanceCommonTestCase, name)
526575
}
576+
577+
func testAccEssScalingGroupServerGroupForGwlb(name string) string {
578+
return fmt.Sprintf(`
579+
%s
580+
variable "name" {
581+
default = "%s"
582+
}
583+
584+
resource "alicloud_ess_scaling_group" "default" {
585+
min_size = "0"
586+
max_size = "2"
587+
default_cooldown = 200
588+
scaling_group_name = "${var.name}"
589+
removal_policies = ["OldestInstance"]
590+
vswitch_ids = ["${alicloud_vswitch.default.id}"]
591+
}
592+
593+
data "alicloud_images" "default2" {
594+
name_regex = "^win"
595+
most_recent = true
596+
owners = "system"
597+
}
598+
data "alicloud_instance_types" "c6" {
599+
instance_type_family = "ecs.n4"
600+
availability_zone = "${data.alicloud_zones.default.zones.0.id}"
601+
}
602+
data "alicloud_resource_manager_resource_groups" "default" {}
603+
604+
resource "alicloud_ess_scaling_configuration" "default" {
605+
scaling_group_id = alicloud_ess_scaling_group.default.id
606+
image_id = data.alicloud_images.default2.images[0].id
607+
instance_type = data.alicloud_instance_types.c6.instance_types[0].id
608+
security_group_id = alicloud_security_group.default.id
609+
force_delete = true
610+
active = true
611+
enable = true
612+
}
613+
resource "alicloud_gwlb_server_group" "default" {
614+
scheduler = "5TCH"
615+
protocol = "GENEVE"
616+
server_group_type = "Instance"
617+
connection_drain_config {
618+
connection_drain_enabled = true
619+
connection_drain_timeout = "1"
620+
}
621+
vpc_id = alicloud_vpc.default.id
622+
server_group_name = format("%%s4", var.name)
623+
}
624+
`, EcsInstanceCommonTestCase, name)
625+
}

website/docs/r/ess_server_group_attachment.html.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ The following arguments are supported:
132132

133133
* `scaling_group_id` - (Required, ForceNew) ID of the scaling group.
134134
* `server_group_id` - (Required, ForceNew) ID of Server Group.
135-
* `type` - (Required, ForceNew) The type of server group N. Valid values: ALB, NLB.
136-
* `port` - (Required, ForceNew) - The port will be used for Server Group backend server.
137-
* `weight` - (Required, ForceNew) The weight of an ECS instance attached to the Server Group.
135+
* `type` - (Required, ForceNew) The type of server group N. Valid values: ALB, NLB, GWLB.
136+
* `port` - (Required, Optional, ForceNew) - The port will be used for Server Group backend server.
137+
* `weight` - (Required, Optional, ForceNew) The weight of an ECS instance attached to the Server Group.
138138
* `force_attach` - (Optional) If instances of scaling group are attached/removed from backend server when
139139
server group from scaling group. Default to false.
140140

0 commit comments

Comments
 (0)