github.com/ottenhoff/terraform@v0.7.0-rc1.0.20160607213102-ac2d195cc560/builtin/providers/azurerm/resource_arm_availability_set.go (about)

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