github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_express_route_circuit.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"bytes"
     5  	"log"
     6  	"strings"
     7  
     8  	"fmt"
     9  
    10  	"github.com/Azure/azure-sdk-for-go/arm/network"
    11  	"github.com/hashicorp/errwrap"
    12  	"github.com/hashicorp/terraform/helper/hashcode"
    13  	"github.com/hashicorp/terraform/helper/schema"
    14  	"github.com/hashicorp/terraform/helper/validation"
    15  )
    16  
    17  func resourceArmExpressRouteCircuit() *schema.Resource {
    18  	return &schema.Resource{
    19  		Create: resourceArmExpressRouteCircuitCreateOrUpdate,
    20  		Read:   resourceArmExpressRouteCircuitRead,
    21  		Update: resourceArmExpressRouteCircuitCreateOrUpdate,
    22  		Delete: resourceArmExpressRouteCircuitDelete,
    23  		Importer: &schema.ResourceImporter{
    24  			State: schema.ImportStatePassthrough,
    25  		},
    26  
    27  		Schema: map[string]*schema.Schema{
    28  			"name": {
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  
    34  			"resource_group_name": {
    35  				Type:     schema.TypeString,
    36  				Required: true,
    37  				ForceNew: true,
    38  			},
    39  
    40  			"location": locationSchema(),
    41  
    42  			"service_provider_name": {
    43  				Type:             schema.TypeString,
    44  				Required:         true,
    45  				ForceNew:         true,
    46  				DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
    47  			},
    48  
    49  			"peering_location": {
    50  				Type:             schema.TypeString,
    51  				Required:         true,
    52  				ForceNew:         true,
    53  				DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
    54  			},
    55  
    56  			"bandwidth_in_mbps": {
    57  				Type:     schema.TypeInt,
    58  				Required: true,
    59  			},
    60  
    61  			"sku": {
    62  				Type:     schema.TypeSet,
    63  				Required: true,
    64  				MinItems: 1,
    65  				MaxItems: 1,
    66  				Elem: &schema.Resource{
    67  					Schema: map[string]*schema.Schema{
    68  						"tier": {
    69  							Type:     schema.TypeString,
    70  							Required: true,
    71  							ValidateFunc: validation.StringInSlice([]string{
    72  								string(network.ExpressRouteCircuitSkuTierStandard),
    73  								string(network.ExpressRouteCircuitSkuTierPremium),
    74  							}, true),
    75  							DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
    76  						},
    77  
    78  						"family": {
    79  							Type:     schema.TypeString,
    80  							Required: true,
    81  							ValidateFunc: validation.StringInSlice([]string{
    82  								string(network.MeteredData),
    83  								string(network.UnlimitedData),
    84  							}, true),
    85  							DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
    86  						},
    87  					},
    88  				},
    89  				Set: resourceArmExpressRouteCircuitSkuHash,
    90  			},
    91  
    92  			"allow_classic_operations": {
    93  				Type:     schema.TypeBool,
    94  				Optional: true,
    95  				Default:  false,
    96  			},
    97  
    98  			"service_provider_provisioning_state": {
    99  				Type:     schema.TypeString,
   100  				Computed: true,
   101  			},
   102  
   103  			"service_key": {
   104  				Type:     schema.TypeString,
   105  				Computed: true,
   106  			},
   107  
   108  			"tags": tagsSchema(),
   109  		},
   110  	}
   111  }
   112  
   113  func resourceArmExpressRouteCircuitCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
   114  	client := meta.(*ArmClient)
   115  	ercClient := client.expressRouteCircuitClient
   116  
   117  	log.Printf("[INFO] preparing arguments for Azure ARM ExpressRouteCircuit creation.")
   118  
   119  	name := d.Get("name").(string)
   120  	resGroup := d.Get("resource_group_name").(string)
   121  	location := d.Get("location").(string)
   122  	serviceProviderName := d.Get("service_provider_name").(string)
   123  	peeringLocation := d.Get("peering_location").(string)
   124  	bandwidthInMbps := int32(d.Get("bandwidth_in_mbps").(int))
   125  	sku := expandExpressRouteCircuitSku(d)
   126  	allowRdfeOps := d.Get("allow_classic_operations").(bool)
   127  	tags := d.Get("tags").(map[string]interface{})
   128  	expandedTags := expandTags(tags)
   129  
   130  	erc := network.ExpressRouteCircuit{
   131  		Name:     &name,
   132  		Location: &location,
   133  		Sku:      sku,
   134  		ExpressRouteCircuitPropertiesFormat: &network.ExpressRouteCircuitPropertiesFormat{
   135  			AllowClassicOperations: &allowRdfeOps,
   136  			ServiceProviderProperties: &network.ExpressRouteCircuitServiceProviderProperties{
   137  				ServiceProviderName: &serviceProviderName,
   138  				PeeringLocation:     &peeringLocation,
   139  				BandwidthInMbps:     &bandwidthInMbps,
   140  			},
   141  		},
   142  		Tags: expandedTags,
   143  	}
   144  
   145  	_, error := ercClient.CreateOrUpdate(resGroup, name, erc, make(chan struct{}))
   146  	err := <-error
   147  	if err != nil {
   148  		return errwrap.Wrapf("Error Creating/Updating ExpressRouteCircuit {{err}}", err)
   149  	}
   150  
   151  	read, err := ercClient.Get(resGroup, name)
   152  	if err != nil {
   153  		return errwrap.Wrapf("Error Getting ExpressRouteCircuit {{err}}", err)
   154  	}
   155  	if read.ID == nil {
   156  		return fmt.Errorf("Cannot read ExpressRouteCircuit %s (resource group %s) ID", name, resGroup)
   157  	}
   158  
   159  	d.SetId(*read.ID)
   160  
   161  	return resourceArmExpressRouteCircuitRead(d, meta)
   162  }
   163  
   164  func resourceArmExpressRouteCircuitRead(d *schema.ResourceData, meta interface{}) error {
   165  	erc, resGroup, err := retrieveErcByResourceId(d.Id(), meta)
   166  	if err != nil {
   167  		return err
   168  	}
   169  
   170  	if erc == nil {
   171  		d.SetId("")
   172  		log.Printf("[INFO] Express Route Circuit %q not found. Removing from state", d.Get("name").(string))
   173  		return nil
   174  	}
   175  
   176  	d.Set("name", erc.Name)
   177  	d.Set("resource_group_name", resGroup)
   178  	d.Set("location", erc.Location)
   179  
   180  	if erc.ServiceProviderProperties != nil {
   181  		d.Set("service_provider_name", erc.ServiceProviderProperties.ServiceProviderName)
   182  		d.Set("peering_location", erc.ServiceProviderProperties.PeeringLocation)
   183  		d.Set("bandwidth_in_mbps", erc.ServiceProviderProperties.BandwidthInMbps)
   184  	}
   185  
   186  	if erc.Sku != nil {
   187  		d.Set("sku", schema.NewSet(resourceArmExpressRouteCircuitSkuHash, flattenExpressRouteCircuitSku(erc.Sku)))
   188  	}
   189  
   190  	d.Set("service_provider_provisioning_state", string(erc.ServiceProviderProvisioningState))
   191  	d.Set("service_key", erc.ServiceKey)
   192  	d.Set("allow_classic_operations", erc.AllowClassicOperations)
   193  
   194  	flattenAndSetTags(d, erc.Tags)
   195  
   196  	return nil
   197  }
   198  
   199  func resourceArmExpressRouteCircuitDelete(d *schema.ResourceData, meta interface{}) error {
   200  	ercClient := meta.(*ArmClient).expressRouteCircuitClient
   201  
   202  	resGroup, name, err := extractResourceGroupAndErcName(d.Id())
   203  	if err != nil {
   204  		return errwrap.Wrapf("Error Parsing Azure Resource ID {{err}}", err)
   205  	}
   206  
   207  	_, error := ercClient.Delete(resGroup, name, make(chan struct{}))
   208  	err = <-error
   209  	return err
   210  }
   211  
   212  func expandExpressRouteCircuitSku(d *schema.ResourceData) *network.ExpressRouteCircuitSku {
   213  	skuSettings := d.Get("sku").(*schema.Set)
   214  	v := skuSettings.List()[0].(map[string]interface{}) // [0] is guarded by MinItems in schema.
   215  	tier := v["tier"].(string)
   216  	family := v["family"].(string)
   217  	name := fmt.Sprintf("%s_%s", tier, family)
   218  
   219  	return &network.ExpressRouteCircuitSku{
   220  		Name:   &name,
   221  		Tier:   network.ExpressRouteCircuitSkuTier(tier),
   222  		Family: network.ExpressRouteCircuitSkuFamily(family),
   223  	}
   224  }
   225  
   226  func flattenExpressRouteCircuitSku(sku *network.ExpressRouteCircuitSku) []interface{} {
   227  	return []interface{}{
   228  		map[string]interface{}{
   229  			"tier":   string(sku.Tier),
   230  			"family": string(sku.Family),
   231  		},
   232  	}
   233  }
   234  
   235  func resourceArmExpressRouteCircuitSkuHash(v interface{}) int {
   236  	var buf bytes.Buffer
   237  	m := v.(map[string]interface{})
   238  	buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["tier"].(string))))
   239  	buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["family"].(string))))
   240  
   241  	return hashcode.String(buf.String())
   242  }