github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/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 _, error := elasticPoolsClient.CreateOrUpdate(resGroup, serverName, name, elasticPool, make(chan struct{})) 105 err := <-error 106 if err != nil { 107 return err 108 } 109 110 read, err := elasticPoolsClient.Get(resGroup, serverName, name) 111 if err != nil { 112 return err 113 } 114 if read.ID == nil { 115 return fmt.Errorf("Cannot read SQL ElasticPool %s (resource group %s) ID", name, resGroup) 116 } 117 118 d.SetId(*read.ID) 119 120 return resourceArmSqlElasticPoolRead(d, meta) 121 } 122 123 func resourceArmSqlElasticPoolRead(d *schema.ResourceData, meta interface{}) error { 124 client := meta.(*ArmClient) 125 elasticPoolsClient := client.sqlElasticPoolsClient 126 127 resGroup, serverName, name, err := parseArmSqlElasticPoolId(d.Id()) 128 if err != nil { 129 return err 130 } 131 132 resp, err := elasticPoolsClient.Get(resGroup, serverName, name) 133 if err != nil { 134 if resp.StatusCode == http.StatusNotFound { 135 d.SetId("") 136 return nil 137 } 138 return fmt.Errorf("Error making Read request on Sql Elastic Pool %s: %s", name, err) 139 } 140 141 d.Set("name", resp.Name) 142 d.Set("resource_group_name", resGroup) 143 d.Set("location", azureRMNormalizeLocation(*resp.Location)) 144 d.Set("server_name", serverName) 145 146 elasticPool := resp.ElasticPoolProperties 147 148 if elasticPool != nil { 149 d.Set("edition", string(elasticPool.Edition)) 150 d.Set("dtu", int(*elasticPool.Dtu)) 151 d.Set("db_dtu_min", int(*elasticPool.DatabaseDtuMin)) 152 d.Set("db_dtu_max", int(*elasticPool.DatabaseDtuMax)) 153 d.Set("pool_size", int(*elasticPool.StorageMB)) 154 155 if elasticPool.CreationDate != nil { 156 d.Set("creation_date", elasticPool.CreationDate.Format(time.RFC3339)) 157 } 158 } 159 160 flattenAndSetTags(d, resp.Tags) 161 162 return nil 163 } 164 165 func resourceArmSqlElasticPoolDelete(d *schema.ResourceData, meta interface{}) error { 166 client := meta.(*ArmClient) 167 elasticPoolsClient := client.sqlElasticPoolsClient 168 169 resGroup, serverName, name, err := parseArmSqlElasticPoolId(d.Id()) 170 if err != nil { 171 return err 172 } 173 174 _, err = elasticPoolsClient.Delete(resGroup, serverName, name) 175 176 return err 177 } 178 179 func getArmSqlElasticPoolProperties(d *schema.ResourceData) *sql.ElasticPoolProperties { 180 edition := sql.ElasticPoolEdition(d.Get("edition").(string)) 181 dtu := int32(d.Get("dtu").(int)) 182 183 props := &sql.ElasticPoolProperties{ 184 Edition: edition, 185 Dtu: &dtu, 186 } 187 188 if databaseDtuMin, ok := d.GetOk("db_dtu_min"); ok { 189 databaseDtuMin := int32(databaseDtuMin.(int)) 190 props.DatabaseDtuMin = &databaseDtuMin 191 } 192 193 if databaseDtuMax, ok := d.GetOk("db_dtu_max"); ok { 194 databaseDtuMax := int32(databaseDtuMax.(int)) 195 props.DatabaseDtuMax = &databaseDtuMax 196 } 197 198 if poolSize, ok := d.GetOk("pool_size"); ok { 199 poolSize := int32(poolSize.(int)) 200 props.StorageMB = &poolSize 201 } 202 203 return props 204 } 205 206 func parseArmSqlElasticPoolId(sqlElasticPoolId string) (string, string, string, error) { 207 id, err := parseAzureResourceID(sqlElasticPoolId) 208 if err != nil { 209 return "", "", "", fmt.Errorf("[ERROR] Unable to parse SQL ElasticPool ID '%s': %+v", sqlElasticPoolId, err) 210 } 211 212 return id.ResourceGroup, id.Path["servers"], id.Path["elasticPools"], nil 213 } 214 215 func validateSqlElasticPoolEdition() schema.SchemaValidateFunc { 216 return validation.StringInSlice([]string{ 217 string(sql.ElasticPoolEditionBasic), 218 string(sql.ElasticPoolEditionStandard), 219 string(sql.ElasticPoolEditionPremium), 220 }, false) 221 }