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

[Fix] fix update when source_port not set #2715

Merged
merged 4 commits into from
Nov 11, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,30 @@ func TestAccFWRuleV2_TCPUpdate(t *testing.T) {
})
}

// Customer issue https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/issues/2711
func TestAccFWRuleV2_emptySourcePort(t *testing.T) {
rname := "opentelekomcloud_fw_rule_v2.egress_test"
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: testAccCheckFWRuleV2Destroy,
Steps: []resource.TestStep{
{
Config: testAccFWRuleV2emptySourcePort(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(rname, "protocol", "udp"),
),
},
{
Config: testAccFWRuleV2emptySourcePort_update(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(rname, "protocol", "udp"),
),
},
},
})
}

func testAccCheckFWRuleV2Destroy(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
networkingClient, err := config.NetworkingV2Client(env.OS_REGION_NAME)
Expand Down Expand Up @@ -350,3 +374,77 @@ resource "opentelekomcloud_fw_rule_v2" "rule_1" {
enabled = "true"
}
`

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

resource "opentelekomcloud_fw_rule_v2" "egress_test" {

description = "egress test"
action = "allow"
protocol = "udp"
source_ip_address = "192.168.1.0/29"
destination_ip_address = "0.0.0.0/0"
destination_port = "1234"

enabled = "true"
}

resource "opentelekomcloud_fw_policy_v2" "egress" {
name = "egress"

rules = [opentelekomcloud_fw_rule_v2.egress_test.id]
}

data "opentelekomcloud_networking_port_v2" "this" {
network_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
device_owner = "network:router_interface_distributed"
}

resource "opentelekomcloud_fw_firewall_group_v2" "this" {
name = "test"
egress_policy_id = opentelekomcloud_fw_policy_v2.egress.id
ports = [
data.opentelekomcloud_networking_port_v2.this.id
]
}
`, common.DataSourceSubnet)
}

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

resource "opentelekomcloud_fw_rule_v2" "egress_test" {

description = "egress test"
action = "allow"
protocol = "udp"
source_ip_address = "192.168.1.0/24"
destination_ip_address = "0.0.0.0/0"
destination_port = "1234"

enabled = "true"
}

resource "opentelekomcloud_fw_policy_v2" "egress" {
name = "egress"

rules = [opentelekomcloud_fw_rule_v2.egress_test.id]
}

data "opentelekomcloud_networking_port_v2" "this" {
network_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.network_id
device_owner = "network:router_interface_distributed"
}

resource "opentelekomcloud_fw_firewall_group_v2" "this" {
name = "test"
egress_policy_id = opentelekomcloud_fw_policy_v2.egress.id
ports = [
data.opentelekomcloud_networking_port_v2.this.id
]
}
`, common.DataSourceSubnet)
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ func resourceFWRuleV2Update(ctx context.Context, d *schema.ResourceData, meta in

if d.Get("protocol").(string) != "icmp" && (updateOpts.DestinationPort == nil && updateOpts.SourcePort == nil) {
updateOpts.DestinationPort = pointerto.String(d.Get("destination_port").(string))
updateOpts.SourcePort = pointerto.String(d.Get("source_port").(string))
if d.Get("source_port").(string) != "" {
updateOpts.SourcePort = pointerto.String(d.Get("source_port").(string))
}
}

log.Printf("[DEBUG] Updating firewall rules: %#v", updateOpts)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
**[IAM]** Fix updating rule when source_port is empty in ``resource/opentelekomcloud_fw_rule_v2`` (`#2715 <https://github.com/opentelekomcloud/terraform-provider-opentelekomcloud/issues/2715>`_)