Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ELB v3 pools and members #1150

Merged
merged 1 commit into from
May 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/resources/elb_member.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
subcategory: "Dedicated Load Balance (Dedicated ELB)"
---

# huaweicloud\_elb\_member

Manages an ELB member resource within HuaweiCloud.

## Example Usage

```hcl
resource "huaweicloud_elb_member" "member_1" {
address = "192.168.199.23"
protocol_port = 8080
pool_id = var.pool_id
subnet_id = var.subnet_id
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) The region in which to create the ELB member resource.
If omitted, the the provider-level region will be used.
Changing this creates a new member.

* `pool_id` - (Required, String, ForceNew) The id of the pool that this member will be
assigned to.

* `subnet_id` - (Required, String, ForceNew) The subnet in which to access the member

* `name` - (Optional, String) Human-readable name for the member.

* `address` - (Required, String, ForceNew) The IP address of the member to receive traffic from
the load balancer. Changing this creates a new member.

* `protocol_port` - (Required, Int, ForceNew) The port on which to listen for client traffic.
Changing this creates a new member.

* `weight` - (Optional, Int) A positive integer value that indicates the relative
portion of traffic that this member should receive from the pool. For
example, a member with a weight of 10 receives five times as much traffic
as a member with a weight of 2.


## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The unique ID for the member.

## Timeouts
This resource provides the following timeouts configuration options:
- `create` - Default is 10 minute.
- `update` - Default is 10 minute.
- `delete` - Default is 10 minute.
79 changes: 79 additions & 0 deletions docs/resources/elb_pool.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
subcategory: "Dedicated Load Balance (Dedicated ELB)"
---

# huaweicloud\_elb\_pool

Manages an ELB pool resource within HuaweiCloud.

## Example Usage

```hcl
resource "huaweicloud_elb_pool" "pool_1" {
protocol = "HTTP"
lb_method = "ROUND_ROBIN"
listener_id = "{{ listener_id }}"

persistence {
type = "HTTP_COOKIE"
cookie_name = "testCookie"
}
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) The region in which to create the ELB pool resource.
If omitted, the the provider-level region will be used.
Changing this creates a new pool.

* `name` - (Optional, String) Human-readable name for the pool.

* `description` - (Optional, String) Human-readable description for the pool.

* `protocol` - (Required, String, ForceNew) The protocol - can either be TCP, UDP or HTTP.

- When the protocol used by the listener is UDP, the protocol of the backend pool must be UDP.
- When the protocol used by the listener is TCP, the protocol of the backend pool must be TCP.
- When the protocol used by the listener is HTTP or TERMINATED_HTTPS, the protocol of the backend pool must be HTTP.

Changing this creates a new pool.

* `loadbalancer_id` - (Optional, String, ForceNew) The load balancer on which to provision this
pool. Changing this creates a new pool.
Note: One of LoadbalancerID or ListenerID must be provided.

* `listener_id` - (Optional, String, ForceNew) The Listener on which the members of the pool
will be associated with. Changing this creates a new pool.
Note: One of LoadbalancerID or ListenerID must be provided.

* `lb_method` - (Required, String) The load balancing algorithm to
distribute traffic to the pool's members. Must be one of
ROUND_ROBIN, LEAST_CONNECTIONS, or SOURCE_IP.

* `persistence` - (Optional, List, ForceNew) Omit this field to prevent session persistence. Indicates
whether connections in the same session will be processed by the same Pool
member or not. Changing this creates a new pool.

The `persistence` argument supports:

* `type` - (Required, String, ForceNew) The type of persistence mode. The current specification
supports SOURCE_IP, HTTP_COOKIE, and APP_COOKIE.

* `cookie_name` - (Optional, String, ForceNew) The name of the cookie if persistence mode is set
appropriately. Required if `type = APP_COOKIE`.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The unique ID for the pool.

