Skip to content

Commit bb5545f

Browse files
authored
fix(datastore): Fixes passkey compatibility with API (#29)
1 parent a69c915 commit bb5545f

File tree

3 files changed

+87
-4
lines changed

3 files changed

+87
-4
lines changed

internal/provider/datastore.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/resource"
1212
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1313
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
14+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
15+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/listplanmodifier"
16+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
1417
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1518
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1619
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -40,10 +43,16 @@ func (r *datastoreResource) Schema(_ context.Context, _ resource.SchemaRequest,
4043
"id": schema.StringAttribute{
4144
MarkdownDescription: "The ID of the datastore.",
4245
Computed: true,
46+
PlanModifiers: []planmodifier.String{
47+
stringplanmodifier.UseStateForUnknown(),
48+
},
4349
},
4450
"created_at": schema.Int64Attribute{
4551
MarkdownDescription: "The timestamp when the datastore was created.",
4652
Computed: true,
53+
PlanModifiers: []planmodifier.Int64{
54+
int64planmodifier.UseStateForUnknown(),
55+
},
4756
},
4857
"disable_pass_key": schema.BoolAttribute{
4958
MarkdownDescription: "Disable the passkey for the datastore.",
@@ -59,10 +68,16 @@ func (r *datastoreResource) Schema(_ context.Context, _ resource.SchemaRequest,
5968
Required: false,
6069
Computed: true,
6170
Sensitive: true,
71+
PlanModifiers: []planmodifier.String{
72+
stringplanmodifier.UseStateForUnknown(),
73+
},
6274
},
6375
"addr": schema.StringAttribute{
6476
MarkdownDescription: "The address of the datastore.",
6577
Computed: true,
78+
PlanModifiers: []planmodifier.String{
79+
stringplanmodifier.UseStateForUnknown(),
80+
},
6681
},
6782
"name": schema.StringAttribute{
6883
MarkdownDescription: "The name of the datastore.",
@@ -134,38 +149,59 @@ func (r *datastoreResource) Schema(_ context.Context, _ resource.SchemaRequest,
134149
MarkdownDescription: "Dragonfly-specific configuration.",
135150
Optional: true,
136151
Computed: true,
152+
PlanModifiers: []planmodifier.Object{
153+
objectplanmodifier.UseStateForUnknown(),
154+
},
137155
Attributes: map[string]schema.Attribute{
138156
"cache_mode": schema.BoolAttribute{
139157
MarkdownDescription: "Enable cache mode for memory management.",
140158
Optional: true,
141159
Computed: true,
160+
PlanModifiers: []planmodifier.Bool{
161+
boolplanmodifier.UseStateForUnknown(),
162+
},
142163
},
143164
"bullmq": schema.BoolAttribute{
144165
MarkdownDescription: "Enable BullMQ compatibility.",
145166
Optional: true,
146167
Computed: true,
168+
PlanModifiers: []planmodifier.Bool{
169+
boolplanmodifier.UseStateForUnknown(),
170+
},
147171
},
148172
"tls": schema.BoolAttribute{
149173
MarkdownDescription: "Enable TLS.",
150174
Optional: true,
151175
Computed: true,
176+
PlanModifiers: []planmodifier.Bool{
177+
boolplanmodifier.UseStateForUnknown(),
178+
},
152179
},
153180
"sidekiq": schema.BoolAttribute{
154181
MarkdownDescription: "Enable Sidekiq compatibility.",
155182
Optional: true,
156183
Computed: true,
184+
PlanModifiers: []planmodifier.Bool{
185+
boolplanmodifier.UseStateForUnknown(),
186+
},
157187
},
158188
"memcached": schema.BoolAttribute{
159189
MarkdownDescription: "Enable Memcached protocol.",
160190
Optional: true,
161191
Computed: true,
192+
PlanModifiers: []planmodifier.Bool{
193+
boolplanmodifier.UseStateForUnknown(),
194+
},
162195
},
163196
"acl_rules": schema.ListAttribute{
164197
MarkdownDescription: "List of ACL rules.",
165198
ElementType: types.StringType,
166199
Optional: true,
167200
Computed: true,
168201
Sensitive: true,
202+
PlanModifiers: []planmodifier.List{
203+
listplanmodifier.UseStateForUnknown(),
204+
},
169205
},
170206
},
171207
},

internal/provider/datastore_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package provider
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67
"testing"
78

89
dfcloud "github.com/dragonflydb/terraform-provider-dfcloud/internal/sdk"
@@ -75,6 +76,8 @@ func TestAcc_DatastoreResource(t *testing.T) {
7576
resource.TestCheckResourceAttr("dfcloud_datastore.test", "tier.replicas", "1"),
7677
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.cache_mode", "false"),
7778
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.tls", "false"),
79+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.acl_rules.#", "1"),
80+
resource.TestMatchResourceAttr("dfcloud_datastore.test", "dragonfly.acl_rules.0", regexp.MustCompile(`USER default ON \>([a-zA-Z0-9]+) ~\* &\* \+@ALL`)),
7881
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "id"),
7982
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "addr"),
8083
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "created_at"),
@@ -87,6 +90,28 @@ func TestAcc_DatastoreResource(t *testing.T) {
8790
ImportState: true,
8891
ImportStateVerify: true,
8992
},
93+
{
94+
Config: testAccDatastoreResourceConfigUpdated(name),
95+
Check: resource.ComposeAggregateTestCheckFunc(
96+
testCheckDatastoreExists("dfcloud_datastore.test"),
97+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "name", name),
98+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "location.provider", "aws"),
99+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "location.region", "eu-west-1"),
100+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "location.availability_zones.#", "1"),
101+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "location.availability_zones.0", "euw1-az2"),
102+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "tier.performance_tier", "dev"),
103+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "tier.max_memory_bytes", "3000000000"),
104+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "tier.replicas", "0"),
105+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.cache_mode", "false"),
106+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.tls", "false"),
107+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.acl_rules.#", "1"),
108+
resource.TestMatchResourceAttr("dfcloud_datastore.test", "dragonfly.acl_rules.0", regexp.MustCompile(`USER default ON \>([a-zA-Z0-9]+) ~\* &\* \+@ALL`)),
109+
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "id"),
110+
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "addr"),
111+
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "created_at"),
112+
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "password"),
113+
),
114+
},
90115
},
91116
})
92117
}
@@ -115,6 +140,9 @@ func TestAcc_DatastoreResource_withCluster(t *testing.T) {
115140
resource.TestCheckResourceAttr("dfcloud_datastore.test", "tier.replicas", "1"),
116141
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.cache_mode", "false"),
117142
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.tls", "false"),
143+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "dragonfly.acl_rules.#", "1"),
144+
resource.TestMatchResourceAttr("dfcloud_datastore.test", "dragonfly.acl_rules.0", regexp.MustCompile(`USER default ON \>([a-zA-Z0-9]+) ~\* &\* \+@ALL`)),
145+
resource.TestCheckResourceAttr("dfcloud_datastore.test", "location.availability_zones.0", "euw1-az2"),
118146
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "id"),
119147
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "addr"),
120148
resource.TestCheckResourceAttrSet("dfcloud_datastore.test", "created_at"),
@@ -174,3 +202,23 @@ resource "dfcloud_datastore" "test" {
174202
}
175203
`, name)
176204
}
205+
206+
func testAccDatastoreResourceConfigUpdated(name string) string {
207+
return fmt.Sprintf(`
208+
resource "dfcloud_datastore" "test" {
209+
name = %[1]q
210+
211+
location = {
212+
provider = "aws"
213+
region = "eu-west-1"
214+
availability_zones = ["euw1-az2"]
215+
}
216+
217+
tier = {
218+
max_memory_bytes = 3000000000 # 3GB
219+
performance_tier = "dev"
220+
replicas = 0
221+
}
222+
}
223+
`, name)
224+
}

internal/resource_model/datastore.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ func (d *Datastore) FromConfig(ctx context.Context, in *dfcloud.Datastore) {
9494
}
9595

9696
aclRules, _ := types.ListValueFrom(ctx, types.StringType, in.Config.Dragonfly.AclRules)
97+
9798
d.Dragonfly = types.ObjectValueMust(map[string]attr.Type{
9899
"cache_mode": types.BoolType,
99100
"tls": types.BoolType,
@@ -144,7 +145,7 @@ func IntoDatastoreConfig(in Datastore) *dfcloud.Datastore {
144145
datastore.Config.NetworkID = in.NetworkId.ValueString()
145146
}
146147

147-
if in.DisablePassKey.ValueBool() {
148+
if in.DisablePassKey.ValueBool() && in.Password.IsUnknown() {
148149
datastore.Config.DisablePasskey = in.DisablePassKey.ValueBool()
149150
}
150151

@@ -177,9 +178,7 @@ func IntoDatastoreConfig(in Datastore) *dfcloud.Datastore {
177178

178179
if in.Dragonfly.Attributes()["acl_rules"] != nil {
179180
var rules dfcloud.AclRuleArray
180-
for _, rule := range in.Dragonfly.Attributes()["acl_rules"].(types.List).Elements() {
181-
rules = append(rules, rule.String())
182-
}
181+
in.Dragonfly.Attributes()["acl_rules"].(types.List).ElementsAs(context.Background(), &rules, false)
183182
datastore.Config.Dragonfly.AclRules = &rules
184183
}
185184

0 commit comments

Comments
 (0)