github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azure/resource_azure_sql_database_server.go (about) 1 package azure 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/Azure/azure-sdk-for-go/management/sql" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 // resourceAzureDatabaseServer returns the *schema.Resource associated 12 // to a database server on Azure. 13 func resourceAzureSqlDatabaseServer() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceAzureSqlDatabaseServerCreate, 16 Read: resourceAzureSqlDatabaseServerRead, 17 Delete: resourceAzureSqlDatabaseServerDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Computed: true, 23 ForceNew: true, 24 }, 25 "location": &schema.Schema{ 26 Type: schema.TypeString, 27 Required: true, 28 ForceNew: true, 29 }, 30 "username": &schema.Schema{ 31 Type: schema.TypeString, 32 Required: true, 33 ForceNew: true, 34 }, 35 "password": &schema.Schema{ 36 Type: schema.TypeString, 37 Required: true, 38 ForceNew: true, 39 }, 40 "version": &schema.Schema{ 41 Type: schema.TypeString, 42 Optional: true, 43 Default: "12.0", 44 ForceNew: true, 45 }, 46 "url": &schema.Schema{ 47 Type: schema.TypeString, 48 Computed: true, 49 }, 50 }, 51 } 52 } 53 54 // resourceAzureSqlDatabaseServerCreate does all the necessary API calls to 55 // create an SQL database server off Azure. 56 func resourceAzureSqlDatabaseServerCreate(d *schema.ResourceData, meta interface{}) error { 57 sqlClient := meta.(*Client).sqlClient 58 59 log.Println("[INFO] Began constructing SQL Server creation request.") 60 params := sql.DatabaseServerCreateParams{ 61 Location: d.Get("location").(string), 62 AdministratorLogin: d.Get("username").(string), 63 AdministratorLoginPassword: d.Get("password").(string), 64 Version: d.Get("version").(string), 65 } 66 67 log.Println("[INFO] Issuing SQL Server creation request to Azure.") 68 name, err := sqlClient.CreateServer(params) 69 if err != nil { 70 return fmt.Errorf("Error creating SQL Server on Azure: %s", err) 71 } 72 73 d.Set("name", name) 74 75 d.SetId(name) 76 return resourceAzureSqlDatabaseServerRead(d, meta) 77 } 78 79 // resourceAzureSqlDatabaseServerRead does all the necessary API calls to 80 // read the state of the SQL database server off Azure. 81 func resourceAzureSqlDatabaseServerRead(d *schema.ResourceData, meta interface{}) error { 82 sqlClient := meta.(*Client).sqlClient 83 84 log.Println("[INFO] Sending SQL Servers list query to Azure.") 85 srvList, err := sqlClient.ListServers() 86 if err != nil { 87 return fmt.Errorf("Error issuing SQL Servers list query to Azure: %s", err) 88 } 89 90 // search for our particular server: 91 name := d.Get("name") 92 for _, srv := range srvList.DatabaseServers { 93 if srv.Name == name { 94 d.Set("url", srv.FullyQualifiedDomainName) 95 d.Set("state", srv.State) 96 return nil 97 } 98 } 99 100 // if reached here; it means out server doesn't exist, so we must untrack it: 101 d.SetId("") 102 return nil 103 } 104 105 // resourceAzureSqlDatabaseServerDelete does all the necessary API calls to 106 // delete the SQL database server off Azure. 107 func resourceAzureSqlDatabaseServerDelete(d *schema.ResourceData, meta interface{}) error { 108 sqlClient := meta.(*Client).sqlClient 109 110 log.Println("[INFO] Sending SQL Server deletion request to Azure.") 111 name := d.Get("name").(string) 112 err := sqlClient.DeleteServer(name) 113 if err != nil { 114 return fmt.Errorf("Error while issuing SQL Server deletion request to Azure: %s", err) 115 } 116 117 return nil 118 }