github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_virtual_network.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/network"
     9  	"github.com/hashicorp/terraform/helper/hashcode"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceArmVirtualNetwork() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceArmVirtualNetworkCreate,
    17  		Read:   resourceArmVirtualNetworkRead,
    18  		Update: resourceArmVirtualNetworkCreate,
    19  		Delete: resourceArmVirtualNetworkDelete,
    20  		Importer: &schema.ResourceImporter{
    21  			State: schema.ImportStatePassthrough,
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"name": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"address_space": {
    32  				Type:     schema.TypeList,
    33  				Required: true,
    34  				Elem: &schema.Schema{
    35  					Type: schema.TypeString,
    36  				},
    37  			},
    38  
    39  			"dns_servers": {
    40  				Type:     schema.TypeList,
    41  				Optional: true,
    42  				Elem: &schema.Schema{
    43  					Type: schema.TypeString,
    44  				},
    45  			},
    46  
    47  			"subnet": {
    48  				Type:     schema.TypeSet,
    49  				Optional: true,
    50  				Computed: true,
    51  				Elem: &schema.Resource{
    52  					Schema: map[string]*schema.Schema{
    53  						"name": {
    54  							Type:     schema.TypeString,
    55  							Required: true,
    56  						},
    57  						"address_prefix": {
    58  							Type:     schema.TypeString,
    59  							Required: true,
    60  						},
    61  						"security_group": {
    62  							Type:     schema.TypeString,
    63  							Optional: true,
    64  						},
    65  					},
    66  				},
    67  				Set: resourceAzureSubnetHash,
    68  			},
    69  
    70  			"location": {
    71  				Type:      schema.TypeString,
    72  				Required:  true,
    73  				ForceNew:  true,
    74  				StateFunc: azureRMNormalizeLocation,
    75  			},
    76  
    77  			"resource_group_name": {
    78  				Type:     schema.TypeString,
    79  				Required: true,
    80  				ForceNew: true,
    81  			},
    82  
    83  			"tags": tagsSchema(),
    84  		},
    85  	}
    86  }
    87  
    88  func resourceArmVirtualNetworkCreate(d *schema.ResourceData, meta interface{}) error {
    89  	client := meta.(*ArmClient)
    90  	vnetClient := client.vnetClient
    91  
    92  	log.Printf("[INFO] preparing arguments for Azure ARM virtual network creation.")
    93  
    94  	name := d.Get("name").(string)
    95  	location := d.Get("location").(string)
    96  	resGroup := d.Get("resource_group_name").(string)
    97  	tags := d.Get("tags").(map[string]interface{})
    98  
    99  	vnet := network.VirtualNetwork{
   100  		Name:       &name,
   101  		Location:   &location,
   102  		Properties: getVirtualNetworkProperties(d),
   103  		Tags:       expandTags(tags),
   104  	}
   105  
   106  	_, err := vnetClient.CreateOrUpdate(resGroup, name, vnet, make(chan struct{}))
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	read, err := vnetClient.Get(resGroup, name, "")
   112  	if err != nil {
   113  		return err
   114  	}
   115  	if read.ID == nil {
   116  		return fmt.Errorf("Cannot read Virtual Network %s (resource group %s) ID", name, resGroup)
   117  	}
   118  
   119  	d.SetId(*read.ID)
   120  
   121  	return resourceArmVirtualNetworkRead(d, meta)
   122  }
   123  
   124  func resourceArmVirtualNetworkRead(d *schema.ResourceData, meta interface{}) error {
   125  	vnetClient := meta.(*ArmClient).vnetClient
   126  
   127  	id, err := parseAzureResourceID(d.Id())
   128  	if err != nil {
   129  		return err
   130  	}
   131  	resGroup := id.ResourceGroup
   132  	name := id.Path["virtualNetworks"]
   133  
   134  	resp, err := vnetClient.Get(resGroup, name, "")
   135  	if err != nil {
   136  		return fmt.Errorf("Error making Read request on Azure virtual network %s: %s", name, err)
   137  	}
   138  	if resp.StatusCode == http.StatusNotFound {
   139  		d.SetId("")
   140  		return nil
   141  	}
   142  	vnet := *resp.Properties
   143  
   144  	// update appropriate values
   145  	d.Set("name", resp.Name)
   146  	d.Set("location", resp.Location)
   147  	d.Set("address_space", vnet.AddressSpace.AddressPrefixes)
   148  
   149  	subnets := &schema.Set{
   150  		F: resourceAzureSubnetHash,
   151  	}
   152  
   153  	for _, subnet := range *vnet.Subnets {
   154  		s := map[string]interface{}{}
   155  
   156  		s["name"] = *subnet.Name
   157  		s["address_prefix"] = *subnet.Properties.AddressPrefix
   158  		if subnet.Properties.NetworkSecurityGroup != nil {
   159  			s["security_group"] = *subnet.Properties.NetworkSecurityGroup.ID
   160  		}
   161  
   162  		subnets.Add(s)
   163  	}
   164  	d.Set("subnet", subnets)
   165  
   166  	dnses := []string{}
   167  	for _, dns := range *vnet.DhcpOptions.DNSServers {
   168  		dnses = append(dnses, dns)
   169  	}
   170  	d.Set("dns_servers", dnses)
   171  
   172  	flattenAndSetTags(d, resp.Tags)
   173  
   174  	return nil
   175  }
   176  
   177  func resourceArmVirtualNetworkDelete(d *schema.ResourceData, meta interface{}) error {
   178  	vnetClient := meta.(*ArmClient).vnetClient
   179  
   180  	id, err := parseAzureResourceID(d.Id())
   181  	if err != nil {
   182  		return err
   183  	}
   184  	resGroup := id.ResourceGroup
   185  	name := id.Path["virtualNetworks"]
   186  
   187  	_, err = vnetClient.Delete(resGroup, name, make(chan struct{}))
   188  
   189  	return err
   190  }
   191  
   192  func getVirtualNetworkProperties(d *schema.ResourceData) *network.VirtualNetworkPropertiesFormat {
   193  	// first; get address space prefixes:
   194  	prefixes := []string{}
   195  	for _, prefix := range d.Get("address_space").([]interface{}) {
   196  		prefixes = append(prefixes, prefix.(string))
   197  	}
   198  
   199  	// then; the dns servers:
   200  	dnses := []string{}
   201  	for _, dns := range d.Get("dns_servers").([]interface{}) {
   202  		dnses = append(dnses, dns.(string))
   203  	}
   204  
   205  	// then; the subnets:
   206  	subnets := []network.Subnet{}
   207  	if subs := d.Get("subnet").(*schema.Set); subs.Len() > 0 {
   208  		for _, subnet := range subs.List() {
   209  			subnet := subnet.(map[string]interface{})
   210  
   211  			name := subnet["name"].(string)
   212  			prefix := subnet["address_prefix"].(string)
   213  			secGroup := subnet["security_group"].(string)
   214  
   215  			var subnetObj network.Subnet
   216  			subnetObj.Name = &name
   217  			subnetObj.Properties = &network.SubnetPropertiesFormat{}
   218  			subnetObj.Properties.AddressPrefix = &prefix
   219  
   220  			if secGroup != "" {
   221  				subnetObj.Properties.NetworkSecurityGroup = &network.SecurityGroup{
   222  					ID: &secGroup,
   223  				}
   224  			}
   225  
   226  			subnets = append(subnets, subnetObj)
   227  		}
   228  	}
   229  
   230  	// finally; return the struct:
   231  	return &network.VirtualNetworkPropertiesFormat{
   232  		AddressSpace: &network.AddressSpace{
   233  			AddressPrefixes: &prefixes,
   234  		},
   235  		DhcpOptions: &network.DhcpOptions{
   236  			DNSServers: &dnses,
   237  		},
   238  		Subnets: &subnets,
   239  	}
   240  }
   241  
   242  func resourceAzureSubnetHash(v interface{}) int {
   243  	m := v.(map[string]interface{})
   244  	subnet := m["name"].(string) + m["address_prefix"].(string)
   245  	if securityGroup, present := m["security_group"]; present {
   246  		subnet = subnet + securityGroup.(string)
   247  	}
   248  	return hashcode.String(subnet)
   249  }
   250  
   251  func virtualNetworkStateRefreshFunc(client *ArmClient, resourceGroupName string, networkName string) resource.StateRefreshFunc {
   252  	return func() (interface{}, string, error) {
   253  		res, err := client.vnetClient.Get(resourceGroupName, networkName, "")
   254  		if err != nil {
   255  			return nil, "", fmt.Errorf("Error issuing read request in virtualNetworkStateRefreshFunc to Azure ARM for virtual network '%s' (RG: '%s'): %s", networkName, resourceGroupName, err)
   256  		}
   257  
   258  		return res, *res.Properties.ProvisioningState, nil
   259  	}
   260  }