From 1d24b6502392586766fc8899a7321529274e903d Mon Sep 17 00:00:00 2001 From: David MICHENEAU Date: Thu, 14 Nov 2024 22:44:19 +0100 Subject: [PATCH] chore: fix nul pointer when datasource was not found --- .golangci.yml | 1 - docs/resources/edgegateway_nat_rule.md | 2 +- internal/provider/edgegw/nat_rule_resource.go | 1 + internal/provider/edgegw/nat_rule_schema.go | 6 +- .../edgegw_nat_rule_datasource_test.go | 73 ++- .../testsacc/edgegw_nat_rule_resource_test.go | 557 ++++++++++-------- 6 files changed, 364 insertions(+), 276 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 0c111473..eba86a0c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -73,7 +73,6 @@ linters: - gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false] - gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false] - godox # Tool for detection of FIXME, TODO and other comment keywords [fast: true, auto-fix: false] - - gomnd # An analyzer to detect magic numbers. [fast: true, auto-fix: false] - lll # Reports long lines [fast: true, auto-fix: false] - maintidx # maintidx measures the maintainability index of each function. [fast: true, auto-fix: false] - nestif # Reports deeply nested if statements [fast: true, auto-fix: false] diff --git a/docs/resources/edgegateway_nat_rule.md b/docs/resources/edgegateway_nat_rule.md index e42ecac4..2ceee31a 100644 --- a/docs/resources/edgegateway_nat_rule.md +++ b/docs/resources/edgegateway_nat_rule.md @@ -61,7 +61,6 @@ resource "cloudavenue_edgegateway_nat_rule" "example-reflexive" { ### Required -- `external_address` (String) The external address for the NAT Rule. This must be supplied as a single IP or Network CIDR. For a DNAT rule, this is the external facing IP Address for incoming traffic. For an SNAT rule, this is the external facing IP Address for outgoing traffic. These IPs are typically allocated/suballocated IP Addresses on the Edge Gateway. For a REFLEXIVE rule, these are the external facing IPs. - `internal_address` (String) The internal address for the NAT Rule. This must be supplied as a single IP or Network CIDR. For a DNAT rule, this is the internal IP address for incoming traffic. For an SNAT rule, this is the internal IP Address for outgoing traffic. For a REFLEXIVE rule, these are the internal IPs. These IPs are typically the Private IPs that are allocated to workloads. - `name` (String) (ForceNew) The Name of the Nat Rule. - `rule_type` (String) (ForceNew) Nat Rule type. Value must be one of: `DNAT` (Rule translates the external IP to an internal IP and is used for inbound traffic.), `NO_DNAT` (Prevents external IP translation.), `SNAT` (Translates an internal IP to an external IP and is used for outbound traffic.), `NO_SNAT` (Prevents internal IP translation.), `REFLEXIVE` (This translates an internal IP to an external IP and vice versa.). @@ -73,6 +72,7 @@ resource "cloudavenue_edgegateway_nat_rule" "example-reflexive" { - `edge_gateway_id` (String) (ForceNew) The ID of the Edge Gateway. Ensure that one and only one attribute from this collection is set : `edge_gateway_name`, `edge_gateway_id`. - `edge_gateway_name` (String) (ForceNew) The Name of the Edge Gateway. Ensure that one and only one attribute from this collection is set : `edge_gateway_name`, `edge_gateway_id`. - `enabled` (Boolean) Enable or Disable the Nat Rule. Value defaults to `true`. +- `external_address` (String) The external address for the NAT Rule. This must be supplied as a single IP or Network CIDR. For a DNAT rule, this is the external facing IP Address for incoming traffic. For an SNAT rule, this is the external facing IP Address for outgoing traffic. These IPs are typically allocated/suballocated IP Addresses on the Edge Gateway. For a REFLEXIVE rule, these are the external facing IPs. If the value of [`rule_type`](#rule_type) attribute is one of `NO_DNAT` or `NO_SNAT` this attribute is **NULL**. - `firewall_match` (String) You can set a firewall match rule to determine how firewall is applied during NAT. Value must be one of: `MATCH_INTERNAL_ADDRESS` (Applies firewall rule to the internal address of a NAT rule.), `MATCH_EXTERNAL_ADDRESS` (Applies firewall rule to the external address of a NAT rule.), `BYPASS` (Skip applying firewall rule to NAT rule.). - `priority` (Number) If an address has multiple NAT rule, you can assign these rule different priorities to determine the order in which they are applied. A lower value means a higher priority for this rule. Value defaults to `0`. - `snat_destination_address` (String) The destination addresses to match in the SNAT Rule. This must be supplied as a single IP or Network CIDR. Providing no value for this field results in match with ANY destination network. If the value of [`rule_type`](#rule_type) attribute is one of `DNAT`, `NO_DNAT` or `REFLEXIVE` this attribute is **NULL**. diff --git a/internal/provider/edgegw/nat_rule_resource.go b/internal/provider/edgegw/nat_rule_resource.go index a151a66c..d0f646ca 100644 --- a/internal/provider/edgegw/nat_rule_resource.go +++ b/internal/provider/edgegw/nat_rule_resource.go @@ -386,6 +386,7 @@ func (r *natRuleResource) read(planOrState *NATRuleModel) (stateRefreshed *NATRu } if err != nil { if govcd.ContainsNotFound(err) { + diags.AddError("Error NAT Rule Not Found", err.Error()) return nil, false, diags } diags.AddError("Error retrieving NAT Rule ID", err.Error()) diff --git a/internal/provider/edgegw/nat_rule_schema.go b/internal/provider/edgegw/nat_rule_schema.go index c324387e..4e534cd9 100644 --- a/internal/provider/edgegw/nat_rule_schema.go +++ b/internal/provider/edgegw/nat_rule_schema.go @@ -149,7 +149,11 @@ func natRuleSchema(_ context.Context) superschema.Schema { MarkdownDescription: "The external address for the NAT Rule. This must be supplied as a single IP or Network CIDR. For a DNAT rule, this is the external facing IP Address for incoming traffic. For an SNAT rule, this is the external facing IP Address for outgoing traffic. These IPs are typically allocated/suballocated IP Addresses on the Edge Gateway. For a REFLEXIVE rule, these are the external facing IPs.", }, Resource: &schemaR.StringAttribute{ - Required: true, + // Required: true, + Optional: true, + Validators: []validator.String{ + fstringvalidator.NullIfAttributeIsOneOf(path.MatchRoot("rule_type"), []attr.Value{types.StringValue("NO_DNAT"), types.StringValue("NO_SNAT")}), + }, // TODO - Validator of IP or IP/CIDR }, DataSource: &schemaD.StringAttribute{ diff --git a/internal/testsacc/edgegw_nat_rule_datasource_test.go b/internal/testsacc/edgegw_nat_rule_datasource_test.go index 16ff6cdb..8f2e1a74 100644 --- a/internal/testsacc/edgegw_nat_rule_datasource_test.go +++ b/internal/testsacc/edgegw_nat_rule_datasource_test.go @@ -1,32 +1,73 @@ package testsacc import ( + "context" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/helpers/testsacc" +) + +var _ testsacc.TestACC = &NATRuleDataSource{} + +const ( + NATRuleDataSourceName = testsacc.ResourceName("data.cloudavenue_edgegateway_nat_rule") ) -const testAccNATRuleDataSourceConfig = ` -data "cloudavenue_edgegateway_nat_rule" "example" { - depends_on = [cloudavenue_edgegateway.example_with_vdc, cloudavenue_edgegateway_nat_rule.example] - edge_gateway_id = cloudavenue_edgegateway.example_with_vdc.id - name = "example-snat" +type NATRuleDataSource struct{} + +func NewNATRuleDataSourceTest() testsacc.TestACC { + return &NATRuleDataSource{} } -` -func TestAccNatRuleDataSource(t *testing.T) { - dataSourceName := "data.cloudavenue_edgegateway_nat_rule.example" +// GetResourceName returns the name of the resource. +func (r *NATRuleDataSource) GetResourceName() string { + return NATRuleDataSourceName.String() +} +func (r *NATRuleDataSource) DependenciesConfig() (resp testsacc.DependenciesConfigResponse) { + resp.Append(GetResourceConfig()[NATRuleResourceName]().GetDefaultConfig) + return +} + +func (r *NATRuleDataSource) Tests(ctx context.Context) map[testsacc.TestName]func(ctx context.Context, resourceName string) testsacc.Test { + return map[testsacc.TestName]func(ctx context.Context, resourceName string) testsacc.Test{ + // * Test One (with edge_gateway_id) + "example": func(_ context.Context, _ string) testsacc.Test { + return testsacc.Test{ + // ! Create testing + Create: testsacc.TFConfig{ + TFConfig: ` + data "cloudavenue_edgegateway_nat_rule" "example" { + edge_gateway_id = cloudavenue_edgegateway.example.id + name = "example" + }`, + Checks: GetResourceConfig()[NATRuleResourceName]().GetDefaultChecks(), + }, + } + }, + // * Test Two (with edge_gateway_name) + "example_2": func(_ context.Context, _ string) testsacc.Test { + return testsacc.Test{ + // ! Create testing + Create: testsacc.TFConfig{ + TFConfig: ` + data "cloudavenue_edgegateway_nat_rule" "example" { + edge_gateway_name = cloudavenue_edgegateway.example.name + name = "example" + }`, + Checks: GetResourceConfig()[NATRuleResourceName]().GetDefaultChecks(), + }, + } + }, + } +} + +func TestAccNATRuleDataSource(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { TestAccPreCheck(t) }, ProtoV6ProviderFactories: TestAccProtoV6ProviderFactories, - Steps: []resource.TestStep{ - // Read testing - { - // Apply test - Config: ConcatTests(testAccNATRuleResourceConfigSnat, testAccNATRuleDataSourceConfig, testAccEdgeGatewayResourceConfig), - Check: natRuleSnatTestCheck(dataSourceName), - }, - }, + Steps: testsacc.GenerateTests(&NATRuleDataSource{}), }) } diff --git a/internal/testsacc/edgegw_nat_rule_resource_test.go b/internal/testsacc/edgegw_nat_rule_resource_test.go index 4cf3684b..3fd48b15 100644 --- a/internal/testsacc/edgegw_nat_rule_resource_test.go +++ b/internal/testsacc/edgegw_nat_rule_resource_test.go @@ -1,285 +1,328 @@ package testsacc import ( + "context" "fmt" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/orange-cloudavenue/terraform-provider-cloudavenue/internal/helpers/testsacc" "github.com/orange-cloudavenue/terraform-provider-cloudavenue/pkg/uuid" ) -const testAccNATRuleResourceConfigSnat = ` -resource "cloudavenue_edgegateway_nat_rule" "example" { - edge_gateway_id = cloudavenue_edgegateway.example_with_vdc.id - - name = "example-snat" - rule_type = "SNAT" - description = "description SNAT example" - - # Using primary_ip from edge gateway - external_address = "89.32.25.10" - internal_address = "11.11.11.0/24" - snat_destination_address = "8.8.8.8" - - priority = 10 -} -` - -const testAccNATRuleResourceConfigDnat = ` -resource "cloudavenue_edgegateway_nat_rule" "example" { - edge_gateway_id = cloudavenue_edgegateway.example_with_vdc.id - - name = "example-dnat" - rule_type = "DNAT" - description = "description DNAT example" - - # Using primary_ip from edge gateway - external_address = "89.32.25.10" - internal_address = "11.11.11.4" - - dnat_external_port = "8080" -} -` - -const testAccNATRuleResourceConfigReflexive = ` -resource "cloudavenue_edgegateway_nat_rule" "example" { - edge_gateway_id = cloudavenue_edgegateway.example_with_vdc.id - - name = "example-reflexive" - rule_type = "REFLEXIVE" - description = "description REFLEXIVE example" - - # Using primary_ip from edge gateway - external_address = "89.32.25.10" - internal_address = "192.168.0.1" -} -` - -const testAccNATRuleResourceConfigUpdateSnat = ` -resource "cloudavenue_edgegateway_nat_rule" "example" { - edge_gateway_id = cloudavenue_edgegateway.example_with_vdc.id - - name = "example-snat" - rule_type = "SNAT" - description = "description SNAT example Updated!!" - - # Using primary_ip from edge gateway - external_address = "89.32.25.10" - internal_address = "11.11.11.0/24" - snat_destination_address = "9.9.9.9" - - priority = 0 -} -` - -const testAccNATRuleResourceConfigUpdateDnat = ` -resource "cloudavenue_edgegateway_nat_rule" "example" { - edge_gateway_id = cloudavenue_edgegateway.example_with_vdc.id - - name = "example-dnat" - rule_type = "DNAT" - description = "description DNAT example Updated!!" - - # Using primary_ip from edge gateway - external_address = "89.32.25.10" - internal_address = "4.11.11.11" - - priority = 25 -} -` - -const testAccNATRuleResourceConfigDnatWithVDCGroup = ` -resource "cloudavenue_edgegateway_nat_rule" "example" { - edge_gateway_id = cloudavenue_edgegateway.example_with_group.id - - name = "example-dnat" - rule_type = "DNAT" - description = "description DNAT example" - - # Using primary_ip from edge gateway - external_address = "89.32.25.10" - internal_address = "11.11.11.4" - - dnat_external_port = "8080" -} -` - -const testAccNATRuleResourceConfigUpdateDnatWithVDCGroup = ` -resource "cloudavenue_edgegateway_nat_rule" "example" { - edge_gateway_id = cloudavenue_edgegateway.example_with_group.id - - name = "example-dnat" - rule_type = "DNAT" - description = "description DNAT example Updated!!" - - # Using primary_ip from edge gateway - external_address = "89.32.25.10" - internal_address = "4.11.11.11" - - priority = 25 -} -` - -func natRuleSnatTestCheck(resourceName string) resource.TestCheckFunc { - return resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet(resourceName, "id"), - resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), - resource.TestCheckResourceAttrSet(resourceName, "external_address"), - resource.TestCheckResourceAttr(resourceName, "name", "example-snat"), - resource.TestCheckResourceAttr(resourceName, "description", "description SNAT example"), - resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.0/24"), - resource.TestCheckResourceAttr(resourceName, "rule_type", "SNAT"), - resource.TestCheckResourceAttr(resourceName, "snat_destination_address", "8.8.8.8"), - resource.TestCheckResourceAttr(resourceName, "priority", "10"), - resource.TestCheckNoResourceAttr(resourceName, "dnat_external_port"), - ) -} +var _ testsacc.TestACC = &NATRuleResource{} + +const ( + NATRuleResourceName = testsacc.ResourceName("cloudavenue_edgegateway_nat_rule") +) -func natRuleSnatUpdateTestCheck(resourceName string) resource.TestCheckFunc { - return resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet(resourceName, "id"), - resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), - resource.TestCheckResourceAttrSet(resourceName, "external_address"), - resource.TestCheckResourceAttr(resourceName, "name", "example-snat"), - resource.TestCheckResourceAttr(resourceName, "description", "description SNAT example Updated!!"), - resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.0/24"), - resource.TestCheckResourceAttr(resourceName, "rule_type", "SNAT"), - resource.TestCheckResourceAttr(resourceName, "snat_destination_address", "9.9.9.9"), - resource.TestCheckResourceAttr(resourceName, "priority", "0"), - resource.TestCheckNoResourceAttr(resourceName, "dnat_external_port"), - ) +type NATRuleResource struct{} + +func NewNATRuleResourceTest() testsacc.TestACC { + return &NATRuleResource{} } -func natRuleDnatTestCheck(resourceName string) resource.TestCheckFunc { - return resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet(resourceName, "id"), - resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), - resource.TestCheckResourceAttrSet(resourceName, "external_address"), - resource.TestCheckResourceAttr(resourceName, "name", "example-dnat"), - resource.TestCheckResourceAttr(resourceName, "description", "description DNAT example"), - resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.4"), - resource.TestCheckResourceAttr(resourceName, "rule_type", "DNAT"), - resource.TestCheckResourceAttr(resourceName, "dnat_external_port", "8080"), - resource.TestCheckResourceAttr(resourceName, "priority", "0"), - resource.TestCheckNoResourceAttr(resourceName, "snat_destination_address"), - ) +func (r *NATRuleResource) GetResourceName() string { + return NATRuleResourceName.String() } -func natRuleDnatUpdateTestCheck(resourceName string) resource.TestCheckFunc { - return resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet(resourceName, "id"), - resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), - resource.TestCheckResourceAttrSet(resourceName, "external_address"), - resource.TestCheckResourceAttr(resourceName, "name", "example-dnat"), - resource.TestCheckResourceAttr(resourceName, "description", "description DNAT example Updated!!"), - resource.TestCheckResourceAttr(resourceName, "internal_address", "4.11.11.11"), - resource.TestCheckResourceAttr(resourceName, "rule_type", "DNAT"), - resource.TestCheckNoResourceAttr(resourceName, "dnat_external_port"), - resource.TestCheckResourceAttr(resourceName, "priority", "25"), - resource.TestCheckNoResourceAttr(resourceName, "snat_destination_address"), - ) +func (r *NATRuleResource) DependenciesConfig() (resp testsacc.DependenciesConfigResponse) { + resp.Append(GetResourceConfig()[EdgeGatewayResourceName]().GetDefaultConfig) + return } -func natRuleReflexiveTestCheck(resourceName string) resource.TestCheckFunc { - return resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttrSet(resourceName, "id"), - resource.TestCheckResourceAttrSet(resourceName, "edge_gateway_id"), - resource.TestCheckResourceAttrSet(resourceName, "external_address"), - resource.TestCheckResourceAttr(resourceName, "name", "example-reflexive"), - resource.TestCheckResourceAttr(resourceName, "description", "description REFLEXIVE example"), - resource.TestCheckResourceAttr(resourceName, "internal_address", "192.168.0.1"), - resource.TestCheckResourceAttr(resourceName, "rule_type", "REFLEXIVE"), - resource.TestCheckResourceAttr(resourceName, "priority", "0"), - resource.TestCheckNoResourceAttr(resourceName, "snat_destination_address"), - resource.TestCheckNoResourceAttr(resourceName, "dnat_external_port"), - ) +func (r *NATRuleResource) Tests(ctx context.Context) map[testsacc.TestName]func(ctx context.Context, resourceName string) testsacc.Test { + return map[testsacc.TestName]func(ctx context.Context, resourceName string) testsacc.Test{ + // * First test named "example" + "example": func(_ context.Context, resourceName string) testsacc.Test { + return testsacc.Test{ + CommonChecks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), + resource.TestCheckResourceAttr(resourceName, "name", "example"), + resource.TestCheckResourceAttrSet(resourceName, "description"), + resource.TestCheckResourceAttrSet(resourceName, "internal_address"), + resource.TestCheckResourceAttrSet(resourceName, "priority"), + }, + // ! Create testing + Create: testsacc.TFConfig{ + TFConfig: testsacc.GenerateFromTemplate(resourceName, ` + resource "cloudavenue_edgegateway_nat_rule" "example" { + edge_gateway_id = cloudavenue_edgegateway.example.id + + name = "example" + rule_type = "SNAT" + description = "description SNAT example" + + # Using primary_ip from edge gateway + external_address = "89.32.25.10" + internal_address = "11.11.11.0/24" + snat_destination_address = "8.8.8.8" + + priority = 10 + }`), + Checks: []resource.TestCheckFunc{ + // ! base checks + resource.TestCheckResourceAttr(resourceName, "external_address", "89.32.25.10"), + resource.TestCheckResourceAttr(resourceName, "description", "description SNAT example"), + resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.0/24"), + resource.TestCheckResourceAttr(resourceName, "rule_type", "SNAT"), + resource.TestCheckResourceAttr(resourceName, "snat_destination_address", "8.8.8.8"), + resource.TestCheckResourceAttr(resourceName, "priority", "10"), + }, + }, + // ! Update testing + Updates: []testsacc.TFConfig{ + { + TFConfig: testsacc.GenerateFromTemplate(resourceName, ` + resource "cloudavenue_edgegateway_nat_rule" "example" { + edge_gateway_id = cloudavenue_edgegateway.example.id + + name = "example" + rule_type = "SNAT" + description = "description SNAT example Updated!!" + + # Using primary_ip from edge gateway + external_address = "89.32.25.10" + internal_address = "11.11.11.0/24" + snat_destination_address = "9.9.9.9" + + priority = 0 + }`), + Checks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttr(resourceName, "external_address", "89.32.25.10"), + resource.TestCheckResourceAttr(resourceName, "description", "description SNAT example Updated!!"), + resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.0/24"), + resource.TestCheckResourceAttr(resourceName, "rule_type", "SNAT"), + resource.TestCheckResourceAttr(resourceName, "snat_destination_address", "9.9.9.9"), + resource.TestCheckResourceAttr(resourceName, "priority", "0"), + resource.TestCheckNoResourceAttr(resourceName, "dnat_external_port"), + }, + }, + }, + // ! Imports testing + Imports: []testsacc.TFImport{ + { + ImportStateIDBuilder: []string{"edge_gateway_id", "name"}, + ImportState: true, + ImportStateVerify: true, + ImportStateIDFunc: testAccNATRuleResourceImportStateIDFuncWithIDAndName(resourceName), + }, + { + ImportStateIDBuilder: []string{"edge_gateway_name", "id"}, + ImportState: true, + ImportStateVerify: true, + ImportStateIDFunc: testAccNATRuleResourceImportStateIDFuncWithNameAndID(resourceName), + }, + }, + } + }, + "example_no_snat": func(_ context.Context, resourceName string) testsacc.Test { + return testsacc.Test{ + CommonChecks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), + resource.TestCheckResourceAttr(resourceName, "name", "example-no-snat"), + resource.TestCheckResourceAttr(resourceName, "description", "description NO SNAT example"), + resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.0/24"), + resource.TestCheckResourceAttr(resourceName, "rule_type", "NO_SNAT"), + resource.TestCheckResourceAttr(resourceName, "priority", "10"), + }, + // ! Create testing + Create: testsacc.TFConfig{ + TFConfig: testsacc.GenerateFromTemplate(resourceName, ` + resource "cloudavenue_edgegateway_nat_rule" "example_no_snat" { + edge_gateway_id = cloudavenue_edgegateway.example.id + + name = "example-no-snat" + rule_type = "NO_SNAT" + description = "description NO SNAT example" + + # Using primary_ip from edge gateway + internal_address = "11.11.11.0/24" + + priority = 10 + }`), + Checks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), + resource.TestCheckResourceAttr(resourceName, "name", "example-no-snat"), + resource.TestCheckResourceAttr(resourceName, "description", "description NO SNAT example"), + resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.0/24"), + resource.TestCheckResourceAttr(resourceName, "rule_type", "NO_SNAT"), + resource.TestCheckResourceAttr(resourceName, "priority", "10"), + }, + }, + } + }, + "example_dnat": func(_ context.Context, resourceName string) testsacc.Test { + return testsacc.Test{ + CommonChecks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), + resource.TestCheckResourceAttr(resourceName, "name", "example-dnat"), + resource.TestCheckResourceAttr(resourceName, "rule_type", "DNAT"), + }, + // ! Create testing + Create: testsacc.TFConfig{ + TFConfig: testsacc.GenerateFromTemplate(resourceName, ` + resource "cloudavenue_edgegateway_nat_rule" "example_dnat" { + edge_gateway_id = cloudavenue_edgegateway.example.id + + name = "example-dnat" + rule_type = "DNAT" + description = "description DNAT example" + + # Using primary_ip from edge gateway + external_address = "89.32.25.10" + internal_address = "11.11.11.4" + + dnat_external_port = "8080" + }`), + Checks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "external_address"), + resource.TestCheckResourceAttr(resourceName, "description", "description DNAT example"), + resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.4"), + resource.TestCheckResourceAttr(resourceName, "dnat_external_port", "8080"), + resource.TestCheckResourceAttr(resourceName, "priority", "0"), + resource.TestCheckNoResourceAttr(resourceName, "snat_destination_address"), + }, + }, + // ! Update testing + Updates: []testsacc.TFConfig{ + { + TFConfig: testsacc.GenerateFromTemplate(resourceName, ` + resource "cloudavenue_edgegateway_nat_rule" "example_dnat" { + edge_gateway_id = cloudavenue_edgegateway.example.id + + name = "example-dnat" + rule_type = "DNAT" + description = "description DNAT example Updated!!" + + # Using primary_ip from edge gateway + external_address = "89.32.25.10" + internal_address = "4.11.11.11" + + priority = 25 + }`), + Checks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "external_address"), + resource.TestCheckResourceAttr(resourceName, "description", "description DNAT example Updated!!"), + resource.TestCheckResourceAttr(resourceName, "internal_address", "4.11.11.11"), + resource.TestCheckNoResourceAttr(resourceName, "dnat_external_port"), + resource.TestCheckResourceAttr(resourceName, "priority", "25"), + resource.TestCheckNoResourceAttr(resourceName, "snat_destination_address"), + }, + }, + }, + } + }, + "example_reflexive": func(_ context.Context, resourceName string) testsacc.Test { + return testsacc.Test{ + CommonChecks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrSet(resourceName, "edge_gateway_id"), + resource.TestCheckResourceAttrSet(resourceName, "external_address"), + resource.TestCheckResourceAttr(resourceName, "name", "example-reflexive"), + resource.TestCheckResourceAttr(resourceName, "description", "description REFLEXIVE example"), + resource.TestCheckResourceAttr(resourceName, "priority", "0"), + }, + // ! Create testing + Create: testsacc.TFConfig{ + TFConfig: testsacc.GenerateFromTemplate(resourceName, ` + resource "cloudavenue_edgegateway_nat_rule" "example_reflexive" { + edge_gateway_id = cloudavenue_edgegateway.example.id + + name = "example-reflexive" + rule_type = "REFLEXIVE" + description = "description REFLEXIVE example" + + # Using primary_ip from edge gateway + external_address = "89.32.25.10" + internal_address = "192.168.0.1" + }`), + Checks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttr(resourceName, "internal_address", "192.168.0.1"), + resource.TestCheckResourceAttr(resourceName, "external_address", "89.32.25.10"), + resource.TestCheckResourceAttr(resourceName, "rule_type", "REFLEXIVE"), + }, + }, + } + }, + "example_with_vdc_group": func(_ context.Context, resourceName string) testsacc.Test { + return testsacc.Test{ + CommonChecks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "id"), + resource.TestCheckResourceAttrWith(resourceName, "edge_gateway_id", uuid.TestIsType(uuid.Gateway)), + resource.TestCheckResourceAttr(resourceName, "name", "example-with-vdc-group"), + resource.TestCheckResourceAttr(resourceName, "rule_type", "DNAT"), + }, + CommonDependencies: func() (resp testsacc.DependenciesConfigResponse) { + resp.Append(GetResourceConfig()[EdgeGatewayResourceName]().GetSpecificConfig("example_with_vdc_group")) + return + }, + // ! Create testing + Create: testsacc.TFConfig{ + TFConfig: testsacc.GenerateFromTemplate(resourceName, ` + resource "cloudavenue_edgegateway_nat_rule" "example_with_vdc_group" { + edge_gateway_id = cloudavenue_edgegateway.example_with_vdc_group.id + + name = "example-with-vdc-group" + rule_type = "DNAT" + description = "description DNAT example" + + # Using primary_ip from edge gateway + external_address = "89.32.25.10" + internal_address = "11.11.11.4" + + dnat_external_port = "8080" + }`), + Checks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "external_address"), + resource.TestCheckResourceAttr(resourceName, "description", "description DNAT example"), + resource.TestCheckResourceAttr(resourceName, "internal_address", "11.11.11.4"), + resource.TestCheckResourceAttr(resourceName, "dnat_external_port", "8080"), + resource.TestCheckResourceAttr(resourceName, "priority", "0"), + }, + }, + // ! Update testing + Updates: []testsacc.TFConfig{ + { + TFConfig: testsacc.GenerateFromTemplate(resourceName, ` + resource "cloudavenue_edgegateway_nat_rule" "example_with_vdc_group" { + edge_gateway_id = cloudavenue_edgegateway.example_with_group.id + + name = "example-with-vdc-group" + rule_type = "DNAT" + description = "description DNAT with VDC example Updated!!" + + # Using primary_ip from edge gateway + external_address = "89.32.25.10" + internal_address = "4.11.11.11" + + priority = 25 + }`), + Checks: []resource.TestCheckFunc{ + resource.TestCheckResourceAttrSet(resourceName, "external_address"), + resource.TestCheckResourceAttr(resourceName, "description", "description DNAT with VDC example Updated!!"), + resource.TestCheckResourceAttr(resourceName, "internal_address", "4.11.11.11"), + resource.TestCheckNoResourceAttr(resourceName, "dnat_external_port"), + resource.TestCheckResourceAttr(resourceName, "priority", "25"), + }, + }, + }, + } + }, + } } func TestAccNATRuleResource(t *testing.T) { - resourceName := "cloudavenue_edgegateway_nat_rule.example" - resource.Test(t, resource.TestCase{ PreCheck: func() { TestAccPreCheck(t) }, ProtoV6ProviderFactories: TestAccProtoV6ProviderFactories, - Steps: []resource.TestStep{ - // * Test with VDC - { - // Apply test Snat - Config: ConcatTests(testAccNATRuleResourceConfigSnat, testAccEdgeGatewayResourceConfig), - Check: natRuleSnatTestCheck(resourceName), - }, - { - // Update test Snat - Config: ConcatTests(testAccNATRuleResourceConfigUpdateSnat, testAccEdgeGatewayResourceConfig), - Check: natRuleSnatUpdateTestCheck(resourceName), - }, - { - // Delete test Snat - Destroy: true, - Config: ConcatTests(testAccNATRuleResourceConfigUpdateSnat, testAccEdgeGatewayResourceConfig), - }, - { - // Apply test Dnat - Config: ConcatTests(testAccNATRuleResourceConfigDnat, testAccEdgeGatewayResourceConfig), - Check: natRuleDnatTestCheck(resourceName), - }, - { - // Update test Dnat - Config: ConcatTests(testAccNATRuleResourceConfigUpdateDnat, testAccEdgeGatewayResourceConfig), - Check: natRuleDnatUpdateTestCheck(resourceName), - }, - { - // Delete test Dnat - Destroy: true, - Config: ConcatTests(testAccNATRuleResourceConfigUpdateDnat, testAccEdgeGatewayResourceConfig), - }, - { - // Apply test Reflexive - Config: ConcatTests(testAccNATRuleResourceConfigReflexive, testAccEdgeGatewayResourceConfig), - Check: natRuleReflexiveTestCheck(resourceName), - }, - { - // Import test ID and Name - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: testAccNATRuleResourceImportStateIDFuncWithIDAndName(resourceName), - }, - { - // Import test Name and ID - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: testAccNATRuleResourceImportStateIDFuncWithNameAndID(resourceName), - }, - // * Test with VDCGroup - { - // Apply test Dnat - Config: ConcatTests(testAccNATRuleResourceConfigDnatWithVDCGroup, testAccEdgeGatewayGroupResourceConfig), - Check: natRuleDnatTestCheck(resourceName), - }, - { - // Update test Dnat - Config: ConcatTests(testAccNATRuleResourceConfigUpdateDnatWithVDCGroup, testAccEdgeGatewayGroupResourceConfig), - Check: natRuleDnatUpdateTestCheck(resourceName), - }, - { - // Import test ID and Name - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: testAccNATRuleResourceImportStateIDFuncWithIDAndName(resourceName), - }, - { - // Import test Name and ID - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - ImportStateIdFunc: testAccNATRuleResourceImportStateIDFuncWithNameAndID(resourceName), - }, - }, + Steps: testsacc.GenerateTests(&NATRuleResource{}), }) }