Scaffold the Sql Server AzureRM Resource
This commit is contained in:
parent
cd78db31b7
commit
f6bee13bd4
|
@ -61,6 +61,7 @@ func Provider() terraform.ResourceProvider {
|
|||
"azurerm_storage_blob": resourceArmStorageBlob(),
|
||||
"azurerm_storage_queue": resourceArmStorageQueue(),
|
||||
"azurerm_dns_zone": resourceArmDnsZone(),
|
||||
"azurerm_sql_server": resourceArmSqlServer(),
|
||||
},
|
||||
ConfigureFunc: providerConfigure,
|
||||
}
|
||||
|
|
|
@ -0,0 +1,153 @@
|
|||
package azurerm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/schema"
|
||||
"github.com/jen20/riviera/azure"
|
||||
"github.com/jen20/riviera/sql"
|
||||
)
|
||||
|
||||
func resourceArmSqlServer() *schema.Resource {
|
||||
return &schema.Resource{
|
||||
Create: resourceArmSqlServerCreate,
|
||||
Read: resourceArmSqlServerRead,
|
||||
Update: resourceArmSqlServerCreate,
|
||||
Delete: resourceArmSqlServerDelete,
|
||||
|
||||
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,
|
||||
},
|
||||
|
||||
"version": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
|
||||
"administrator_login": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
|
||||
"administrator_login_password": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Required: true,
|
||||
},
|
||||
|
||||
"fully_qualified_domain_name": &schema.Schema{
|
||||
Type: schema.TypeString,
|
||||
Computed: true,
|
||||
},
|
||||
|
||||
"tags": tagsSchema(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func resourceArmSqlServerCreate(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*ArmClient)
|
||||
rivieraClient := client.rivieraClient
|
||||
|
||||
tags := d.Get("tags").(map[string]interface{})
|
||||
|
||||
createRequest := rivieraClient.NewRequest()
|
||||
createRequest.Command = &sql.CreateOrUpdateServer{
|
||||
Name: d.Get("name").(string),
|
||||
Location: d.Get("location").(string),
|
||||
ResourceGroupName: d.Get("resource_group_name").(string),
|
||||
AdministratorLogin: azure.String(d.Get("administrator_login").(string)),
|
||||
AdministratorLoginPassword: azure.String(d.Get("administrator_login_password").(string)),
|
||||
Version: azure.String(d.Get("version").(string)),
|
||||
Tags: expandTags(tags),
|
||||
}
|
||||
|
||||
createResponse, err := createRequest.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error creating SQL Server: %s", err)
|
||||
}
|
||||
if !createResponse.IsSuccessful() {
|
||||
return fmt.Errorf("Error creating SQL Server: %s", createResponse.Error)
|
||||
}
|
||||
|
||||
readRequest := rivieraClient.NewRequest()
|
||||
readRequest.Command = &sql.GetServer{
|
||||
Name: d.Get("name").(string),
|
||||
ResourceGroupName: d.Get("resource_group_name").(string),
|
||||
}
|
||||
|
||||
readResponse, err := readRequest.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error reading SQL Server: %s", err)
|
||||
}
|
||||
if !readResponse.IsSuccessful() {
|
||||
return fmt.Errorf("Error reading SQL Server: %s", readResponse.Error)
|
||||
}
|
||||
|
||||
resp := readResponse.Parsed.(*sql.GetServerResponse)
|
||||
d.SetId(*resp.ID)
|
||||
|
||||
return resourceArmSqlServerRead(d, meta)
|
||||
}
|
||||
|
||||
func resourceArmSqlServerRead(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*ArmClient)
|
||||
rivieraClient := client.rivieraClient
|
||||
|
||||
readRequest := rivieraClient.NewRequestForURI(d.Id())
|
||||
readRequest.Command = &sql.GetServer{}
|
||||
|
||||
readResponse, err := readRequest.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error reading SQL Server: %s", err)
|
||||
}
|
||||
if !readResponse.IsSuccessful() {
|
||||
log.Printf("[INFO] Error reading SQL Server %q - removing from state", d.Id())
|
||||
d.SetId("")
|
||||
return fmt.Errorf("Error reading SQL Server: %s", readResponse.Error)
|
||||
}
|
||||
|
||||
resp := readResponse.Parsed.(*sql.GetServerResponse)
|
||||
|
||||
d.Set("fully_qualified_domain_name", resp.FullyQualifiedDomainName)
|
||||
d.Set("administrator_login", resp.AdministratorLogin)
|
||||
d.Set("version", resp.Version)
|
||||
|
||||
flattenAndSetTags(d, resp.Tags)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func resourceArmSqlServerDelete(d *schema.ResourceData, meta interface{}) error {
|
||||
client := meta.(*ArmClient)
|
||||
rivieraClient := client.rivieraClient
|
||||
|
||||
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
|
||||
deleteRequest.Command = &sql.DeleteServer{}
|
||||
|
||||
deleteResponse, err := deleteRequest.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Error deleting SQL Server: %s", err)
|
||||
}
|
||||
if !deleteResponse.IsSuccessful() {
|
||||
return fmt.Errorf("Error deleting SQL Server: %s", deleteResponse.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
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 TestAccAzureRMSqlServer_basic(t *testing.T) {
|
||||
ri := acctest.RandInt()
|
||||
config := fmt.Sprintf(testAccAzureRMSqlServer_basic, ri, ri)
|
||||
|
||||
resource.Test(t, resource.TestCase{
|
||||
PreCheck: func() { testAccPreCheck(t) },
|
||||
Providers: testAccProviders,
|
||||
CheckDestroy: testCheckAzureRMSqlServerDestroy,
|
||||
Steps: []resource.TestStep{
|
||||
resource.TestStep{
|
||||
Config: config,
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
testCheckAzureRMSqlServerExists("azurerm_sql_server.test"),
|
||||
),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func testCheckAzureRMSqlServerExists(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.GetServer{}
|
||||
|
||||
readResponse, err := readRequest.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Bad: GetServer: %s", err)
|
||||
}
|
||||
if !readResponse.IsSuccessful() {
|
||||
return fmt.Errorf("Bad: GetServer: %s", readResponse.Error)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func testCheckAzureRMSqlServerDestroy(s *terraform.State) error {
|
||||
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
|
||||
|
||||
for _, rs := range s.RootModule().Resources {
|
||||
if rs.Type != "azurerm_sql_server" {
|
||||
continue
|
||||
}
|
||||
|
||||
readRequest := conn.NewRequestForURI(rs.Primary.ID)
|
||||
readRequest.Command = &sql.GetServer{}
|
||||
|
||||
readResponse, err := readRequest.Execute()
|
||||
if err != nil {
|
||||
return fmt.Errorf("Bad: GetServer: %s", err)
|
||||
}
|
||||
|
||||
if readResponse.IsSuccessful() {
|
||||
return fmt.Errorf("Bad: SQL Server still exists: %s", readResponse.Error)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var testAccAzureRMSqlServer_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"
|
||||
}
|
||||
`
|
Loading…
Reference in New Issue