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

[FWaaS_V2]: Add fw_policy_v2 #1584

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
136 changes: 136 additions & 0 deletions openstack/data_source_openstack_fw_policy_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package openstack

import (
"context"
"log"

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

"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/policies"
)

func dataSourceFWPolicyV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFWPolicyV2Read,

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

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

"policy_id": {
nikParasyr marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Optional: true,
},

"tenant_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},

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

"shared": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},

"audited": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},

"rules": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceFWPolicyV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}

listOpts := policies.ListOpts{}

if v, ok := d.GetOk("name"); ok {
listOpts.Name = v.(string)
}

if v, ok := d.GetOk("description"); ok {
listOpts.Description = v.(string)
}

if v, ok := d.GetOk("policy_id"); ok {
listOpts.ID = v.(string)
}

if v, ok := d.GetOk("tenant_id"); ok {
listOpts.TenantID = v.(string)
}

if v, ok := d.GetOk("shared"); ok {
shared := v.(bool)
listOpts.Shared = &shared
}

if v, ok := d.GetOk("audited"); ok {
audited := v.(bool)
listOpts.Audited = &audited
}

pages, err := policies.List(networkingClient, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to list openstack_fw_policy_v2 policies: %s", err)
}

allFWPolicies, err := policies.ExtractPolicies(pages)
if err != nil {
return diag.Errorf("Unable to retrieve openstack_fw_policy_v2: %s", err)
}

if len(allFWPolicies) < 1 {
return diag.Errorf("Your openstack_fw_policy_v2 query returned no results")
}

if len(allFWPolicies) > 1 {
return diag.Errorf("Your openstack_fw_policy_v2 query returned more than one result")
}

policy := allFWPolicies[0]

log.Printf("[DEBUG] Retrieved openstack_fw_policy_v2 %s: %#v", policy.ID, policy)

d.SetId(policy.ID)

d.Set("name", policy.Name)
d.Set("tenant_id", policy.TenantID)
d.Set("description", policy.Description)
d.Set("shared", policy.Shared)
d.Set("audited", policy.Audited)
d.Set("rules", policy.Rules)
d.Set("region", GetRegion(d, config))

return nil
}
160 changes: 160 additions & 0 deletions openstack/data_source_openstack_fw_policy_v2_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package openstack

import (
"fmt"
"testing"

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

func TestAccFWPolicyV2DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
testAccPreCheckFW(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccFWPolicyV2DataSourceBasic,
},
{
Config: testAccFWPolicyV2DataSourceName(),
Check: resource.ComposeTestCheckFunc(
testAccCheckFWPolicyV2DataSourceID("data.openstack_fw_policy_v2.policy_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "name", "policy_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "description", "My firewall policy"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "shared", "false"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "audited", "false"),
resource.TestCheckResourceAttrSet(
"data.openstack_fw_policy_v2.policy_1", "tenant_id"),
),
},
},
})
}

func TestAccFWPolicyV2DataSource_shared(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAdminOnly(t)
testAccPreCheckFW(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccFWPolicyV2DataSourceBasic,
},
{
Config: testAccFWPolicyV2DataSourceShared,
Check: resource.ComposeTestCheckFunc(
testAccCheckFWPolicyV2DataSourceID("data.openstack_fw_policy_v2.policy_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "name", "policy_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "description", "My firewall policy"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "shared", "true"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "audited", "true"),
resource.TestCheckResourceAttrSet(
"data.openstack_fw_policy_v2.policy_1", "tenant_id"),
),
},
},
})
}

func TestAccFWPolicyV2DataSource_FWPolicyID(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
testAccPreCheckFW(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccFWPolicyV2DataSourceBasic,
},
{
Config: testAccFWPolicyV2DataSourcePolicyID(),
Check: resource.ComposeTestCheckFunc(
testAccCheckFWPolicyV2DataSourceID("data.openstack_fw_policy_v2.policy_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_policy_v2.policy_1", "name", "policy_1"),
),
},
},
})
}

func testAccCheckFWPolicyV2DataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find firewall policy data source: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Firewall policy data source ID not set")
}

