Skip to content

Commit ac0dca5

Browse files
authored
manage m3db default namespace (#761)
* manage m3db default namespace
1 parent da2450a commit ac0dca5

8 files changed

+120
-70
lines changed

ovh/data_cloud_project_database_m3db_namespace_test.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ resource "ovh_cloud_project_database_m3db_namespace" "namespace" {
2727
cluster_id = ovh_cloud_project_database.db.id
2828
name = "%s"
2929
resolution = "%s"
30-
retention_period_duration = "%s"
3130
}
3231
3332
data "ovh_cloud_project_database_m3db_namespace" "namespace" {
@@ -48,7 +47,6 @@ func TestAccCloudProjectDatabaseM3dbNamespaceDataSource_basic(t *testing.T) {
4847
description := acctest.RandomWithPrefix(test_prefix)
4948
name := "mynamespace"
5049
resolution := "P2D"
51-
periodDuration := "P2D"
5250

5351
config := fmt.Sprintf(
5452
testAccCloudProjectDatabaseM3dbNamespaceDatasourceConfig_Basic,
@@ -59,7 +57,6 @@ func TestAccCloudProjectDatabaseM3dbNamespaceDataSource_basic(t *testing.T) {
5957
flavor,
6058
name,
6159
resolution,
62-
periodDuration,
6360
)
6461

6562
resource.Test(t, resource.TestCase{
@@ -74,9 +71,15 @@ func TestAccCloudProjectDatabaseM3dbNamespaceDataSource_basic(t *testing.T) {
7471
resource.TestCheckResourceAttr(
7572
"data.ovh_cloud_project_database_m3db_namespace.namespace", "resolution", resolution),
7673
resource.TestCheckResourceAttr(
77-
"data.ovh_cloud_project_database_m3db_namespace.namespace", "retention_period_duration", periodDuration),
74+
"data.ovh_cloud_project_database_m3db_namespace.namespace", "retention_block_size_duration", resolution),
75+
resource.TestCheckResourceAttr(
76+
"data.ovh_cloud_project_database_m3db_namespace.namespace", "retention_period_duration", "P2D"),
77+
resource.TestCheckResourceAttr(
78+
"data.ovh_cloud_project_database_m3db_namespace.namespace", "snapshot_enabled", "true"),
7879
resource.TestCheckResourceAttr(
7980
"data.ovh_cloud_project_database_m3db_namespace.namespace", "type", "aggregated"),
81+
resource.TestCheckResourceAttr(
82+
"data.ovh_cloud_project_database_m3db_namespace.namespace", "writes_to_commit_log_enabled", "true"),
8083
),
8184
},
8285
},

ovh/data_cloud_project_database_m3db_namespaces_test.go

-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ resource "ovh_cloud_project_database_m3db_namespace" "namespace" {
2727
cluster_id = ovh_cloud_project_database.db.id
2828
name = "%s"
2929
resolution = "%s"
30-
retention_period_duration = "%s"
3130
}
3231
3332
@@ -48,7 +47,6 @@ func TestAccCloudProjectDatabaseM3dbNamespacesDataSource_basic(t *testing.T) {
4847
description := acctest.RandomWithPrefix(test_prefix)
4948
name := "mynamespace"
5049
resolution := "P2D"
51-
periodDuration := "PT48H"
5250

5351
config := fmt.Sprintf(
5452
testAccCloudProjectDatabaseM3dbNamespacesDatasourceConfig_Basic,
@@ -59,7 +57,6 @@ func TestAccCloudProjectDatabaseM3dbNamespacesDataSource_basic(t *testing.T) {
5957
flavor,
6058
name,
6159
resolution,
62-
periodDuration,
6360
)
6461

6562
resource.Test(t, resource.TestCase{

ovh/import_cloud_project_database_m3db_namespace_test.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,10 @@ func TestAccCloudProjectDatabaseM3dbNamespace_importBasic(t *testing.T) {
1717
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
1818
}
1919
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
20-
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_M3DB_FLAVOR_TEST")
21-
if flavor == "" {
22-
flavor = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
23-
}
20+
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
2421
description := acctest.RandomWithPrefix(test_prefix)
2522
name := "mynamespace"
2623
resolution := "P2D"
27-
periodDuration := "PT48H"
2824

2925
config := fmt.Sprintf(
3026
testAccCloudProjectDatabaseM3dbNamespaceConfig_basic,
@@ -35,7 +31,6 @@ func TestAccCloudProjectDatabaseM3dbNamespace_importBasic(t *testing.T) {
3531
flavor,
3632
name,
3733
resolution,
38-
periodDuration,
3934
)
4035

4136
resource.Test(t, resource.TestCase{

ovh/import_cloud_project_database_m3db_user_test.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ func TestAccCloudProjectDatabaseM3dbUser_importBasic(t *testing.T) {
1717
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
1818
}
1919
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
20-
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_M3DB_FLAVOR_TEST")
21-
if flavor == "" {
22-
flavor = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
23-
}
20+
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
2421
description := acctest.RandomWithPrefix(test_prefix)
2522
name := "johndoe"
2623
group := "mygroup"

ovh/resource_cloud_project_database_m3db_namespace.go

+76-33
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ func resourceCloudProjectDatabaseM3dbNamespace() *schema.Resource {
5555
Required: true,
5656
DiffSuppressFunc: DiffDurationRfc3339,
5757
},
58+
59+
// Optional
5860
"retention_block_data_expiration_duration": {
5961
Type: schema.TypeString,
6062
Description: "Controls how long we wait before expiring stale data",
@@ -73,30 +75,34 @@ func resourceCloudProjectDatabaseM3dbNamespace() *schema.Resource {
7375
Optional: true,
7476
DiffSuppressFunc: DiffDurationRfc3339,
7577
},
78+
79+
// Optional/Computed
80+
"retention_block_size_duration": {
81+
Type: schema.TypeString,
82+
Description: "Controls how long to keep a block in memory before flushing to a fileset on disk",
83+
ForceNew: true,
84+
Optional: true,
85+
Computed: true,
86+
DiffSuppressFunc: DiffDurationRfc3339,
87+
},
7688
"retention_period_duration": {
7789
Type: schema.TypeString,
7890
Description: "Controls the duration of time that M3DB will retain data for the namespace",
79-
Required: true,
91+
Optional: true,
92+
Computed: true,
8093
DiffSuppressFunc: DiffDurationRfc3339,
8194
},
8295
"snapshot_enabled": {
8396
Type: schema.TypeBool,
8497
Description: "Defines whether M3db will create snapshot files for this namespace",
8598
Optional: true,
99+
Computed: true,
86100
},
87101
"writes_to_commit_log_enabled": {
88102
Type: schema.TypeBool,
89103
Description: "Defines whether M3db will include writes to this namespace in the commit log",
90104
Optional: true,
91-
},
92-
93-
//Optional/Computed
94-
"retention_block_size_duration": {
95-
Type: schema.TypeString,
96-
Description: "Controls how long to keep a block in memory before flushing to a fileset on disk",
97-
Optional: true,
98-
Computed: true,
99-
DiffSuppressFunc: DiffDurationRfc3339,
105+
Computed: true,
100106
},
101107

102108
// Computed
@@ -110,52 +116,83 @@ func resourceCloudProjectDatabaseM3dbNamespace() *schema.Resource {
110116
}
111117

112118
func resourceCloudProjectDatabaseM3dbNamespaceImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
113-
givenId := d.Id()
119+
givenID := d.Id()
114120
n := 3
115-
splitId := strings.SplitN(givenId, "/", n)
116-
if len(splitId) != n {
121+
splitID := strings.SplitN(givenID, "/", n)
122+
if len(splitID) != n {
117123
return nil, fmt.Errorf("import Id is not service_name/cluster_id/id formatted")
118124
}
119-
serviceName := splitId[0]
120-
clusterId := splitId[1]
121-
id := splitId[2]
125+
serviceName := splitID[0]
126+
clusterID := splitID[1]
127+
id := splitID[2]
122128
d.SetId(id)
123-
d.Set("cluster_id", clusterId)
129+
d.Set("cluster_id", clusterID)
124130
d.Set("service_name", serviceName)
125131

126132
results := make([]*schema.ResourceData, 1)
127133
results[0] = d
128134
return results, nil
129135
}
130136

137+
func m3dbNamespaceToMap(d *schema.ResourceData) map[string]interface{} {
138+
obj := make(map[string]interface{})
139+
obj["resolution"] = d.Get("resolution")
140+
obj["retention_block_data_expiration_duration"] = d.Get("retention_block_data_expiration_duration")
141+
obj["retention_block_size_duration"] = d.Get("retention_block_size_duration")
142+
obj["retention_buffer_future_duration"] = d.Get("retention_buffer_future_duration")
143+
obj["retention_buffer_past_duration"] = d.Get("retention_buffer_past_duration")
144+
obj["retention_period_duration"] = d.Get("retention_period_duration")
145+
obj["snapshot_enabled"] = d.Get("snapshot_enabled")
146+
obj["type"] = d.Get("type")
147+
obj["writes_to_commit_log_enabled"] = d.Get("writes_to_commit_log_enabled")
148+
149+
return obj
150+
}
151+
131152
func resourceCloudProjectDatabaseM3dbNamespaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
153+
name := d.Get("name").(string)
154+
if name == "default" {
155+
obj := m3dbNamespaceToMap(d)
156+
157+
diags := dataSourceCloudProjectDatabaseM3dbNamespaceRead(ctx, d, meta)
158+
if diags.HasError() {
159+
return diags
160+
}
161+
162+
for k, v := range obj {
163+
d.Set(k, v)
164+
}
165+
166+
return resourceCloudProjectDatabaseM3dbNamespaceUpdate(ctx, d, meta)
167+
}
168+
132169
config := meta.(*Config)
133170
serviceName := d.Get("service_name").(string)
134-
clusterId := d.Get("cluster_id").(string)
171+
clusterID := d.Get("cluster_id").(string)
135172

136173
endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace",
137174
url.PathEscape(serviceName),
138-
url.PathEscape(clusterId),
175+
url.PathEscape(clusterID),
139176
)
140177

141178
// Should read one time to
142179
listRes := make([]string, 0)
143-
log.Printf("[DEBUG] Will read namespaces from cluster %s from project %s", clusterId, serviceName)
180+
log.Printf("[DEBUG] Will read namespaces from cluster %s from project %s", clusterID, serviceName)
144181
if err := config.OVHClient.GetWithContext(ctx, endpoint, &listRes); err != nil {
145182
return diag.Errorf("Error calling GET %s:\n\t %q", endpoint, err)
146183
}
147184

148185
params := (&CloudProjectDatabaseM3dbNamespaceCreateOpts{}).FromResource(d)
149186
res := &CloudProjectDatabaseM3dbNamespaceResponse{}
150187

151-
log.Printf("[DEBUG] Will create namespace: %+v for cluster %s from project %s", params, clusterId, serviceName)
188+
log.Printf("[DEBUG] Will create namespace: %+v for cluster %s from project %s", params, clusterID, serviceName)
152189
err := config.OVHClient.PostWithContext(ctx, endpoint, params, res)
153190
if err != nil {
154191
return diag.Errorf("calling Post %s with params %+v:\n\t %q", endpoint, params, err)
155192
}
156193

157194
log.Printf("[DEBUG] Waiting for namespace %s to be READY", res.Id)
158-
err = waitForCloudProjectDatabaseM3dbNamespaceReady(ctx, config.OVHClient, serviceName, clusterId, res.Id, d.Timeout(schema.TimeoutCreate))
195+
err = waitForCloudProjectDatabaseM3dbNamespaceReady(ctx, config.OVHClient, serviceName, clusterID, res.Id, d.Timeout(schema.TimeoutCreate))
159196
if err != nil {
160197
return diag.Errorf("timeout while waiting namespace %s to be READY: %s", res.Id, err.Error())
161198
}
@@ -169,17 +206,17 @@ func resourceCloudProjectDatabaseM3dbNamespaceCreate(ctx context.Context, d *sch
169206
func resourceCloudProjectDatabaseM3dbNamespaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
170207
config := meta.(*Config)
171208
serviceName := d.Get("service_name").(string)
172-
clusterId := d.Get("cluster_id").(string)
209+
clusterID := d.Get("cluster_id").(string)
173210
id := d.Id()
174211

175212
endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace/%s",
176213
url.PathEscape(serviceName),
177-
url.PathEscape(clusterId),
214+
url.PathEscape(clusterID),
178215
url.PathEscape(id),
179216
)
180217
res := &CloudProjectDatabaseM3dbNamespaceResponse{}
181218

182-
log.Printf("[DEBUG] Will read namespace %s from cluster %s from project %s", id, clusterId, serviceName)
219+
log.Printf("[DEBUG] Will read namespace %s from cluster %s from project %s", id, clusterID, serviceName)
183220
if err := config.OVHClient.GetWithContext(ctx, endpoint, res); err != nil {
184221
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
185222
}
@@ -198,24 +235,24 @@ func resourceCloudProjectDatabaseM3dbNamespaceRead(ctx context.Context, d *schem
198235
func resourceCloudProjectDatabaseM3dbNamespaceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
199236
config := meta.(*Config)
200237
serviceName := d.Get("service_name").(string)
201-
clusterId := d.Get("cluster_id").(string)
238+
clusterID := d.Get("cluster_id").(string)
202239
id := d.Id()
203240

204241
endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace/%s",
205242
url.PathEscape(serviceName),
206-
url.PathEscape(clusterId),
243+
url.PathEscape(clusterID),
207244
url.PathEscape(id),
208245
)
209246
params := (&CloudProjectDatabaseM3dbNamespaceUpdateOpts{}).FromResource(d)
210247

211-
log.Printf("[DEBUG] Will update namespace: %+v from cluster %s from project %s", params, clusterId, serviceName)
248+
log.Printf("[DEBUG] Will update namespace: %+v from cluster %s from project %s", params, clusterID, serviceName)
212249
err := config.OVHClient.PutWithContext(ctx, endpoint, params, nil)
213250
if err != nil {
214251
return diag.Errorf("calling Put %s with params %+v:\n\t %q", endpoint, params, err)
215252
}
216253

217254
log.Printf("[DEBUG] Waiting for namespace %s to be READY", id)
218-
err = waitForCloudProjectDatabaseM3dbNamespaceReady(ctx, config.OVHClient, serviceName, clusterId, id, d.Timeout(schema.TimeoutUpdate))
255+
err = waitForCloudProjectDatabaseM3dbNamespaceReady(ctx, config.OVHClient, serviceName, clusterID, id, d.Timeout(schema.TimeoutUpdate))
219256
if err != nil {
220257
return diag.Errorf("timeout while waiting namespace %s to be READY: %s", id, err.Error())
221258
}
@@ -225,25 +262,31 @@ func resourceCloudProjectDatabaseM3dbNamespaceUpdate(ctx context.Context, d *sch
225262
}
226263

227264
func resourceCloudProjectDatabaseM3dbNamespaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
265+
name := d.Get("name").(string)
266+
if name == "default" {
267+
d.SetId("")
268+
return nil
269+
}
270+
228271
config := meta.(*Config)
229272
serviceName := d.Get("service_name").(string)
230-
clusterId := d.Get("cluster_id").(string)
273+
clusterID := d.Get("cluster_id").(string)
231274
id := d.Id()
232275

233276
endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace/%s",
234277
url.PathEscape(serviceName),
235-
url.PathEscape(clusterId),
278+
url.PathEscape(clusterID),
236279
url.PathEscape(id),
237280
)
238281

239-
log.Printf("[DEBUG] Will delete namespace %s from cluster %s from project %s", id, clusterId, serviceName)
282+
log.Printf("[DEBUG] Will delete namespace %s from cluster %s from project %s", id, clusterID, serviceName)
240283
err := config.OVHClient.DeleteWithContext(ctx, endpoint, nil)
241284
if err != nil {
242285
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
243286
}
244287

245288
log.Printf("[DEBUG] Waiting for namespace %s to be DELETED", id)
246-
err = waitForCloudProjectDatabaseM3dbNamespaceDeleted(ctx, config.OVHClient, serviceName, clusterId, id, d.Timeout(schema.TimeoutDelete))
289+
err = waitForCloudProjectDatabaseM3dbNamespaceDeleted(ctx, config.OVHClient, serviceName, clusterID, id, d.Timeout(schema.TimeoutDelete))
247290
if err != nil {
248291
return diag.Errorf("timeout while waiting namespace %s to be DELETED: %s", id, err.Error())
249292
}

ovh/resource_cloud_project_database_m3db_namespace_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ resource "ovh_cloud_project_database_m3db_namespace" "namespace" {
2727
cluster_id = ovh_cloud_project_database.db.id
2828
name = "%s"
2929
resolution = "%s"
30-
retention_period_duration = "%s"
3130
}
3231
`
3332

@@ -38,14 +37,10 @@ func TestAccCloudProjectDatabaseM3dbNamespace_basic(t *testing.T) {
3837
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
3938
}
4039
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
41-
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_M3DB_FLAVOR_TEST")
42-
if flavor == "" {
43-
flavor = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
44-
}
40+
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
4541
description := acctest.RandomWithPrefix(test_prefix)
4642
name := "mynamespace"
4743
resolution := "P2D"
48-
periodDuration := "P2D"
4944

5045
config := fmt.Sprintf(
5146
testAccCloudProjectDatabaseM3dbNamespaceConfig_basic,
@@ -56,7 +51,6 @@ func TestAccCloudProjectDatabaseM3dbNamespace_basic(t *testing.T) {
5651
flavor,
5752
name,
5853
resolution,
59-
periodDuration,
6054
)
6155

6256
resource.Test(t, resource.TestCase{
@@ -71,9 +65,15 @@ func TestAccCloudProjectDatabaseM3dbNamespace_basic(t *testing.T) {
7165
resource.TestCheckResourceAttr(
7266
"ovh_cloud_project_database_m3db_namespace.namespace", "resolution", resolution),
7367
resource.TestCheckResourceAttr(
74-
"ovh_cloud_project_database_m3db_namespace.namespace", "retention_period_duration", periodDuration),
68+
"ovh_cloud_project_database_m3db_namespace.namespace", "retention_block_size_duration", resolution),
69+
resource.TestCheckResourceAttr(
70+
"ovh_cloud_project_database_m3db_namespace.namespace", "retention_period_duration", "P2D"),
71+
resource.TestCheckResourceAttr(
72+
"ovh_cloud_project_database_m3db_namespace.namespace", "snapshot_enabled", "true"),
7573
resource.TestCheckResourceAttr(
7674
"ovh_cloud_project_database_m3db_namespace.namespace", "type", "aggregated"),
75+
resource.TestCheckResourceAttr(
76+
"ovh_cloud_project_database_m3db_namespace.namespace", "writes_to_commit_log_enabled", "true"),
7777
),
7878
},
7979
},

0 commit comments

Comments
 (0)