github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_availability_set.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"strings"
     8  
     9  	"github.com/Azure/azure-sdk-for-go/arm/compute"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  	"github.com/jen20/riviera/azure"
    12  )
    13  
    14  func resourceArmAvailabilitySet() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceArmAvailabilitySetCreate,
    17  		Read:   resourceArmAvailabilitySetRead,
    18  		Update: resourceArmAvailabilitySetCreate,
    19  		Delete: resourceArmAvailabilitySetDelete,
    20  		Importer: &schema.ResourceImporter{
    21  			State: schema.ImportStatePassthrough,
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"name": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"resource_group_name": {
    32  				Type:     schema.TypeString,
    33  				Required: true,
    34  				ForceNew: true,
    35  			},
    36  
    37  			"location": locationSchema(),
    38  
    39  			"platform_update_domain_count": {
    40  				Type:     schema.TypeInt,
    41  				Optional: true,
    42  				Default:  5,
    43  				ForceNew: true,
    44  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    45  					value := v.(int)
    46  					if value > 20 {
    47  						errors = append(errors, fmt.Errorf(
    48  							"Maximum value for `platform_update_domain_count` is 20"))
    49  					}
    50  					return
    51  				},
    52  			},
    53  
    54  			"platform_fault_domain_count": {
    55  				Type:     schema.TypeInt,
    56  				Optional: true,
    57  				Default:  3,
    58  				ForceNew: true,
    59  				ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
    60  					value := v.(int)
    61  					if value > 3 {
    62  						errors = append(errors, fmt.Errorf(
    63  							"Maximum value for (%s) is 3", k))
    64  					}
    65  					return
    66  				},
    67  			},
    68  
    69  			"managed": {
    70  				Type:     schema.TypeBool,
    71  				Optional: true,
    72  				Default:  false,
    73  				ForceNew: true,
    74  			},
    75  
    76  			"tags": tagsSchema(),
    77  		},
    78  	}
    79  }
    80  
    81  func resourceArmAvailabilitySetCreate(d *schema.ResourceData, meta interface{}) error {
    82  	client := meta.(*ArmClient)
    83  	availSetClient := client.availSetClient
    84  
    85  	log.Printf("[INFO] preparing arguments for Azure ARM Availability Set creation.")
    86  
    87  	name := d.Get("name").(string)
    88  	location := d.Get("location").(string)
    89  	resGroup := d.Get("resource_group_name").(string)
    90  	updateDomainCount := d.Get("platform_update_domain_count").(int)
    91  	faultDomainCount := d.Get("platform_fault_domain_count").(int)
    92  	tags := d.Get("tags").(map[string]interface{})
    93  	managed := d.Get("managed").(bool)
    94  
    95  	availSet := compute.AvailabilitySet{
    96  		Name:     &name,
    97  		Location: &location,
    98  		AvailabilitySetProperties: &compute.AvailabilitySetProperties{
    99  			PlatformFaultDomainCount:  azure.Int32(int32(faultDomainCount)),
   100  			PlatformUpdateDomainCount: azure.Int32(int32(updateDomainCount)),
   101  		},
   102  		Tags: expandTags(tags),
   103  	}
   104  
   105  	if managed == true {
   106  		n := "Aligned"
   107  		availSet.Sku = &compute.Sku{
   108  			Name: &n,
   109  		}
   110  	}
   111  
   112  	resp, err := availSetClient.CreateOrUpdate(resGroup, name, availSet)
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	d.SetId(*resp.ID)
   118  
   119  	return resourceArmAvailabilitySetRead(d, meta)
   120  }
   121  
   122  func resourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) error {
   123  	availSetClient := meta.(*ArmClient).availSetClient
   124  
   125  	id, err := parseAzureResourceID(d.Id())
   126  	if err != nil {
   127  		return err
   128  	}
   129  	resGroup := id.ResourceGroup
   130  	name := id.Path["availabilitySets"]
   131  
   132  	resp, err := availSetClient.Get(resGroup, name)
   133  	if err != nil {
   134  		if resp.StatusCode == http.StatusNotFound {
   135  			d.SetId("")
   136  			return nil
   137  		}
   138  		return fmt.Errorf("Error making Read request on Azure Availability Set %s: %s", name, err)
   139  	}
   140  
   141  	availSet := *resp.AvailabilitySetProperties
   142  	d.Set("resource_group_name", resGroup)
   143  	d.Set("platform_update_domain_count", availSet.PlatformUpdateDomainCount)
   144  	d.Set("platform_fault_domain_count", availSet.PlatformFaultDomainCount)
   145  	d.Set("name", resp.Name)
   146  	d.Set("location", resp.Location)
   147  
   148  	if resp.Sku != nil && resp.Sku.Name != nil {
   149  		d.Set("managed", strings.EqualFold(*resp.Sku.Name, "Aligned"))
   150  	}
   151  
   152  	flattenAndSetTags(d, resp.Tags)
   153  
   154  	return nil
   155  }
   156  
   157  func resourceArmAvailabilitySetDelete(d *schema.ResourceData, meta interface{}) error {
   158  	availSetClient := meta.(*ArmClient).availSetClient
   159  
   160  	id, err := parseAzureResourceID(d.Id())
   161  	if err != nil {
   162  		return err
   163  	}
   164  	resGroup := id.ResourceGroup
   165  	name := id.Path["availabilitySets"]
   166  
   167  	_, err = availSetClient.Delete(resGroup, name)
   168  
   169  	return err
   170  }