github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/azurerm/resource_arm_storage_table.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	"github.com/Azure/azure-sdk-for-go/storage"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceArmStorageTable() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceArmStorageTableCreate,
    15  		Read:   resourceArmStorageTableRead,
    16  		Delete: resourceArmStorageTableDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"name": {
    20  				Type:         schema.TypeString,
    21  				Required:     true,
    22  				ForceNew:     true,
    23  				ValidateFunc: validateArmStorageTableName,
    24  			},
    25  			"resource_group_name": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  			"storage_account_name": {
    31  				Type:     schema.TypeString,
    32  				Required: true,
    33  				ForceNew: true,
    34  			},
    35  		},
    36  	}
    37  }
    38  
    39  func validateArmStorageTableName(v interface{}, k string) (ws []string, errors []error) {
    40  	value := v.(string)
    41  	if value == "table" {
    42  		errors = append(errors, fmt.Errorf(
    43  			"Table Storage %q cannot use the word `table`: %q",
    44  			k, value))
    45  	}
    46  	if !regexp.MustCompile(`^[A-Za-z][A-Za-z0-9]{6,63}$`).MatchString(value) {
    47  		errors = append(errors, fmt.Errorf(
    48  			"Table Storage %q cannot begin with a numeric character, only alphanumeric characters are allowed and must be between 6 and 63 characters long: %q",
    49  			k, value))
    50  	}
    51  
    52  	return
    53  }
    54  
    55  func resourceArmStorageTableCreate(d *schema.ResourceData, meta interface{}) error {
    56  	armClient := meta.(*ArmClient)
    57  
    58  	resourceGroupName := d.Get("resource_group_name").(string)
    59  	storageAccountName := d.Get("storage_account_name").(string)
    60  
    61  	tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName)
    62  	if err != nil {
    63  		return err
    64  	}
    65  	if !accountExists {
    66  		return fmt.Errorf("Storage Account %q Not Found", storageAccountName)
    67  	}
    68  
    69  	name := d.Get("name").(string)
    70  	table := tableClient.GetTableReference(name)
    71  
    72  	log.Printf("[INFO] Creating table %q in storage account %q.", name, storageAccountName)
    73  
    74  	timeout := uint(60)
    75  	options := &storage.TableOptions{}
    76  	err = table.Create(timeout, storage.NoMetadata, options)
    77  	if err != nil {
    78  		return fmt.Errorf("Error creating table %q in storage account %q: %s", name, storageAccountName, err)
    79  	}
    80  
    81  	d.SetId(name)
    82  
    83  	return resourceArmStorageTableRead(d, meta)
    84  }
    85  
    86  func resourceArmStorageTableRead(d *schema.ResourceData, meta interface{}) error {
    87  	armClient := meta.(*ArmClient)
    88  
    89  	resourceGroupName := d.Get("resource_group_name").(string)
    90  	storageAccountName := d.Get("storage_account_name").(string)
    91  
    92  	tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	if !accountExists {
    97  		log.Printf("[DEBUG] Storage account %q not found, removing table %q from state", storageAccountName, d.Id())
    98  		d.SetId("")
    99  		return nil
   100  	}
   101  
   102  	name := d.Get("name").(string)
   103  	metaDataLevel := storage.MinimalMetadata
   104  	options := &storage.QueryTablesOptions{}
   105  	tables, err := tableClient.QueryTables(metaDataLevel, options)
   106  	if err != nil {
   107  		return fmt.Errorf("Failed to retrieve storage tables in account %q: %s", name, err)
   108  	}
   109  
   110  	var found bool
   111  	for _, table := range tables.Tables {
   112  		tableName := string(table.Name)
   113  		if tableName == name {
   114  			found = true
   115  			d.Set("name", tableName)
   116  		}
   117  	}
   118  
   119  	if !found {
   120  		log.Printf("[INFO] Storage table %q does not exist in account %q, removing from state...", name, storageAccountName)
   121  		d.SetId("")
   122  	}
   123  
   124  	return nil
   125  }
   126  
   127  func resourceArmStorageTableDelete(d *schema.ResourceData, meta interface{}) error {
   128  	armClient := meta.(*ArmClient)
   129  
   130  	resourceGroupName := d.Get("resource_group_name").(string)
   131  	storageAccountName := d.Get("storage_account_name").(string)
   132  
   133  	tableClient, accountExists, err := armClient.getTableServiceClientForStorageAccount(resourceGroupName, storageAccountName)
   134  	if err != nil {
   135  		return err
   136  	}
   137  	if !accountExists {
   138  		log.Printf("[INFO] Storage Account %q doesn't exist so the table won't exist", storageAccountName)
   139  		return nil
   140  	}
   141  
   142  	name := d.Get("name").(string)
   143  	table := tableClient.GetTableReference(name)
   144  	timeout := uint(60)
   145  	options := &storage.TableOptions{}
   146  
   147  	log.Printf("[INFO] Deleting storage table %q in account %q", name, storageAccountName)
   148  	if err := table.Delete(timeout, options); err != nil {
   149  		return fmt.Errorf("Error deleting storage table %q from storage account %q: %s", name, storageAccountName, err)
   150  	}
   151  
   152  	d.SetId("")
   153  	return nil
   154  }