-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FWaaS_V2]: Add FW Group V2 resource (#1588)
Add resourceFWGroupV2 CRUD Add resourceFWGroupV2 tests Add resourceFWGroupV2 docs Add import_openstack_fw_group_v2_test Enable openstack_fw_group_v2 in provider.go
- Loading branch information
Showing
10 changed files
with
1,113 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package openstack | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/gophercloud/gophercloud" | ||
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/groups" | ||
) | ||
|
||
func fwGroupV2RefreshFunc(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
group, err := groups.Get(networkingClient, id).Extract() | ||
if err != nil { | ||
return nil, "", err | ||
} | ||
|
||
return group, group.Status, nil | ||
} | ||
} | ||
|
||
func fwGroupV2DeleteFunc(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc { | ||
return func() (interface{}, string, error) { | ||
group, err := groups.Get(networkingClient, id).Extract() | ||
|
||
if err != nil { | ||
if _, ok := err.(gophercloud.ErrDefault404); ok { | ||
return "", "DELETED", nil | ||
} | ||
return nil, "", fmt.Errorf("Unexpected error: %s", err) | ||
} | ||
|
||
return group, "DELETING", nil | ||
} | ||
} | ||
|
||
func fwGroupV2IngressPolicyDeleteFunc(networkingClient *gophercloud.ServiceClient, d *schema.ResourceData, ctx context.Context, groupID string) diag.Diagnostics { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"PENDING_CREATE", "PENDING_UPDATE"}, | ||
Target: []string{"ACTIVE", "INACTIVE", "DOWN"}, | ||
Refresh: fwGroupV2RefreshFunc(networkingClient, d.Id()), | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
Delay: 0, | ||
MinTimeout: 2 * time.Second, | ||
} | ||
|
||
_, err := stateConf.WaitForStateContext(ctx) | ||
if err != nil { | ||
return diag.Errorf("Error waiting for openstack_fw_group_v2 %s to become active: %s", d.Id(), err) | ||
} | ||
|
||
_, err = groups.RemoveIngressPolicy(networkingClient, groupID).Extract() | ||
if err != nil { | ||
return diag.Errorf("Error removing ingress firewall policy from openstack_fw_group_v2 %s: %s", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func fwGroupV2EgressPolicyDeleteFunc(networkingClient *gophercloud.ServiceClient, d *schema.ResourceData, ctx context.Context, groupID string) diag.Diagnostics { | ||
stateConf := &resource.StateChangeConf{ | ||
Pending: []string{"PENDING_CREATE", "PENDING_UPDATE"}, | ||
Target: []string{"ACTIVE", "INACTIVE", "DOWN"}, | ||
Refresh: fwGroupV2RefreshFunc(networkingClient, d.Id()), | ||
Timeout: d.Timeout(schema.TimeoutUpdate), | ||
Delay: 0, | ||
MinTimeout: 2 * time.Second, | ||
} | ||
|
||
_, err := stateConf.WaitForStateContext(ctx) | ||
if err != nil { | ||
return diag.Errorf("Error waiting for openstack_fw_group_v2 %s to become active: %s", d.Id(), err) | ||
} | ||
|
||
_, err = groups.RemoveEgressPolicy(networkingClient, groupID).Extract() | ||
if err != nil { | ||
return diag.Errorf("Error removing egress firewall policy from openstack_fw_group_v2 %s: %s", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} |
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,32 @@ | ||
package openstack | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccFWGroupV2_importBasic(t *testing.T) { | ||
resourceName := "openstack_fw_group_v2.group_1" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { | ||
testAccPreCheck(t) | ||
testAccPreCheckNonAdminOnly(t) | ||
testAccPreCheckFW(t) | ||
}, | ||
ProviderFactories: testAccProviders, | ||
CheckDestroy: testAccCheckFWGroupV2Destroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccFWGroupV2Basic1, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
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
Oops, something went wrong.