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