github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  		Importer: &schema.ResourceImporter{
    20  			State: schema.ImportStatePassthrough,
    21  		},
    22  
    23  		Schema: map[string]*schema.Schema{
    24  			"name": {
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  
    30  			"resource_group_name": {
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  
    36  			"location": locationSchema(),
    37  
    38  			"platform_update_domain_count": {
    39  				Type:     schema.TypeInt,
    40  				Optional: true,
    41  				Default:  5,
    42  				ForceNew: true,
    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": {
    54  				Type:     schema.TypeInt,
    55  				Optional: true,
    56  				Default:  3,
    57  				ForceNew: true,
    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  		AvailabilitySetProperties: &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 err != nil {
   118  		if resp.StatusCode == http.StatusNotFound {
   119  			d.SetId("")
   120  			return nil
   121  		}
   122  		return fmt.Errorf("Error making Read request on Azure Availability Set %s: %s", name, err)
   123  	}
   124  
   125  	availSet := *resp.AvailabilitySetProperties
   126  	d.Set("resource_group_name", resGroup)
   127  	d.Set("platform_update_domain_count", availSet.PlatformUpdateDomainCount)
   128  	d.Set("platform_fault_domain_count", availSet.PlatformFaultDomainCount)
   129  	d.Set("name", resp.Name)
   130  	d.Set("location", resp.Location)
   131  
   132  	flattenAndSetTags(d, resp.Tags)
   133  
   134  	return nil
   135  }
   136  
   137  func resourceArmAvailabilitySetDelete(d *schema.ResourceData, meta interface{}) error {
   138  	availSetClient := meta.(*ArmClient).availSetClient
   139  
   140  	id, err := parseAzureResourceID(d.Id())
   141  	if err != nil {
   142  		return err
   143  	}
   144  	resGroup := id.ResourceGroup
   145  	name := id.Path["availabilitySets"]
   146  
   147  	_, err = availSetClient.Delete(resGroup, name)
   148  
   149  	return err
   150  }