-
Notifications
You must be signed in to change notification settings - Fork 363
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
nikParasyr
merged 5 commits into
terraform-provider-openstack:main
from
Koodt:fwaas_v2_add_group_datasource
Jun 29, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
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 | ||
} |
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,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) | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 thelistOpts
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decided to remove Optional
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood
Add
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, sorry