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_vpn_gateway_nat_rule - support for port_range #16724

Merged
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
290 changes: 255 additions & 35 deletions internal/services/network/vpn_gateway_nat_rule_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -18,10 +19,10 @@ import (
)

func resourceVPNGatewayNatRule() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceVPNGatewayNatRuleCreateUpdate,
resource := &pluginsdk.Resource{
Create: resourceVPNGatewayNatRuleCreate,
Read: resourceVPNGatewayNatRuleRead,
Update: resourceVPNGatewayNatRuleCreateUpdate,
Update: resourceVPNGatewayNatRuleUpdate,
Delete: resourceVPNGatewayNatRuleDelete,

Timeouts: &pluginsdk.ResourceTimeout{
Expand Down Expand Up @@ -53,21 +54,63 @@ func resourceVPNGatewayNatRule() *pluginsdk.Resource {
ValidateFunc: validate.VpnGatewayID,
},

"external_address_space_mappings": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsCIDR,
"external_mapping": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: !features.FourPointOhBeta(),
ExactlyOneOf: func() []string {
out := []string{
"external_mapping",
}
if !features.FourPointOhBeta() {
out = append(out, "external_address_space_mappings")
}
return out
}(),
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"address_space": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsCIDR,
},

"port_range": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"internal_address_space_mappings": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsCIDR,
"internal_mapping": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: !features.FourPointOhBeta(),
ExactlyOneOf: func() []string {
out := []string{
"internal_mapping",
}
if !features.FourPointOhBeta() {
out = append(out, "internal_address_space_mappings")
}
return out
}(),
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"address_space": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.IsCIDR,
},

"port_range": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

Expand Down Expand Up @@ -103,12 +146,56 @@ func resourceVPNGatewayNatRule() *pluginsdk.Resource {
},
},
}

if !features.FourPointOhBeta() {
resource.Schema["external_address_space_mappings"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
Deprecated: "`external_address_space_mappings` will be removed in favour of the property `external_mapping` in version 4.0 of the AzureRM Provider.",
ExactlyOneOf: func() []string {
out := []string{
"external_mapping",
}
if !features.FourPointOhBeta() {
out = append(out, "external_address_space_mappings")
}
return out
}(),
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsCIDR,
},
}

resource.Schema["internal_address_space_mappings"] = &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
Deprecated: "`internal_address_space_mappings` will be removed in favour of the property `internal_mapping` in version 4.0 of the AzureRM Provider.",
ExactlyOneOf: func() []string {
out := []string{
"internal_mapping",
}
if !features.FourPointOhBeta() {
out = append(out, "internal_address_space_mappings")
}
return out
}(),
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.IsCIDR,
},
}
}

return resource
}

func resourceVPNGatewayNatRuleCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
func resourceVPNGatewayNatRuleCreate(d *pluginsdk.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).Network.NatRuleClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

vpnGatewayId, err := parse.VpnGatewayID(d.Get("vpn_gateway_id").(string))
Expand All @@ -118,39 +205,53 @@ func resourceVPNGatewayNatRuleCreateUpdate(d *pluginsdk.ResourceData, meta inter

id := parse.NewVpnGatewayNatRuleID(subscriptionId, d.Get("resource_group_name").(string), vpnGatewayId.Name, d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.VpnGatewayName, id.NatRuleName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}
existing, err := client.Get(ctx, id.ResourceGroup, id.VpnGatewayName, id.NatRuleName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_vpn_gateway_nat_rule", id.ID())
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}
if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_vpn_gateway_nat_rule", id.ID())
}

props := network.VpnGatewayNatRule{
Name: utils.String(d.Get("name").(string)),
VpnGatewayNatRuleProperties: &network.VpnGatewayNatRuleProperties{
ExternalMappings: expandVpnGatewayNatRuleAddressSpaceMappings(d.Get("external_address_space_mappings").(*pluginsdk.Set).List()),
InternalMappings: expandVpnGatewayNatRuleAddressSpaceMappings(d.Get("internal_address_space_mappings").(*pluginsdk.Set).List()),
Mode: network.VpnNatRuleMode(d.Get("mode").(string)),
Type: network.VpnNatRuleType(d.Get("type").(string)),
Mode: network.VpnNatRuleMode(d.Get("mode").(string)),
Type: network.VpnNatRuleType(d.Get("type").(string)),
},
}

if !features.FourPointOhBeta() {
if v, ok := d.GetOk("external_address_space_mappings"); ok {
props.VpnGatewayNatRuleProperties.ExternalMappings = expandVpnGatewayNatRuleAddressSpaceMappings(v.([]interface{}))
}

if v, ok := d.GetOk("internal_address_space_mappings"); ok {
props.VpnGatewayNatRuleProperties.InternalMappings = expandVpnGatewayNatRuleAddressSpaceMappings(v.([]interface{}))
}
}

if v, ok := d.GetOk("external_mapping"); ok {
props.VpnGatewayNatRuleProperties.ExternalMappings = expandVpnGatewayNatRuleMappings(v.([]interface{}))
}

if v, ok := d.GetOk("internal_mapping"); ok {
props.VpnGatewayNatRuleProperties.InternalMappings = expandVpnGatewayNatRuleMappings(v.([]interface{}))
}

if v, ok := d.GetOk("ip_configuration_id"); ok {
props.VpnGatewayNatRuleProperties.IPConfigurationID = utils.String(v.(string))
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.VpnGatewayName, id.NatRuleName, props)
if err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
return fmt.Errorf("creating %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of the %s: %+v", id, err)
return fmt.Errorf("waiting for creation of the %s: %+v", id, err)
}

