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