2016-08-01 00:46:15 +02:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2016-08-11 06:00:23 +02:00
|
|
|
"regexp"
|
2016-08-01 00:46:15 +02:00
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/trafficmanager"
|
|
|
|
"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 resourceArmTrafficManagerEndpoint() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmTrafficManagerEndpointCreate,
|
|
|
|
Read: resourceArmTrafficManagerEndpointRead,
|
|
|
|
Update: resourceArmTrafficManagerEndpointCreate,
|
|
|
|
Delete: resourceArmTrafficManagerEndpointDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"type": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
2016-08-10 14:32:35 +02:00
|
|
|
ValidateFunc: validation.StringInSlice([]string{"azureEndpoints", "nestedEndpoints", "externalEndpoints"}, false),
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"profile_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"target": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
// when targeting an Azure resource the FQDN of that resource will be set as the target
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"target_resource_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"endpoint_status": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"weight": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2016-08-10 14:32:35 +02:00
|
|
|
ValidateFunc: validation.IntBetween(1, 1000),
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"priority": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
2016-08-10 14:32:35 +02:00
|
|
|
ValidateFunc: validation.IntBetween(1, 1000),
|
2016-08-01 00:46:15 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"endpoint_location": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
// when targeting an Azure resource the location of that resource will be set on the endpoint
|
|
|
|
Computed: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
},
|
|
|
|
|
|
|
|
"min_child_endpoints": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"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
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmTrafficManagerEndpointCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).trafficManagerEndpointsClient
|
|
|
|
|
|
|
|
log.Printf("[INFO] preparing arguments for ARM TrafficManager Endpoint creation.")
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
endpointType := d.Get("type").(string)
|
|
|
|
fullEndpointType := fmt.Sprintf("Microsoft.Network/TrafficManagerProfiles/%s", endpointType)
|
|
|
|
profileName := d.Get("profile_name").(string)
|
|
|
|
resGroup := d.Get("resource_group_name").(string)
|
|
|
|
|
|
|
|
params := trafficmanager.Endpoint{
|
2016-12-06 09:39:47 +01:00
|
|
|
Name: &name,
|
|
|
|
Type: &fullEndpointType,
|
|
|
|
EndpointProperties: getArmTrafficManagerEndpointProperties(d),
|
2016-08-01 00:46:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err := client.CreateOrUpdate(resGroup, profileName, endpointType, name, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := client.Get(resGroup, profileName, endpointType, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read TrafficManager endpoint %s (resource group %s) ID", name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmTrafficManagerEndpointRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmTrafficManagerEndpointRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).trafficManagerEndpointsClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
2016-08-11 06:00:23 +02:00
|
|
|
|
|
|
|
// lookup endpointType in Azure ID path
|
|
|
|
var endpointType string
|
|
|
|
typeRegex := regexp.MustCompile("azureEndpoints|externalEndpoints|nestedEndpoints")
|
|
|
|
for k := range id.Path {
|
|
|
|
if typeRegex.MatchString(k) {
|
|
|
|
endpointType = k
|
|
|
|
}
|
|
|
|
}
|
2016-08-01 00:46:15 +02:00
|
|
|
profileName := id.Path["trafficManagerProfiles"]
|
|
|
|
|
|
|
|
// endpoint name is keyed by endpoint type in ARM ID
|
|
|
|
name := id.Path[endpointType]
|
|
|
|
|
|
|
|
resp, err := client.Get(resGroup, profileName, endpointType, 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 TrafficManager Endpoint %s: %s", name, err)
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
endpoint := *resp.EndpointProperties
|
2016-08-01 00:46:15 +02:00
|
|
|
|
2016-09-27 19:29:50 +02:00
|
|
|
d.Set("resource_group_name", resGroup)
|
2016-08-01 00:46:15 +02:00
|
|
|
d.Set("name", resp.Name)
|
2016-08-11 06:00:23 +02:00
|
|
|
d.Set("type", endpointType)
|
|
|
|
d.Set("profile_name", profileName)
|
2016-08-01 00:46:15 +02:00
|
|
|
d.Set("endpoint_status", endpoint.EndpointStatus)
|
|
|
|
d.Set("target_resource_id", endpoint.TargetResourceID)
|
|
|
|
d.Set("target", endpoint.Target)
|
|
|
|
d.Set("weight", endpoint.Weight)
|
|
|
|
d.Set("priority", endpoint.Priority)
|
|
|
|
d.Set("endpoint_location", endpoint.EndpointLocation)
|
|
|
|
d.Set("endpoint_monitor_status", endpoint.EndpointMonitorStatus)
|
|
|
|
d.Set("min_child_endpoints", endpoint.MinChildEndpoints)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmTrafficManagerEndpointDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient).trafficManagerEndpointsClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
endpointType := d.Get("type").(string)
|
|
|
|
profileName := id.Path["trafficManagerProfiles"]
|
|
|
|
|
|
|
|
// endpoint name is keyed by endpoint type in ARM ID
|
|
|
|
name := id.Path[endpointType]
|
|
|
|
|
|
|
|
_, err = client.Delete(resGroup, profileName, endpointType, name)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func getArmTrafficManagerEndpointProperties(d *schema.ResourceData) *trafficmanager.EndpointProperties {
|
|
|
|
var endpointProps trafficmanager.EndpointProperties
|
|
|
|
|
|
|
|
if targetResID := d.Get("target_resource_id").(string); targetResID != "" {
|
|
|
|
endpointProps.TargetResourceID = &targetResID
|
|
|
|
}
|
|
|
|
|
|
|
|
if target := d.Get("target").(string); target != "" {
|
|
|
|
endpointProps.Target = &target
|
|
|
|
}
|
|
|
|
|
|
|
|
if status := d.Get("endpoint_status").(string); status != "" {
|
|
|
|
endpointProps.EndpointStatus = &status
|
|
|
|
}
|
|
|
|
|
|
|
|
if weight := d.Get("weight").(int); weight != 0 {
|
|
|
|
w64 := int64(weight)
|
|
|
|
endpointProps.Weight = &w64
|
|
|
|
}
|
|
|
|
|
|
|
|
if priority := d.Get("priority").(int); priority != 0 {
|
|
|
|
p64 := int64(priority)
|
|
|
|
endpointProps.Priority = &p64
|
|
|
|
}
|
|
|
|
|
|
|
|
if location := d.Get("endpoint_location").(string); location != "" {
|
|
|
|
endpointProps.EndpointLocation = &location
|
|
|
|
}
|
|
|
|
|
|
|
|
if minChildEndpoints := d.Get("min_child_endpoints").(int); minChildEndpoints != 0 {
|
|
|
|
mci64 := int64(minChildEndpoints)
|
|
|
|
endpointProps.MinChildEndpoints = &mci64
|
|
|
|
}
|
|
|
|
|
|
|
|
return &endpointProps
|
|
|
|
}
|