forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_sql_database.go
207 lines (162 loc) · 4.89 KB
/
resource_sql_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
package google
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/sqladmin/v1beta4"
)
func resourceSqlDatabase() *schema.Resource {
return &schema.Resource{
Create: resourceSqlDatabaseCreate,
Read: resourceSqlDatabaseRead,
Update: resourceSqlDatabaseUpdate,
Delete: resourceSqlDatabaseDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"instance": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"self_link": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"charset": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"collation": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)
d.SetId(instance_name + ":" + database_name)
db := &sqladmin.Database{
Name: database_name,
Instance: instance_name,
Charset: d.Get("charset").(string),
Collation: d.Get("collation").(string),
}
mutexKV.Lock(instanceMutexKey(project, instance_name))
defer mutexKV.Unlock(instanceMutexKey(project, instance_name))
op, err := config.clientSqlAdmin.Databases.Insert(project, instance_name,
db).Do()
if err != nil {
return fmt.Errorf("Error, failed to insert "+
"database %s into instance %s: %s", database_name,
instance_name, err)
}
err = sqladminOperationWait(config, op, project, "Insert Database")
if err != nil {
return fmt.Errorf("Error, failure waiting for insertion of %s "+
"into %s: %s", database_name, instance_name, err)
}
return resourceSqlDatabaseRead(d, meta)
}
func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
s := strings.Split(d.Id(), ":")
if len(s) != 2 {
return fmt.Errorf("Error, failure importing database %s. "+
"ID format is instance:name", d.Id())
}
instance_name := s[0]
database_name := s[1]
db, err := config.clientSqlAdmin.Databases.Get(project, instance_name,
database_name).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("SQL Database %q in instance %q", database_name, instance_name))
}
d.Set("instance", db.Instance)
d.Set("name", db.Name)
d.Set("self_link", db.SelfLink)
d.SetId(instance_name + ":" + database_name)
d.Set("charset", db.Charset)
d.Set("collation", db.Collation)
d.Set("project", project)
return nil
}
func resourceSqlDatabaseUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)
db := &sqladmin.Database{
Name: database_name,
Instance: instance_name,
Charset: d.Get("charset").(string),
Collation: d.Get("collation").(string),
}
mutexKV.Lock(instanceMutexKey(project, instance_name))
defer mutexKV.Unlock(instanceMutexKey(project, instance_name))
op, err := config.clientSqlAdmin.Databases.Update(project, instance_name, database_name,
db).Do()
if err != nil {
return fmt.Errorf("Error, failed to update "+
"database %s in instance %s: %s", database_name,
instance_name, err)
}
err = sqladminOperationWait(config, op, project, "Update Database")
if err != nil {
return fmt.Errorf("Error, failure waiting for update of %s "+
"into %s: %s", database_name, instance_name, err)
}
return resourceSqlDatabaseRead(d, meta)
}
func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return err
}
database_name := d.Get("name").(string)
instance_name := d.Get("instance").(string)
mutexKV.Lock(instanceMutexKey(project, instance_name))
defer mutexKV.Unlock(instanceMutexKey(project, instance_name))
op, err := config.clientSqlAdmin.Databases.Delete(project, instance_name,
database_name).Do()
if err != nil {
return fmt.Errorf("Error, failed to delete"+
"database %s in instance %s: %s", database_name,
instance_name, err)
}
err = sqladminOperationWait(config, op, project, "Delete Database")
if err != nil {
return fmt.Errorf("Error, failure waiting for deletion of %s "+
"in %s: %s", database_name, instance_name, err)
}
return nil
}