2016-02-05 00:36:50 +01:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/jen20/riviera/azure"
|
|
|
|
"github.com/jen20/riviera/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceArmSqlDatabase() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmSqlDatabaseCreate,
|
|
|
|
Read: resourceArmSqlDatabaseRead,
|
|
|
|
Update: resourceArmSqlDatabaseCreate,
|
|
|
|
Delete: resourceArmSqlDatabaseDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-10-03 23:04:46 +02:00
|
|
|
"name": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"location": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
StateFunc: azureRMNormalizeLocation,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"resource_group_name": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"server_name": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"create_mode": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Default: "Default",
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"source_database_id": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"restore_point_in_time": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"edition": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ValidateFunc: validateArmSqlDatabaseEdition,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"collation": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"max_size_bytes": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"requested_service_objective_id": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"requested_service_objective_name": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"source_database_deletion_date": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"elastic_pool_name": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"encryption": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"creation_date": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
2016-10-03 23:04:46 +02:00
|
|
|
"default_secondary_location": {
|
2016-02-05 00:36:50 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
rivieraClient := client.rivieraClient
|
|
|
|
|
|
|
|
tags := d.Get("tags").(map[string]interface{})
|
|
|
|
expandedTags := expandTags(tags)
|
|
|
|
|
|
|
|
command := &sql.CreateOrUpdateDatabase{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
Location: d.Get("location").(string),
|
|
|
|
ResourceGroupName: d.Get("resource_group_name").(string),
|
|
|
|
ServerName: d.Get("server_name").(string),
|
|
|
|
Tags: *expandedTags,
|
|
|
|
CreateMode: azure.String(d.Get("create_mode").(string)),
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("source_database_id"); ok {
|
|
|
|
command.SourceDatabaseID = azure.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("edition"); ok {
|
|
|
|
command.Edition = azure.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("collation"); ok {
|
|
|
|
command.Collation = azure.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("max_size_bytes"); ok {
|
|
|
|
command.MaxSizeBytes = azure.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("source_database_deletion_date"); ok {
|
|
|
|
command.SourceDatabaseDeletionDate = azure.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("requested_service_objective_id"); ok {
|
|
|
|
command.RequestedServiceObjectiveID = azure.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("requested_service_objective_name"); ok {
|
|
|
|
command.RequestedServiceObjectiveName = azure.String(v.(string))
|
|
|
|
}
|
|
|
|
|
|
|
|
createRequest := rivieraClient.NewRequest()
|
|
|
|
createRequest.Command = command
|
|
|
|
|
|
|
|
createResponse, err := createRequest.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error creating SQL Database: %s", err)
|
|
|
|
}
|
|
|
|
if !createResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Error creating SQL Database: %s", createResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
readRequest := rivieraClient.NewRequest()
|
|
|
|
readRequest.Command = &sql.GetDatabase{
|
|
|
|
Name: d.Get("name").(string),
|
|
|
|
ResourceGroupName: d.Get("resource_group_name").(string),
|
|
|
|
ServerName: d.Get("server_name").(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
readResponse, err := readRequest.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading SQL Database: %s", err)
|
|
|
|
}
|
|
|
|
if !readResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Error reading SQL Database: %s", readResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := readResponse.Parsed.(*sql.GetDatabaseResponse)
|
|
|
|
d.SetId(*resp.ID)
|
|
|
|
|
|
|
|
return resourceArmSqlDatabaseRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
rivieraClient := client.rivieraClient
|
|
|
|
|
|
|
|
readRequest := rivieraClient.NewRequestForURI(d.Id())
|
|
|
|
readRequest.Command = &sql.GetDatabase{}
|
|
|
|
|
|
|
|
readResponse, err := readRequest.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error reading SQL Database: %s", err)
|
|
|
|
}
|
|
|
|
if !readResponse.IsSuccessful() {
|
|
|
|
log.Printf("[INFO] Error reading SQL Database %q - removing from state", d.Id())
|
|
|
|
d.SetId("")
|
|
|
|
return fmt.Errorf("Error reading SQL Database: %s", readResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp := readResponse.Parsed.(*sql.GetDatabaseResponse)
|
|
|
|
|
|
|
|
d.Set("name", resp.Name)
|
|
|
|
d.Set("creation_date", resp.CreationDate)
|
|
|
|
d.Set("default_secondary_location", resp.DefaultSecondaryLocation)
|
|
|
|
|
2016-11-01 11:55:02 +01:00
|
|
|
flattenAndSetTags(d, resp.Tags)
|
|
|
|
|
2016-02-05 00:36:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
rivieraClient := client.rivieraClient
|
|
|
|
|
|
|
|
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
|
|
|
|
deleteRequest.Command = &sql.DeleteDatabase{}
|
|
|
|
|
|
|
|
deleteResponse, err := deleteRequest.Execute()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting SQL Database: %s", err)
|
|
|
|
}
|
|
|
|
if !deleteResponse.IsSuccessful() {
|
|
|
|
return fmt.Errorf("Error deleting SQL Database: %s", deleteResponse.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateArmSqlDatabaseEdition(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
editions := map[string]bool{
|
2016-10-03 23:04:46 +02:00
|
|
|
"Basic": true,
|
|
|
|
"Standard": true,
|
|
|
|
"Premium": true,
|
|
|
|
"DataWarehouse": true,
|
2016-02-05 00:36:50 +01:00
|
|
|
}
|
|
|
|
if !editions[v.(string)] {
|
2016-10-03 23:04:46 +02:00
|
|
|
errors = append(errors, fmt.Errorf("SQL Database Edition can only be Basic, Standard, Premium or DataWarehouse"))
|
2016-02-05 00:36:50 +01:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|