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