github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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/schema"
    10  )
    11  
    12  func resourceArmSubnet() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmSubnetCreate,
    15  		Read:   resourceArmSubnetRead,
    16  		Update: resourceArmSubnetCreate,
    17  		Delete: resourceArmSubnetDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": {
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"resource_group_name": {
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"virtual_network_name": {
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"address_prefix": {
    42  				Type:     schema.TypeString,
    43  				Required: true,
    44  			},
    45  
    46  			"network_security_group_id": {
    47  				Type:     schema.TypeString,
    48  				Optional: true,
    49  				Computed: true,
    50  			},
    51  
    52  			"route_table_id": {
    53  				Type:     schema.TypeString,
    54  				Optional: true,
    55  				Computed: true,
    56  			},
    57  
    58  			"ip_configurations": {
    59  				Type:     schema.TypeSet,
    60  				Optional: true,
    61  				Computed: true,
    62  				Elem:     &schema.Schema{Type: schema.TypeString},
    63  				Set:      schema.HashString,
    64  			},
    65  		},
    66  	}
    67  }
    68  
    69  func resourceArmSubnetCreate(d *schema.ResourceData, meta interface{}) error {
    70  	client := meta.(*ArmClient)
    71  	subnetClient := client.subnetClient
    72  
    73  	log.Printf("[INFO] preparing arguments for Azure ARM Subnet creation.")
    74  
    75  	name := d.Get("name").(string)
    76  	vnetName := d.Get("virtual_network_name").(string)
    77  	resGroup := d.Get("resource_group_name").(string)
    78  	addressPrefix := d.Get("address_prefix").(string)
    79  
    80  	armMutexKV.Lock(name)
    81  	defer armMutexKV.Unlock(name)
    82  
    83  	armMutexKV.Lock(vnetName)
    84  	defer armMutexKV.Unlock(vnetName)
    85  
    86  	properties := network.SubnetPropertiesFormat{
    87  		AddressPrefix: &addressPrefix,
    88  	}
    89  
    90  	if v, ok := d.GetOk("network_security_group_id"); ok {
    91  		nsgId := v.(string)
    92  		properties.NetworkSecurityGroup = &network.SecurityGroup{
    93  			ID: &nsgId,
    94  		}
    95  
    96  		networkSecurityGroupName, err := parseNetworkSecurityGroupName(nsgId)
    97  		if err != nil {
    98  			return err
    99  		}
   100  
   101  		armMutexKV.Lock(networkSecurityGroupName)
   102  		defer armMutexKV.Unlock(networkSecurityGroupName)
   103  	}
   104  
   105  	if v, ok := d.GetOk("route_table_id"); ok {
   106  		rtId := v.(string)
   107  		properties.RouteTable = &network.RouteTable{
   108  			ID: &rtId,
   109  		}
   110  
   111  		routeTableName, err := parseRouteTableName(rtId)
   112  		if err != nil {
   113  			return err
   114  		}
   115  
   116  		armMutexKV.Lock(routeTableName)
   117  		defer armMutexKV.Unlock(routeTableName)
   118  	}
   119  
   120  	subnet := network.Subnet{
   121  		Name: &name,
   122  		SubnetPropertiesFormat: &properties,
   123  	}
   124  
   125  	_, error := subnetClient.CreateOrUpdate(resGroup, vnetName, name, subnet, make(chan struct{}))
   126  	err := <-error
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	read, err := subnetClient.Get(resGroup, vnetName, name, "")
   132  	if err != nil {
   133  		return err
   134  	}
   135  	if read.ID == nil {
   136  		return fmt.Errorf("Cannot read Subnet %s/%s (resource group %s) ID", vnetName, name, resGroup)
   137  	}
   138  
   139  	d.SetId(*read.ID)
   140  
   141  	return resourceArmSubnetRead(d, meta)
   142  }
   143  
   144  func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error {
   145  	subnetClient := meta.(*ArmClient).subnetClient
   146  
   147  	id, err := parseAzureResourceID(d.Id())
   148  	if err != nil {
   149  		return err
   150  	}
   151  	resGroup := id.ResourceGroup
   152  	vnetName := id.Path["virtualNetworks"]
   153  	name := id.Path["subnets"]
   154  
   155  	resp, err := subnetClient.Get(resGroup, vnetName, name, "")
   156  
   157  	if err != nil {
   158  		if resp.StatusCode == http.StatusNotFound {
   159  			d.SetId("")
   160  			return nil
   161  		}
   162  		return fmt.Errorf("Error making Read request on Azure Subnet %s: %s", name, err)
   163  	}
   164  
   165  	d.Set("name", name)
   166  	d.Set("resource_group_name", resGroup)
   167  	d.Set("virtual_network_name", vnetName)
   168  	d.Set("address_prefix", resp.SubnetPropertiesFormat.AddressPrefix)
   169  
   170  	if resp.SubnetPropertiesFormat.NetworkSecurityGroup != nil {
   171  		d.Set("network_security_group_id", resp.SubnetPropertiesFormat.NetworkSecurityGroup.ID)
   172  	}
   173  
   174  	if resp.SubnetPropertiesFormat.RouteTable != nil {
   175  		d.Set("route_table_id", resp.SubnetPropertiesFormat.RouteTable.ID)
   176  	}
   177  
   178  	if resp.SubnetPropertiesFormat.IPConfigurations != nil {
   179  		ips := make([]string, 0, len(*resp.SubnetPropertiesFormat.IPConfigurations))
   180  		for _, ip := range *resp.SubnetPropertiesFormat.IPConfigurations {
   181  			ips = append(ips, *ip.ID)
   182  		}
   183  
   184  		if err := d.Set("ip_configurations", ips); err != nil {
   185  			return err
   186  		}
   187  	} else {
   188  		d.Set("ip_configurations", []string{})
   189  	}
   190  
   191  	return nil
   192  }
   193  
   194  func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error {
   195  	subnetClient := meta.(*ArmClient).subnetClient
   196  
   197  	id, err := parseAzureResourceID(d.Id())
   198  	if err != nil {
   199  		return err
   200  	}
   201  	resGroup := id.ResourceGroup
   202  	name := id.Path["subnets"]
   203  	vnetName := id.Path["virtualNetworks"]
   204  
   205  	if v, ok := d.GetOk("network_security_group_id"); ok {
   206  		networkSecurityGroupId := v.(string)
   207  		networkSecurityGroupName, err := parseNetworkSecurityGroupName(networkSecurityGroupId)
   208  		if err != nil {
   209  			return err
   210  		}
   211  
   212  		armMutexKV.Lock(networkSecurityGroupName)
   213  		defer armMutexKV.Unlock(networkSecurityGroupName)
   214  	}
   215  
   216  	if v, ok := d.GetOk("route_table_id"); ok {
   217  		rtId := v.(string)
   218  		routeTableName, err := parseRouteTableName(rtId)
   219  		if err != nil {
   220  			return err
   221  		}
   222  
   223  		armMutexKV.Lock(routeTableName)
   224  		defer armMutexKV.Unlock(routeTableName)
   225  	}
   226  
   227  	armMutexKV.Lock(vnetName)
   228  	defer armMutexKV.Unlock(vnetName)
   229  
   230  	armMutexKV.Lock(name)
   231  	defer armMutexKV.Unlock(name)
   232  
   233  	_, error := subnetClient.Delete(resGroup, vnetName, name, make(chan struct{}))
   234  	err = <-error
   235  
   236  	return err
   237  }