Skip to content

Commit

Permalink
virtualnetwork: allow address_prefix to be null
Browse files Browse the repository at this point in the history
The azure subnet resource allows for multiple address prefixes using the
address_prefixes argument. The virtual network resource allows inline subnets,
but does not current support this argument. When the address_prefixes argument
is used, address_prefix will be null when terraform refreshes its state causing
terraform to panic. This code updates the virtual network resource to allow
address_prefixes to be used and for address_prefix to be null.

https://bugzilla.redhat.com/show_bug.cgi?id=1808973
  • Loading branch information
jhixson74 committed Mar 5, 2020
1 parent 4e2e024 commit 742f690
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions azurerm/internal/services/network/resource_arm_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,17 @@ func resourceArmVirtualNetwork() *schema.Resource {
},

"address_prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.NoEmptyStrings,
Type: schema.TypeString,
Optional: true,
Deprecated: "Use the `address_prefixes` property instead.",
ConflictsWith: []string{"subnet.0.address_prefixes"},
},

"address_prefixes": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ConflictsWith: []string{"subnet.0.address_prefix"},
},

"security_group": {
Expand Down Expand Up @@ -307,7 +315,29 @@ func expandVirtualNetworkProperties(ctx context.Context, d *schema.ResourceData,
}
log.Printf("[INFO] Completed GET of Subnet props ")

prefix := subnet["address_prefix"].(string)
var prefixSet bool
properties := network.SubnetPropertiesFormat{}
if value, ok := subnet["address_prefixes"]; ok {
var addressPrefixes []string
for _, item := range value.([]interface{}) {
addressPrefixes = append(addressPrefixes, item.(string))
}
properties.AddressPrefixes = &addressPrefixes
prefixSet = len(addressPrefixes) > 0
}
if value, ok := subnet["address_prefix"]; ok {
addressPrefix := value.(string)
properties.AddressPrefix = &addressPrefix
prefixSet = len(addressPrefix) > 0
}
if properties.AddressPrefixes != nil && len(*properties.AddressPrefixes) == 1 {
properties.AddressPrefix = &(*properties.AddressPrefixes)[0]
properties.AddressPrefixes = nil
}
if !prefixSet {
return nil, fmt.Errorf("[ERROR] either address_prefix or address_prefixes is required")
}

secGroup := subnet["security_group"].(string)

//set the props from config and leave the rest intact
Expand All @@ -316,7 +346,8 @@ func expandVirtualNetworkProperties(ctx context.Context, d *schema.ResourceData,
subnetObj.SubnetPropertiesFormat = &network.SubnetPropertiesFormat{}
}

subnetObj.SubnetPropertiesFormat.AddressPrefix = &prefix
subnetObj.SubnetPropertiesFormat.AddressPrefixes = properties.AddressPrefixes
subnetObj.SubnetPropertiesFormat.AddressPrefix = properties.AddressPrefix

if secGroup != "" {
subnetObj.SubnetPropertiesFormat.NetworkSecurityGroup = &network.SecurityGroup{
Expand Down Expand Up @@ -399,6 +430,14 @@ func flattenVirtualNetworkSubnets(input *[]network.Subnet) *schema.Set {
}

if props := subnet.SubnetPropertiesFormat; props != nil {
if prefixes := props.AddressPrefixes; prefixes != nil {
var addressPrefixes []string
for _, prefix := range *prefixes {
addressPrefixes = append(addressPrefixes, prefix)
}
output["address_prefixes"] = addressPrefixes
}

if prefix := props.AddressPrefix; prefix != nil {
output["address_prefix"] = *prefix
}
Expand Down Expand Up @@ -434,8 +473,21 @@ func resourceAzureSubnetHash(v interface{}) int {

if m, ok := v.(map[string]interface{}); ok {
buf.WriteString(m["name"].(string))
buf.WriteString(m["address_prefix"].(string))

if v, ok := m["address_prefixes"]; ok {
switch v.(type) {
case []string:
for _, a := range v.([]string) {
buf.WriteString(a)
}
case []interface{}:
for _, a := range v.([]interface{}) {
buf.WriteString(a.(string))
}
}
}
if v, ok := m["address_prefix"]; ok {
buf.WriteString(v.(string))
}
if v, ok := m["security_group"]; ok {
buf.WriteString(v.(string))
}
Expand Down

0 comments on commit 742f690

Please sign in to comment.