Skip to content

Commit

Permalink
Add ELB v3 l7policy and l7rule (#1161)
Browse files Browse the repository at this point in the history
  • Loading branch information
niuzhenguo authored May 25, 2021
1 parent 1b0f8d5 commit 57d5f25
Show file tree
Hide file tree
Showing 11 changed files with 1,530 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/resources/elb_l7policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
subcategory: "Dedicated Load Balance (Dedicated ELB)"
---

# huaweicloud\_elb\_l7policy

Manages an ELB L7 Policy resource within HuaweiCloud.

## Example Usage

```hcl
resource "huaweicloud_elb_l7policy" "policy_1" {
name = "policy_1"
description = "test description"
listener_id = {{ listener_id }}
redirect_pool_id = {{ pool_id }}
}
```

## Argument Reference

The following arguments are supported:

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

* `name` - (Optional, String) Human-readable name for the L7 Policy. Does not have
to be unique.

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

* `listener_id` - (Required, String, ForceNew) The Listener on which the L7 Policy will be associated with.
Changing this creates a new L7 Policy.

* `redirect_pool_id` - (Required, String) Requests matching this policy will be redirected to the pool with this ID.

## Attributes Reference

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

* `id` - The unique ID for the L7 policy.

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

# huaweicloud\_elb\_l7rule

Manages an ELB L7 Rule resource within HuaweiCloud.

## Example Usage

```hcl
resource "huaweicloud_elb_l7rule" "l7rule_1" {
l7policy_id = {{ policy_id }}
type = "PATH"
compare_type = "EQUAL_TO"
value = "/api"
}
```

## Argument Reference

The following arguments are supported:

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

* `type` - (Required, String, ForceNew) The L7 Rule type - can either be HOST\_NAME or PATH. Changing this creates a new L7 Rule.

* `compare_type` - (Required, String) The comparison type for the L7 rule - can either be
STARTS\_WITH, EQUAL_TO or REGEX

* `l7policy_id` - (Required, String, ForceNew) The ID of the L7 Policy. Changing this creates a new
L7 Rule.

* `value` - (Required, String) The value to use for the comparison.

## Attributes Reference

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

* `id` - The unique ID for the L7 Rule.

## 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: 2 additions & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,8 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_dns_zone": ResourceDNSZoneV2(),
"huaweicloud_dws_cluster": resourceDwsCluster(),
"huaweicloud_elb_certificate": ResourceCertificateV3(),
"huaweicloud_elb_l7policy": ResourceL7PolicyV3(),
"huaweicloud_elb_l7rule": ResourceL7RuleV3(),
"huaweicloud_elb_listener": ResourceListenerV3(),
"huaweicloud_elb_loadbalancer": ResourceLoadBalancerV3(),
"huaweicloud_elb_ipgroup": ResourceIpGroupV3(),
Expand Down
217 changes: 217 additions & 0 deletions huaweicloud/resource_huaweicloud_elb_l7policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package huaweicloud

import (
"fmt"
"log"
"time"

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

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

func ResourceL7PolicyV3() *schema.Resource {
return &schema.Resource{
Create: resourceL7PolicyV3Create,
Read: resourceL7PolicyV3Read,
Update: resourceL7PolicyV3Update,
Delete: resourceL7PolicyV3Delete,

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,
},

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

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

"redirect_pool_id": {
Type: schema.TypeString,
Required: true,
},
},
}
}

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

createOpts := l7policies.CreateOpts{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
Action: "REDIRECT_TO_POOL",
ListenerID: d.Get("listener_id").(string),
RedirectPoolID: d.Get("redirect_pool_id").(string),
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)
l7Policy, err := l7policies.Create(lbClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating L7 Policy: %s", err)
}

timeout := d.Timeout(schema.TimeoutCreate)
// Wait for L7 Policy to become active before continuing
err = waitForElbV3Policy(lbClient, l7Policy.ID, "ACTIVE", nil, timeout)
if err != nil {
return err
}

d.SetId(l7Policy.ID)

return resourceL7PolicyV3Read(d, meta)
}

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

l7Policy, err := l7policies.Get(lbClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "L7 Policy")
}

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

d.Set("description", l7Policy.Description)
d.Set("name", l7Policy.Name)
d.Set("listener_id", l7Policy.ListenerID)
d.Set("redirect_pool_id", l7Policy.RedirectPoolID)
d.Set("region", GetRegion(d, config))

return nil
}

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

var updateOpts l7policies.UpdateOpts

if d.HasChange("name") {
name := d.Get("name").(string)
updateOpts.Name = &name
}
if d.HasChange("description") {
description := d.Get("description").(string)
updateOpts.Description = &description
}
if d.HasChange("redirect_pool_id") {
redirectPoolID := d.Get("redirect_pool_id").(string)
updateOpts.RedirectPoolID = &redirectPoolID
}

log.Printf("[DEBUG] Updating L7 Policy %s with options: %#v", d.Id(), updateOpts)
_, err = l7policies.Update(lbClient, d.Id(), updateOpts).Extract()
if err != nil {
return fmt.Errorf("Unable to update L7 Policy %s: %s", d.Id(), err)
}

timeout := d.Timeout(schema.TimeoutUpdate)
err = waitForElbV3Policy(lbClient, d.Id(), "ACTIVE", nil, timeout)
if err != nil {
return err
}

return resourceL7PolicyV3Read(d, meta)
}

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

log.Printf("[DEBUG] Attempting to delete L7 Policy %s", d.Id())
err = l7policies.Delete(lbClient, d.Id()).ExtractErr()
if err != nil {
return CheckDeleted(d, err, "Error deleting L7 Policy")
}

timeout := d.Timeout(schema.TimeoutDelete)
err = waitForElbV3Policy(lbClient, d.Id(), "DELETED", nil, timeout)
if err != nil {
return err
}

return nil
}

func waitForElbV3Policy(elbClient *golangsdk.ServiceClient,
id string, target string, pending []string, timeout time.Duration) error {

log.Printf("[DEBUG] Waiting for policy %s to become %s", id, target)

stateConf := &resource.StateChangeConf{
Target: []string{target},
Pending: pending,
Refresh: resourceElbV3PolicyRefreshFunc(elbClient, id),
Timeout: timeout,
Delay: 5 * time.Second,
PollInterval: 3 * time.Second,
}

_, err := stateConf.WaitForState()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
switch target {
case "DELETED":
return nil
default:
return fmt.Errorf("Error: policy %s not found: %s", id, err)
}
}
return fmt.Errorf("Error waiting for policy %s to become %s: %s", id, target, err)
}

return nil
}

func resourceElbV3PolicyRefreshFunc(elbClient *golangsdk.ServiceClient,
id string) resource.StateRefreshFunc {

return func() (interface{}, string, error) {
policy, err := l7policies.Get(elbClient, id).Extract()
if err != nil {
return nil, "", err
}

return policy, policy.ProvisioningStatus, nil
}
}
Loading

0 comments on commit 57d5f25

Please sign in to comment.