github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_subnet.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/resource"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceArmSubnet() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceArmSubnetCreate,
    16  		Read:   resourceArmSubnetRead,
    17  		Update: resourceArmSubnetCreate,
    18  		Delete: resourceArmSubnetDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": {
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"resource_group_name": {
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  
    33  			"virtual_network_name": {
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  
    39  			"address_prefix": {
    40  				Type:     schema.TypeString,
    41  				Required: true,
    42  			},
    43  
    44  			"network_security_group_id": {
    45  				Type:     schema.TypeString,
    46  				Optional: true,
    47  				Computed: true,
    48  			},
    49  
    50  			"route_table_id": {
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				Computed: true,
    54  			},
    55  
    56  			"ip_configurations": {
    57  				Type:     schema.TypeSet,
    58  				Optional: true,
    59  				Computed: true,
    60  				Elem:     &schema.Schema{Type: schema.TypeString},
    61  				Set:      schema.HashString,
    62  			},
    63  		},
    64  	}
    65  }
    66  
    67  func resourceArmSubnetCreate(d *schema.ResourceData, meta interface{}) error {
    68  	client := meta.(*ArmClient)
    69  	subnetClient := client.subnetClient
    70  
    71  	log.Printf("[INFO] preparing arguments for Azure ARM Subnet creation.")
    72  
    73  	name := d.Get("name").(string)
    74  	vnetName := d.Get("virtual_network_name").(string)
    75  	resGroup := d.Get("resource_group_name").(string)
    76  	addressPrefix := d.Get("address_prefix").(string)
    77  
    78  	armMutexKV.Lock(vnetName)
    79  	defer armMutexKV.Unlock(vnetName)
    80  
    81  	properties := network.SubnetPropertiesFormat{
    82  		AddressPrefix: &addressPrefix,
    83  	}
    84  
    85  	if v, ok := d.GetOk("network_security_group_id"); ok {
    86  		nsgId := v.(string)
    87  		properties.NetworkSecurityGroup = &network.SecurityGroup{
    88  			ID: &nsgId,
    89  		}
    90  	}
    91  
    92  	if v, ok := d.GetOk("route_table_id"); ok {
    93  		rtId := v.(string)
    94  		properties.RouteTable = &network.RouteTable{
    95  			ID: &rtId,
    96  		}
    97  	}
    98  
    99  	subnet := network.Subnet{
   100  		Name:       &name,
   101  		Properties: &properties,
   102  	}
   103  
   104  	_, err := subnetClient.CreateOrUpdate(resGroup, vnetName, name, subnet, make(chan struct{}))
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	read, err := subnetClient.Get(resGroup, vnetName, name, "")
   110  	if err != nil {
   111  		return err
   112  	}
   113  	if read.ID == nil {
   114  		return fmt.Errorf("Cannot read Subnet %s/%s (resource group %s) ID", vnetName, name, resGroup)
   115  	}
   116  
   117  	d.SetId(*read.ID)
   118  
   119  	return resourceArmSubnetRead(d, meta)
   120  }
   121  
   122  func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error {
   123  	subnetClient := meta.(*ArmClient).subnetClient
   124  
   125  	id, err := parseAzureResourceID(d.Id())
   126  	if err != nil {
   127  		return err
   128  	}
   129  	resGroup := id.ResourceGroup
   130  	vnetName := id.Path["virtualNetworks"]
   131  	name := id.Path["subnets"]
   132  
   133  	resp, err := subnetClient.Get(resGroup, vnetName, name, "")
   134  
   135  	if err != nil {
   136  		return fmt.Errorf("Error making Read request on Azure Subnet %s: %s", name, err)
   137  	}
   138  	if resp.StatusCode == http.StatusNotFound {
   139  		d.SetId("")
   140  		return nil
   141  	}
   142  
   143  	if resp.Properties.IPConfigurations != nil && len(*resp.Properties.IPConfigurations) > 0 {
   144  		ips := make([]string, 0, len(*resp.Properties.IPConfigurations))
   145  		for _, ip := range *resp.Properties.IPConfigurations {
   146  			ips = append(ips, *ip.ID)
   147  		}
   148  
   149  		if err := d.Set("ip_configurations", ips); err != nil {
   150  			return err
   151  		}
   152  	}
   153  
   154  	return nil
   155  }
   156  
   157  func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error {
   158  	subnetClient := meta.(*ArmClient).subnetClient
   159  
   160  	id, err := parseAzureResourceID(d.Id())
   161  	if err != nil {
   162  		return err
   163  	}
   164  	resGroup := id.ResourceGroup
   165  	name := id.Path["subnets"]
   166  	vnetName := id.Path["virtualNetworks"]
   167  
   168  	armMutexKV.Lock(vnetName)
   169  	defer armMutexKV.Unlock(vnetName)
   170  
   171  	_, err = subnetClient.Delete(resGroup, vnetName, name, make(chan struct{}))
   172  
   173  	return err
   174  }
   175  
   176  func subnetRuleStateRefreshFunc(client *ArmClient, resourceGroupName string, virtualNetworkName string, subnetName string) resource.StateRefreshFunc {
   177  	return func() (interface{}, string, error) {
   178  		res, err := client.subnetClient.Get(resourceGroupName, virtualNetworkName, subnetName, "")
   179  		if err != nil {
   180  			return nil, "", fmt.Errorf("Error issuing read request in subnetRuleStateRefreshFunc to Azure ARM for subnet '%s' (RG: '%s') (VNN: '%s'): %s", subnetName, resourceGroupName, virtualNetworkName, err)
   181  		}
   182  
   183  		return res, *res.Properties.ProvisioningState, nil
   184  	}
   185  }