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_virtual_network - removing crash points #1208

Merged
merged 2 commits into from
May 7, 2018
Merged
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
98 changes: 66 additions & 32 deletions azurerm/resource_arm_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func resourceArmVirtualNetwork() *schema.Resource {
ForceNew: true,
},

"resource_group_name": resourceGroupNameSchema(),

"location": locationSchema(),

"address_space": {
Type: schema.TypeList,
Required: true,
Expand Down Expand Up @@ -71,10 +75,6 @@ func resourceArmVirtualNetwork() *schema.Resource {
Set: resourceAzureSubnetHash,
},

"location": locationSchema(),

"resource_group_name": resourceGroupNameSchema(),

"tags": tagsSchema(),
},
}
Expand All @@ -90,7 +90,7 @@ func resourceArmVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) e
location := azureRMNormalizeLocation(d.Get("location").(string))
resGroup := d.Get("resource_group_name").(string)
tags := d.Get("tags").(map[string]interface{})
vnetProperties, vnetPropsErr := getVirtualNetworkProperties(ctx, d, meta)
vnetProperties, vnetPropsErr := expandVirtualNetworkProperties(ctx, d, meta)
if vnetPropsErr != nil {
return vnetPropsErr
}
Expand Down Expand Up @@ -162,39 +162,27 @@ func resourceArmVirtualNetworkRead(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("Error making Read request on Virtual Network %q (Resource Group %q): %+v", name, resGroup, err)
}

vnet := *resp.VirtualNetworkPropertiesFormat

// update appropriate values
d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("address_space", vnet.AddressSpace.AddressPrefixes)
if location := resp.Location; location != nil {
d.Set("location", azureRMNormalizeLocation(*location))
}

subnets := &schema.Set{
F: resourceAzureSubnetHash,
}

for _, subnet := range *vnet.Subnets {
s := map[string]interface{}{}

s["name"] = *subnet.Name
s["address_prefix"] = *subnet.SubnetPropertiesFormat.AddressPrefix
if subnet.SubnetPropertiesFormat.NetworkSecurityGroup != nil {
s["security_group"] = *subnet.SubnetPropertiesFormat.NetworkSecurityGroup.ID
if props := resp.VirtualNetworkPropertiesFormat; props != nil {
if space := props.AddressSpace; space != nil {
d.Set("address_space", space.AddressPrefixes)
}

subnets.Add(s)
}
d.Set("subnet", subnets)
subnets := flattenVirtualNetworkSubnets(props.Subnets)
if err := d.Set("subnet", subnets); err != nil {
return fmt.Errorf("Error setting `subnets`: %+v", err)
}

if vnet.DhcpOptions != nil && vnet.DhcpOptions.DNSServers != nil {
dnses := []string{}
for _, dns := range *vnet.DhcpOptions.DNSServers {
dnses = append(dnses, dns)
dnsServers := flattenVirtualNetworkDNSServers(props.DhcpOptions)
if err := d.Set("dns_servers", dnsServers); err != nil {
return fmt.Errorf("Error setting `dns_servers`: %+v", err)
}
d.Set("dns_servers", dnses)

}

flattenAndSetTags(d, resp.Tags)
Expand Down Expand Up @@ -234,7 +222,7 @@ func resourceArmVirtualNetworkDelete(d *schema.ResourceData, meta interface{}) e
return nil
}

func getVirtualNetworkProperties(ctx context.Context, d *schema.ResourceData, meta interface{}) (*network.VirtualNetworkPropertiesFormat, error) {
func expandVirtualNetworkProperties(ctx context.Context, d *schema.ResourceData, meta interface{}) (*network.VirtualNetworkPropertiesFormat, error) {
// first; get address space prefixes:
prefixes := []string{}
for _, prefix := range d.Get("address_space").([]interface{}) {
Expand Down Expand Up @@ -300,6 +288,51 @@ func getVirtualNetworkProperties(ctx context.Context, d *schema.ResourceData, me
// finally; return the struct:
return properties, nil
}
func flattenVirtualNetworkSubnets(input *[]network.Subnet) *schema.Set {
results := &schema.Set{
F: resourceAzureSubnetHash,
}

if subnets := input; subnets != nil {
for _, subnet := range *input {
output := map[string]interface{}{}

if name := subnet.Name; name != nil {
output["name"] = *name
}

if props := subnet.SubnetPropertiesFormat; props != nil {
if prefix := props.AddressPrefix; prefix != nil {
output["address_prefix"] = *prefix
}

if nsg := props.NetworkSecurityGroup; nsg != nil {
if nsg.ID != nil {
output["security_group"] = *nsg.ID
}
}
}

results.Add(output)
}
}

return results
}

func flattenVirtualNetworkDNSServers(input *network.DhcpOptions) []string {
results := make([]string, 0)

if input != nil {
if servers := input.DNSServers; servers != nil {
for _, dns := range *servers {
results = append(results, dns)
}
}
}

return results
}

func resourceAzureSubnetHash(v interface{}) int {
var buf bytes.Buffer
Expand All @@ -326,8 +359,9 @@ func getExistingSubnet(ctx context.Context, resGroup string, vnetName string, su
return nil, err
}

existingSubnet.SubnetPropertiesFormat = &network.SubnetPropertiesFormat{}
existingSubnet.SubnetPropertiesFormat.AddressPrefix = resp.SubnetPropertiesFormat.AddressPrefix
existingSubnet.SubnetPropertiesFormat = &network.SubnetPropertiesFormat{
AddressPrefix: resp.SubnetPropertiesFormat.AddressPrefix,
}

if resp.SubnetPropertiesFormat.NetworkSecurityGroup != nil {
existingSubnet.SubnetPropertiesFormat.NetworkSecurityGroup = resp.SubnetPropertiesFormat.NetworkSecurityGroup
Expand All @@ -338,7 +372,7 @@ func getExistingSubnet(ctx context.Context, resGroup string, vnetName string, su
}

if resp.SubnetPropertiesFormat.IPConfigurations != nil {
ips := make([]string, 0, len(*resp.SubnetPropertiesFormat.IPConfigurations))
ips := make([]string, 0)
for _, ip := range *resp.SubnetPropertiesFormat.IPConfigurations {
ips = append(ips, *ip.ID)
}
Expand Down