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

azurerm_palo_alto_local_rulestack_rule - correctly populate protocol property #26510

Merged
merged 5 commits into from
Jul 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
74 changes: 51 additions & 23 deletions internal/services/paloalto/local_rulestack_rule_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
certificates "github.com/hashicorp/go-azure-sdk/resource-manager/paloaltonetworks/2022-08-29/certificateobjectlocalrulestack"
"github.com/hashicorp/go-azure-sdk/resource-manager/paloaltonetworks/2022-08-29/localrules"
"github.com/hashicorp/go-azure-sdk/resource-manager/paloaltonetworks/2022-08-29/localrulestacks"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/locks"
"github.com/hashicorp/terraform-provider-azurerm/internal/sdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/paloalto/schema"
Expand Down Expand Up @@ -62,7 +63,7 @@ func (r LocalRuleStackRule) ResourceType() string {
}

func (r LocalRuleStackRule) Arguments() map[string]*pluginsdk.Schema {
return map[string]*pluginsdk.Schema{
schema := map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
Expand Down Expand Up @@ -146,15 +147,34 @@ func (r LocalRuleStackRule) Arguments() map[string]*pluginsdk.Schema {
Default: false,
},

"protocol": {
Type: pluginsdk.TypeString,
Optional: true,
Default: protocolApplicationDefault,
ValidateFunc: validate.ProtocolWithPort,
ConflictsWith: []string{"protocol_ports"},
"enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: true,
},

"protocol_ports": {
"source": schema.SourceSchema(),

"tags": commonschema.Tags(),
}

if !features.FourPointOhBeta() {
schema["protocol"] = &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Optional: true,
Default: protocolApplicationDefault,
ValidateFunc: validation.Any(
validate.ProtocolWithPort,
validation.StringInSlice([]string{protocolApplicationDefault}, false),
),
ConflictsWith: []string{"protocol_ports"},
// if `protocol_ports` is set, the default value should not be used
DiffSuppressFunc: func(k, old, new string, d *pluginsdk.ResourceData) bool {
return len(d.Get("protocol_ports").([]interface{})) > 0 && old == "" && new == protocolApplicationDefault
},
}

schema["protocol_ports"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
MinItems: 1,
Expand All @@ -163,18 +183,30 @@ func (r LocalRuleStackRule) Arguments() map[string]*pluginsdk.Schema {
ValidateFunc: validate.ProtocolWithPort,
},
ConflictsWith: []string{"protocol"},
},

"enabled": {
Type: pluginsdk.TypeBool,
}
} else {
schema["protocol"] = &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Optional: true,
Default: true,
},

"source": schema.SourceSchema(),

"tags": commonschema.Tags(),
ValidateFunc: validation.Any(
validate.ProtocolWithPort,
validation.StringInSlice([]string{protocolApplicationDefault}, false),
),
ExactlyOneOf: []string{"protocol", "protocol_ports"},
}

schema["protocol_ports"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
MinItems: 1,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validate.ProtocolWithPort,
},
ExactlyOneOf: []string{"protocol", "protocol_ports"},
}
}
return schema
}