return nil
}
}

const testAccFWPolicyV2DataSourceBasic = `
resource "openstack_fw_policy_v2" "policy_1" {
name = "policy_1"
description = "My firewall policy"
}
`

const testAccFWPolicyV2DataSourceShared = `
resource "openstack_fw_policy_v2" "policy_1" {
name = "policy_1"
description = "My firewall policy"
shared = true
audited = true
}

data "openstack_fw_policy_v2" "policy_1" {
name = "${openstack_fw_policy_v2.policy_1.name}"
description = "${openstack_fw_policy_v2.policy_1.description}"
tenant_id = "${openstack_fw_policy_v2.policy_1.tenant_id}"
shared = true
audited = true
}
`

func testAccFWPolicyV2DataSourceName() string {
return fmt.Sprintf(`
%s

data "openstack_fw_policy_v2" "policy_1" {
name = "policy_1"
description = "My firewall policy"
tenant_id = "${openstack_fw_policy_v2.policy_1.tenant_id}"
shared = false
audited = false
}
`, testAccFWPolicyV2DataSourceBasic)
}

func testAccFWPolicyV2DataSourcePolicyID() string {
return fmt.Sprintf(`
%s

data "openstack_fw_policy_v2" "policy_1" {
policy_id = "${openstack_fw_policy_v2.policy_1.id}"
}
`, testAccFWPolicyV2DataSourceBasic)
}
8 changes: 5 additions & 3 deletions openstack/data_source_openstack_fw_rule_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,18 @@ func dataSourceFWRuleV2Read(ctx context.Context, d *schema.ResourceData, meta in
}

if v, ok := d.GetOk("shared"); ok {
listOpts.Shared = v.(*bool)
shared := v.(bool)
listOpts.Shared = &shared
}

if v, ok := d.GetOk("enabled"); ok {
listOpts.Enabled = v.(*bool)
enabled := v.(bool)
listOpts.Enabled = &enabled
}

pages, err := rules.List(networkingClient, listOpts).AllPages()
if err != nil {
return diag.FromErr(err)
return diag.Errorf("Unable to list openstack_fw_rule_v2 rules: %s", err)
}

allFWRules, err := rules.ExtractRules(pages)
Expand Down
4 changes: 3 additions & 1 deletion openstack/data_source_openstack_fw_rule_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestAccFWRuleV2DataSource_basic(t *testing.T) {
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
testAccPreCheckFW(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -56,6 +57,7 @@ func TestAccFWRuleV2DataSource_FWRuleID(t *testing.T) {
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
testAccPreCheckFW(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -127,7 +129,7 @@ func testAccFWRuleV2DataSourceRuleID() string {
%s

data "openstack_fw_rule_v2" "rule_1" {
rule_id = "${openstack_fw_rule_v2.rule_1.id}"
rule_id = "${openstack_fw_rule_v2.rule_1.id}"
}
`, testAccFWRuleV2DataSourceBasic)
}
34 changes: 34 additions & 0 deletions openstack/fw_policy_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package openstack

import (
"log"

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

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/fwaas_v2/policies"
)

func fwPolicyV2DeleteFunc(networkingClient *gophercloud.ServiceClient, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
err := policies.Delete(networkingClient, id).Err
Copy link
Collaborator

@kayrus kayrus Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to add an ErrDefault404 check here as well. There is a chance that the policy can be deleted manually between checks and this will cause TF to hang.

Copy link
Contributor Author

@Koodt Koodt Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

if err == nil {
return "", "DELETED", nil
}

if err != nil {
switch err.(type) {
case gophercloud.ErrDefault404:
// This error usually means that the policy was deleted manually
log.Printf("[DEBUG] Unable to find openstack_fw_policy_v2 %s: %s", id, err)
return "", "DELETED", nil
case gophercloud.ErrDefault409:
// This error usually means that the policy is attached to a firewall.
log.Printf("[DEBUG] Error to delete openstack_fw_policy_v2 %s: %s", id, err)
return nil, "ACTIVE", nil
}
}

return nil, "ACTIVE", err
}
}