From 655f9d0c8d745355e5db984a93a5b116f64b1edc Mon Sep 17 00:00:00 2001 From: Lance52259 <74246744+Lance52259@users.noreply.github.com> Date: Mon, 1 Feb 2021 10:05:42 +0800 Subject: [PATCH] Fix the problem that any type of DNAT rule cannot open all ports (#886) --- .../resource_huaweicloud_nat_dnat_rule_v2.go | 16 +---- ...ource_huaweicloud_nat_dnat_rule_v2_test.go | 65 +++++++++++++++---- 2 files changed, 55 insertions(+), 26 deletions(-) diff --git a/huaweicloud/resource_huaweicloud_nat_dnat_rule_v2.go b/huaweicloud/resource_huaweicloud_nat_dnat_rule_v2.go index 90e53e53c5..c391cbec81 100644 --- a/huaweicloud/resource_huaweicloud_nat_dnat_rule_v2.go +++ b/huaweicloud/resource_huaweicloud_nat_dnat_rule_v2.go @@ -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 { diff --git a/huaweicloud/resource_huaweicloud_nat_dnat_rule_v2_test.go b/huaweicloud/resource_huaweicloud_nat_dnat_rule_v2_test.go index f7123d0a53..6249217d59 100644 --- a/huaweicloud/resource_huaweicloud_nat_dnat_rule_v2_test.go +++ b/huaweicloud/resource_huaweicloud_nat_dnat_rule_v2_test.go @@ -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) @@ -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" @@ -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"] @@ -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 @@ -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)) }