func (r LocalRuleStackRule) Attributes() map[string]*pluginsdk.Schema {
Expand Down Expand Up @@ -341,11 +373,7 @@ func (r LocalRuleStackRule) Read() sdk.ResourceFunc {
}
state.NegateDestination = boolEnumAsBoolRule(props.NegateDestination)
state.NegateSource = boolEnumAsBoolRule(props.NegateSource)
if v := pointer.From(props.Protocol); v != "" && !strings.EqualFold(v, protocolApplicationDefault) {
state.Protocol = v
} else {
state.Protocol = protocolApplicationDefault
}
state.Protocol = pointer.From(props.Protocol)
state.ProtocolPorts = pointer.From(props.ProtocolPortList)
state.RuleEnabled = stateEnumAsBool(props.RuleState)
state.Source = schema.FlattenSource(props.Source, *id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
)

Expand Down Expand Up @@ -182,6 +183,34 @@ func (r LocalRuleResource) Exists(ctx context.Context, client *clients.Client, s
}

func (r LocalRuleResource) basic(data acceptance.TestData) string {
if features.FourPointOhBeta() {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%[1]s

resource "azurerm_palo_alto_local_rulestack_rule" "test" {
name = "testacc-palr-%[2]d"
rulestack_id = azurerm_palo_alto_local_rulestack.test.id
priority = 100
action = "Allow"
protocol = "application-default"

applications = ["any"]

destination {
cidrs = ["any"]
}

source {
cidrs = ["any"]
}
}
`, r.template(data), data.RandomInteger)
}

return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand Down Expand Up @@ -267,9 +296,30 @@ resource "azurerm_palo_alto_local_rulestack_rule" "test" {
}

func (r LocalRuleResource) requiresImport(data acceptance.TestData) string {
return fmt.Sprintf(`
if features.FourPointOhBeta() {
return fmt.Sprintf(`
%[1]s

resource "azurerm_palo_alto_local_rulestack_rule" "import" {
name = azurerm_palo_alto_local_rulestack_rule.test.name
rulestack_id = azurerm_palo_alto_local_rulestack_rule.test.rulestack_id
priority = azurerm_palo_alto_local_rulestack_rule.test.priority
action = "Allow"
applications = azurerm_palo_alto_local_rulestack_rule.test.applications
protocol = azurerm_palo_alto_local_rulestack_rule.test.protocol

destination {
cidrs = azurerm_palo_alto_local_rulestack_rule.test.destination.0.cidrs
}

source {
cidrs = azurerm_palo_alto_local_rulestack_rule.test.source.0.cidrs
}
}
`, r.basic(data), data.RandomInteger)
}

return fmt.Sprintf(`
%[1]s

resource "azurerm_palo_alto_local_rulestack_rule" "import" {
Expand All @@ -291,6 +341,34 @@ resource "azurerm_palo_alto_local_rulestack_rule" "import" {
}

func (r LocalRuleResource) withDestination(data acceptance.TestData) string {
if features.FourPointOhBeta() {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

%[1]s

resource "azurerm_palo_alto_local_rulestack_rule" "test" {
name = "testacc-palr-%[2]d"
rulestack_id = azurerm_palo_alto_local_rulestack.test.id
priority = 100
action = "Allow"
protocol = "application-default"

applications = ["any"]

destination {
countries = ["US", "GB"]
}

source {
countries = ["US", "GB"]
}
}
`, r.template(data), data.RandomInteger)
}

return fmt.Sprintf(`
provider "azurerm" {
features {}
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/palo_alto_local_rulestack_rule.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ resource "azurerm_palo_alto_local_rulestack_rule" "example" {
rulestack_id = azurerm_palo_alto_local_rulestack.example.id
priority = 1000
action = "Allow"
protocol = "application-default"

applications = ["any"]

Expand Down Expand Up @@ -85,6 +86,8 @@ The following arguments are supported:

* `protocol` - (Optional) The Protocol and port to use in the form `[protocol]:[port_number]` e.g. `TCP:8080` or `UDP:53`. Conflicts with `protocol_ports`. Defaults to `application-default`.

~> **NOTE**: In 4.0 or later versions of the provider, the default of `protocol` will no longer be set by provider, exactly one of `protocol` and `protocol_ports` must be specified. You need to explicitly specify `protocol="application-default"` to keep the the current default of the `protocol`.

* `protocol_ports` - (Optional) Specifies a list of Protocol:Port entries. E.g. `[ "TCP:80", "UDP:5431" ]`. Conflicts with `protocol`.

* `tags` - (Optional) A mapping of tags which should be assigned to the Palo Alto Local Rulestack Rule.
Expand Down
Loading