2015-12-08 23:30:12 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-12-09 02:25:05 +01:00
|
|
|
"net/http"
|
2015-12-09 00:50:48 +01:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-12-08 23:30:12 +01:00
|
|
|
"time"
|
|
|
|
|
2016-01-08 21:06:09 +01:00
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/resources/resources"
|
2015-12-08 23:30:12 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmResourceGroup() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmResourceGroupCreate,
|
|
|
|
Read: resourceArmResourceGroupRead,
|
2016-01-18 19:12:54 +01:00
|
|
|
Update: resourceArmResourceGroupUpdate,
|
2015-12-08 23:30:12 +01:00
|
|
|
Exists: resourceArmResourceGroupExists,
|
|
|
|
Delete: resourceArmResourceGroupDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
2015-12-09 00:50:48 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validateArmResourceGroupName,
|
2015-12-08 23:30:12 +01:00
|
|
|
},
|
|
|
|
"location": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
},
|
2016-01-18 19:12:54 +01:00
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
2015-12-08 23:30:12 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-09 00:50:48 +01:00
|
|
|
func validateArmResourceGroupName(v interface{}, k string) (ws []string, es []error) {
|
|
|
|
value := v.(string)
|
|
|
|
|
|
|
|
if len(value) > 80 {
|
|
|
|
es = append(es, fmt.Errorf("%q may not exceed 80 characters in length", k))
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(value, ".") {
|
|
|
|
es = append(es, fmt.Errorf("%q may not end with a period", k))
|
|
|
|
}
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
if matched := regexp.MustCompile(`[\(\)\.a-zA-Z0-9_-]`).Match([]byte(value)); !matched {
|
2015-12-09 00:50:48 +01:00
|
|
|
es = append(es, fmt.Errorf("%q may only contain alphanumeric characters, dash, underscores, parentheses and periods", k))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-01-18 19:12:54 +01:00
|
|
|
func resourceArmResourceGroupUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
resGroupClient := client.resourceGroupClient
|
|
|
|
|
|
|
|
if !d.HasChange("tags") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
|
|
|
|
newTags := d.Get("tags").(map[string]interface{})
|
|
|
|
_, err := resGroupClient.Patch(name, resources.ResourceGroup{
|
|
|
|
Tags: expandTags(newTags),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error issuing Azure ARM create request to update resource group %q: %s", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceArmResourceGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
func resourceArmResourceGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
resGroupClient := client.resourceGroupClient
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
location := d.Get("location").(string)
|
2016-01-18 19:12:54 +01:00
|
|
|
tags := d.Get("tags").(map[string]interface{})
|
2015-12-08 23:30:12 +01:00
|
|
|
|
|
|
|
rg := resources.ResourceGroup{
|
|
|
|
Name: &name,
|
|
|
|
Location: &location,
|
2016-01-18 19:12:54 +01:00
|
|
|
Tags: expandTags(tags),
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
resp, err := resGroupClient.CreateOrUpdate(name, rg)
|
2015-12-08 23:30:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error issuing Azure ARM create request for resource group '%s': %s", name, err)
|
|
|
|
}
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
d.SetId(*resp.ID)
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
log.Printf("[DEBUG] Waiting for Resource Group (%s) to become available", name)
|
2015-12-08 23:30:12 +01:00
|
|
|
stateConf := &resource.StateChangeConf{
|
|
|
|
Pending: []string{"Accepted"},
|
|
|
|
Target: "Succeeded",
|
2015-12-09 02:25:05 +01:00
|
|
|
Refresh: resourceGroupStateRefreshFunc(client, name),
|
2015-12-08 23:30:12 +01:00
|
|
|
Timeout: 10 * time.Minute,
|
|
|
|
}
|
|
|
|
if _, err := stateConf.WaitForState(); err != nil {
|
2015-12-09 02:25:05 +01:00
|
|
|
return fmt.Errorf("Error waiting for Resource Group (%s) to become available: %s", name, err)
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceArmResourceGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmResourceGroupRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
resGroupClient := meta.(*ArmClient).resourceGroupClient
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := id.ResourceGroup
|
2015-12-08 23:30:12 +01:00
|
|
|
|
|
|
|
res, err := resGroupClient.Get(name)
|
|
|
|
if err != nil {
|
2015-12-09 02:25:05 +01:00
|
|
|
if res.StatusCode == http.StatusNotFound {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-08 23:30:12 +01:00
|
|
|
return fmt.Errorf("Error issuing read request to Azure ARM for resource group '%s': %s", name, err)
|
|
|
|
}
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
d.Set("name", res.Name)
|
|
|
|
d.Set("location", res.Location)
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-01-18 19:12:54 +01:00
|
|
|
flattenAndSetTags(d, res.Tags)
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmResourceGroupExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
|
|
|
resGroupClient := meta.(*ArmClient).resourceGroupClient
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
name := id.ResourceGroup
|
2015-12-08 23:30:12 +01:00
|
|
|
|
|
|
|
resp, err := resGroupClient.CheckExistence(name)
|
|
|
|
if err != nil {
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmResourceGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
resGroupClient := meta.(*ArmClient).resourceGroupClient
|
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
name := id.ResourceGroup
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2015-12-09 02:25:05 +01:00
|
|
|
_, err = resGroupClient.Delete(name)
|
2015-12-08 23:30:12 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceGroupStateRefreshFunc(client *ArmClient, id string) resource.StateRefreshFunc {
|
|
|
|
return func() (interface{}, string, error) {
|
|
|
|
res, err := client.resourceGroupClient.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, "", fmt.Errorf("Error issuing read request in resourceGroupStateRefreshFunc to Azure ARM for resource group '%s': %s", id, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, *res.Properties.ProvisioningState, nil
|
|
|
|
}
|
|
|
|
}
|