Skip to content

Commit

Permalink
Add huaweicloud_lb_whitelist_v2 resource (#261)
Browse files Browse the repository at this point in the history
Signed-off-by: ShiChangkuo <[email protected]>
  • Loading branch information
ShiChangkuo authored Feb 3, 2020
1 parent b4a7fcc commit 3cd3507
Show file tree
Hide file tree
Showing 9 changed files with 489 additions and 0 deletions.
16 changes: 16 additions & 0 deletions huaweicloud/diff_suppress_funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package huaweicloud

import (
"reflect"
"sort"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/jen20/awspolicyequivalence"
)
Expand Down Expand Up @@ -31,3 +35,15 @@ func suppressComputedFixedWhenFloatingIp(k, old, new string, d *schema.ResourceD
}
return false
}

func suppressLBWhitelistDiffs(k, old, new string, d *schema.ResourceData) bool {
if len(old) != len(new) {
return false
}
old_array := strings.Split(old, ",")
new_array := strings.Split(new, ",")
sort.Strings(old_array)
sort.Strings(new_array)

return reflect.DeepEqual(old_array, new_array)
}
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_lb_monitor_v2": resourceMonitorV2(),
"huaweicloud_lb_l7policy_v2": resourceL7PolicyV2(),
"huaweicloud_lb_l7rule_v2": resourceL7RuleV2(),
"huaweicloud_lb_whitelist_v2": resourceWhitelistV2(),
"huaweicloud_mrs_cluster_v1": resourceMRSClusterV1(),
"huaweicloud_mrs_job_v1": resourceMRSJobV1(),
"huaweicloud_networking_network_v2": resourceNetworkingNetworkV2(),
Expand Down
140 changes: 140 additions & 0 deletions huaweicloud/resource_huaweicloud_lb_whitelist_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package huaweicloud

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/whitelists"
)

func resourceWhitelistV2() *schema.Resource {
return &schema.Resource{
Create: resourceWhitelistV2Create,
Read: resourceWhitelistV2Read,
Update: resourceWhitelistV2Update,
Delete: resourceWhitelistV2Delete,

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{
"tenant_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

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

"enable_whitelist": {
Type: schema.TypeBool,
Optional: true,
},

"whitelist": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressLBWhitelistDiffs,
},
},
}
}

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

enableWhitelist := d.Get("enable_whitelist").(bool)
createOpts := whitelists.CreateOpts{
TenantId: d.Get("tenant_id").(string),
ListenerId: d.Get("listener_id").(string),
EnableWhitelist: &enableWhitelist,
Whitelist: d.Get("whitelist").(string),
}

log.Printf("[DEBUG] Create Options: %#v", createOpts)
wl, err := whitelists.Create(networkingClient, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud Whitelist: %s", err)
}

d.SetId(wl.ID)
return resourceWhitelistV2Read(d, meta)
}

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

wl, err := whitelists.Get(networkingClient, d.Id()).Extract()
if err != nil {
return CheckDeleted(d, err, "whitelist")
}

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

d.SetId(wl.ID)
d.Set("tenant_id", wl.TenantId)
d.Set("listener_id", wl.ListenerId)
d.Set("enable_whitelist", wl.EnableWhitelist)
d.Set("whitelist", wl.Whitelist)

return nil
}

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

var updateOpts whitelists.UpdateOpts
if d.HasChange("enable_whitelist") {
ew := d.Get("enable_whitelist").(bool)
updateOpts.EnableWhitelist = &ew
}
if d.HasChange("whitelist") {
updateOpts.Whitelist = d.Get("whitelist").(string)
}

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

return resourceWhitelistV2Read(d, meta)
}

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

log.Printf("[DEBUG] Attempting to delete whitelist %s", d.Id())
err = whitelists.Delete(networkingClient, d.Id()).ExtractErr()
if err != nil {
return fmt.Errorf("Error deleting HuaweiCloud whitelist: %s", err)
}
d.SetId("")
return nil
}
127 changes: 127 additions & 0 deletions huaweicloud/resource_huaweicloud_lb_whitelist_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package huaweicloud

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/whitelists"
)

func TestAccLBV2Whitelist_basic(t *testing.T) {
var whitelist whitelists.Whitelist

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLBV2WhitelistDestroy,
Steps: []resource.TestStep{
{
Config: TestAccLBV2WhitelistConfig_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckLBV2WhitelistExists("huaweicloud_lb_whitelist_v2.whitelist_1", &whitelist),
),
},
{
Config: TestAccLBV2WhitelistConfig_update,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("huaweicloud_lb_whitelist_v2.whitelist_1", "enable_whitelist", "true"),
),
},
},
})
}

func testAccCheckLBV2WhitelistDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}

for _, rs := range s.RootModule().Resources {
if rs.Type != "huaweicloud_lb_whitelist_v2" {
continue
}

_, err := whitelists.Get(networkingClient, rs.Primary.ID).Extract()
if err == nil {
return fmt.Errorf("Whitelist still exists: %s", rs.Primary.ID)
}
}

return nil
}

func testAccCheckLBV2WhitelistExists(n string, whitelist *whitelists.Whitelist) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

config := testAccProvider.Meta().(*Config)
networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud networking client: %s", err)
}

found, err := whitelists.Get(networkingClient, rs.Primary.ID).Extract()
if err != nil {
return err
}

if found.ID != rs.Primary.ID {
return fmt.Errorf("Whitelist not found")
}

*whitelist = *found

return nil
}
}

var TestAccLBV2WhitelistConfig_basic = fmt.Sprintf(`
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_whitelist_v2" "whitelist_1" {
enable_whitelist = true
whitelist = "192.168.11.1,192.168.0.1/24"
listener_id = "${huaweicloud_lb_listener_v2.listener_1.id}"
}
`, OS_SUBNET_ID)

var TestAccLBV2WhitelistConfig_update = fmt.Sprintf(`
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_whitelist_v2" "whitelist_1" {
enable_whitelist = true
whitelist = "192.168.11.1,192.168.0.1/24,192.168.201.18/8"
listener_id = "${huaweicloud_lb_listener_v2.listener_1.id}"
}
`, OS_SUBNET_ID)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3cd3507

Please sign in to comment.