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

Add simple validations to prevent crash #2305

Merged
merged 1 commit into from
Nov 14, 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
43 changes: 18 additions & 25 deletions azurerm/resource_arm_virtual_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-04-01/network"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

Expand All @@ -27,9 +28,10 @@ func resourceArmVirtualNetwork() *schema.Resource {

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.NoZeroValues,
},

"resource_group_name": resourceGroupNameSchema(),
Expand All @@ -39,16 +41,19 @@ func resourceArmVirtualNetwork() *schema.Resource {
"address_space": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
},

"dns_servers": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
},

Expand All @@ -59,12 +64,14 @@ func resourceArmVirtualNetwork() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"address_prefix": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
"security_group": {
Type: schema.TypeString,
Expand Down Expand Up @@ -227,19 +234,6 @@ func resourceArmVirtualNetworkDelete(d *schema.ResourceData, meta interface{}) e
}

func expandVirtualNetworkProperties(ctx context.Context, d *schema.ResourceData, meta interface{}) (*network.VirtualNetworkPropertiesFormat, error) {
// first; get address space prefixes:
prefixes := make([]string, 0)
for _, prefix := range d.Get("address_space").([]interface{}) {
prefixes = append(prefixes, prefix.(string))
}

// then; the dns servers:
dnses := make([]string, 0)
for _, dns := range d.Get("dns_servers").([]interface{}) {
dnses = append(dnses, dns.(string))
}

// then; the subnets:
subnets := make([]network.Subnet, 0)
if subs := d.Get("subnet").(*schema.Set); subs.Len() > 0 {
for _, subnet := range subs.List() {
Expand Down Expand Up @@ -282,14 +276,13 @@ func expandVirtualNetworkProperties(ctx context.Context, d *schema.ResourceData,

properties := &network.VirtualNetworkPropertiesFormat{
AddressSpace: &network.AddressSpace{
AddressPrefixes: &prefixes,
AddressPrefixes: utils.ExpandStringArray(d.Get("address_space").([]interface{})),
},
DhcpOptions: &network.DhcpOptions{
DNSServers: &dnses,
DNSServers: utils.ExpandStringArray(d.Get("dns_servers").([]interface{})),
},
Subnets: &subnets,
}
// finally; return the struct:
return properties, nil
}
func flattenVirtualNetworkSubnets(input *[]network.Subnet) *schema.Set {
Expand Down