2016-08-01 00:46:15 +02:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
|
|
|
|
"github.com/hashicorp/terraform/helper/hashcode"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2016-08-10 14:32:35 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/validation"
|
2016-08-01 00:46:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmTrafficManagerProfile() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmTrafficManagerProfileCreate,
|
|
|
|
Read: resourceArmTrafficManagerProfileRead,
|
|
|
|
Update: resourceArmTrafficManagerProfileCreate,
|
|
|
|
Delete: resourceArmTrafficManagerProfileDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"profile_status": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2016-08-10 14:32:35 +02:00
|
|
|
ValidateFunc: validation.StringInSlice([]string{"Enabled", "Disabled"}, true),
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"traffic_routing_method": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2016-08-10 14:32:35 +02:00
|
|
|
ValidateFunc: validation.StringInSlice([]string{"Performance", "Weighted", "Priority"}, false),
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"dns_config": {
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"relative_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
"ttl": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
2016-08-10 14:32:35 +02:00
|
|
|
ValidateFunc: validation.IntBetween(30, 999999),
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAzureRMTrafficManagerDNSConfigHash,
|
|
|
|
},
|
|
|
|
|
|
|
|
// inlined from dns_config for ease of use
|
|
|
|
"fqdn": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"monitor_config": {
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Required: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"protocol": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2016-08-10 14:32:35 +02:00
|
|
|
ValidateFunc: validation.StringInSlice([]string{"http", "https"}, false),
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
"port": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
2016-08-10 14:32:35 +02:00
|
|
|
ValidateFunc: validation.IntBetween(1, 65535),
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
"path": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Set: resourceAzureRMTrafficManagerMonitorConfigHash,
|
|
|
|
},
|
|
|
|
|
|
|
|
"resource_group_name": {
|
2016-09-28 15:12:00 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
DiffSuppressFunc: resourceAzurermResourceGroupNameDiffSuppress,
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmTrafficManagerProfileCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).trafficManagerProfilesClient
|
|
|
|
|
|
|
|
log.Printf("[INFO] preparing arguments for Azure ARM virtual network creation.")
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
// must be provided in request
|
|
|
|
location := "global"
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
tags := d.Get("tags").(map[string]interface{})
|
|
|
|
|
|
|
|
profile := trafficmanager.Profile{
|
2016-12-06 09:39:47 +01:00
|
|
|
Name: &name,
|
|
|
|
Location: &location,
|
|
|
|
ProfileProperties: getArmTrafficManagerProfileProperties(d),
|
|
|
|
Tags: expandTags(tags),
|
2016-08-01 00:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.CreateOrUpdate(resGroup, name, profile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := client.Get(resGroup, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read TrafficManager profile %s (resource group %s) ID", name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmTrafficManagerProfileRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmTrafficManagerProfileRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).trafficManagerProfilesClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["trafficManagerProfiles"]
|
|
|
|
|
|
|
|
resp, err := client.Get(resGroup, name)
|
provider/azurerm: Reordering the checks after an Azure API Get
We are receiving suggestions of a panic as follows:
```
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference
2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f]
2016/09/01 07:21:55 [DEBUG] plugin: terraform:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0)
```
This is because the code is as follows:
```
resp, err := client.Get(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
```
When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first
```
resp, err := client.Get(resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
```
2016-09-01 16:31:42 +02:00
|
|
|
if err != nil {
|
2016-09-30 18:57:59 +02:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
provider/azurerm: Reordering the checks after an Azure API Get
We are receiving suggestions of a panic as follows:
```
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference
2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f]
2016/09/01 07:21:55 [DEBUG] plugin: terraform:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0)
```
This is because the code is as follows:
```
resp, err := client.Get(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
```
When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first
```
resp, err := client.Get(resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
```
2016-09-01 16:31:42 +02:00
|
|
|
return fmt.Errorf("Error making Read request on Traffic Manager Profile %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
profile := *resp.ProfileProperties
|
2016-08-01 00:46:15 +02:00
|
|
|
|
|
|
|
// update appropriate values
|
2016-09-27 19:41:40 +02:00
|
|
|
d.Set("resource_group_name", resGroup)
|
2016-08-01 00:46:15 +02:00
|
|
|
d.Set("name", resp.Name)
|
|
|
|
d.Set("profile_status", profile.ProfileStatus)
|
|
|
|
d.Set("traffic_routing_method", profile.TrafficRoutingMethod)
|
|
|
|
|
|
|
|
dnsFlat := flattenAzureRMTrafficManagerProfileDNSConfig(profile.DNSConfig)
|
|
|
|
d.Set("dns_config", schema.NewSet(resourceAzureRMTrafficManagerDNSConfigHash, dnsFlat))
|
|
|
|
|
|
|
|
// fqdn is actually inside DNSConfig, inlined for simpler reference
|
|
|
|
d.Set("fqdn", profile.DNSConfig.Fqdn)
|
|
|
|
|
|
|
|
monitorFlat := flattenAzureRMTrafficManagerProfileMonitorConfig(profile.MonitorConfig)
|
|
|
|
d.Set("monitor_config", schema.NewSet(resourceAzureRMTrafficManagerMonitorConfigHash, monitorFlat))
|
|
|
|
|
|
|
|
flattenAndSetTags(d, resp.Tags)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmTrafficManagerProfileDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).trafficManagerProfilesClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["trafficManagerProfiles"]
|
|
|
|
|
|
|
|
_, err = client.Delete(resGroup, name)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getArmTrafficManagerProfileProperties(d *schema.ResourceData) *trafficmanager.ProfileProperties {
|
|
|
|
routingMethod := d.Get("traffic_routing_method").(string)
|
|
|
|
props := &trafficmanager.ProfileProperties{
|
|
|
|
TrafficRoutingMethod: &routingMethod,
|
|
|
|
DNSConfig: expandArmTrafficManagerDNSConfig(d),
|
|
|
|
MonitorConfig: expandArmTrafficManagerMonitorConfig(d),
|
|
|
|
}
|
|
|
|
|
|
|
|
if status, ok := d.GetOk("profile_status"); ok {
|
|
|
|
s := status.(string)
|
|
|
|
props.ProfileStatus = &s
|
|
|
|
}
|
|
|
|
|
|
|
|
return props
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandArmTrafficManagerMonitorConfig(d *schema.ResourceData) *trafficmanager.MonitorConfig {
|
|
|
|
monitorSets := d.Get("monitor_config").(*schema.Set).List()
|
|
|
|
monitor := monitorSets[0].(map[string]interface{})
|
|
|
|
|
|
|
|
proto := monitor["protocol"].(string)
|
|
|
|
port := int64(monitor["port"].(int))
|
|
|
|
path := monitor["path"].(string)
|
|
|
|
|
|
|
|
return &trafficmanager.MonitorConfig{
|
|
|
|
Protocol: &proto,
|
|
|
|
Port: &port,
|
|
|
|
Path: &path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandArmTrafficManagerDNSConfig(d *schema.ResourceData) *trafficmanager.DNSConfig {
|
|
|
|
dnsSets := d.Get("dns_config").(*schema.Set).List()
|
|
|
|
dns := dnsSets[0].(map[string]interface{})
|
|
|
|
|
|
|
|
name := dns["relative_name"].(string)
|
|
|
|
ttl := int64(dns["ttl"].(int))
|
|
|
|
|
|
|
|
return &trafficmanager.DNSConfig{
|
|
|
|
RelativeName: &name,
|
|
|
|
TTL: &ttl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenAzureRMTrafficManagerProfileDNSConfig(dns *trafficmanager.DNSConfig) []interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
|
|
|
|
result["relative_name"] = *dns.RelativeName
|
|
|
|
result["ttl"] = int(*dns.TTL)
|
|
|
|
|
|
|
|
return []interface{}{result}
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenAzureRMTrafficManagerProfileMonitorConfig(cfg *trafficmanager.MonitorConfig) []interface{} {
|
|
|
|
result := make(map[string]interface{})
|
|
|
|
|
|
|
|
result["protocol"] = *cfg.Protocol
|
|
|
|
result["port"] = int(*cfg.Port)
|
|
|
|
result["path"] = *cfg.Path
|
|
|
|
|
|
|
|
return []interface{}{result}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureRMTrafficManagerDNSConfigHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["relative_name"].(string)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["ttl"].(int)))
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceAzureRMTrafficManagerMonitorConfigHash(v interface{}) int {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
m := v.(map[string]interface{})
|
|
|
|
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", strings.ToLower(m["protocol"].(string))))
|
|
|
|
buf.WriteString(fmt.Sprintf("%d-", m["port"].(int)))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s-", m["path"].(string)))
|
|
|
|
|
|
|
|
return hashcode.String(buf.String())
|
|
|
|
}
|