-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_database.go
299 lines (248 loc) · 6.67 KB
/
resource_database.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package main
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceDatabase() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Database name",
ForceNew: true,
},
"containment": {
Type: schema.TypeString,
Required: false,
Optional: true,
Description: "CONTAINMENT parameter",
ForceNew: false,
ValidateFunc: validateContainment,
},
"nested_triggers": {
Type: schema.TypeString,
Required: false,
Optional: true,
Description: "NESTED_TRIGGERS parameter",
ForceNew: false,
ValidateFunc: validateOnOff,
},
"trustworthy": {
Type: schema.TypeString,
Required: false,
Optional: true,
Description: "TRUSTWORTHY parameter",
ForceNew: false,
ValidateFunc: validateOnOff,
},
"default_language": {
Type: schema.TypeString,
Required: false,
Optional: true,
Description: "DEFAULT_LANGUAGE parameter",
ForceNew: false,
},
"default_fulltext_language": {
Type: schema.TypeString,
Required: false,
Optional: true,
Description: "DEFAULT_FULLTEXT_LANGUAGE parameter",
ForceNew: false,
},
"compatibility_level": {
Type: schema.TypeInt,
Required: false,
Optional: true,
Description: "COMPATIBILITY_LEVEL parameter (runs in a separate ALTER DATABASE statement)",
ForceNew: false,
ValidateFunc: validateCompatibilityLevel,
},
},
Read: resourceDatabaseRead,
Create: resourceDatabaseCreate,
Update: resourceDatabaseUpdate,
Delete: resourceDatabaseDelete,
}
}
func resourceDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
db, err := connectToMSSQL(meta.(*MSSQLConfiguration))
if err != nil {
return err
}
var stmt strings.Builder
var opts []string
var hasOptions bool
hasOptions = false
containment := d.Get("containment")
nestedTriggers := d.Get("nested_triggers")
trustworthy := d.Get("trustworthy")
defaultLanguage := d.Get("default_language")
defaultFulltextLanguage := d.Get("default_fulltext_language")
compatibilityLevel := d.Get("compatibility_level")
name := d.Get("name")
fmt.Fprintf(&stmt, "USE [master]; ")
fmt.Fprintf(&stmt, "CREATE DATABASE [%s]", name)
if containment != "" {
fmt.Fprintf(&stmt, " CONTAINMENT=%s", containment)
}
if defaultFulltextLanguage != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("DEFAULT_FULLTEXT_LANGUAGE=%s", defaultFulltextLanguage))
}
if defaultLanguage != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("DEFAULT_LANGUAGE=%s", defaultLanguage))
}
if nestedTriggers != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("NESTED_TRIGGERS=%s", nestedTriggers))
}
if trustworthy != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("TRUSTWORTHY %s", trustworthy))
}
if hasOptions {
fmt.Fprintf(&stmt, " WITH ")
}
fmt.Fprintf(&stmt, "%s", strings.Join(opts, ", "))
fmt.Fprintf(&stmt, "; ")
if compatibilityLevel != "" {
fmt.Fprintf(&stmt, "ALTER DATABASE [%s] SET COMPATIBILITY_LEVEL=%d;", name, compatibilityLevel)
}
log.Print(stmt.String())
s, err := db.Prepare(stmt.String())
if err != nil {
log.Fatal(err)
return err
}
_, err = s.Exec()
if err != nil {
log.Fatal(err)
return err
}
d.SetId(name.(string))
defer db.Close()
return nil
}
func resourceDatabaseRead(d *schema.ResourceData, meta interface{}) error {
db, err := connectToMSSQL(meta.(*MSSQLConfiguration))
resultFound := false
if err != nil {
return err
}
var stmt strings.Builder
name := d.Get("name")
fmt.Fprintf(&stmt, "USE [master]; ")
fmt.Fprintf(&stmt, "SELECT name FROM sys.databases WHERE name = N'%s';", name)
log.Print(fmt.Printf("resourceLoginRead query: %s", stmt.String()))
rows, err := db.Query(stmt.String())
if err != nil {
log.Fatal("Cannot query: ", err.Error())
return err
}
defer rows.Close()
for rows.Next() {
var nameResult string
err = rows.Scan(&nameResult)
if err != nil {
log.Fatal(err)
continue
}
log.Print(fmt.Printf("resourceLoginRead query scan result: %s", nameResult))
resultFound = true
}
if !resultFound {
d.SetId("")
}
defer db.Close()
return nil
}
func resourceDatabaseUpdate(d *schema.ResourceData, meta interface{}) error {
db, err := connectToMSSQL(meta.(*MSSQLConfiguration))
var opts []string
if err != nil {
return err
}
var stmt strings.Builder
var hasOptions bool
hasOptions = false
containment := d.Get("containment")
nestedTriggers := d.Get("nested_triggers")
trustworthy := d.Get("trustworthy")
defaultLanguage := d.Get("default_language")
defaultFulltextLanguage := d.Get("default_fulltext_language")
compatibilityLevel := d.Get("compatibility_level")
name := d.Get("name")
fmt.Fprintf(&stmt, "USE [master]; ")
fmt.Fprintf(&stmt, "ALTER DATABASE [%s]", name)
if containment != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("CONTAINMENT=%s", containment))
}
if defaultFulltextLanguage != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("DEFAULT_FULLTEXT_LANGUAGE=%s", defaultFulltextLanguage))
}
if defaultLanguage != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("DEFAULT_LANGUAGE=%s", defaultLanguage))
}
if nestedTriggers != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("NESTED_TRIGGERS=%s", nestedTriggers))
}
if trustworthy != "" {
hasOptions = true
opts = append(opts, fmt.Sprintf("TRUSTWORTHY %s", trustworthy))
}
if hasOptions {
fmt.Fprintf(&stmt, " SET ")
}
fmt.Fprintf(&stmt, "%s", strings.Join(opts, ", "))
fmt.Fprintf(&stmt, "; ")
if compatibilityLevel != "" {
fmt.Fprintf(&stmt, "ALTER DATABASE [%s] SET COMPATIBILITY_LEVEL=%d;", name, compatibilityLevel)
}
log.Print(stmt.String())
s, err := db.Prepare(stmt.String())
if err != nil {
log.Fatal(err)
return err
}
_, err = s.Exec()
if err != nil {
log.Fatal(err)
return err
}
d.SetId(name.(string))
defer db.Close()
return nil
}
func resourceDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
db, err := connectToMSSQL(meta.(*MSSQLConfiguration))
if err != nil {
return err
}
var stmt strings.Builder
name := d.Get("name")
fmt.Fprintf(&stmt, "USE [master]; ")
fmt.Fprintf(&stmt, "ALTER DATABASE [%s] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;", name)
fmt.Fprintf(&stmt, "DROP DATABASE [%s];", name)
log.Print(stmt.String())
s, err := db.Prepare(stmt.String())
if err != nil {
log.Fatal(err)
return err
}
_, err = s.Exec()
if err != nil {
log.Fatal(err)
return err
}
d.SetId("")
defer db.Close()
return nil
}