terraform/builtin/providers/azurerm/resource_arm_sql_server.go

162 lines
4.3 KiB
Go
Raw Normal View History

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,
provider/azurerm: enable import of more resources (#10195) * provider/azurerm: support import of virtual_machine TF_ACC=1 go test ./builtin/providers/azurerm -v -run "TestAccAzureRMVirtualMachine_(basic|import)" -timeout 120m === RUN TestAccAzureRMVirtualMachine_importBasic --- PASS: TestAccAzureRMVirtualMachine_importBasic (561.08s) === RUN TestAccAzureRMVirtualMachine_basicLinuxMachine --- PASS: TestAccAzureRMVirtualMachine_basicLinuxMachine (677.49s) === RUN TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears --- PASS: TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears (674.21s) === RUN TestAccAzureRMVirtualMachine_basicWindowsMachine --- PASS: TestAccAzureRMVirtualMachine_basicWindowsMachine (1105.18s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 3017.970s * provider/azurerm: support import of servicebus_namespace TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMServiceBusNamespace_import -timeout 120m === RUN TestAccAzureRMServiceBusNamespace_importBasic --- PASS: TestAccAzureRMServiceBusNamespace_importBasic (345.80s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 345.879s * provider/azurerm: document import of servicebus_topic and servicebus_subscription * provider/azurerm: support import of dns record resources TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMDns[A-z]+Record_importBasic -timeout 120m === RUN TestAccAzureRMDnsARecord_importBasic --- PASS: TestAccAzureRMDnsARecord_importBasic (102.84s) === RUN TestAccAzureRMDnsAAAARecord_importBasic --- PASS: TestAccAzureRMDnsAAAARecord_importBasic (100.59s) === RUN TestAccAzureRMDnsCNameRecord_importBasic --- PASS: TestAccAzureRMDnsCNameRecord_importBasic (98.94s) === RUN TestAccAzureRMDnsMxRecord_importBasic --- PASS: TestAccAzureRMDnsMxRecord_importBasic (107.30s) === RUN TestAccAzureRMDnsNsRecord_importBasic --- PASS: TestAccAzureRMDnsNsRecord_importBasic (98.55s) === RUN TestAccAzureRMDnsSrvRecord_importBasic --- PASS: TestAccAzureRMDnsSrvRecord_importBasic (100.19s) === RUN TestAccAzureRMDnsTxtRecord_importBasic --- PASS: TestAccAzureRMDnsTxtRecord_importBasic (97.49s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 706.000s * provider/azurerm: support import of cdn_endpoint, document profile import TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMCdnEndpoint_import -timeout 120m === RUN TestAccAzureRMCdnEndpoint_importWithTags --- PASS: TestAccAzureRMCdnEndpoint_importWithTags (207.83s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 207.907s * provider/azurerm: support import of sql_server, fix sql_firewall import TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMSql[A-z]+_importBasic -timeout 120m === RUN TestAccAzureRMSqlFirewallRule_importBasic --- PASS: TestAccAzureRMSqlFirewallRule_importBasic (153.72s) === RUN TestAccAzureRMSqlServer_importBasic --- PASS: TestAccAzureRMSqlServer_importBasic (119.83s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 273.630s
2016-11-22 13:13:11 +01:00
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": locationSchema(),
"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,
Sensitive: 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{})
2016-02-04 00:40:32 +01:00
expandedTags := expandTags(tags)
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)),
2016-02-04 00:40:32 +01:00
Tags: *expandedTags,
}
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
provider/azurerm: enable import of more resources (#10195) * provider/azurerm: support import of virtual_machine TF_ACC=1 go test ./builtin/providers/azurerm -v -run "TestAccAzureRMVirtualMachine_(basic|import)" -timeout 120m === RUN TestAccAzureRMVirtualMachine_importBasic --- PASS: TestAccAzureRMVirtualMachine_importBasic (561.08s) === RUN TestAccAzureRMVirtualMachine_basicLinuxMachine --- PASS: TestAccAzureRMVirtualMachine_basicLinuxMachine (677.49s) === RUN TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears --- PASS: TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears (674.21s) === RUN TestAccAzureRMVirtualMachine_basicWindowsMachine --- PASS: TestAccAzureRMVirtualMachine_basicWindowsMachine (1105.18s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 3017.970s * provider/azurerm: support import of servicebus_namespace TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMServiceBusNamespace_import -timeout 120m === RUN TestAccAzureRMServiceBusNamespace_importBasic --- PASS: TestAccAzureRMServiceBusNamespace_importBasic (345.80s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 345.879s * provider/azurerm: document import of servicebus_topic and servicebus_subscription * provider/azurerm: support import of dns record resources TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMDns[A-z]+Record_importBasic -timeout 120m === RUN TestAccAzureRMDnsARecord_importBasic --- PASS: TestAccAzureRMDnsARecord_importBasic (102.84s) === RUN TestAccAzureRMDnsAAAARecord_importBasic --- PASS: TestAccAzureRMDnsAAAARecord_importBasic (100.59s) === RUN TestAccAzureRMDnsCNameRecord_importBasic --- PASS: TestAccAzureRMDnsCNameRecord_importBasic (98.94s) === RUN TestAccAzureRMDnsMxRecord_importBasic --- PASS: TestAccAzureRMDnsMxRecord_importBasic (107.30s) === RUN TestAccAzureRMDnsNsRecord_importBasic --- PASS: TestAccAzureRMDnsNsRecord_importBasic (98.55s) === RUN TestAccAzureRMDnsSrvRecord_importBasic --- PASS: TestAccAzureRMDnsSrvRecord_importBasic (100.19s) === RUN TestAccAzureRMDnsTxtRecord_importBasic --- PASS: TestAccAzureRMDnsTxtRecord_importBasic (97.49s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 706.000s * provider/azurerm: support import of cdn_endpoint, document profile import TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMCdnEndpoint_import -timeout 120m === RUN TestAccAzureRMCdnEndpoint_importWithTags --- PASS: TestAccAzureRMCdnEndpoint_importWithTags (207.83s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 207.907s * provider/azurerm: support import of sql_server, fix sql_firewall import TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMSql[A-z]+_importBasic -timeout 120m === RUN TestAccAzureRMSqlFirewallRule_importBasic --- PASS: TestAccAzureRMSqlFirewallRule_importBasic (153.72s) === RUN TestAccAzureRMSqlServer_importBasic --- PASS: TestAccAzureRMSqlServer_importBasic (119.83s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 273.630s
2016-11-22 13:13:11 +01:00
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
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)
provider/azurerm: enable import of more resources (#10195) * provider/azurerm: support import of virtual_machine TF_ACC=1 go test ./builtin/providers/azurerm -v -run "TestAccAzureRMVirtualMachine_(basic|import)" -timeout 120m === RUN TestAccAzureRMVirtualMachine_importBasic --- PASS: TestAccAzureRMVirtualMachine_importBasic (561.08s) === RUN TestAccAzureRMVirtualMachine_basicLinuxMachine --- PASS: TestAccAzureRMVirtualMachine_basicLinuxMachine (677.49s) === RUN TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears --- PASS: TestAccAzureRMVirtualMachine_basicLinuxMachine_disappears (674.21s) === RUN TestAccAzureRMVirtualMachine_basicWindowsMachine --- PASS: TestAccAzureRMVirtualMachine_basicWindowsMachine (1105.18s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 3017.970s * provider/azurerm: support import of servicebus_namespace TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMServiceBusNamespace_import -timeout 120m === RUN TestAccAzureRMServiceBusNamespace_importBasic --- PASS: TestAccAzureRMServiceBusNamespace_importBasic (345.80s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 345.879s * provider/azurerm: document import of servicebus_topic and servicebus_subscription * provider/azurerm: support import of dns record resources TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMDns[A-z]+Record_importBasic -timeout 120m === RUN TestAccAzureRMDnsARecord_importBasic --- PASS: TestAccAzureRMDnsARecord_importBasic (102.84s) === RUN TestAccAzureRMDnsAAAARecord_importBasic --- PASS: TestAccAzureRMDnsAAAARecord_importBasic (100.59s) === RUN TestAccAzureRMDnsCNameRecord_importBasic --- PASS: TestAccAzureRMDnsCNameRecord_importBasic (98.94s) === RUN TestAccAzureRMDnsMxRecord_importBasic --- PASS: TestAccAzureRMDnsMxRecord_importBasic (107.30s) === RUN TestAccAzureRMDnsNsRecord_importBasic --- PASS: TestAccAzureRMDnsNsRecord_importBasic (98.55s) === RUN TestAccAzureRMDnsSrvRecord_importBasic --- PASS: TestAccAzureRMDnsSrvRecord_importBasic (100.19s) === RUN TestAccAzureRMDnsTxtRecord_importBasic --- PASS: TestAccAzureRMDnsTxtRecord_importBasic (97.49s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 706.000s * provider/azurerm: support import of cdn_endpoint, document profile import TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMCdnEndpoint_import -timeout 120m === RUN TestAccAzureRMCdnEndpoint_importWithTags --- PASS: TestAccAzureRMCdnEndpoint_importWithTags (207.83s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 207.907s * provider/azurerm: support import of sql_server, fix sql_firewall import TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMSql[A-z]+_importBasic -timeout 120m === RUN TestAccAzureRMSqlFirewallRule_importBasic --- PASS: TestAccAzureRMSqlFirewallRule_importBasic (153.72s) === RUN TestAccAzureRMSqlServer_importBasic --- PASS: TestAccAzureRMSqlServer_importBasic (119.83s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 273.630s
2016-11-22 13:13:11 +01:00
d.Set("name", id.Path["servers"])
d.Set("resource_group_name", id.ResourceGroup)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
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
}