Skip to content

Commit

Permalink
Add lbaas_listeners to as_group_v1 (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo authored Nov 27, 2019
1 parent beee96c commit c0e60c8
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
59 changes: 58 additions & 1 deletion huaweicloud/resource_huaweicloud_as_group_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ func resourceASGroup() *schema.Resource {
ValidateFunc: resourceASGroupValidateListenerId,
Description: "The system supports the binding of up to three ELB listeners, the IDs of which are separated using a comma.",
},
"lbaas_listeners": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"pool_id": {
Type: schema.TypeString,
Required: true,
},
"protocol_port": {
Type: schema.TypeInt,
Required: true,
},
"weight": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
},
},
},
ForceNew: false,
},
"available_zones": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -256,6 +278,24 @@ func getAllSecurityGroups(d *schema.ResourceData, meta interface{}) []Group {
return Groups
}

func getAllLBaaSListeners(d *schema.ResourceData, meta interface{}) []groups.LBaaSListenerOpts {
var aslisteners []groups.LBaaSListenerOpts

listeners := d.Get("lbaas_listeners").([]interface{})
for _, v := range listeners {
listener := v.(map[string]interface{})
s := groups.LBaaSListenerOpts{
PoolID: listener["pool_id"].(string),
ProtocolPort: listener["protocol_port"].(int),
Weight: listener["weight"].(int),
}
aslisteners = append(aslisteners, s)
}

log.Printf("[DEBUG] getAllLBaaSListeners: %#v", aslisteners)
return aslisteners
}

