mirror of https://github.com/hashicorp/terraform
parent
b35a19a8f7
commit
d89088246c
@ -0,0 +1,257 @@
|
||||
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{
|
||||
"name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"location": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
StateFunc: azureRMNormalizeLocation,
|
||||
},
|
||||
|
||||
"resource_group_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"server_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
ForceNew: true,
|
||||
},
|
||||
|
||||
"create_mode": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Default: "Default",
|
||||
},
|
||||
|
||||
"source_database_id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"restore_point_in_time": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"edition": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
ValidateFunc: validateArmSqlDatabaseEdition,
|
||||
},
|
||||
|
||||
"collation": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"max_size_bytes": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"requested_service_objective_id": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"requested_service_objective_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"source_database_deletion_date": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"elastic_pool_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"encryption": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"creation_date": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"default_secondary_location": &schema.Schema{
|
||||
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)
|
||||
|
||||
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{
|
||||
"Basic": true,
|
||||
"Standard": true,
|
||||
"Premium": true,
|
||||
}
|
||||
|
||||
if !editions[v.(string)] {
|
||||
errors = append(errors, fmt.Errorf("SQL Database Edition can only be Basic, Standard or Premium"))
|
||||
}
|
||||
return
|
||||
}
|
||||
@ -0,0 +1,229 @@
|
||||
package azurerm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/acctest"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
"github.com/jen20/riviera/sql"
|
||||
)
|
||||
|
||||
func TestResourceAzureRMSqlDatabaseEdition_validation(t *testing.T) {
|
||||
cases := []struct {
|
||||
Value string
|
||||
ErrCount int
|
||||
}{
|
||||
{
|
||||
Value: "Random",
|
||||
ErrCount: 1,
|
||||
},
|
||||
{
|
||||
Value: "Basic",
|
||||
ErrCount: 0,
|
||||
},
|
||||
{
|
||||
Value: "Standard",
|
||||
ErrCount: 0,
|
||||
},
|
||||
{
|
||||
Value: "Premium",
|
||||
ErrCount: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
_, errors := validateArmSqlDatabaseEdition(tc.Value, "azurerm_sql_database")
|
||||
|
||||
if len(errors) != tc.ErrCount {
|
||||
t.Fatalf("Expected the Azure RM SQL Database edition to trigger a validation error")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccAzureRMSqlDatabase_basic(t *testing.T) {
|
||||
ri := acctest.RandInt()
|
||||
config := fmt.Sprintf(testAccAzureRMSqlDatabase_basic, ri, ri, ri)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: config,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccAzureRMSqlDatabase_withTags(t *testing.T) {
|
||||
ri := acctest.RandInt()
|
||||
preConfig := fmt.Sprintf(testAccAzureRMSqlDatabase_withTags, ri, ri, ri)
|
||||
postConfig := fmt.Sprintf(testAccAzureRMSqlDatabase_withTagsUpdate, ri, ri, ri)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testCheckAzureRMSqlDatabaseDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: preConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"azurerm_sql_database.test", "tags.#", "2"),
|
||||
),
|
||||
},
|
||||
|
||||
resource.TestStep{
|
||||
Config: postConfig,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testCheckAzureRMSqlDatabaseExists("azurerm_sql_database.test"),
|
||||
resource.TestCheckResourceAttr(
|
||||
"azurerm_sql_database.test", "tags.#", "1"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testCheckAzureRMSqlDatabaseExists(name string) resource.TestCheckFunc {
|
||||
return func(s *terraform.State) error {
|
||||
|
||||
rs, ok := s.RootModule().Resources[name]
|
||||
if !ok {
|
||||
return fmt.Errorf("Not found: %s", name)
|
||||
}
|
||||
|
||||
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
|
||||
|
||||
readRequest := conn.NewRequestForURI(rs.Primary.ID)
|
||||
readRequest.Command = &sql.GetDatabase{}
|
||||
|
||||
readResponse, err := readRequest.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Bad: GetDatabase: %s", err)
|
||||
}
|
||||
if !readResponse.IsSuccessful() {
|
||||
return fmt.Errorf("Bad: GetDatabase: %s", readResponse.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func testCheckAzureRMSqlDatabaseDestroy(s *terraform.State) error {
|
||||
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
|
||||
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "azurerm_sql_database" {
|
||||
continue
|
||||
}
|
||||
|
||||
readRequest := conn.NewRequestForURI(rs.Primary.ID)
|
||||
readRequest.Command = &sql.GetDatabase{}
|
||||
|
||||
readResponse, err := readRequest.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Bad: GetDatabase: %s", err)
|
||||
}
|
||||
|
||||
if readResponse.IsSuccessful() {
|
||||
return fmt.Errorf("Bad: SQL Database still exists: %s", readResponse.Error)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var testAccAzureRMSqlDatabase_basic = `
|
||||
resource "azurerm_resource_group" "test" {
|
||||
name = "acctest_rg_%d"
|
||||
location = "West US"
|
||||
}
|
||||
resource "azurerm_sql_server" "test" {
|
||||
name = "acctestsqlserver%d"
|
||||
resource_group_name = "${azurerm_resource_group.test.name}"
|
||||
location = "West US"
|
||||
version = "12.0"
|
||||
administrator_login = "mradministrator"
|
||||
administrator_login_password = "thisIsDog11"
|
||||
}
|
||||
|
||||
resource "azurerm_sql_database" "test" {
|
||||
name = "acctestdb%d"
|
||||
resource_group_name = "${azurerm_resource_group.test.name}"
|
||||
server_name = "${azurerm_sql_server.test.name}"
|
||||
location = "West US"
|
||||
edition = "Standard"
|
||||
collation = "SQL_Latin1_General_CP1_CI_AS"
|
||||
max_size_bytes = "1073741824"
|
||||
requested_service_objective_name = "S0"
|
||||
}
|
||||
`
|
||||
|
||||
var testAccAzureRMSqlDatabase_withTags = `
|
||||
resource "azurerm_resource_group" "test" {
|
||||
name = "acctest_rg_%d"
|
||||
location = "West US"
|
||||
}
|
||||
resource "azurerm_sql_server" "test" {
|
||||
name = "acctestsqlserver%d"
|
||||
resource_group_name = "${azurerm_resource_group.test.name}"
|
||||
location = "West US"
|
||||
version = "12.0"
|
||||
administrator_login = "mradministrator"
|
||||
administrator_login_password = "thisIsDog11"
|
||||
}
|
||||
|
||||
resource "azurerm_sql_database" "test" {
|
||||
name = "acctestdb%d"
|
||||
resource_group_name = "${azurerm_resource_group.test.name}"
|
||||
server_name = "${azurerm_sql_server.test.name}"
|
||||
location = "West US"
|
||||
edition = "Standard"
|
||||
collation = "SQL_Latin1_General_CP1_CI_AS"
|
||||
max_size_bytes = "1073741824"
|
||||
requested_service_objective_name = "S0"
|
||||
|
||||
tags {
|
||||
environment = "staging"
|
||||
database = "test"
|
||||
}
|
||||
}
|
||||
`
|
||||
|
||||
var testAccAzureRMSqlDatabase_withTagsUpdate = `
|
||||
resource "azurerm_resource_group" "test" {
|
||||
name = "acctest_rg_%d"
|
||||
location = "West US"
|
||||
}
|
||||
resource "azurerm_sql_server" "test" {
|
||||
name = "acctestsqlserver%d"
|
||||
resource_group_name = "${azurerm_resource_group.test.name}"
|
||||
location = "West US"
|
||||
version = "12.0"
|
||||
administrator_login = "mradministrator"
|
||||
administrator_login_password = "thisIsDog11"
|
||||
}
|
||||
|
||||
resource "azurerm_sql_database" "test" {
|
||||
name = "acctestdb%d"
|
||||
resource_group_name = "${azurerm_resource_group.test.name}"
|
||||
server_name = "${azurerm_sql_server.test.name}"
|
||||
location = "West US"
|
||||
edition = "Standard"
|
||||
collation = "SQL_Latin1_General_CP1_CI_AS"
|
||||
max_size_bytes = "1073741824"
|
||||
requested_service_objective_name = "S0"
|
||||
|
||||
tags {
|
||||
environment = "production"
|
||||
}
|
||||
}
|
||||
`
|
||||
@ -1,26 +1,25 @@
|
||||
package azure
|
||||
|
||||
// ApiCall must be implemented by structures which represent requests to the
|
||||
// APICall must be implemented by structures which represent requests to the
|
||||
// ARM API in order that the generic request handling layer has sufficient
|
||||
// information to execute requests.
|
||||
type ApiCall interface {
|
||||
ApiInfo() ApiInfo
|
||||
type APICall interface {
|
||||
APIInfo() APIInfo
|
||||
}
|
||||
|
||||
// ApiInfo contains information about a request to the ARM API - which API
|
||||
// APIInfo contains information about a request to the ARM API - which API
|
||||
// version is required, the HTTP method to use, and a factory function for
|
||||
// responses.
|
||||
type ApiInfo struct {
|
||||
ApiVersion string
|
||||
Method string
|
||||
SkipArmBoilerplate bool
|
||||
URLPathFunc func() string
|
||||
ResponseTypeFunc func() interface{}
|
||||
type APIInfo struct {
|
||||
APIVersion string
|
||||
Method string
|
||||
URLPathFunc func() string
|
||||
ResponseTypeFunc func() interface{}
|
||||
}
|
||||
|
||||
// HasBody returns true if the API Request should have a body. This is usually
|
||||
// the case for PUT, PATCH or POST operations, but is not the case for GET operations.
|
||||
// TODO(jen20): This may need revisiting at some point.
|
||||
func (apiInfo ApiInfo) HasBody() bool {
|
||||
func (apiInfo APIInfo) HasBody() bool {
|
||||
return apiInfo.Method == "POST" || apiInfo.Method == "PUT" || apiInfo.Method == "PATCH"
|
||||
}
|
||||
|
||||
@ -0,0 +1,73 @@
|
||||
---
|
||||
layout: "azurerm"
|
||||
page_title: "Azure Resource Manager: azurerm_sql_database"
|
||||
sidebar_current: "docs-azurerm-resource-sql-database"
|
||||
description: |-
|
||||
Create a SQL Database.
|
||||
---
|
||||
|
||||
# azurerm\_sql\_database
|
||||
|
||||
Allows you to manage an Azure SQL Database
|
||||
|
||||
## Example Usage
|
||||
|
||||
```
|
||||
resource "azurerm_resource_group" "test" {
|
||||
name = "acceptanceTestResourceGroup1"
|
||||
location = "West US"
|
||||
}
|
||||
resource "azurerm_sql_database" "test" {
|
||||
name = "MySQLDatabase"
|
||||
resource_group_name = "${azurerm_resource_group.test.name}"
|
||||
location = "West US"
|
||||
|
||||
|
||||
tags {
|
||||
environment = "production"
|
||||
}
|
||||
}
|
||||
```
|
||||
## Argument Reference
|
||||
|
||||
The following arguments are supported:
|
||||
|
||||
* `name` - (Required) The name of the SQL Server.
|
||||
|
||||
* `resource_group_name` - (Required) The name of the resource group in which to
|
||||
create the sql server.
|
||||
|
||||
* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.
|
||||
|
||||
* `server_name` - (Required) The name of the SQL Server on which to create the database.
|
||||
|
||||
* `create_mode` - (Optional) Specifies the type of database to create. Defaults to `Default`. See below for the accepted values/
|
||||
|
||||
* `source_database_id` - (Optional) The URI of the source database if `create_mode` value is not `Default`.
|
||||
|
||||
* `restore_point_in_time` - (Optional) The point in time for the restore. Only applies if `create_mode` is `PointInTimeRestore` e.g. 2013-11-08T22:00:40Z
|
||||
|
||||
* `edition` - (Optional) The edition of the database to be created. Applies only if `create_mode` is `Default`. Valid values are: `Basic`, `Standard`, `Premium`.
|
||||
|
||||
* `collation` - (Optional) The name of the collation. Applies only if `create_mode` is `Default`.
|
||||
|
||||
* `max_size_bytes` - (Optional) The maximum size that the database can grow to. Applies only if `create_mode` is `Default`.
|
||||
|
||||
* `requested_service_object_id` - (Optional) Use `requested_service_object_id` or `requested_service_object_name` to set the performance level for the database.
|
||||
Valid values are: `S0`, `S1`, `S2`, `S3`, `P1`, `P2`, `P4`, `P6`, `P11` and `ElasticPool`.
|
||||
|
||||
* `requested_service_object_name` - (Optional) Use `requested_service_object_name` or `requested_service_object_id` to set the performance level for the database.
|
||||
|
||||
* `source_database_deletion_date` - (Optional) The deletion date time of the source database. Only applies to deleted databases where `create_mode` is `PointInTimeRestore`.
|
||||
|
||||
* `elastic_pool_name` - (Optional) The name of the elastic database pool.
|
||||
|
||||
* `tags` - (Optional) A mapping of tags to assign to the resource.
|
||||
|
||||
## Attributes Reference
|
||||
|
||||
The following attributes are exported:
|
||||
|
||||
* `id` - The SQL Database ID.
|
||||
* `creation_data` - The creation date of the SQL Database.
|
||||
* `default_secondary_location` - The default secondary location of the SQL Database.
|
||||
Loading…
Reference in new issue