2016-10-07 20:14:26 +02:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/network"
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/jen20/riviera/azure"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerProbe() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmLoadBalancerProbeCreate,
|
|
|
|
Read: resourceArmLoadBalancerProbeRead,
|
|
|
|
Update: resourceArmLoadBalancerProbeCreate,
|
|
|
|
Delete: resourceArmLoadBalancerProbeDelete,
|
2017-02-01 18:17:51 +01:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: loadBalancerSubResourceStateImporter,
|
|
|
|
},
|
2016-10-07 20:14:26 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2017-02-01 18:17:51 +01:00
|
|
|
"location": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
ForceNew: true,
|
|
|
|
Optional: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
DiffSuppressFunc: azureRMSuppressLocationDiff,
|
|
|
|
Deprecated: "location is no longer used",
|
|
|
|
},
|
2016-10-07 20:14:26 +02:00
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"loadbalancer_id": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"protocol": {
|
2017-02-22 21:30:07 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
Optional: true,
|
|
|
|
StateFunc: ignoreCaseStateFunc,
|
|
|
|
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
|
2016-10-07 20:14:26 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"port": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"request_path": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"interval_in_seconds": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 15,
|
|
|
|
},
|
|
|
|
|
|
|
|
"number_of_probes": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 2,
|
|
|
|
},
|
|
|
|
|
2017-02-01 18:17:51 +01:00
|
|
|
"load_balancer_rules": {
|
2016-10-07 20:14:26 +02:00
|
|
|
Type: schema.TypeSet,
|
|
|
|
Computed: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerProbeCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
lbClient := client.loadBalancerClient
|
|
|
|
|
2016-10-19 14:51:47 +02:00
|
|
|
loadBalancerID := d.Get("loadbalancer_id").(string)
|
|
|
|
armMutexKV.Lock(loadBalancerID)
|
|
|
|
defer armMutexKV.Unlock(loadBalancerID)
|
|
|
|
|
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
|
2016-10-07 20:14:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
|
|
|
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newProbe, err := expandAzureRmLoadBalancerProbe(d, loadBalancer)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Expanding Probe {{err}}", err)
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
probes := append(*loadBalancer.LoadBalancerPropertiesFormat.Probes, *newProbe)
|
2016-11-11 12:09:00 +01:00
|
|
|
|
|
|
|
existingProbe, existingProbeIndex, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string))
|
|
|
|
if exists {
|
2017-01-12 14:57:07 +01:00
|
|
|
if d.Get("name").(string) == *existingProbe.Name {
|
|
|
|
// this probe is being updated/reapplied remove old copy from the slice
|
2016-11-11 12:09:00 +01:00
|
|
|
probes = append(probes[:existingProbeIndex], probes[existingProbeIndex+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
loadBalancer.LoadBalancerPropertiesFormat.Probes = &probes
|
2016-10-07 20:14:26 +02:00
|
|
|
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := lbClient.Get(resGroup, loadBalancerName, "")
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
|
|
|
|
}
|
|
|
|
|
2016-10-17 18:51:09 +02:00
|
|
|
var createdProbe_id string
|
2016-12-06 09:39:47 +01:00
|
|
|
for _, Probe := range *(*read.LoadBalancerPropertiesFormat).Probes {
|
2016-10-18 11:44:15 +02:00
|
|
|
if *Probe.Name == d.Get("name").(string) {
|
|
|
|
createdProbe_id = *Probe.ID
|
2016-10-17 18:51:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if createdProbe_id != "" {
|
|
|
|
d.SetId(createdProbe_id)
|
|
|
|
} else {
|
2016-10-18 15:00:38 +02:00
|
|
|
return fmt.Errorf("Cannot find created LoadBalancer Probe ID %q", createdProbe_id)
|
2016-10-17 18:51:09 +02:00
|
|
|
}
|
2016-10-07 20:14:26 +02:00
|
|
|
|
|
|
|
log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
|
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"Accepted", "Updating"},
|
|
|
|
Target: []string{"Succeeded"},
|
|
|
|
Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName),
|
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceArmLoadBalancerProbeRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerProbeRead(d *schema.ResourceData, meta interface{}) error {
|
2017-02-01 18:17:51 +01:00
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := id.Path["probes"]
|
|
|
|
|
2016-10-17 18:52:25 +02:00
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
|
2016-10-07 20:14:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
2017-02-01 18:17:51 +01:00
|
|
|
log.Printf("[INFO] LoadBalancer %q not found. Removing from state", name)
|
2016-10-07 20:14:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-02-01 18:17:51 +01:00
|
|
|
config, _, exists := findLoadBalancerProbeByName(loadBalancer, name)
|
2017-01-31 16:55:17 +01:00
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
2017-02-01 18:17:51 +01:00
|
|
|
log.Printf("[INFO] LoadBalancer Probe %q not found. Removing from state", name)
|
2017-01-31 16:55:17 +01:00
|
|
|
return nil
|
2016-10-07 20:14:26 +02:00
|
|
|
}
|
|
|
|
|
2017-01-31 16:55:17 +01:00
|
|
|
d.Set("name", config.Name)
|
2017-02-01 18:17:51 +01:00
|
|
|
d.Set("resource_group_name", id.ResourceGroup)
|
2017-01-31 16:55:17 +01:00
|
|
|
d.Set("protocol", config.ProbePropertiesFormat.Protocol)
|
|
|
|
d.Set("interval_in_seconds", config.ProbePropertiesFormat.IntervalInSeconds)
|
|
|
|
d.Set("number_of_probes", config.ProbePropertiesFormat.NumberOfProbes)
|
|
|
|
d.Set("port", config.ProbePropertiesFormat.Port)
|
|
|
|
d.Set("request_path", config.ProbePropertiesFormat.RequestPath)
|
|
|
|
|
2017-02-01 18:17:51 +01:00
|
|
|
var load_balancer_rules []string
|
|
|
|
if config.ProbePropertiesFormat.LoadBalancingRules != nil {
|
|
|
|
for _, ruleConfig := range *config.ProbePropertiesFormat.LoadBalancingRules {
|
|
|
|
load_balancer_rules = append(load_balancer_rules, *ruleConfig.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.Set("load_balancer_rules", load_balancer_rules)
|
|
|
|
|
2016-10-07 20:14:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmLoadBalancerProbeDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
lbClient := client.loadBalancerClient
|
|
|
|
|
2016-10-19 14:51:47 +02:00
|
|
|
loadBalancerID := d.Get("loadbalancer_id").(string)
|
|
|
|
armMutexKV.Lock(loadBalancerID)
|
|
|
|
defer armMutexKV.Unlock(loadBalancerID)
|
|
|
|
|
|
|
|
loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
|
2016-10-07 20:14:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
_, index, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string))
|
|
|
|
if !exists {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-06 09:39:47 +01:00
|
|
|
oldProbes := *loadBalancer.LoadBalancerPropertiesFormat.Probes
|
2016-10-07 20:14:26 +02:00
|
|
|
newProbes := append(oldProbes[:index], oldProbes[index+1:]...)
|
2016-12-06 09:39:47 +01:00
|
|
|
loadBalancer.LoadBalancerPropertiesFormat.Probes = &newProbes
|
2016-10-07 20:14:26 +02:00
|
|
|
|
|
|
|
resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := lbClient.Get(resGroup, loadBalancerName, "")
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
|
|
|
|
}
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func expandAzureRmLoadBalancerProbe(d *schema.ResourceData, lb *network.LoadBalancer) (*network.Probe, error) {
|
|
|
|
|
|
|
|
properties := network.ProbePropertiesFormat{
|
|
|
|
NumberOfProbes: azure.Int32(int32(d.Get("number_of_probes").(int))),
|
|
|
|
IntervalInSeconds: azure.Int32(int32(d.Get("interval_in_seconds").(int))),
|
|
|
|
Port: azure.Int32(int32(d.Get("port").(int))),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("protocol"); ok {
|
|
|
|
properties.Protocol = network.ProbeProtocol(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("request_path"); ok {
|
|
|
|
properties.RequestPath = azure.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
probe := network.Probe{
|
2016-12-06 09:39:47 +01:00
|
|
|
Name: azure.String(d.Get("name").(string)),
|
|
|
|
ProbePropertiesFormat: &properties,
|
2016-10-07 20:14:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &probe, nil
|
|
|
|
}
|