github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_loadbalancer_backend_address_pool.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 resourceArmLoadBalancerBackendAddressPool() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceArmLoadBalancerBackendAddressPoolCreate,
    18  		Read:   resourceArmLoadBalancerBackendAddressPoolRead,
    19  		Delete: resourceArmLoadBalancerBackendAddressPoolDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  
    28  			"location": {
    29  				Type:      schema.TypeString,
    30  				Required:  true,
    31  				ForceNew:  true,
    32  				StateFunc: azureRMNormalizeLocation,
    33  			},
    34  
    35  			"resource_group_name": {
    36  				Type:     schema.TypeString,
    37  				Required: true,
    38  				ForceNew: true,
    39  			},
    40  
    41  			"loadbalancer_id": {
    42  				Type:     schema.TypeString,
    43  				Required: true,
    44  				ForceNew: true,
    45  			},
    46  
    47  			"backend_ip_configurations": {
    48  				Type:     schema.TypeSet,
    49  				Computed: true,
    50  				Elem:     &schema.Schema{Type: schema.TypeString},
    51  				Set:      schema.HashString,
    52  			},
    53  
    54  			"load_balancing_rules": {
    55  				Type:     schema.TypeSet,
    56  				Computed: true,
    57  				Elem:     &schema.Schema{Type: schema.TypeString},
    58  				Set:      schema.HashString,
    59  			},
    60  		},
    61  	}
    62  }
    63  
    64  func resourceArmLoadBalancerBackendAddressPoolCreate(d *schema.ResourceData, meta interface{}) error {
    65  	client := meta.(*ArmClient)
    66  	lbClient := client.loadBalancerClient
    67  
    68  	loadBalancerID := d.Get("loadbalancer_id").(string)
    69  	armMutexKV.Lock(loadBalancerID)
    70  	defer armMutexKV.Unlock(loadBalancerID)
    71  
    72  	loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
    73  	if err != nil {
    74  		return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
    75  	}
    76  	if !exists {
    77  		d.SetId("")
    78  		log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
    79  		return nil
    80  	}
    81  
    82  	_, _, exists = findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
    83  	if exists {
    84  		return fmt.Errorf("A BackEnd Address Pool with name %q already exists.", d.Get("name").(string))
    85  	}
    86  
    87  	backendAddressPools := append(*loadBalancer.Properties.BackendAddressPools, expandAzureRmLoadBalancerBackendAddressPools(d))
    88  	loadBalancer.Properties.BackendAddressPools = &backendAddressPools
    89  	resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
    90  	if err != nil {
    91  		return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
    92  	}
    93  
    94  	_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
    95  	if err != nil {
    96  		return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
    97  	}
    98  
    99  	read, err := lbClient.Get(resGroup, loadBalancerName, "")
   100  	if err != nil {
   101  		return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
   102  	}
   103  	if read.ID == nil {
   104  		return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
   105  	}
   106  
   107  	var pool_id string
   108  	for _, BackendAddressPool := range *(*read.Properties).BackendAddressPools {
   109  		if *BackendAddressPool.Name == d.Get("name").(string) {
   110  			pool_id = *BackendAddressPool.ID
   111  		}
   112  	}
   113  
   114  	if pool_id != "" {
   115  		d.SetId(pool_id)
   116  	} else {
   117  		return fmt.Errorf("Cannot find created LoadBalancer Backend Address Pool ID %q", pool_id)
   118  	}
   119  
   120  	log.Printf("[DEBUG] Waiting for LoadBalancer (%s) to become available", loadBalancerName)
   121  	stateConf := &resource.StateChangeConf{
   122  		Pending: []string{"Accepted", "Updating"},
   123  		Target:  []string{"Succeeded"},
   124  		Refresh: loadbalancerStateRefreshFunc(client, resGroup, loadBalancerName),
   125  		Timeout: 10 * time.Minute,
   126  	}
   127  	if _, err := stateConf.WaitForState(); err != nil {
   128  		return fmt.Errorf("Error waiting for LoadBalancer (%s) to become available: %s", loadBalancerName, err)
   129  	}
   130  
   131  	return resourceArmLoadBalancerBackendAddressPoolRead(d, meta)
   132  }
   133  
   134  func resourceArmLoadBalancerBackendAddressPoolRead(d *schema.ResourceData, meta interface{}) error {
   135  	loadBalancer, exists, err := retrieveLoadBalancerById(d.Get("loadbalancer_id").(string), meta)
   136  	if err != nil {
   137  		return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
   138  	}
   139  	if !exists {
   140  		d.SetId("")
   141  		log.Printf("[INFO] LoadBalancer %q not found. Removing from state", d.Get("name").(string))
   142  		return nil
   143  	}
   144  
   145  	configs := *loadBalancer.Properties.BackendAddressPools
   146  	for _, config := range configs {
   147  		if *config.Name == d.Get("name").(string) {
   148  			d.Set("name", config.Name)
   149  
   150  			if config.Properties.BackendIPConfigurations != nil {
   151  				backend_ip_configurations := make([]string, 0, len(*config.Properties.BackendIPConfigurations))
   152  				for _, backendConfig := range *config.Properties.BackendIPConfigurations {
   153  					backend_ip_configurations = append(backend_ip_configurations, *backendConfig.ID)
   154  				}
   155  
   156  				d.Set("backend_ip_configurations", backend_ip_configurations)
   157  			}
   158  
   159  			if config.Properties.LoadBalancingRules != nil {
   160  				load_balancing_rules := make([]string, 0, len(*config.Properties.LoadBalancingRules))
   161  				for _, rule := range *config.Properties.LoadBalancingRules {
   162  					load_balancing_rules = append(load_balancing_rules, *rule.ID)
   163  				}
   164  
   165  				d.Set("backend_ip_configurations", load_balancing_rules)
   166  			}
   167  
   168  			break
   169  		}
   170  	}
   171  
   172  	return nil
   173  }
   174  
   175  func resourceArmLoadBalancerBackendAddressPoolDelete(d *schema.ResourceData, meta interface{}) error {
   176  	client := meta.(*ArmClient)
   177  	lbClient := client.loadBalancerClient
   178  
   179  	loadBalancerID := d.Get("loadbalancer_id").(string)
   180  	armMutexKV.Lock(loadBalancerID)
   181  	defer armMutexKV.Unlock(loadBalancerID)
   182  
   183  	loadBalancer, exists, err := retrieveLoadBalancerById(loadBalancerID, meta)
   184  	if err != nil {
   185  		return errwrap.Wrapf("Error Getting LoadBalancer By ID {{err}}", err)
   186  	}
   187  	if !exists {
   188  		d.SetId("")
   189  		return nil
   190  	}
   191  
   192  	_, index, exists := findLoadBalancerBackEndAddressPoolByName(loadBalancer, d.Get("name").(string))
   193  	if !exists {
   194  		return nil
   195  	}
   196  
   197  	oldBackEndPools := *loadBalancer.Properties.BackendAddressPools
   198  	newBackEndPools := append(oldBackEndPools[:index], oldBackEndPools[index+1:]...)
   199  	loadBalancer.Properties.BackendAddressPools = &newBackEndPools
   200  
   201  	resGroup, loadBalancerName, err := resourceGroupAndLBNameFromId(d.Get("loadbalancer_id").(string))
   202  	if err != nil {
   203  		return errwrap.Wrapf("Error Getting LoadBalancer Name and Group: {{err}}", err)
   204  	}
   205  
   206  	_, err = lbClient.CreateOrUpdate(resGroup, loadBalancerName, *loadBalancer, make(chan struct{}))
   207  	if err != nil {
   208  		return errwrap.Wrapf("Error Creating/Updating LoadBalancer {{err}}", err)
   209  	}
   210  
   211  	read, err := lbClient.Get(resGroup, loadBalancerName, "")
   212  	if err != nil {
   213  		return errwrap.Wrapf("Error Getting LoadBalancer {{err}}", err)
   214  	}
   215  	if read.ID == nil {
   216  		return fmt.Errorf("Cannot read LoadBalancer %s (resource group %s) ID", loadBalancerName, resGroup)
   217  	}
   218  
   219  	return nil
   220  }
   221  
   222  func expandAzureRmLoadBalancerBackendAddressPools(d *schema.ResourceData) network.BackendAddressPool {
   223  	return network.BackendAddressPool{
   224  		Name: azure.String(d.Get("name").(string)),
   225  	}
   226  }