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