func getInstancesInGroup(asClient *golangsdk.ServiceClient, groupID string, opts instances.ListOptsBuilder) ([]instances.Instance, error) {
var insList []instances.Instance
page, err := instances.List(asClient, groupID, opts).AllPages()
Expand Down Expand Up @@ -381,6 +421,8 @@ func resourceASGroupCreate(d *schema.ResourceData, meta interface{}) error {
secGroups := getAllSecurityGroups(d, meta)
asgSecGroups := expandGroups(secGroups)

asgLBaaSListeners := getAllLBaaSListeners(d, meta)

log.Printf("[DEBUG] available_zones: %#v", d.Get("available_zones"))
createOpts := groups.CreateOpts{
Name: d.Get("scaling_group_name").(string),
Expand All @@ -390,6 +432,7 @@ func resourceASGroupCreate(d *schema.ResourceData, meta interface{}) error {
MaxInstanceNumber: maxNum,
CoolDownTime: d.Get("cool_down_time").(int),
LBListenerID: d.Get("lb_listener_id").(string),
LBaaSListeners: asgLBaaSListeners,
AvailableZones: getAllAvailableZones(d),
Networks: asgNetworks,
SecurityGroup: asgSecGroups,
Expand Down Expand Up @@ -443,6 +486,7 @@ func resourceASGroupRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Retrieved ASGroup %q availablezones: %+v", d.Id(), asg.AvailableZones)
log.Printf("[DEBUG] Retrieved ASGroup %q networks: %+v", d.Id(), asg.Networks)
log.Printf("[DEBUG] Retrieved ASGroup %q secgroups: %+v", d.Id(), asg.SecurityGroups)
log.Printf("[DEBUG] Retrieved ASGroup %q lbaaslisteners: %+v", d.Id(), asg.LBaaSListeners)

// set properties based on the read info
d.Set("scaling_group_name", asg.Name)
Expand All @@ -459,6 +503,16 @@ func resourceASGroupRead(d *schema.ResourceData, meta interface{}) error {
if len(asg.Notifications) >= 1 {
d.Set("notifications", asg.Notifications)
}
if len(asg.LBaaSListeners) >= 1 {
listeners := make([]map[string]interface{}, len(asg.LBaaSListeners))
for i, listener := range asg.LBaaSListeners {
listeners[i] = make(map[string]interface{})
listeners[i]["pool_id"] = listener.PoolID
listeners[i]["protocol_port"] = listener.ProtocolPort
listeners[i]["weight"] = listener.Weight
}
d.Set("lbaas_listeners", listeners)
}

var opts instances.ListOptsBuilder
allIns, err := getInstancesInGroup(asClient, d.Id(), opts)
Expand All @@ -480,7 +534,7 @@ func resourceASGroupUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error creating HuaweiCloud autoscaling client: %s", err)
}
d.Partial(true)
if d.HasChange("min_instance_number") || d.HasChange("max_instance_number") || d.HasChange("desire_instance_number") {
if d.HasChange("min_instance_number") || d.HasChange("max_instance_number") || d.HasChange("desire_instance_number") || d.HasChange("lbaas_listeners") {
minNum := d.Get("min_instance_number").(int)
maxNum := d.Get("max_instance_number").(int)
desireNum := d.Get("desire_instance_number").(int)
Expand All @@ -498,6 +552,8 @@ func resourceASGroupUpdate(d *schema.ResourceData, meta interface{}) error {

secGroups := getAllSecurityGroups(d, meta)
asgSecGroups := expandGroups(secGroups)

asgLBaaSListeners := getAllLBaaSListeners(d, meta)
updateOpts := groups.UpdateOpts{
Name: d.Get("scaling_group_name").(string),
ConfigurationID: d.Get("scaling_configuration_id").(string),
Expand All @@ -506,6 +562,7 @@ func resourceASGroupUpdate(d *schema.ResourceData, meta interface{}) error {
MaxInstanceNumber: d.Get("max_instance_number").(int),
CoolDownTime: d.Get("cool_down_time").(int),
LBListenerID: d.Get("lb_listener_id").(string),
LBaaSListeners: asgLBaaSListeners,
AvailableZones: getAllAvailableZones(d),
Networks: asgNetworks,
SecurityGroup: asgSecGroups,
Expand Down
29 changes: 27 additions & 2 deletions huaweicloud/resource_huaweicloud_as_group_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func TestAccASV1Group_basic(t *testing.T) {
Config: testASV1Group_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckASV1GroupExists("huaweicloud_as_group_v1.hth_as_group", &asGroup),
resource.TestCheckResourceAttr(
"huaweicloud_as_group_v1.hth_as_group", "lbaas_listeners.0.protocol_port", "8080"),
),
},
},
Expand Down Expand Up @@ -91,10 +93,29 @@ resource "huaweicloud_networking_secgroup_v2" "secgroup" {
}
resource "huaweicloud_compute_keypair_v2" "hth_key" {
name = "hth_key"
name = "as_key"
public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAjpC1hwiOCCmKEWxJ4qzTTsJbKzndLo1BCz5PcwtUnflmU+gHJtWMZKpuEGVi29h0A/+ydKek1O18k10Ff+4tyFjiHDQAT9+OfgWf7+b1yK+qDip3X1C0UPMbwHlTfSGWLGZquwhvEFx9k3h/M+VtMvwR1lJ9LUyTAImnNjWG7TAIPmui30HvM2UiFEmqkr4ijq45MyX2+fLIePLRIFuu1p4whjHAQYufqyno3BS48icQb4p6iVEZPo4AE2o9oIyQvj2mx4dk5Y8CgSETOZTYDOR3rU2fZTRDRgPJDH9FWvQjF5tA0p3d9CoWWd2s6GKKbfoUIi8R/Db1BSPJwkqB jrp-hp-pc"
}
resource "huaweicloud_lb_loadbalancer_v2" "loadbalancer_1" {
name = "loadbalancer_1"
vip_subnet_id = "%s"
}
resource "huaweicloud_lb_listener_v2" "listener_1" {
name = "listener_1"
protocol = "HTTP"
protocol_port = 8080
loadbalancer_id = "${huaweicloud_lb_loadbalancer_v2.loadbalancer_1.id}"
}
resource "huaweicloud_lb_pool_v2" "pool_1" {
name = "pool_1"
protocol = "HTTP"
lb_method = "ROUND_ROBIN"
listener_id = "${huaweicloud_lb_listener_v2.listener_1.id}"
}
resource "huaweicloud_as_configuration_v1" "hth_as_config"{
scaling_configuration_name = "hth_as_config"
instance_config {
Expand All @@ -117,6 +138,10 @@ resource "huaweicloud_as_group_v1" "hth_as_group"{
security_groups {
id = "${huaweicloud_networking_secgroup_v2.secgroup.id}"
}
lbaas_listeners {
pool_id = "${huaweicloud_lb_pool_v2.pool_1.id}"
protocol_port = "${huaweicloud_lb_listener_v2.listener_1.protocol_port}"
}
vpc_id = "%s"
}
`, OS_IMAGE_ID, OS_NETWORK_ID, OS_VPC_ID)
`, OS_SUBNET_ID, OS_IMAGE_ID, OS_NETWORK_ID, OS_VPC_ID)
14 changes: 14 additions & 0 deletions website/docs/r/as_group_v1.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ The following arguments are supported:
* `lb_listener_id` - (Optional) The ELB listener IDs. The system supports up to
three ELB listeners, the IDs of which are separated using a comma (,).

* `lbaas_listeners` - (Optional) An array of one or more enhanced load balancer.
The system supports the binding of up to three load balancers. The field is
alternative to lb_listener_id. The lbaas_listeners object structure is
documented below.

* `available_zones` - (Optional) The availability zones in which to create
the instances in the autoscaling group.

Expand Down Expand Up @@ -153,6 +158,15 @@ The `security_groups` block supports:

* `id` - (Required) The UUID of the security group.

The `lbaas_listeners` block supports:

* `pool_id` - (Required) Specifies the backend ECS group ID.
* `protocol_port` - (Required) Specifies the backend protocol, which is the port on which
a backend ECS listens for traffic. The number of the port ranges from 1 to 65535.
* `weight` - (Optional) Specifies the weight, which determines the portion of requests a
backend ECS processes compared to other backend ECSs added to the same listener. The value
of this parameter ranges from 0 to 100. The default value is 1.

## Attributes Reference

The following attributes are exported:
Expand Down

0 comments on commit c0e60c8

Please sign in to comment.