github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/azurerm/resource_arm_sql_elasticpool.go (about)

     1  package azurerm
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/Azure/azure-sdk-for-go/arm/sql"
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"github.com/hashicorp/terraform/helper/validation"
     8  	"log"
     9  	"net/http"
    10  	"time"
    11  )
    12  
    13  func resourceArmSqlElasticPool() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceArmSqlElasticPoolCreate,
    16  		Read:   resourceArmSqlElasticPoolRead,
    17  		Update: resourceArmSqlElasticPoolCreate,
    18  		Delete: resourceArmSqlElasticPoolDelete,
    19  
    20  		Importer: &schema.ResourceImporter{
    21  			State: schema.ImportStatePassthrough,
    22  		},
    23  
    24  		Schema: map[string]*schema.Schema{
    25  			"name": {
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  
    31  			"location": locationSchema(),
    32  
    33  			"resource_group_name": {
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  
    39  			"server_name": {
    40  				Type:     schema.TypeString,
    41  				Required: true,
    42  				ForceNew: true,
    43  			},
    44  
    45  			"edition": {
    46  				Type:         schema.TypeString,
    47  				Required:     true,
    48  				ForceNew:     true,
    49  				ValidateFunc: validateSqlElasticPoolEdition(),
    50  			},
    51  
    52  			"dtu": {
    53  				Type:     schema.TypeInt,
    54  				Required: true,
    55  			},
    56  
    57  			"db_dtu_min": {
    58  				Type:     schema.TypeInt,
    59  				Optional: true,
    60  				Computed: true,
    61  			},
    62  
    63  			"db_dtu_max": {
    64  				Type:     schema.TypeInt,
    65  				Optional: true,
    66  				Computed: true,
    67  			},
    68  
    69  			"pool_size": {
    70  				Type:     schema.TypeInt,
    71  				Optional: true,
    72  				Computed: true,
    73  			},
    74  
    75  			"creation_date": {
    76  				Type:     schema.TypeString,
    77  				Computed: true,
    78  			},
    79  
    80  			"tags": tagsSchema(),
    81  		},
    82  	}
    83  }
    84  
    85  func resourceArmSqlElasticPoolCreate(d *schema.ResourceData, meta interface{}) error {
    86  	client := meta.(*ArmClient)
    87  	elasticPoolsClient := client.sqlElasticPoolsClient
    88  
    89  	log.Printf("[INFO] preparing arguments for Azure ARM SQL ElasticPool creation.")
    90  
    91  	name := d.Get("name").(string)
    92  	serverName := d.Get("server_name").(string)
    93  	location := d.Get("location").(string)
    94  	resGroup := d.Get("resource_group_name").(string)
    95  	tags := d.Get("tags").(map[string]interface{})
    96  
    97  	elasticPool := sql.ElasticPool{
    98  		Name:                  &name,
    99  		Location:              &location,
   100  		ElasticPoolProperties: getArmSqlElasticPoolProperties(d),
   101  		Tags: expandTags(tags),
   102  	}
   103  
   104  	_, err := elasticPoolsClient.CreateOrUpdate(resGroup, serverName, name, elasticPool, make(chan struct{}))
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	read, err := elasticPoolsClient.Get(resGroup, serverName, name)
   110  	if err != nil {
   111  		return err
   112  	}
   113  	if read.ID == nil {
   114  		return fmt.Errorf("Cannot read SQL ElasticPool %s (resource group %s) ID", name, resGroup)
   115  	}
   116  
   117  	d.SetId(*read.ID)
   118  
   119  	return resourceArmSqlElasticPoolRead(d, meta)
   120  }
   121  
   122  func resourceArmSqlElasticPoolRead(d *schema.ResourceData, meta interface{}) error {
   123  	client := meta.(*ArmClient)
   124  	elasticPoolsClient := client.sqlElasticPoolsClient
   125  
   126  	resGroup, serverName, name, err := parseArmSqlElasticPoolId(d.Id())
   127  	if err != nil {
   128  		return err
   129  	}
   130  
   131  	resp, err := elasticPoolsClient.Get(resGroup, serverName, name)
   132  	if err != nil {
   133  		if resp.StatusCode == http.StatusNotFound {
   134  			d.SetId("")
   135  			return nil
   136  		}
   137  		return fmt.Errorf("Error making Read request on Sql Elastic Pool %s: %s", name, err)
   138  	}
   139  
   140  	d.Set("name", resp.Name)
   141  	d.Set("resource_group_name", resGroup)
   142  	d.Set("location", azureRMNormalizeLocation(*resp.Location))
   143  	d.Set("server_name", serverName)
   144  
   145  	elasticPool := resp.ElasticPoolProperties
   146  
   147  	if elasticPool != nil {
   148  		d.Set("edition", string(elasticPool.Edition))
   149  		d.Set("dtu", int(*elasticPool.Dtu))
   150  		d.Set("db_dtu_min", int(*elasticPool.DatabaseDtuMin))
   151  		d.Set("db_dtu_max", int(*elasticPool.DatabaseDtuMax))
   152  		d.Set("pool_size", int(*elasticPool.StorageMB))
   153  
   154  		if elasticPool.CreationDate != nil {
   155  			d.Set("creation_date", elasticPool.CreationDate.Format(time.RFC3339))
   156  		}
   157  	}
   158  
   159  	flattenAndSetTags(d, resp.Tags)
   160  
   161  	return nil
   162  }
   163  
   164  func resourceArmSqlElasticPoolDelete(d *schema.ResourceData, meta interface{}) error {
   165  	client := meta.(*ArmClient)
   166  	elasticPoolsClient := client.sqlElasticPoolsClient
   167  
   168  	resGroup, serverName, name, err := parseArmSqlElasticPoolId(d.Id())
   169  	if err != nil {
   170  		return err
   171  	}
   172  
   173  	_, err = elasticPoolsClient.Delete(resGroup, serverName, name)
   174  
   175  	return err
   176  }
   177  
   178  func getArmSqlElasticPoolProperties(d *schema.ResourceData) *sql.ElasticPoolProperties {
   179  	edition := sql.ElasticPoolEditions(d.Get("edition").(string))
   180  	dtu := int32(d.Get("dtu").(int))
   181  
   182  	props := &sql.ElasticPoolProperties{
   183  		Edition: edition,
   184  		Dtu:     &dtu,
   185  	}
   186  
   187  	if databaseDtuMin, ok := d.GetOk("db_dtu_min"); ok {
   188  		databaseDtuMin := int32(databaseDtuMin.(int))
   189  		props.DatabaseDtuMin = &databaseDtuMin
   190  	}
   191  
   192  	if databaseDtuMax, ok := d.GetOk("db_dtu_max"); ok {
   193  		databaseDtuMax := int32(databaseDtuMax.(int))
   194  		props.DatabaseDtuMax = &databaseDtuMax
   195  	}
   196  
   197  	if poolSize, ok := d.GetOk("pool_size"); ok {
   198  		poolSize := int32(poolSize.(int))
   199  		props.StorageMB = &poolSize
   200  	}
   201  
   202  	return props
   203  }
   204  
   205  func parseArmSqlElasticPoolId(sqlElasticPoolId string) (string, string, string, error) {
   206  	id, err := parseAzureResourceID(sqlElasticPoolId)
   207  	if err != nil {
   208  		return "", "", "", fmt.Errorf("[ERROR] Unable to parse SQL ElasticPool ID '%s': %+v", sqlElasticPoolId, err)
   209  	}
   210  
   211  	return id.ResourceGroup, id.Path["servers"], id.Path["elasticPools"], nil
   212  }
   213  
   214  func validateSqlElasticPoolEdition() schema.SchemaValidateFunc {
   215  	return validation.StringInSlice([]string{
   216  		string(sql.ElasticPoolEditionsBasic),
   217  		string(sql.ElasticPoolEditionsStandard),
   218  		string(sql.ElasticPoolEditionsPremium),
   219  	}, false)
   220  }