Skip to content

Commit 7213e9c

Browse files
committed
review: move to cluster config block
1 parent 14eb594 commit 7213e9c

File tree

5 files changed

+55
-77
lines changed

5 files changed

+55
-77
lines changed

docs/resources/datastore.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@ Manages a Dragonfly datastore resource.
2323

2424
### Optional
2525

26-
- `cluster` (Boolean) Enable dragonfly swarm cluster.
26+
- `cluster` (Attributes) The cluster configuration for the datastore. (see [below for nested schema](#nestedatt--cluster))
2727
- `disable_pass_key` (Boolean) Disable the passkey for the datastore.
2828
- `dragonfly` (Attributes) Dragonfly-specific configuration. (see [below for nested schema](#nestedatt--dragonfly))
2929
- `maintenance_window` (Attributes) The maintenance window configuration for the datastore. (see [below for nested schema](#nestedatt--maintenance_window))
3030
- `network_id` (String) The ID of the network the datastore should be placed into.
31-
- `shard_memory` (Number) The maximum individual shard memory within a cluster.
3231

3332
### Read-Only
3433

@@ -63,6 +62,14 @@ Optional:
6362
- `replicas` (Number) The number of replicas for the datastore. Default is 0.
6463

6564

65+
<a id="nestedatt--cluster"></a>
66+
### Nested Schema for `cluster`
67+
68+
Optional:
69+
70+
- `shard_memory` (Number) The cluster shard memory.
71+
72+
6673
<a id="nestedatt--dragonfly"></a>
6774
### Nested Schema for `dragonfly`
6875

examples/cluster/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ resource "dfcloud_datastore" "cache-cluster" {
2323
replicas = 1
2424
}
2525

26-
cluster = true
27-
28-
shard_memory = 3000000000
26+
cluster = {
27+
shard_memory = 3000000000
28+
}
2929

3030
dragonfly = {
3131
cache_mode = true

internal/provider/datastore.go

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,54 +8,16 @@ import (
88

99
"github.com/dragonflydb/terraform-provider-dfcloud/internal/resource_model"
1010
dfcloud "github.com/dragonflydb/terraform-provider-dfcloud/internal/sdk"
11-
"github.com/hashicorp/terraform-plugin-framework/path"
1211
"github.com/hashicorp/terraform-plugin-framework/resource"
1312
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1413
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
14+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
1515
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1616
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
17-
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1817
"github.com/hashicorp/terraform-plugin-framework/types"
1918
"github.com/hashicorp/terraform-plugin-log/tflog"
2019
)
2120

22-
type requireClusterForShardMemoryValidator struct{}
23-
24-
func (v requireClusterForShardMemoryValidator) Description(ctx context.Context) string {
25-
return "shard_memory can only be set if cluster is true"
26-
}
27-
28-
func (v requireClusterForShardMemoryValidator) MarkdownDescription(ctx context.Context) string {
29-
return v.Description(ctx)
30-
}
31-
32-
func (v requireClusterForShardMemoryValidator) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) {
33-
// If the value is null or unknown, there is nothing to validate.
34-
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() {
35-
return
36-
}
37-
38-
// Get the value of the 'cluster' attribute.
39-
var cluster types.Bool
40-
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, path.Root("cluster"), &cluster)...)
41-
if resp.Diagnostics.HasError() {
42-
return
43-
}
44-
45-
// If 'cluster' is not enabled, 'shard_memory' cannot be set.
46-
if !cluster.ValueBool() {
47-
resp.Diagnostics.AddAttributeError(
48-
req.Path,
49-
"Invalid Attribute Configuration",
50-
"'shard_memory' can only be set when 'cluster' is enabled.",
51-
)
52-
}
53-
}
54-
55-
func requireClusterForShardMemory() validator.Int64 {
56-
return requireClusterForShardMemoryValidator{}
57-
}
58-
5921
// NewDatastoreResource is a helper function to simplify the provider implementation.
6022
func NewDatastoreResource() resource.Resource {
6123
return &datastoreResource{}
@@ -107,6 +69,21 @@ func (r *datastoreResource) Schema(_ context.Context, _ resource.SchemaRequest,
10769
MarkdownDescription: "The name of the datastore.",
10870
Required: true,
10971
},
72+
"cluster": schema.SingleNestedAttribute{
73+
MarkdownDescription: "The cluster configuration for the datastore.",
74+
Optional: true,
75+
Computed: true,
76+
PlanModifiers: []planmodifier.Object{
77+
objectplanmodifier.RequiresReplaceIfConfigured(),
78+
},
79+
Attributes: map[string]schema.Attribute{
80+
"shard_memory": schema.Int64Attribute{
81+
MarkdownDescription: "The cluster shard memory.",
82+
Optional: true,
83+
Computed: true,
84+
},
85+
},
86+
},
11087
"location": schema.SingleNestedAttribute{
11188
MarkdownDescription: "The location configuration for the datastore.",
11289
Required: true,
@@ -152,20 +129,6 @@ func (r *datastoreResource) Schema(_ context.Context, _ resource.SchemaRequest,
152129
},
153130
},
154131
},
155-
"cluster": schema.BoolAttribute{
156-
MarkdownDescription: "Enable dragonfly swarm cluster.",
157-
Optional: true,
158-
PlanModifiers: []planmodifier.Bool{
159-
boolplanmodifier.RequiresReplace(),
160-
},
161-
},
162-
"shard_memory": schema.Int64Attribute{
163-
MarkdownDescription: "The maximum individual shard memory within a cluster.",
164-
Optional: true,
165-
Validators: []validator.Int64{
166-
requireClusterForShardMemory(),
167-
},
168-
},
169132
"network_id": schema.StringAttribute{
170133
MarkdownDescription: "The ID of the network the datastore should be placed into.",
171134
Optional: true,

internal/provider/datastore_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ func TestAcc_DatastoreResource_withCluster(t *testing.T) {
110110
resource.TestCheckResourceAttr("dfcloud_datastore.test", "location.availability_zones.#", "1"),
111111
resource.TestCheckResourceAttr("dfcloud_datastore.test", "location.availability_zones.0", "euw1-az2"),
112112
resource.TestCheckResourceAttr("dfcloud_datastore.test", "tier.performance_tier", "dev"),
113-
resource.TestCheckResourceAttr("dfcloud_datastore.test", "cluster", "true"),
114-
resource.TestCheckResourceAttr("dfcloud_datastore.test", "shard_memory", "3000000000"),
113+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "cluster.shard_memory", "3000000000"),
115114
resource.TestCheckResourceAttr("dfcloud_datastore.test", "tier.max_memory_bytes", "6000000000"),
116115
resource.TestCheckResourceAttr("dfcloud_datastore.test", "tier.replicas", "1"),
117116
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.cache_mode", "false"),
@@ -137,9 +136,9 @@ func testAccDatastoreClusterResourceConfig(name string) string {
137136
resource "dfcloud_datastore" "test" {
138137
name = %[1]q
139138
140-
cluster = true
141-
142-
shard_memory = "3000000000"
139+
cluster = {
140+
shard_memory = 3000000000
141+
}
143142
144143
location = {
145144
provider = "aws"

internal/resource_model/datastore.go

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ type Datastore struct {
1717
NetworkId types.String `tfsdk:"network_id"`
1818
Location DatastoreLocation `tfsdk:"location"`
1919
Tier DatastoreTier `tfsdk:"tier"`
20-
Cluster types.Bool `tfsdk:"cluster"`
21-
ShardMemory types.Int64 `tfsdk:"shard_memory"`
20+
Cluster types.Object `tfsdk:"cluster"`
2221
Dragonfly types.Object `tfsdk:"dragonfly"`
2322
CreatedAt types.Int64 `tfsdk:"created_at"`
2423
Password types.String `tfsdk:"password"`
@@ -27,6 +26,10 @@ type Datastore struct {
2726
MaintenanceWindow types.Object `tfsdk:"maintenance_window"`
2827
}
2928

29+
type DatastoreClusterConfig struct {
30+
ShardMemory types.Int64 `tfsdk:"shard_memory"`
31+
}
32+
3033
type DatastoreLocation struct {
3134
Provider types.String `tfsdk:"provider"`
3235
Region types.String `tfsdk:"region"`
@@ -44,15 +47,18 @@ func (d *Datastore) FromConfig(ctx context.Context, in *dfcloud.Datastore) {
4447
d.Name = types.StringValue(in.Config.Name)
4548
d.NetworkId = types.StringNull()
4649
d.Tier.Replicas = types.Int64Null()
47-
if cluster := in.Config.Cluster.Enabled; cluster != nil && *cluster {
48-
d.Cluster = types.BoolValue(*in.Config.Cluster.Enabled)
49-
} else {
50-
d.Cluster = types.BoolNull()
51-
}
52-
if shardMemory := in.Config.Cluster.ShardMemory; shardMemory != nil && *shardMemory != 0 {
53-
d.ShardMemory = types.Int64Value(*shardMemory)
50+
if in.Config.Cluster.Enabled != nil && *in.Config.Cluster.Enabled {
51+
if shardMemory := in.Config.Cluster.ShardMemory; shardMemory != nil {
52+
d.Cluster = types.ObjectValueMust(map[string]attr.Type{
53+
"shard_memory": types.Int64Type,
54+
}, map[string]attr.Value{
55+
"shard_memory": types.Int64PointerValue(in.Config.Cluster.ShardMemory),
56+
})
57+
}
5458
} else {
55-
d.ShardMemory = types.Int64Null()
59+
d.Cluster = types.ObjectNull(map[string]attr.Type{
60+
"shard_memory": types.Int64Type,
61+
})
5662
}
5763
d.CreatedAt = types.Int64Value(in.CreatedAt)
5864
d.Location.Provider = types.StringValue(string(in.Config.Location.Provider))
@@ -116,10 +122,6 @@ func IntoDatastoreConfig(in Datastore) *dfcloud.Datastore {
116122
Provider: dfcloud.CloudProvider(in.Location.Provider.ValueString()),
117123
Region: in.Location.Region.ValueString(),
118124
},
119-
Cluster: dfcloud.DatastoreClusterConfig{
120-
Enabled: in.Cluster.ValueBoolPointer(),
121-
ShardMemory: in.ShardMemory.ValueInt64Pointer(),
122-
},
123125
Tier: dfcloud.DatastoreTier{
124126
Memory: uint64(in.Tier.Memory.ValueInt64()),
125127
PerformanceTier: dfcloud.PerformanceTier(in.Tier.PerformanceTier.ValueString()),
@@ -128,6 +130,13 @@ func IntoDatastoreConfig(in Datastore) *dfcloud.Datastore {
128130
},
129131
}
130132

133+
if !in.Cluster.IsUnknown() && !in.Cluster.IsNull() {
134+
enabledCluster := true
135+
if in.Cluster.Attributes()["shard_memory"] != nil {
136+
datastore.Config.Cluster.ShardMemory = lo.ToPtr(*in.Cluster.Attributes()["shard_memory"].(types.Int64).ValueInt64Pointer())
137+
}
138+
datastore.Config.Cluster.Enabled = &enabledCluster
139+
}
131140
_ = in.Location.AvailabilityZones.ElementsAs(context.Background(), &datastore.Config.Location.AvailabilityZones, false)
132141

133142
if !in.NetworkId.IsNull() {

0 commit comments

Comments
 (0)