Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manage m3db default namespace #761

Merged
merged 8 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions ovh/data_cloud_project_database_m3db_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ resource "ovh_cloud_project_database_m3db_namespace" "namespace" {
cluster_id = ovh_cloud_project_database.db.id
name = "%s"
resolution = "%s"
retention_period_duration = "%s"
}

data "ovh_cloud_project_database_m3db_namespace" "namespace" {
Expand All @@ -48,7 +47,6 @@ func TestAccCloudProjectDatabaseM3dbNamespaceDataSource_basic(t *testing.T) {
description := acctest.RandomWithPrefix(test_prefix)
name := "mynamespace"
resolution := "P2D"
periodDuration := "P2D"

config := fmt.Sprintf(
testAccCloudProjectDatabaseM3dbNamespaceDatasourceConfig_Basic,
Expand All @@ -59,7 +57,6 @@ func TestAccCloudProjectDatabaseM3dbNamespaceDataSource_basic(t *testing.T) {
flavor,
name,
resolution,
periodDuration,
)

resource.Test(t, resource.TestCase{
Expand All @@ -74,9 +71,15 @@ func TestAccCloudProjectDatabaseM3dbNamespaceDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_m3db_namespace.namespace", "resolution", resolution),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_m3db_namespace.namespace", "retention_period_duration", periodDuration),
"data.ovh_cloud_project_database_m3db_namespace.namespace", "retention_block_size_duration", resolution),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_m3db_namespace.namespace", "retention_period_duration", "P2D"),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_m3db_namespace.namespace", "snapshot_enabled", "true"),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_m3db_namespace.namespace", "type", "aggregated"),
resource.TestCheckResourceAttr(
"data.ovh_cloud_project_database_m3db_namespace.namespace", "writes_to_commit_log_enabled", "true"),
),
},
},
Expand Down
3 changes: 0 additions & 3 deletions ovh/data_cloud_project_database_m3db_namespaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ resource "ovh_cloud_project_database_m3db_namespace" "namespace" {
cluster_id = ovh_cloud_project_database.db.id
name = "%s"
resolution = "%s"
retention_period_duration = "%s"
}


Expand All @@ -48,7 +47,6 @@ func TestAccCloudProjectDatabaseM3dbNamespacesDataSource_basic(t *testing.T) {
description := acctest.RandomWithPrefix(test_prefix)
name := "mynamespace"
resolution := "P2D"
periodDuration := "PT48H"

config := fmt.Sprintf(
testAccCloudProjectDatabaseM3dbNamespacesDatasourceConfig_Basic,
Expand All @@ -59,7 +57,6 @@ func TestAccCloudProjectDatabaseM3dbNamespacesDataSource_basic(t *testing.T) {
flavor,
name,
resolution,
periodDuration,
)

resource.Test(t, resource.TestCase{
Expand Down
7 changes: 1 addition & 6 deletions ovh/import_cloud_project_database_m3db_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,10 @@ func TestAccCloudProjectDatabaseM3dbNamespace_importBasic(t *testing.T) {
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
}
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_M3DB_FLAVOR_TEST")
if flavor == "" {
flavor = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
}
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
description := acctest.RandomWithPrefix(test_prefix)
name := "mynamespace"
resolution := "P2D"
periodDuration := "PT48H"

config := fmt.Sprintf(
testAccCloudProjectDatabaseM3dbNamespaceConfig_basic,
Expand All @@ -35,7 +31,6 @@ func TestAccCloudProjectDatabaseM3dbNamespace_importBasic(t *testing.T) {
flavor,
name,
resolution,
periodDuration,
)

resource.Test(t, resource.TestCase{
Expand Down
5 changes: 1 addition & 4 deletions ovh/import_cloud_project_database_m3db_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ func TestAccCloudProjectDatabaseM3dbUser_importBasic(t *testing.T) {
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
}
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_M3DB_FLAVOR_TEST")
if flavor == "" {
flavor = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
}
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
description := acctest.RandomWithPrefix(test_prefix)
name := "johndoe"
group := "mygroup"
Expand Down
109 changes: 76 additions & 33 deletions ovh/resource_cloud_project_database_m3db_namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func resourceCloudProjectDatabaseM3dbNamespace() *schema.Resource {
Required: true,
DiffSuppressFunc: DiffDurationRfc3339,
},

// Optional
"retention_block_data_expiration_duration": {
Type: schema.TypeString,
Description: "Controls how long we wait before expiring stale data",
Expand All @@ -73,30 +75,34 @@ func resourceCloudProjectDatabaseM3dbNamespace() *schema.Resource {
Optional: true,
DiffSuppressFunc: DiffDurationRfc3339,
},

// 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",
ForceNew: true,
Optional: true,
Computed: 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,
Optional: true,
Computed: true,
DiffSuppressFunc: DiffDurationRfc3339,
},
"snapshot_enabled": {
Type: schema.TypeBool,
Description: "Defines whether M3db will create snapshot files for this namespace",
Optional: true,
Computed: 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: true,
},

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

func resourceCloudProjectDatabaseM3dbNamespaceImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
givenId := d.Id()
givenID := d.Id()
n := 3
splitId := strings.SplitN(givenId, "/", n)
if len(splitId) != n {
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]
serviceName := splitID[0]
clusterID := splitID[1]
id := splitID[2]
d.SetId(id)
d.Set("cluster_id", clusterId)
d.Set("cluster_id", clusterID)
d.Set("service_name", serviceName)

results := make([]*schema.ResourceData, 1)
results[0] = d
return results, nil
}

func m3dbNamespaceToMap(d *schema.ResourceData) map[string]interface{} {
obj := make(map[string]interface{})
obj["resolution"] = d.Get("resolution")
obj["retention_block_data_expiration_duration"] = d.Get("retention_block_data_expiration_duration")
obj["retention_block_size_duration"] = d.Get("retention_block_size_duration")
obj["retention_buffer_future_duration"] = d.Get("retention_buffer_future_duration")
obj["retention_buffer_past_duration"] = d.Get("retention_buffer_past_duration")
obj["retention_period_duration"] = d.Get("retention_period_duration")
obj["snapshot_enabled"] = d.Get("snapshot_enabled")
obj["type"] = d.Get("type")
obj["writes_to_commit_log_enabled"] = d.Get("writes_to_commit_log_enabled")

return obj
}

func resourceCloudProjectDatabaseM3dbNamespaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
name := d.Get("name").(string)
if name == "default" {
obj := m3dbNamespaceToMap(d)

diags := dataSourceCloudProjectDatabaseM3dbNamespaceRead(ctx, d, meta)
if diags.HasError() {
return diags
}

for k, v := range obj {
d.Set(k, v)
}

return resourceCloudProjectDatabaseM3dbNamespaceUpdate(ctx, d, meta)
}

config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)
clusterID := d.Get("cluster_id").(string)

endpoint := fmt.Sprintf("/cloud/project/%s/database/m3db/%s/namespace",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
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)
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)
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))
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())
}
Expand All @@ -169,17 +206,17 @@ func resourceCloudProjectDatabaseM3dbNamespaceCreate(ctx context.Context, d *sch
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)
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(clusterID),
url.PathEscape(id),
)
res := &CloudProjectDatabaseM3dbNamespaceResponse{}

