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