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 Group V2 datasource #1589

Merged
Show file tree
Hide file tree
Changes from 4 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
178 changes: 178 additions & 0 deletions openstack/data_source_openstack_fw_group_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
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/groups"
)

func dataSourceFWGroupV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceFWGroupV2Read,

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

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

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

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

"tenant_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ConflictsWith: []string{"project_id"},
},

"project_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"tenant_id"},
},

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

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

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

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

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

"status": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Copy link
Collaborator

@kayrus kayrus Jun 29, 2023

Choose a reason for hiding this comment

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

status is marked optional, but it's not set in the listOpts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Add

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Decided to remove Optional

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why? It might be useful to filter groups, which are active.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Understood
Add

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you also adjust the docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, sorry

},
},
}
}

func dataSourceFWGroupV2Read(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 := groups.ListOpts{}

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

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("tenant_id"); ok {
listOpts.TenantID = v.(string)
}

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

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

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

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

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

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

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

allGroups, err := groups.ExtractGroups(pages)
if err != nil {
return diag.Errorf("Unable to retrieve openstack_fw_group_v2: %s", err)
}

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

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

group := allGroups[0]

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

d.SetId(group.ID)

d.Set("name", group.Name)
d.Set("description", group.Description)
d.Set("tenant_id", group.TenantID)
d.Set("project_id", group.ProjectID)
d.Set("shared", group.Shared)
d.Set("admin_state_up", group.AdminStateUp)
d.Set("ingress_firewall_policy_id", group.IngressFirewallPolicyID)
d.Set("egress_firewall_policy_id", group.EgressFirewallPolicyID)
d.Set("ports", group.Ports)
d.Set("status", group.Status)
d.Set("region", GetRegion(d, config))

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

import (
"fmt"
"testing"

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

func TestAccFWGroupV2DataSource_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
testAccPreCheckFW(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccFWGroupV2DataSourceGroup,
},
{
Config: testAccFWGroupV2DataSourceBasic(),
Check: resource.ComposeTestCheckFunc(
testAccCheckFWGroupV2DataSourceID("data.openstack_fw_group_v2.group_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_group_v2.group_1", "name", "group_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_group_v2.group_1", "description", "My firewall group"),
resource.TestCheckResourceAttr(
"data.openstack_fw_group_v2.group_1", "shared", "false"),
resource.TestCheckResourceAttrSet(
"data.openstack_fw_group_v2.group_1", "ingress_firewall_policy_id"),
resource.TestCheckResourceAttrSet(
"data.openstack_fw_group_v2.group_1", "egress_firewall_policy_id"),
),
},
},
})
}

func TestAccFWGroupV2DataSource_shared(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAdminOnly(t)
testAccPreCheckFW(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccFWGroupV2DataSourceGroupShared,
},
{
Config: testAccFWGroupV2DataSourceShared(),
Check: resource.ComposeTestCheckFunc(
testAccCheckFWGroupV2DataSourceID("data.openstack_fw_group_v2.group_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_group_v2.group_1", "name", "group_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_group_v2.group_1", "description", "My firewall group"),
resource.TestCheckResourceAttr(
"data.openstack_fw_group_v2.group_1", "shared", "true"),
resource.TestCheckResourceAttrSet(
"data.openstack_fw_group_v2.group_1", "ingress_firewall_policy_id"),
resource.TestCheckResourceAttrSet(
"data.openstack_fw_group_v2.group_1", "egress_firewall_policy_id"),
),
},
},
})
}

func TestAccFWGroupV2DataSource_FWGroupID(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckNonAdminOnly(t)
testAccPreCheckFW(t)
},
ProviderFactories: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccFWGroupV2DataSourceGroup,
},
{
Config: testAccFWGroupV2DataSourceGroupID(),
Check: resource.ComposeTestCheckFunc(
testAccCheckFWGroupV2DataSourceID("data.openstack_fw_group_v2.group_1"),
resource.TestCheckResourceAttr(
"data.openstack_fw_group_v2.group_1", "name", "group_1"),
),
},
},
})
}

func testAccCheckFWGroupV2DataSourceID(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 group data source: %s", n)
}

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

return nil
}
}

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

resource "openstack_fw_policy_v2" "policy_2" {
name = "policy_2"
description = "My firewall egress policy"
}

resource "openstack_fw_group_v2" "group_1" {
name = "group_1"
description = "My firewall group"
ingress_firewall_policy_id = "${openstack_fw_policy_v2.policy_1.id}"
egress_firewall_policy_id = "${openstack_fw_policy_v2.policy_2.id}"
}
`

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

data "openstack_fw_group_v2" "group_1" {
name = "group_1"
description = "My firewall group"
project_id = "${openstack_fw_group_v2.group_1.project_id}"
shared = false
ingress_firewall_policy_id = "${openstack_fw_policy_v2.policy_1.id}"
egress_firewall_policy_id = "${openstack_fw_policy_v2.policy_2.id}"
}
`, testAccFWGroupV2DataSourceGroup)
}

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

resource "openstack_fw_policy_v2" "policy_2" {
name = "policy_2"
description = "My firewall egress policy"
}

resource "openstack_fw_group_v2" "group_1" {
name = "group_1"
description = "My firewall group"
shared = true
ingress_firewall_policy_id = "${openstack_fw_policy_v2.policy_1.id}"
egress_firewall_policy_id = "${openstack_fw_policy_v2.policy_2.id}"
}
`

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

data "openstack_fw_group_v2" "group_1" {
name = "group_1"
description = "My firewall group"
tenant_id = "${openstack_fw_group_v2.group_1.tenant_id}"
shared = true
ingress_firewall_policy_id = "${openstack_fw_policy_v2.policy_1.id}"
egress_firewall_policy_id = "${openstack_fw_policy_v2.policy_2.id}"
}
`, testAccFWGroupV2DataSourceGroupShared)
}

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

data "openstack_fw_group_v2" "group_1" {
group_id = "${openstack_fw_group_v2.group_1.id}"
}
`, testAccFWGroupV2DataSourceGroup)
}
1 change: 1 addition & 0 deletions openstack/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ func Provider() *schema.Provider {
"openstack_containerinfra_clustertemplate_v1": dataSourceContainerInfraClusterTemplateV1(),
"openstack_containerinfra_cluster_v1": dataSourceContainerInfraCluster(),
"openstack_dns_zone_v2": dataSourceDNSZoneV2(),
"openstack_fw_group_v2": dataSourceFWGroupV2(),
"openstack_fw_policy_v1": dataSourceFWPolicyV1(),
"openstack_fw_policy_v2": dataSourceFWPolicyV2(),
"openstack_fw_rule_v2": dataSourceFWRuleV2(),
Expand Down