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