2015-12-08 23:30:12 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2015-12-09 00:50:48 +01:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2015-12-08 23:30:12 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2016-02-18 23:50:43 +01:00
|
|
|
"github.com/jen20/riviera/azure"
|
2015-12-08 23:30:12 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2016-07-11 13:30:22 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2015-12-08 23:30:12 +01:00
|
|
|
|
|
|
|
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
|
|
|
},
|
2016-11-29 16:54:00 +01:00
|
|
|
|
|
|
|
"location": locationSchema(),
|
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)
|
2016-02-18 23:50:43 +01:00
|
|
|
rivieraClient := client.rivieraClient
|
2016-01-18 19:12:54 +01:00
|
|
|
|
|
|
|
if !d.HasChange("tags") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
name := d.Get("name").(string)
|
|
|
|
newTags := d.Get("tags").(map[string]interface{})
|
2016-02-18 23:50:43 +01:00
|
|
|
|
|
|
|
updateRequest := rivieraClient.NewRequestForURI(d.Id())
|
|
|
|
updateRequest.Command = &azure.UpdateResourceGroup{
|
|
|
|
Name: name,
|
|
|
|
Tags: *expandTags(newTags),
|
|
|
|
}
|
|
|
|
|
|
|
|
updateResponse, err := updateRequest.Execute()
|
2016-01-18 19:12:54 +01:00
|
|
|
if err != nil {
|
2016-02-18 23:50:43 +01:00
|
|
|
return fmt.Errorf("Error updating resource group: %s", err)
|
|
|
|
}
|
|
|
|
if !updateResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Error updating resource group: %s", updateResponse.Error)
|
2016-01-18 19:12:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceArmResourceGroupRead(d, meta)
|
|
|
|
}
|
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
func resourceArmResourceGroupCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
2016-02-18 23:50:43 +01:00
|
|
|
rivieraClient := client.rivieraClient
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
createRequest := rivieraClient.NewRequest()
|
|
|
|
createRequest.Command = &azure.CreateResourceGroup{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Location: d.Get("location").(string),
|
|
|
|
Tags: *expandTags(d.Get("tags").(map[string]interface{})),
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
createResponse, err := createRequest.Execute()
|
2015-12-08 23:30:12 +01:00
|
|
|
if err != nil {
|
2016-02-18 23:50:43 +01:00
|
|
|
return fmt.Errorf("Error creating resource group: %s", err)
|
|
|
|
}
|
|
|
|
if !createResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Error creating resource group: %s", createResponse.Error)
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
resp := createResponse.Parsed.(*azure.CreateResourceGroupResponse)
|
2015-12-09 02:25:05 +01:00
|
|
|
d.SetId(*resp.ID)
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
// TODO(jen20): Decide whether we need this or not and migrate to use @stack72's work if so
|
|
|
|
// log.Printf("[DEBUG] Waiting for Resource Group (%s) to become available", name)
|
|
|
|
// stateConf := &resource.StateChangeConf{
|
|
|
|
// Pending: []string{"Accepted"},
|
|
|
|
// Target: []string{"Succeeded"},
|
|
|
|
// Refresh: resourceGroupStateRefreshFunc(client, name),
|
|
|
|
// Timeout: 10 * time.Minute,
|
|
|
|
// }
|
|
|
|
// if _, err := stateConf.WaitForState(); err != nil {
|
|
|
|
// 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 {
|
2016-02-18 23:50:43 +01:00
|
|
|
client := meta.(*ArmClient)
|
|
|
|
rivieraClient := client.rivieraClient
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
readRequest := rivieraClient.NewRequestForURI(d.Id())
|
|
|
|
readRequest.Command = &azure.GetResourceGroup{}
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
readResponse, err := readRequest.Execute()
|
2015-12-08 23:30:12 +01:00
|
|
|
if err != nil {
|
2016-02-18 23:50:43 +01:00
|
|
|
return fmt.Errorf("Error reading resource group: %s", err)
|
|
|
|
}
|
|
|
|
if !readResponse.IsSuccessful() {
|
|
|
|
log.Printf("[INFO] Error reading resource group %q - removing from state", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
return fmt.Errorf("Error reading resource group: %s", readResponse.Error)
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
resp := readResponse.Parsed.(*azure.GetResourceGroupResponse)
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
d.Set("name", resp.Name)
|
|
|
|
d.Set("location", resp.Location)
|
|
|
|
flattenAndSetTags(d, resp.Tags)
|
2016-01-18 19:12:54 +01:00
|
|
|
|
2015-12-08 23:30:12 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmResourceGroupExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
2016-02-18 23:50:43 +01:00
|
|
|
client := meta.(*ArmClient)
|
|
|
|
rivieraClient := client.rivieraClient
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
readRequest := rivieraClient.NewRequestForURI(d.Id())
|
|
|
|
readRequest.Command = &azure.GetResourceGroup{}
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
readResponse, err := readRequest.Execute()
|
2015-12-08 23:30:12 +01:00
|
|
|
if err != nil {
|
2016-02-18 23:50:43 +01:00
|
|
|
return false, fmt.Errorf("Error reading resource group: %s", err)
|
|
|
|
}
|
|
|
|
if readResponse.IsSuccessful() {
|
2015-12-08 23:30:12 +01:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
return false, nil
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmResourceGroupDelete(d *schema.ResourceData, meta interface{}) error {
|
2016-02-18 23:50:43 +01:00
|
|
|
client := meta.(*ArmClient)
|
|
|
|
rivieraClient := client.rivieraClient
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
|
|
|
|
deleteRequest.Command = &azure.DeleteResourceGroup{}
|
2015-12-08 23:30:12 +01:00
|
|
|
|
2016-02-18 23:50:43 +01:00
|
|
|
deleteResponse, err := deleteRequest.Execute()
|
2015-12-08 23:30:12 +01:00
|
|
|
if err != nil {
|
2016-02-18 23:50:43 +01:00
|
|
|
return fmt.Errorf("Error deleting resource group: %s", err)
|
|
|
|
}
|
|
|
|
if !deleteResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Error deleting resource group: %s", deleteResponse.Error)
|
2015-12-08 23:30:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|