d.SetId(id.ID())
Expand Down Expand Up @@ -188,18 +289,91 @@ func resourceVPNGatewayNatRuleRead(d *pluginsdk.ResourceData, meta interface{})
d.Set("mode", props.Mode)
d.Set("type", props.Type)

if err := d.Set("external_address_space_mappings", flattenVpnGatewayNatRuleAddressSpaceMappings(props.ExternalMappings)); err != nil {
return fmt.Errorf("setting `external_address_space_mappings`: %+v", err)
if !features.FourPointOhBeta() {
if err := d.Set("external_address_space_mappings", flattenVpnGatewayNatRuleAddressSpaceMappings(props.ExternalMappings)); err != nil {
return fmt.Errorf("setting `external_address_space_mappings`: %+v", err)
}

if err := d.Set("internal_address_space_mappings", flattenVpnGatewayNatRuleAddressSpaceMappings(props.InternalMappings)); err != nil {
return fmt.Errorf("setting `internal_address_space_mappings`: %+v", err)
}
}

if err := d.Set("internal_address_space_mappings", flattenVpnGatewayNatRuleAddressSpaceMappings(props.InternalMappings)); err != nil {
return fmt.Errorf("setting `internal_address_space_mappings`: %+v", err)
if err := d.Set("external_mapping", flattenVpnGatewayNatRuleMappings(props.ExternalMappings)); err != nil {
return fmt.Errorf("setting `external_mapping`: %+v", err)
}

if err := d.Set("internal_mapping", flattenVpnGatewayNatRuleMappings(props.InternalMappings)); err != nil {
return fmt.Errorf("setting `internal_mapping`: %+v", err)
}
}

return nil
}

func resourceVPNGatewayNatRuleUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
client := meta.(*clients.Client).Network.NatRuleClient
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

vpnGatewayId, err := parse.VpnGatewayID(d.Get("vpn_gateway_id").(string))
if err != nil {
return err
}

id := parse.NewVpnGatewayNatRuleID(subscriptionId, d.Get("resource_group_name").(string), vpnGatewayId.Name, d.Get("name").(string))

existing, err := client.Get(ctx, id.ResourceGroup, id.VpnGatewayName, id.NatRuleName)
if err != nil {
return err
}

props := network.VpnGatewayNatRule{
Name: utils.String(d.Get("name").(string)),
VpnGatewayNatRuleProperties: &network.VpnGatewayNatRuleProperties{
Mode: network.VpnNatRuleMode(d.Get("mode").(string)),
Type: network.VpnNatRuleType(d.Get("type").(string)),
ExternalMappings: existing.VpnGatewayNatRuleProperties.ExternalMappings,
InternalMappings: existing.VpnGatewayNatRuleProperties.InternalMappings,
},
}

// d.GetOk always returns true and the value that is set in the previous apply when the property has the attribute `Computed: true`. Hence, at this time d.GetOk cannot identify whether user sets the property in tf config so that it has to identify it via splitting create and update method and using d.HasChange
if !features.FourPointOhBeta() {
if ok := d.HasChange("external_address_space_mappings"); ok {
props.VpnGatewayNatRuleProperties.ExternalMappings = expandVpnGatewayNatRuleAddressSpaceMappings(d.Get("external_address_space_mappings").([]interface{}))
}

if ok := d.HasChange("internal_address_space_mappings"); ok {
props.VpnGatewayNatRuleProperties.InternalMappings = expandVpnGatewayNatRuleAddressSpaceMappings(d.Get("internal_address_space_mappings").([]interface{}))
}
}

if ok := d.HasChange("external_mapping"); ok {
props.VpnGatewayNatRuleProperties.ExternalMappings = expandVpnGatewayNatRuleMappings(d.Get("external_mapping").([]interface{}))
}

if ok := d.HasChange("internal_mapping"); ok {
props.VpnGatewayNatRuleProperties.InternalMappings = expandVpnGatewayNatRuleMappings(d.Get("internal_mapping").([]interface{}))
}

if v, ok := d.GetOk("ip_configuration_id"); ok {
props.VpnGatewayNatRuleProperties.IPConfigurationID = utils.String(v.(string))
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.VpnGatewayName, id.NatRuleName, props)
if err != nil {
return fmt.Errorf("updating %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for update of the %s: %+v", id, err)
}

return resourceVPNGatewayNatRuleRead(d, meta)
}

func resourceVPNGatewayNatRuleDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.NatRuleClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
Expand Down Expand Up @@ -234,6 +408,52 @@ func expandVpnGatewayNatRuleAddressSpaceMappings(input []interface{}) *[]network
return &results
}

func expandVpnGatewayNatRuleMappings(input []interface{}) *[]network.VpnNatRuleMapping {
results := make([]network.VpnNatRuleMapping, 0)

for _, item := range input {
v := item.(map[string]interface{})

result := network.VpnNatRuleMapping{
AddressSpace: utils.String(v["address_space"].(string)),
}

if portRange := v["port_range"].(string); portRange != "" {
result.PortRange = utils.String(portRange)
}

results = append(results, result)
}

return &results
}

func flattenVpnGatewayNatRuleMappings(input *[]network.VpnNatRuleMapping) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

for _, item := range *input {
var addressSpace string
if item.AddressSpace != nil {
addressSpace = *item.AddressSpace
}

var portRange string
if item.PortRange != nil {
portRange = *item.PortRange
}

results = append(results, map[string]interface{}{
"address_space": addressSpace,
"port_range": portRange,
})
}

return results
}

func flattenVpnGatewayNatRuleAddressSpaceMappings(input *[]network.VpnNatRuleMapping) []interface{} {
results := make([]interface{}, 0)
if input == nil {
Expand Down
Loading