github.com/pbthorste/terraform@v0.8.6-0.20170127005045-deb56bd93da2/builtin/providers/azurerm/resource_arm_loadbalancer_probe.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/arm/network"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  	"github.com/jen20/riviera/azure"
    13  )
    14  
    15  func resourceArmLoadBalancerProbe() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceArmLoadBalancerProbeCreate,
    18  		Read:   resourceArmLoadBalancerProbeRead,
    19  		Update: resourceArmLoadBalancerProbeCreate,
    20  		Delete: resourceArmLoadBalancerProbeDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": {
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"location": locationSchema(),
    30  
    31  			"resource_group_name": {
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"loadbalancer_id": {
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"protocol": {
    44  				Type:     schema.TypeString,
    45  				Computed: true,
    46  				Optional: true,
    47  			},
    48  
    49  			"port": {
    50  				Type:     schema.TypeInt,
    51  				Required: true,
    52  			},
    53  
    54  			"request_path": {
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  			},
    58  
    59  			"interval_in_seconds": {
    60  				Type:     schema.TypeInt,
    61  				Optional: true,
    62  				Default:  15,
    63  			},
    64  
    65  			"number_of_probes": {
    66  				Type:     schema.TypeInt,
    67  				Optional: true,
    68  				Default:  2,
    69  			},
    70  
    71  			"load_balance_rules": {
    72  				Type:     schema.TypeSet,
    73  				Computed: true,
    74  				Elem:     &schema.Schema{Type: schema.TypeString},
    75  				Set:      schema.HashString,
    76  			},
    77  		},
    78  	}
    79  }
    80  
    81  func resourceArmLoadBalancerProbeCreate(d *schema.ResourceData, meta interface{}) error {
    82  	client := meta.(*ArmClient)
    83  	lbClient := client.loadBalancerClient
    84  
    85  	loadBalancerID := d.Get("loadbalancer_id").(string)
    86  	armMutexKV.Lock(loadBalancerID)
    87  	defer armMutexKV.Unlock(loadBalancerID)
    88  
    89  	loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
    90  	if err != nil {
    91  		return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
    92  	}
    93  	if !exists {
    94  		d.SetId("")
    95  		log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
    96  		return nil
    97  	}
    98  
    99  	newProbe, err := expandAzureRmLoadBalancerProbe(d, loadBalancer)
   100  	if err != nil {
   101  		return errwrap.Wrapf("Error Expanding Probe {{err}}", err)
   102  	}
   103  
   104  	probes := append(*loadBalancer.LoadBalancerPropertiesFormat.Probes, *newProbe)
   105  
   106  	existingProbe, existingProbeIndex, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string))
   107  	if exists {
   108  		if d.Get("name").(string) == *existingProbe.Name {
   109  			// this probe is being updated/reapplied remove old copy from the slice
   110  			probes = append(probes[:existingProbeIndex], probes[existingProbeIndex+1:]...)
   111  		}
   112  	}
   113  
   114  	loadBalancer.LoadBalancerPropertiesFormat.Probes = &probes
   115  	resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
   116  	if err != nil {
   117  		return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
   118  	}
   119  
   120  	_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
   121  	if err != nil {
   122  		return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
   123  	}
   124  
   125  	read, err := lbClient.Get(resGroup, loadBalancerName, "")
   126  	if err != nil {
   127  		return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
   128  	}
   129  	if read.ID == nil {
   130  		return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
   131  	}
   132  
   133  	var createdProbe_id string
   134  	for _, Probe := range *(*read.LoadBalancerPropertiesFormat).Probes {
   135  		if *Probe.Name == d.Get("name").(string) {
   136  			createdProbe_id = *Probe.ID
   137  		}
   138  	}
   139  
   140  	if createdProbe_id != "" {
   141  		d.SetId(createdProbe_id)
   142  	} else {
   143  		return fmt.Errorf("Cannot find created LoadBalancer Probe ID %q", createdProbe_id)
   144  	}
   145  
   146  	log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
   147  	stateConf := &resource.StateChangeConf{
   148  		Pending: []string{"Accepted", "Updating"},
   149  		Target:  []string{"Succeeded"},
   150  		Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName),
   151  		Timeout: 10 * time.Minute,
   152  	}
   153  	if _, err := stateConf.WaitForState(); err != nil {
   154  		return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err)
   155  	}
   156  
   157  	return resourceArmLoadBalancerProbeRead(d, meta)
   158  }
   159  
   160  func resourceArmLoadBalancerProbeRead(d *schema.ResourceData, meta interface{}) error {
   161  	loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
   162  	if err != nil {
   163  		return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
   164  	}
   165  	if !exists {
   166  		d.SetId("")
   167  		log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
   168  		return nil
   169  	}
   170  
   171  	configs := *loadBalancer.LoadBalancerPropertiesFormat.Probes
   172  	for _, config := range configs {
   173  		if *config.Name == d.Get("name").(string) {
   174  			d.Set("name", config.Name)
   175  
   176  			d.Set("protocol", config.ProbePropertiesFormat.Protocol)
   177  			d.Set("interval_in_seconds", config.ProbePropertiesFormat.IntervalInSeconds)
   178  			d.Set("number_of_probes", config.ProbePropertiesFormat.NumberOfProbes)
   179  			d.Set("port", config.ProbePropertiesFormat.Port)
   180  			d.Set("request_path", config.ProbePropertiesFormat.RequestPath)
   181  
   182  			break
   183  		}
   184  	}
   185  
   186  	return nil
   187  }
   188  
   189  func resourceArmLoadBalancerProbeDelete(d *schema.ResourceData, meta interface{}) error {
   190  	client := meta.(*ArmClient)
   191  	lbClient := client.loadBalancerClient
   192  
   193  	loadBalancerID := d.Get("loadbalancer_id").(string)
   194  	armMutexKV.Lock(loadBalancerID)
   195  	defer armMutexKV.Unlock(loadBalancerID)
   196  
   197  	loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
   198  	if err != nil {
   199  		return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
   200  	}
   201  	if !exists {
   202  		d.SetId("")
   203  		return nil
   204  	}
   205  
   206  	_, index, exists := findLoadBalancerProbeByName(loadBalancer, d.Get("name").(string))
   207  	if !exists {
   208  		return nil
   209  	}
   210  
   211  	oldProbes := *loadBalancer.LoadBalancerPropertiesFormat.Probes
   212  	newProbes := append(oldProbes[:index], oldProbes[index+1:]...)
   213  	loadBalancer.LoadBalancerPropertiesFormat.Probes = &newProbes
   214  
   215  	resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
   216  	if err != nil {
   217  		return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
   218  	}
   219  
   220  	_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
   221  	if err != nil {
   222  		return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
   223  	}
   224  
   225  	read, err := lbClient.Get(resGroup, loadBalancerName, "")
   226  	if err != nil {
   227  		return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
   228  	}
   229  	if read.ID == nil {
   230  		return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
   231  	}
   232  
   233  	return nil
   234  }
   235  
   236  func expandAzureRmLoadBalancerProbe(d *schema.ResourceData, lb *network.LoadBalancer) (*network.Probe, error) {
   237  
   238  	properties := network.ProbePropertiesFormat{
   239  		NumberOfProbes:    azure.Int32(int32(d.Get("number_of_probes").(int))),
   240  		IntervalInSeconds: azure.Int32(int32(d.Get("interval_in_seconds").(int))),
   241  		Port:              azure.Int32(int32(d.Get("port").(int))),
   242  	}
   243  
   244  	if v, ok := d.GetOk("protocol"); ok {
   245  		properties.Protocol = network.ProbeProtocol(v.(string))
   246  	}
   247  
   248  	if v, ok := d.GetOk("request_path"); ok {
   249  		properties.RequestPath = azure.String(v.(string))
   250  	}
   251  
   252  	probe := network.Probe{
   253  		Name: azure.String(d.Get("name").(string)),
   254  		ProbePropertiesFormat: &properties,
   255  	}
   256  
   257  	return &probe, nil
   258  }