forked from ovh/terraform-provider-ovh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_cloud_project_database.go
351 lines (317 loc) · 10.4 KB
/
resource_cloud_project_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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package ovh
import (
"fmt"
"log"
"net/url"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceCloudProjectDatabase() *schema.Resource {
return &schema.Resource{
Create: resourceCloudProjectDatabaseCreate,
Read: resourceCloudProjectDatabaseRead,
Delete: resourceCloudProjectDatabaseDelete,
Update: resourceCloudProjectDatabaseUpdate,
Importer: &schema.ResourceImporter{
State: resourceCloudProjectDatabaseImportState,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(40 * time.Minute),
Delete: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"description": {
Type: schema.TypeString,
Description: "Description of the cluster",
Optional: true,
},
"engine": {
Type: schema.TypeString,
Description: "Name of the engine of the service",
ForceNew: true,
Required: true,
},
"flavor": {
Type: schema.TypeString,
Description: "The node flavor used for this cluster",
Required: true,
},
"kafka_rest_api": {
Type: schema.TypeBool,
Description: "Defines whether the REST API is enabled on a Kafka cluster",
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Get("engine").(string) != "kafka" || new == old
},
},
"nodes": {
Type: schema.TypeList,
Description: "List of nodes composing the service",
Required: true,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"network_id": {
Type: schema.TypeString,
Description: "Private network ID in which the node is. It's the regional openstackId of the private network.",
ForceNew: true,
Optional: true,
},
"region": {
Type: schema.TypeString,
Description: "Region of the node",
ForceNew: true,
Required: true,
},
"subnet_id": {
Type: schema.TypeString,
Description: "Private subnet ID in which the node is",
Optional: true,
},
},
},
},
"opensearch_acls_enabled": {
Type: schema.TypeBool,
Description: "Defines whether the ACLs are enabled on an Opensearch cluster",
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Get("engine").(string) != "opensearch" || new == old
},
},
"plan": {
Type: schema.TypeString,
Description: "Plan of the cluster",
Required: true,
},
"version": {
Type: schema.TypeString,
Description: "Version of the engine deployed on the cluster",
Required: true,
},
//Computed
"backup_time": {
Type: schema.TypeString,
Description: "Time on which backups start every day",
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Description: "Date of the creation of the cluster",
Computed: true,
},
"endpoints": {
Type: schema.TypeList,
Description: "List of all endpoints of the service",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"component": {
Type: schema.TypeString,
Description: "Type of component the URI relates to",
Computed: true,
},
"domain": {
Type: schema.TypeString,
Description: "Domain of the cluster",
Computed: true,
},
"path": {
Type: schema.TypeString,
Description: "Path of the endpoint",
Computed: true,
},
"port": {
Type: schema.TypeInt,
Description: "Connection port for the endpoint",
Computed: true,
},
"scheme": {
Type: schema.TypeString,
Description: "Scheme used to generate the URI",
Computed: true,
},
"ssl": {
Type: schema.TypeBool,
Description: "Defines whether the endpoint uses SSL",
Computed: true,
},
"ssl_mode": {
Type: schema.TypeString,
Description: "SSL mode used to connect to the service if the SSL is enabled",
Computed: true,
},
"uri": {
Type: schema.TypeString,
Description: "URI of the endpoint",
Computed: true,
},
},
},
},
"maintenance_time": {
Type: schema.TypeString,
Description: "Time on which maintenances can start every day",
Computed: true,
},
"network_type": {
Type: schema.TypeString,
Description: "Type of network of the cluster",
Computed: true,
},
"status": {
Type: schema.TypeString,
Description: "Current status of the cluster",
Computed: true,
},
"disk_size": {
Type: schema.TypeInt,
Description: "Disk size attributes of the cluster",
Optional: true,
},
},
}
}
func resourceCloudProjectDatabaseImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
n := 3
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
return nil, fmt.Errorf("Import Id is not service_name/engine/databaseId formatted")
}
serviceName := splitId[0]
engine := splitId[1]
id := splitId[2]
d.SetId(id)
d.Set("engine", engine)
d.Set("service_name", serviceName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceCloudProjectDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
)
err, params := (&CloudProjectDatabaseCreateOpts{}).FromResource(d)
if err != nil {
return fmt.Errorf("multi region cluster not available yet : %q", err)
}
res := &CloudProjectDatabaseResponse{}
log.Printf("[DEBUG] Will create Database: %+v", params)
err = config.OVHClient.Post(endpoint, params, res)
if err != nil {
return fmt.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, err)
}
log.Printf("[DEBUG] Waiting for database %s to be READY", res.Id)
err = waitForCloudProjectDatabaseReady(config.OVHClient, serviceName, engine, res.Id, d.Timeout(schema.TimeoutCreate))
if err != nil {
return fmt.Errorf("timeout while waiting database %s to be READY: %w", res.Id, err)
}
log.Printf("[DEBUG] database %s is READY", res.Id)
d.SetId(res.Id)
if (engine == "kafka" && d.Get("kafka_rest_api").(bool)) || (engine == "opensearch" && d.Get("opensearch_acls_enabled").(bool)) {
return resourceCloudProjectDatabaseUpdate(d, meta)
}
return resourceCloudProjectDatabaseRead(d, meta)
}
func resourceCloudProjectDatabaseRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
serviceEndpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(d.Id()),
)
res := &CloudProjectDatabaseResponse{}
log.Printf("[DEBUG] Will read database %s from project: %s", d.Id(), serviceName)
if err := config.OVHClient.Get(serviceEndpoint, res); err != nil {
return helpers.CheckDeleted(d, err, serviceEndpoint)
}
nodesEndpoint := fmt.Sprintf("%s/node", serviceEndpoint)
nodeList := &[]string{}
if err := config.OVHClient.Get(nodesEndpoint, nodeList); err != nil {
return fmt.Errorf("unable to get database %s nodes: %v", res.Id, err)
}
if len(*nodeList) == 0 {
return fmt.Errorf("no node found for database %s", res.Id)
}
nodeEndpoint := fmt.Sprintf("%s/%s", nodesEndpoint, url.PathEscape((*nodeList)[0]))
node := &CloudProjectDatabaseNodes{}
if err := config.OVHClient.Get(nodeEndpoint, node); err != nil {
return fmt.Errorf("unable to get database %s node %s: %v", res.Id, (*nodeList)[0], err)
}
res.Region = node.Region
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}
log.Printf("[DEBUG] Read Database %+v", res)
return nil
}
func resourceCloudProjectDatabaseUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(d.Id()),
)
err, params := (&CloudProjectDatabaseUpdateOpts{}).FromResource(d)
if err != nil {
return fmt.Errorf("multi region cluster not available yet : %q", err)
}
log.Printf("[DEBUG] Will update database: %+v", params)
err = config.OVHClient.Put(endpoint, params, nil)
if err != nil {
return fmt.Errorf("calling Put %s with params %v:\n\t %q", endpoint, params, err)
}
log.Printf("[DEBUG] Waiting for database %s to be READY", d.Id())
err = waitForCloudProjectDatabaseReady(config.OVHClient, serviceName, engine, d.Id(), d.Timeout(schema.TimeoutUpdate))
if err != nil {
return fmt.Errorf("timeout while waiting database %s to be READY: %w", d.Id(), err)
}
log.Printf("[DEBUG] database %s is READY", d.Id())
return resourceCloudProjectDatabaseRead(d, meta)
}
func resourceCloudProjectDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
engine := d.Get("engine").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/%s/%s",
url.PathEscape(serviceName),
url.PathEscape(engine),
url.PathEscape(d.Id()),
)
log.Printf("[DEBUG] Will delete database %s from project: %s", d.Id(), serviceName)
err := config.OVHClient.Delete(endpoint, nil)
if err != nil {
return helpers.CheckDeleted(d, err, endpoint)
}
log.Printf("[DEBUG] Waiting for database %s to be DELETED", d.Id())
err = waitForCloudProjectDatabaseDeleted(config.OVHClient, serviceName, engine, d.Id(), d.Timeout(schema.TimeoutDelete))
if err != nil {
return fmt.Errorf("timeout while waiting database %s to be DELETED: %v", d.Id(), err)
}
log.Printf("[DEBUG] database %s is DELETED", d.Id())
d.SetId("")
return nil
}