github.com/leeprovoost/terraform@v0.6.10-0.20160119085442-96f3f76118e7/builtin/providers/azurerm/resource_arm_resource_group.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 "regexp" 8 "strings" 9 "time" 10 11 "github.com/Azure/azure-sdk-for-go/arm/resources/resources" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/helper/schema" 14 ) 15 16 func resourceArmResourceGroup() *schema.Resource { 17 return &schema.Resource{ 18 Create: resourceArmResourceGroupCreate, 19 Read: resourceArmResourceGroupRead, 20 Update: resourceArmResourceGroupUpdate, 21 Exists: resourceArmResourceGroupExists, 22 Delete: resourceArmResourceGroupDelete, 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 resGroupClient := client.resourceGroupClient 64 65 if !d.HasChange("tags") { 66 return nil 67 } 68 69 name := d.Get("name").(string) 70 71 newTags := d.Get("tags").(map[string]interface{}) 72 _, err := resGroupClient.Patch(name, resources.ResourceGroup{ 73 Tags: expandTags(newTags), 74 }) 75 if err != nil { 76 return fmt.Errorf("Error issuing Azure ARM create request to update resource group %q: %s", name, err) 77 } 78 79 return resourceArmResourceGroupRead(d, meta) 80 } 81 82 func resourceArmResourceGroupCreate(d *schema.ResourceData, meta interface{}) error { 83 client := meta.(*ArmClient) 84 resGroupClient := client.resourceGroupClient 85 86 name := d.Get("name").(string) 87 location := d.Get("location").(string) 88 tags := d.Get("tags").(map[string]interface{}) 89 90 rg := resources.ResourceGroup{ 91 Name: &name, 92 Location: &location, 93 Tags: expandTags(tags), 94 } 95 96 resp, err := resGroupClient.CreateOrUpdate(name, rg) 97 if err != nil { 98 return fmt.Errorf("Error issuing Azure ARM create request for resource group '%s': %s", name, err) 99 } 100 101 d.SetId(*resp.ID) 102 103 log.Printf("[DEBUG] Waiting for Resource Group (%s) to become available", name) 104 stateConf := &resource.StateChangeConf{ 105 Pending: []string{"Accepted"}, 106 Target: "Succeeded", 107 Refresh: resourceGroupStateRefreshFunc(client, name), 108 Timeout: 10 * time.Minute, 109 } 110 if _, err := stateConf.WaitForState(); err != nil { 111 return fmt.Errorf("Error waiting for Resource Group (%s) to become available: %s", name, err) 112 } 113 114 return resourceArmResourceGroupRead(d, meta) 115 } 116 117 func resourceArmResourceGroupRead(d *schema.ResourceData, meta interface{}) error { 118 resGroupClient := meta.(*ArmClient).resourceGroupClient 119 120 id, err := parseAzureResourceID(d.Id()) 121 if err != nil { 122 return err 123 } 124 name := id.ResourceGroup 125 126 res, err := resGroupClient.Get(name) 127 if err != nil { 128 if res.StatusCode == http.StatusNotFound { 129 d.SetId("") 130 return nil 131 } 132 return fmt.Errorf("Error issuing read request to Azure ARM for resource group '%s': %s", name, err) 133 } 134 135 d.Set("name", res.Name) 136 d.Set("location", res.Location) 137 138 flattenAndSetTags(d, res.Tags) 139 140 return nil 141 } 142 143 func resourceArmResourceGroupExists(d *schema.ResourceData, meta interface{}) (bool, error) { 144 resGroupClient := meta.(*ArmClient).resourceGroupClient 145 146 id, err := parseAzureResourceID(d.Id()) 147 if err != nil { 148 return false, err 149 } 150 name := id.ResourceGroup 151 152 resp, err := resGroupClient.CheckExistence(name) 153 if err != nil { 154 if resp.StatusCode != 200 { 155 return false, err 156 } 157 158 return true, nil 159 } 160 161 return true, nil 162 } 163 164 func resourceArmResourceGroupDelete(d *schema.ResourceData, meta interface{}) error { 165 resGroupClient := meta.(*ArmClient).resourceGroupClient 166 167 id, err := parseAzureResourceID(d.Id()) 168 if err != nil { 169 return err 170 } 171 name := id.ResourceGroup 172 173 _, err = resGroupClient.Delete(name) 174 if err != nil { 175 return err 176 } 177 178 return nil 179 } 180 181 func resourceGroupStateRefreshFunc(client *ArmClient, id string) resource.StateRefreshFunc { 182 return func() (interface{}, string, error) { 183 res, err := client.resourceGroupClient.Get(id) 184 if err != nil { 185 return nil, "", fmt.Errorf("Error issuing read request in resourceGroupStateRefreshFunc to Azure ARM for resource group '%s': %s", id, err) 186 } 187 188 return res, *res.Properties.ProvisioningState, nil 189 } 190 }