github.com/IBM-Cloud/terraform@v0.6.4-0.20170726051544-8872b87621df/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 _, err := ercClient.CreateOrUpdate(resGroup, name, erc, make(chan struct{})) 146 if err != nil { 147 return errwrap.Wrapf("Error Creating/Updating ExpressRouteCircuit {{err}}", err) 148 } 149 150 read, err := ercClient.Get(resGroup, name) 151 if err != nil { 152 return errwrap.Wrapf("Error Getting ExpressRouteCircuit {{err}}", err) 153 } 154 if read.ID == nil { 155 return fmt.Errorf("Cannot read ExpressRouteCircuit %s (resource group %s) ID", name, resGroup) 156 } 157 158 d.SetId(*read.ID) 159 160 return resourceArmExpressRouteCircuitRead(d, meta) 161 } 162 163 func resourceArmExpressRouteCircuitRead(d *schema.ResourceData, meta interface{}) error { 164 erc, resGroup, err := retrieveErcByResourceId(d.Id(), meta) 165 if err != nil { 166 return err 167 } 168 169 if erc == nil { 170 d.SetId("") 171 log.Printf("[INFO] Express Route Circuit %q not found. Removing from state", d.Get("name").(string)) 172 return nil 173 } 174 175 d.Set("name", erc.Name) 176 d.Set("resource_group_name", resGroup) 177 d.Set("location", erc.Location) 178 179 if erc.ServiceProviderProperties != nil { 180 d.Set("service_provider_name", erc.ServiceProviderProperties.ServiceProviderName) 181 d.Set("peering_location", erc.ServiceProviderProperties.PeeringLocation) 182 d.Set("bandwidth_in_mbps", erc.ServiceProviderProperties.BandwidthInMbps) 183 } 184 185 if erc.Sku != nil { 186 d.Set("sku", schema.NewSet(resourceArmExpressRouteCircuitSkuHash, flattenExpressRouteCircuitSku(erc.Sku))) 187 } 188 189 d.Set("service_provider_provisioning_state", string(erc.ServiceProviderProvisioningState)) 190 d.Set("service_key", erc.ServiceKey) 191 d.Set("allow_classic_operations", erc.AllowClassicOperations) 192 193 flattenAndSetTags(d, erc.Tags) 194 195 return nil 196 } 197 198 func resourceArmExpressRouteCircuitDelete(d *schema.ResourceData, meta interface{}) error { 199 ercClient := meta.(*ArmClient).expressRouteCircuitClient 200 201 resGroup, name, err := extractResourceGroupAndErcName(d.Id()) 202 if err != nil { 203 return errwrap.Wrapf("Error Parsing Azure Resource ID {{err}}", err) 204 } 205 206 _, err = ercClient.Delete(resGroup, name, make(chan struct{})) 207 return err 208 } 209 210 func expandExpressRouteCircuitSku(d *schema.ResourceData) *network.ExpressRouteCircuitSku { 211 skuSettings := d.Get("sku").(*schema.Set) 212 v := skuSettings.List()[0].(map[string]interface{}) // [0] is guarded by MinItems in schema. 213 tier := v["tier"].(string) 214 family := v["family"].(string) 215 name := fmt.Sprintf("%s_%s", tier, family) 216 217 return &network.ExpressRouteCircuitSku{ 218 Name: &name, 219 Tier: network.ExpressRouteCircuitSkuTier(tier), 220 Family: network.ExpressRouteCircuitSkuFamily(family), 221 } 222 } 223 224 func flattenExpressRouteCircuitSku(sku *network.ExpressRouteCircuitSku) []interface{} { 225 return []interface{}{ 226 map[string]interface{}{ 227 "tier": string(sku.Tier), 228 "family": string(sku.Family), 229 }, 230 } 231 } 232 233 func resourceArmExpressRouteCircuitSkuHash(v interface{}) int { 234 var buf bytes.Buffer 235 m := v.(map[string]interface{}) 236 buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["tier"].(string)))) 237 buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["family"].(string)))) 238 239 return hashcode.String(buf.String()) 240 }