github.com/peterbale/terraform@v0.9.0-beta2.0.20170315142748-5723acd55547/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(vnetName)
    81  	defer armMutexKV.Unlock(vnetName)
    82  
    83  	properties := network.SubnetPropertiesFormat{
    84  		AddressPrefix: &addressPrefix,
    85  	}
    86  
    87  	if v, ok := d.GetOk("network_security_group_id"); ok {
    88  		nsgId := v.(string)
    89  		properties.NetworkSecurityGroup = &network.SecurityGroup{
    90  			ID: &nsgId,
    91  		}
    92  	}
    93  
    94  	if v, ok := d.GetOk("route_table_id"); ok {
    95  		rtId := v.(string)
    96  		properties.RouteTable = &network.RouteTable{
    97  			ID: &rtId,
    98  		}
    99  	}
   100  
   101  	subnet := network.Subnet{
   102  		Name: &name,
   103  		SubnetPropertiesFormat: &properties,
   104  	}
   105  
   106  	_, err := subnetClient.CreateOrUpdate(resGroup, vnetName, name, subnet, make(chan struct{}))
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	read, err := subnetClient.Get(resGroup, vnetName, name, "")
   112  	if err != nil {
   113  		return err
   114  	}
   115  	if read.ID == nil {
   116  		return fmt.Errorf("Cannot read Subnet %s/%s (resource group %s) ID", vnetName, name, resGroup)
   117  	}
   118  
   119  	d.SetId(*read.ID)
   120  
   121  	return resourceArmSubnetRead(d, meta)
   122  }
   123  
   124  func resourceArmSubnetRead(d *schema.ResourceData, meta interface{}) error {
   125  	subnetClient := meta.(*ArmClient).subnetClient
   126  
   127  	id, err := parseAzureResourceID(d.Id())
   128  	if err != nil {
   129  		return err
   130  	}
   131  	resGroup := id.ResourceGroup
   132  	vnetName := id.Path["virtualNetworks"]
   133  	name := id.Path["subnets"]
   134  
   135  	resp, err := subnetClient.Get(resGroup, vnetName, name, "")
   136  
   137  	if err != nil {
   138  		if resp.StatusCode == http.StatusNotFound {
   139  			d.SetId("")
   140  			return nil
   141  		}
   142  		return fmt.Errorf("Error making Read request on Azure Subnet %s: %s", name, err)
   143  	}
   144  
   145  	d.Set("name", name)
   146  	d.Set("resource_group_name", resGroup)
   147  	d.Set("virtual_network_name", vnetName)
   148  	d.Set("address_prefix", resp.SubnetPropertiesFormat.AddressPrefix)
   149  
   150  	if resp.SubnetPropertiesFormat.NetworkSecurityGroup != nil {
   151  		d.Set("network_security_group_id", resp.SubnetPropertiesFormat.NetworkSecurityGroup.ID)
   152  	}
   153  
   154  	if resp.SubnetPropertiesFormat.RouteTable != nil {
   155  		d.Set("route_table_id", resp.SubnetPropertiesFormat.RouteTable.ID)
   156  	}
   157  
   158  	if resp.SubnetPropertiesFormat.IPConfigurations != nil {
   159  		ips := make([]string, 0, len(*resp.SubnetPropertiesFormat.IPConfigurations))
   160  		for _, ip := range *resp.SubnetPropertiesFormat.IPConfigurations {
   161  			ips = append(ips, *ip.ID)
   162  		}
   163  
   164  		if err := d.Set("ip_configurations", ips); err != nil {
   165  			return err
   166  		}
   167  	} else {
   168  		d.Set("ip_configurations", []string{})
   169  	}
   170  
   171  	return nil
   172  }
   173  
   174  func resourceArmSubnetDelete(d *schema.ResourceData, meta interface{}) error {
   175  	subnetClient := meta.(*ArmClient).subnetClient
   176  
   177  	id, err := parseAzureResourceID(d.Id())
   178  	if err != nil {
   179  		return err
   180  	}
   181  	resGroup := id.ResourceGroup
   182  	name := id.Path["subnets"]
   183  	vnetName := id.Path["virtualNetworks"]
   184  
   185  	armMutexKV.Lock(vnetName)
   186  	defer armMutexKV.Unlock(vnetName)
   187  
   188  	_, err = subnetClient.Delete(resGroup, vnetName, name, make(chan struct{}))
   189  
   190  	return err
   191  }