## Timeouts
This resource provides the following timeouts configuration options:
- `create` - Default is 10 minute.
- `update` - Default is 10 minute.
- `delete` - Default is 10 minute.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-multierror v1.0.0
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/huaweicloud/golangsdk v0.0.0-20210519120215-6602b8a10c02
github.com/huaweicloud/golangsdk v0.0.0-20210521045610-c7bf719c8e93
github.com/jen20/awspolicyequivalence v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
github.com/stretchr/testify v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20191011084731-65d371908596/go.mod
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/huaweicloud/golangsdk v0.0.0-20210519120215-6602b8a10c02 h1:t3VcYe8eUxdRqk0c3PQd50DtOPCEOiNv9LHi9YTNQ5I=
github.com/huaweicloud/golangsdk v0.0.0-20210519120215-6602b8a10c02/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210521045610-c7bf719c8e93 h1:xHX5P34vk5p0Ihp2jZkD2S5KrZIBIaauluMtBvPBeU8=
github.com/huaweicloud/golangsdk v0.0.0-20210521045610-c7bf719c8e93/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
Expand Down
2 changes: 2 additions & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_elb_listener": ResourceListenerV3(),
"huaweicloud_elb_loadbalancer": ResourceLoadBalancerV3(),
"huaweicloud_elb_ipgroup": ResourceIpGroupV3(),
"huaweicloud_elb_pool": ResourcePoolV3(),
"huaweicloud_elb_member": ResourceMemberV3(),
"huaweicloud_evs_snapshot": ResourceEvsSnapshotV2(),
"huaweicloud_evs_volume": ResourceEvsStorageVolumeV3(),
"huaweicloud_fgs_function": resourceFgsFunctionV2(),
Expand Down
174 changes: 174 additions & 0 deletions huaweicloud/resource_huaweicloud_elb_member.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package huaweicloud

import (
"fmt"
"log"
"time"

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

"github.com/huaweicloud/golangsdk/openstack/elb/v3/pools"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)

func ResourceMemberV3() *schema.Resource {
return &schema.Resource{
Create: resourceMemberV3Create,
Read: resourceMemberV3Read,
Update: resourceMemberV3Update,
Delete: resourceMemberV3Delete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"name": {
Type: schema.TypeString,
Optional: true,
},

"address": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"protocol_port": {
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},

"weight": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 1 {
errors = append(errors, fmt.Errorf(
"Only numbers greater than 0 are supported values for 'weight'"))
}
return
},
},

"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceMemberV3Create(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
elbClient, err := config.ElbV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

createOpts := pools.CreateMemberOpts{
Name: d.Get("name").(string),
Address: d.Get("address").(string),
ProtocolPort: d.Get("protocol_port").(int),
Weight: d.Get("weight").(int),
}

// Must omit if not set
if v, ok := d.GetOk("subnet_id"); ok {
createOpts.SubnetID = v.(string)
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)
poolID := d.Get("pool_id").(string)
member, err := pools.CreateMember(elbClient, poolID, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating member: %s", err)
}

d.SetId(member.ID)

return resourceMemberV3Read(d, meta)
}

func resourceMemberV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
elbClient, err := config.ElbV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

member, err := pools.GetMember(elbClient, d.Get("pool_id").(string), d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "member")
}

log.Printf("[DEBUG] Retrieved member %s: %#v", d.Id(), member)

d.Set("name", member.Name)
d.Set("weight", member.Weight)
d.Set("subnet_id", member.SubnetID)
d.Set("address", member.Address)
d.Set("protocol_port", member.ProtocolPort)
d.Set("region", GetRegion(d, config))

return nil
}

func resourceMemberV3Update(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
elbClient, err := config.ElbV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

var updateOpts pools.UpdateMemberOpts
if d.HasChange("name") {
updateOpts.Name = d.Get("name").(string)
}
if d.HasChange("weight") {
updateOpts.Weight = d.Get("weight").(int)
}

log.Printf("[DEBUG] Updating member %s with options: %#v", d.Id(), updateOpts)
poolID := d.Get("pool_id").(string)
_, err = pools.UpdateMember(elbClient, poolID, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Unable to update member %s: %s", d.Id(), err)
}

return resourceMemberV3Read(d, meta)
}

func resourceMemberV3Delete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
elbClient, err := config.ElbV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud elb client: %s", err)
}

poolID := d.Get("pool_id").(string)
err = pools.DeleteMember(elbClient, poolID, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Unable to delete member %s: %s", d.Id(), err)
}
return nil
}
Loading