Skip to content

Commit

Permalink
Fix the problem that any type of DNAT rule cannot open all ports (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lance52259 authored Feb 1, 2021
1 parent 01d3878 commit 655f9d0
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
16 changes: 2 additions & 14 deletions huaweicloud/resource_huaweicloud_nat_dnat_rule_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,13 @@ func resourceNatDnatRuleCreate(d *schema.ResourceData, meta interface{}) error {
if err != nil {
return err
}
e, err = isEmptyValue(reflect.ValueOf(internalServicePortProp))
if err != nil {
return err
}
if !e {
params["internal_service_port"] = internalServicePortProp
}
params["internal_service_port"] = internalServicePortProp

externalServicePortProp, err := navigateValue(opts, []string{"external_service_port"}, nil)
if err != nil {
return err
}
e, err = isEmptyValue(reflect.ValueOf(externalServicePortProp))
if err != nil {
return err
}
if !e {
params["external_service_port"] = externalServicePortProp
}
params["external_service_port"] = externalServicePortProp

natGatewayIDProp, err := navigateValue(opts, []string{"nat_gateway_id"}, nil)
if err != nil {
Expand Down
65 changes: 53 additions & 12 deletions huaweicloud/resource_huaweicloud_nat_dnat_rule_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,32 @@ func TestAccNatDnat_basic(t *testing.T) {
})
}

func TestAccNatDnat_protocol(t *testing.T) {
randSuffix := acctest.RandString(5)
resourceName := "huaweicloud_nat_dnat_rule.dnat"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckNatDnatDestroy,
Steps: []resource.TestStep{
{
Config: testAccNatV2DnatRule_protocol(randSuffix),
Check: resource.ComposeTestCheckFunc(
testAccCheckNatDnatExists(),
resource.TestCheckResourceAttr(resourceName, "protocol", "any"),
resource.TestCheckResourceAttr(resourceName, "status", "ACTIVE"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckNatDnatDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
client, err := config.natV2Client(HW_REGION_NAME)
Expand Down Expand Up @@ -112,10 +138,8 @@ func testAccCheckNatDnatExists() resource.TestCheckFunc {
}
}

func testAccNatV2DnatRule_basic(suffix string) string {
func testAccNatV2DnatRule_base(suffix string) string {
return fmt.Sprintf(`
%s
resource "huaweicloud_vpc_eip" "eip_1" {
publicip {
type = "5_bgp"
Expand All @@ -128,14 +152,6 @@ resource "huaweicloud_vpc_eip" "eip_1" {
}
}
resource "huaweicloud_nat_gateway" "nat_1" {
name = "nat-gateway-basic-%s"
description = "test for terraform"
spec = "1"
internal_network_id = huaweicloud_vpc_subnet.subnet_1.id
router_id = huaweicloud_vpc.vpc_1.id
}
resource "huaweicloud_compute_instance" "instance_1" {
name = "instance-acc-test-%s"
security_groups = ["default"]
Expand All @@ -148,6 +164,14 @@ resource "huaweicloud_compute_instance" "instance_1" {
foo = "bar"
}
}
`, suffix, HW_AVAILABILITY_ZONE)
}

func testAccNatV2DnatRule_basic(suffix string) string {
return fmt.Sprintf(`
%s
%s
resource "huaweicloud_nat_dnat_rule" "dnat" {
nat_gateway_id = huaweicloud_nat_gateway.nat_1.id
Expand All @@ -157,5 +181,22 @@ resource "huaweicloud_nat_dnat_rule" "dnat" {
internal_service_port = 993
external_service_port = 242
}
`, testAccNatPreCondition(suffix), suffix, suffix, HW_AVAILABILITY_ZONE)
`, testAccNatV2Gateway_basic(suffix), testAccNatV2DnatRule_base(suffix))
}

func testAccNatV2DnatRule_protocol(suffix string) string {
return fmt.Sprintf(`
%s
%s
resource "huaweicloud_nat_dnat_rule" "dnat" {
nat_gateway_id = huaweicloud_nat_gateway.nat_1.id
floating_ip_id = huaweicloud_vpc_eip.eip_1.id
private_ip = huaweicloud_compute_instance.instance_1.network.0.fixed_ip_v4
protocol = "any"
internal_service_port = 0
external_service_port = 0
}
`, testAccNatV2Gateway_basic(suffix), testAccNatV2DnatRule_base(suffix))
}

0 comments on commit 655f9d0

Please sign in to comment.