-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathresource_cloud_project_database_m3db_namespace.go
255 lines (221 loc) · 8.56 KB
/
resource_cloud_project_database_m3db_namespace.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
package ovh
import (
"context"
"fmt"
"log"
"net/url"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
)
func resourceCloudProjectDatabaseM3dbNamespace() *schema.Resource {
return &schema.Resource{
CreateContext: resourceCloudProjectDatabaseM3dbNamespaceCreate,
ReadContext: resourceCloudProjectDatabaseM3dbNamespaceRead,
DeleteContext: resourceCloudProjectDatabaseM3dbNamespaceDelete,
UpdateContext: resourceCloudProjectDatabaseM3dbNamespaceUpdate,
Importer: &schema.ResourceImporter{
State: resourceCloudProjectDatabaseM3dbNamespaceImportState,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
Update: schema.DefaultTimeout(20 * 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),
},
"cluster_id": {
Type: schema.TypeString,
Description: "Id of the database cluster",
ForceNew: true,
Required: true,
},
"name": {
Type: schema.TypeString,
Description: "Name of the namespace",
ForceNew: true,
Required: true,
},
"resolution": {
Type: schema.TypeString,
Description: "Resolution for an aggregated namespace",
Required: true,
DiffSuppressFunc: DiffDurationRfc3339,
},
"retention_block_data_expiration_duration": {
Type: schema.TypeString,
Description: "Controls how long we wait before expiring stale data",
Optional: true,
DiffSuppressFunc: DiffDurationRfc3339,
},
"retention_buffer_future_duration": {
Type: schema.TypeString,
Description: "Controls how far into the future writes to the namespace will be accepted",
Optional: true,
DiffSuppressFunc: DiffDurationRfc3339,
},
"retention_buffer_past_duration": {
Type: schema.TypeString,
Description: "Controls how far into the past writes to the namespace will be accepted",
Optional: true,
DiffSuppressFunc: DiffDurationRfc3339,
},
"retention_period_duration": {
Type: schema.TypeString,
Description: "Controls the duration of time that M3DB will retain data for the namespace",
Required: true,
DiffSuppressFunc: DiffDurationRfc3339,
},
"snapshot_enabled": {
Type: schema.TypeBool,
Description: "Defines whether M3db will create snapshot files for this namespace",
Optional: true,
},
"writes_to_commit_log_enabled": {
Type: schema.TypeBool,
Description: "Defines whether M3db will include writes to this namespace in the commit log",
Optional: true,
},
//Optional/Computed
"retention_block_size_duration": {
Type: schema.TypeString,
Description: "Controls how long to keep a block in memory before flushing to a fileset on disk",
Optional: true,
Computed: true,
DiffSuppressFunc: DiffDurationRfc3339,
},
// Computed
"type": {
Type: schema.TypeString,
Description: "Type of namespace",
Computed: true,
},
},
}
}
func resourceCloudProjectDatabaseM3dbNamespaceImportState(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/cluster_id/id formatted")
}
serviceName := splitId[0]
clusterId := splitId[1]
id := splitId[2]
d.SetId(id)
d.Set("cluster_id", clusterId)
d.Set("service_name", serviceName)
results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}
func resourceCloudProjectDatabaseM3dbNamespaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
)
// Should read one time to
listRes := make([]string, 0)
log.Printf("[DEBUG] Will read namespaces from cluster %s from project %s", clusterId, serviceName)
if err := config.OVHClient.GetWithContext(ctx, endpoint, &listRes); err != nil {
return diag.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
}
params := (&CloudProjectDatabaseM3dbNamespaceCreateOpts{}).FromResource(d)
res := &CloudProjectDatabaseM3dbNamespaceResponse{}
log.Printf("[DEBUG] Will create namespace: %+v for cluster %s from project %s", params, clusterId, serviceName)
err := config.OVHClient.PostWithContext(ctx, endpoint, params, res)
if err != nil {
return diag.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, err)
}
log.Printf("[DEBUG] Waiting for namespace %s to be READY", res.Id)
err = waitForCloudProjectDatabaseM3dbNamespaceReady(ctx, config.OVHClient, serviceName, clusterId, res.Id, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.Errorf("timeout while waiting namespace %s to be READY: %s", res.Id, err.Error())
}
log.Printf("[DEBUG] namespace %s is READY", res.Id)
d.SetId(res.Id)
return resourceCloudProjectDatabaseM3dbNamespaceRead(ctx, d, meta)
}
func resourceCloudProjectDatabaseM3dbNamespaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)
id := d.Id()
endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace/%s",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
url.PathEscape(id),
)
res := &CloudProjectDatabaseM3dbNamespaceResponse{}
log.Printf("[DEBUG] Will read namespace %s from cluster %s from project %s", id, clusterId, serviceName)
if err := config.OVHClient.GetWithContext(ctx, endpoint, res); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}
for k, v := range res.ToMap() {
if k != "id" {
d.Set(k, v)
} else {
d.SetId(fmt.Sprint(v))
}
}
return nil
}
func resourceCloudProjectDatabaseM3dbNamespaceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)
id := d.Id()
endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace/%s",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
url.PathEscape(id),
)
params := (&CloudProjectDatabaseM3dbNamespaceUpdateOpts{}).FromResource(d)
log.Printf("[DEBUG] Will update namespace: %+v from cluster %s from project %s", params, clusterId, serviceName)
err := config.OVHClient.PutWithContext(ctx, endpoint, params, nil)
if err != nil {
return diag.Errorf("calling Put %s with params %+v:\n\t %q", endpoint, params, err)
}
log.Printf("[DEBUG] Waiting for namespace %s to be READY", id)
err = waitForCloudProjectDatabaseM3dbNamespaceReady(ctx, config.OVHClient, serviceName, clusterId, id, d.Timeout(schema.TimeoutUpdate))
if err != nil {
return diag.Errorf("timeout while waiting namespace %s to be READY: %s", id, err.Error())
}
log.Printf("[DEBUG] namespace %s is READY", id)
return resourceCloudProjectDatabaseM3dbNamespaceRead(ctx, d, meta)
}
func resourceCloudProjectDatabaseM3dbNamespaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)
id := d.Id()
endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace/%s",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
url.PathEscape(id),
)
log.Printf("[DEBUG] Will delete namespace %s from cluster %s from project %s", id, clusterId, serviceName)
err := config.OVHClient.DeleteWithContext(ctx, endpoint, nil)
if err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}
log.Printf("[DEBUG] Waiting for namespace %s to be DELETED", id)
err = waitForCloudProjectDatabaseM3dbNamespaceDeleted(ctx, config.OVHClient, serviceName, clusterId, id, d.Timeout(schema.TimeoutDelete))
if err != nil {
return diag.Errorf("timeout while waiting namespace %s to be DELETED: %s", id, err.Error())
}
log.Printf("[DEBUG] namespace %s is DELETED", id)
d.SetId("")
return nil
}