log.Printf("[DEBUG] Will read namespace %s from cluster %s from project %s", id, clusterId, serviceName)
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))
}
Expand All @@ -198,24 +235,24 @@ func resourceCloudProjectDatabaseM3dbNamespaceRead(ctx context.Context, d *schem
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)
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(clusterID),
url.PathEscape(id),
)
params := (&CloudProjectDatabaseM3dbNamespaceUpdateOpts{}).FromResource(d)

log.Printf("[DEBUG] Will update namespace: %+v from cluster %s from project %s", params, clusterId, serviceName)
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))
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())
}
Expand All @@ -225,25 +262,31 @@ func resourceCloudProjectDatabaseM3dbNamespaceUpdate(ctx context.Context, d *sch
}

func resourceCloudProjectDatabaseM3dbNamespaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
name := d.Get("name").(string)
if name == "default" {
d.SetId("")
return nil
}

config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(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(clusterID),
url.PathEscape(id),
)

log.Printf("[DEBUG] Will delete namespace %s from cluster %s from project %s", id, clusterId, serviceName)
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))
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())
}
Expand Down
16 changes: 8 additions & 8 deletions ovh/resource_cloud_project_database_m3db_namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ resource "ovh_cloud_project_database_m3db_namespace" "namespace" {
cluster_id = ovh_cloud_project_database.db.id
name = "%s"
resolution = "%s"
retention_period_duration = "%s"
}
`

Expand All @@ -38,14 +37,10 @@ func TestAccCloudProjectDatabaseM3dbNamespace_basic(t *testing.T) {
version = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_VERSION_TEST")
}
region := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_REGION_TEST")
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_M3DB_FLAVOR_TEST")
if flavor == "" {
flavor = os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
}
flavor := os.Getenv("OVH_CLOUD_PROJECT_DATABASE_FLAVOR_TEST")
description := acctest.RandomWithPrefix(test_prefix)
name := "mynamespace"
resolution := "P2D"
periodDuration := "P2D"

config := fmt.Sprintf(
testAccCloudProjectDatabaseM3dbNamespaceConfig_basic,
Expand All @@ -56,7 +51,6 @@ func TestAccCloudProjectDatabaseM3dbNamespace_basic(t *testing.T) {
flavor,
name,
resolution,
periodDuration,
)

resource.Test(t, resource.TestCase{
Expand All @@ -71,9 +65,15 @@ func TestAccCloudProjectDatabaseM3dbNamespace_basic(t *testing.T) {
resource.TestCheckResourceAttr(
"ovh_cloud_project_database_m3db_namespace.namespace", "resolution", resolution),
resource.TestCheckResourceAttr(
"ovh_cloud_project_database_m3db_namespace.namespace", "retention_period_duration", periodDuration),
"ovh_cloud_project_database_m3db_namespace.namespace", "retention_block_size_duration", resolution),
resource.TestCheckResourceAttr(
"ovh_cloud_project_database_m3db_namespace.namespace", "retention_period_duration", "P2D"),
resource.TestCheckResourceAttr(
"ovh_cloud_project_database_m3db_namespace.namespace", "snapshot_enabled", "true"),
resource.TestCheckResourceAttr(
"ovh_cloud_project_database_m3db_namespace.namespace", "type", "aggregated"),
resource.TestCheckResourceAttr(
"ovh_cloud_project_database_m3db_namespace.namespace", "writes_to_commit_log_enabled", "true"),
),
},
},
Expand Down
Loading