github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/azurerm/resource_arm_resource_group.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  	"github.com/jen20/riviera/azure"
    11  )
    12  
    13  func resourceArmResourceGroup() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceArmResourceGroupCreate,
    16  		Read:   resourceArmResourceGroupRead,
    17  		Update: resourceArmResourceGroupUpdate,
    18  		Exists: resourceArmResourceGroupExists,
    19  		Delete: resourceArmResourceGroupDelete,
    20  		Importer: &schema.ResourceImporter{
    21  			State: schema.ImportStatePassthrough,
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"name": &schema.Schema{
    26  				Type:         schema.TypeString,
    27  				Required:     true,
    28  				ForceNew:     true,
    29  				ValidateFunc: validateArmResourceGroupName,
    30  			},
    31  			"location": &schema.Schema{
    32  				Type:      schema.TypeString,
    33  				Required:  true,
    34  				ForceNew:  true,
    35  				StateFunc: azureRMNormalizeLocation,
    36  			},
    37  
    38  			"tags": tagsSchema(),
    39  		},
    40  	}
    41  }
    42  
    43  func validateArmResourceGroupName(v interface{}, k string) (ws []string, es []error) {
    44  	value := v.(string)
    45  
    46  	if len(value) > 80 {
    47  		es = append(es, fmt.Errorf("%q may not exceed 80 characters in length", k))
    48  	}
    49  
    50  	if strings.HasSuffix(value, ".") {
    51  		es = append(es, fmt.Errorf("%q may not end with a period", k))
    52  	}
    53  
    54  	if matched := regexp.MustCompile(`[\(\)\.a-zA-Z0-9_-]`).Match([]byte(value)); !matched {
    55  		es = append(es, fmt.Errorf("%q may only contain alphanumeric characters, dash, underscores, parentheses and periods", k))
    56  	}
    57  
    58  	return
    59  }
    60  
    61  func resourceArmResourceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
    62  	client := meta.(*ArmClient)
    63  	rivieraClient := client.rivieraClient
    64  
    65  	if !d.HasChange("tags") {
    66  		return nil
    67  	}
    68  
    69  	name := d.Get("name").(string)
    70  	newTags := d.Get("tags").(map[string]interface{})
    71  
    72  	updateRequest := rivieraClient.NewRequestForURI(d.Id())
    73  	updateRequest.Command = &azure.UpdateResourceGroup{
    74  		Name: name,
    75  		Tags: *expandTags(newTags),
    76  	}
    77  
    78  	updateResponse, err := updateRequest.Execute()
    79  	if err != nil {
    80  		return fmt.Errorf("Error updating resource group: %s", err)
    81  	}
    82  	if !updateResponse.IsSuccessful() {
    83  		return fmt.Errorf("Error updating resource group: %s", updateResponse.Error)
    84  	}
    85  
    86  	return resourceArmResourceGroupRead(d, meta)
    87  }
    88  
    89  func resourceArmResourceGroupCreate(d *schema.ResourceData, meta interface{}) error {
    90  	client := meta.(*ArmClient)
    91  	rivieraClient := client.rivieraClient
    92  
    93  	createRequest := rivieraClient.NewRequest()
    94  	createRequest.Command = &azure.CreateResourceGroup{
    95  		Name:     d.Get("name").(string),
    96  		Location: d.Get("location").(string),
    97  		Tags:     *expandTags(d.Get("tags").(map[string]interface{})),
    98  	}
    99  
   100  	createResponse, err := createRequest.Execute()
   101  	if err != nil {
   102  		return fmt.Errorf("Error creating resource group: %s", err)
   103  	}
   104  	if !createResponse.IsSuccessful() {
   105  		return fmt.Errorf("Error creating resource group: %s", createResponse.Error)
   106  	}
   107  
   108  	resp := createResponse.Parsed.(*azure.CreateResourceGroupResponse)
   109  	d.SetId(*resp.ID)
   110  
   111  	// TODO(jen20): Decide whether we need this or not and migrate to use @stack72's work if so
   112  	// log.Printf("[DEBUG] Waiting for Resource Group (%s) to become available", name)
   113  	// stateConf := &resource.StateChangeConf{
   114  	// 	Pending: []string{"Accepted"},
   115  	// 	Target:  []string{"Succeeded"},
   116  	// 	Refresh: resourceGroupStateRefreshFunc(client, name),
   117  	// 	Timeout: 10 * time.Minute,
   118  	// }
   119  	// if _, err := stateConf.WaitForState(); err != nil {
   120  	// 	return fmt.Errorf("Error waiting for Resource Group (%s) to become available: %s", name, err)
   121  	// }
   122  
   123  	return resourceArmResourceGroupRead(d, meta)
   124  }
   125  
   126  func resourceArmResourceGroupRead(d *schema.ResourceData, meta interface{}) error {
   127  	client := meta.(*ArmClient)
   128  	rivieraClient := client.rivieraClient
   129  
   130  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   131  	readRequest.Command = &azure.GetResourceGroup{}
   132  
   133  	readResponse, err := readRequest.Execute()
   134  	if err != nil {
   135  		return fmt.Errorf("Error reading resource group: %s", err)
   136  	}
   137  	if !readResponse.IsSuccessful() {
   138  		log.Printf("[INFO] Error reading resource group %q - removing from state", d.Id())
   139  		d.SetId("")
   140  		return fmt.Errorf("Error reading resource group: %s", readResponse.Error)
   141  	}
   142  
   143  	resp := readResponse.Parsed.(*azure.GetResourceGroupResponse)
   144  
   145  	d.Set("name", resp.Name)
   146  	d.Set("location", resp.Location)
   147  	flattenAndSetTags(d, resp.Tags)
   148  
   149  	return nil
   150  }
   151  
   152  func resourceArmResourceGroupExists(d *schema.ResourceData, meta interface{}) (bool, error) {
   153  	client := meta.(*ArmClient)
   154  	rivieraClient := client.rivieraClient
   155  
   156  	readRequest := rivieraClient.NewRequestForURI(d.Id())
   157  	readRequest.Command = &azure.GetResourceGroup{}
   158  
   159  	readResponse, err := readRequest.Execute()
   160  	if err != nil {
   161  		return false, fmt.Errorf("Error reading resource group: %s", err)
   162  	}
   163  	if readResponse.IsSuccessful() {
   164  		return true, nil
   165  	}
   166  
   167  	return false, nil
   168  }
   169  
   170  func resourceArmResourceGroupDelete(d *schema.ResourceData, meta interface{}) error {
   171  	client := meta.(*ArmClient)
   172  	rivieraClient := client.rivieraClient
   173  
   174  	deleteRequest := rivieraClient.NewRequestForURI(d.Id())
   175  	deleteRequest.Command = &azure.DeleteResourceGroup{}
   176  
   177  	deleteResponse, err := deleteRequest.Execute()
   178  	if err != nil {
   179  		return fmt.Errorf("Error deleting resource group: %s", err)
   180  	}
   181  	if !deleteResponse.IsSuccessful() {
   182  		return fmt.Errorf("Error deleting resource group: %s", deleteResponse.Error)
   183  	}
   184  
   185  	return nil
   186  
   187  }