github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/azure/resource_azure_affinity_group.go (about)

     1  package azure
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/Azure/azure-sdk-for-go/management"
     8  	"github.com/Azure/azure-sdk-for-go/management/affinitygroup"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  // resourceAzureAffinityGroup returns the *schema.Resource associated to a
    13  // resource affinity group on Azure.
    14  func resourceAzureAffinityGroup() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceAzureAffinityGroupCreate,
    17  		Read:   resourceAzureAffinityGroupRead,
    18  		Update: resourceAzureAffinityGroupUpdate,
    19  		Exists: resourceAzureAffinityGroupExists,
    20  		Delete: resourceAzureAffinityGroupDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"name": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  			"location": &schema.Schema{
    29  				Type:     schema.TypeString,
    30  				Required: true,
    31  				ForceNew: true,
    32  			},
    33  			"label": &schema.Schema{
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  			},
    37  			"description": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Optional: true,
    40  			},
    41  		},
    42  	}
    43  }
    44  
    45  // resourceAzureAffinityGroupCreate does all the necessary API calls to
    46  // create an affinity group on Azure.
    47  func resourceAzureAffinityGroupCreate(d *schema.ResourceData, meta interface{}) error {
    48  	affinityGroupClient := meta.(*Client).affinityGroupClient
    49  
    50  	log.Println("[INFO] Begun creating Azure Affinity Group creation request.")
    51  	name := d.Get("name").(string)
    52  	params := affinitygroup.CreateAffinityGroupParams{
    53  		Name:     name,
    54  		Label:    d.Get("label").(string),
    55  		Location: d.Get("location").(string),
    56  	}
    57  
    58  	if desc, ok := d.GetOk("description"); ok {
    59  		params.Description = desc.(string)
    60  	}
    61  
    62  	log.Println("[INFO] Sending Affinity Group creation request to Azure.")
    63  	err := affinityGroupClient.CreateAffinityGroup(params)
    64  	if err != nil {
    65  		return fmt.Errorf("Error issuing Azure Affinity Group creation: %s", err)
    66  	}
    67  
    68  	d.SetId(name)
    69  	return nil
    70  }
    71  
    72  // resourceAzureAffinityGroupRead does all the necessary API calls to
    73  // read the state of the affinity group off Azure.
    74  func resourceAzureAffinityGroupRead(d *schema.ResourceData, meta interface{}) error {
    75  	affinityGroupClient := meta.(*Client).affinityGroupClient
    76  
    77  	log.Println("[INFO] Issuing Azure Affinity Group list request.")
    78  	affinityGroups, err := affinityGroupClient.ListAffinityGroups()
    79  	if err != nil {
    80  		return fmt.Errorf("Error obtaining Affinity Group list off Azure: %s", err)
    81  	}
    82  
    83  	var found bool
    84  	name := d.Get("name").(string)
    85  	for _, group := range affinityGroups.AffinityGroups {
    86  		if group.Name == name {
    87  			found = true
    88  			d.Set("location", group.Location)
    89  			d.Set("label", group.Label)
    90  			d.Set("description", group.Description)
    91  			break
    92  		}
    93  	}
    94  
    95  	if !found {
    96  		// it means the affinity group has been deleted in the meantime, so we
    97  		// must stop tracking it:
    98  		d.SetId("")
    99  	}
   100  
   101  	return nil
   102  }
   103  
   104  // resourceAzureAffinityGroupUpdate does all the necessary API calls to
   105  // update the state of the affinity group on Azure.
   106  func resourceAzureAffinityGroupUpdate(d *schema.ResourceData, meta interface{}) error {
   107  	affinityGroupClient := meta.(*Client).affinityGroupClient
   108  
   109  	name := d.Get("name").(string)
   110  	clabel := d.HasChange("label")
   111  	cdesc := d.HasChange("description")
   112  	if clabel || cdesc {
   113  		log.Println("[INFO] Beginning Affinity Group update process.")
   114  		params := affinitygroup.UpdateAffinityGroupParams{}
   115  
   116  		if clabel {
   117  			params.Label = d.Get("label").(string)
   118  		}
   119  		if cdesc {
   120  			params.Description = d.Get("description").(string)
   121  		}
   122  
   123  		log.Println("[INFO] Sending Affinity Group update request to Azure.")
   124  		err := affinityGroupClient.UpdateAffinityGroup(name, params)
   125  		if err != nil {
   126  			return fmt.Errorf("Error updating Azure Affinity Group parameters: %s", err)
   127  		}
   128  	}
   129  
   130  	return nil
   131  }
   132  
   133  // resourceAzureAffinityGroupExists does all the necessary API calls to
   134  // check for the existence of the affinity group on Azure.
   135  func resourceAzureAffinityGroupExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   136  	affinityGroupClient := meta.(*Client).affinityGroupClient
   137  
   138  	log.Println("[INFO] Issuing Azure Affinity Group get request.")
   139  	name := d.Get("name").(string)
   140  	_, err := affinityGroupClient.GetAffinityGroup(name)
   141  	if err != nil {
   142  		if management.IsResourceNotFoundError(err) {
   143  			// it means that the affinity group has been deleted in the
   144  			// meantime, so we must untrack it from the schema:
   145  			d.SetId("")
   146  			return false, nil
   147  		} else {
   148  			return false, fmt.Errorf("Error getting Affinity Group off Azure: %s", err)
   149  		}
   150  	}
   151  
   152  	return true, nil
   153  }
   154  
   155  // resourceAzureAffinityGroupDelete does all the necessary API calls to
   156  // delete the affinity group off Azure.
   157  func resourceAzureAffinityGroupDelete(d *schema.ResourceData, meta interface{}) error {
   158  	affinityGroupClient := meta.(*Client).affinityGroupClient
   159  
   160  	log.Println("[INFO] Sending Affinity Group deletion request to Azure.")
   161  	name := d.Get("name").(string)
   162  	err := affinityGroupClient.DeleteAffinityGroup(name)
   163  	if err != nil {
   164  		return fmt.Errorf("Error deleting Azure Affinity Group: %s", err)
   165  	}
   166  
   167  	return nil
   168  }