-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add huaweicloud_lb_whitelist_v2 resource (#261)
Signed-off-by: ShiChangkuo <[email protected]>
- Loading branch information
1 parent
b4a7fcc
commit 3cd3507
Showing
9 changed files
with
489 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
127
huaweicloud/resource_huaweicloud_lb_whitelist_v2_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
89 changes: 89 additions & 0 deletions
89
.../huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/whitelists/requests.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.