Skip to content

Commit 3c01f4c

Browse files
authored
Merge pull request #146 from ploubser/issue_145
(#145) Add limit_marker_ttl field to kv resource
2 parents 4e582ee + aac3151 commit 3c01f4c

File tree

7 files changed

+71
-44
lines changed

7 files changed

+71
-44
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ resource "jetstream_stream" "TRANSFORM" {
170170
* `allow_direct` - (optional) Allow higher performance, direct access to get individual messages via the $JS.DS.GET API (bool)
171171
* `subject_transform` - (optional) A map of source and destination subjects to transform.
172172
* `allow_msg_ttl` - (optional) Enables Per Message TTLs
173-
* `subject_delete_marker_ttl` - (optional) Enables placing markers when Max Age removes messages, duration specified in seconds (number)
173+
* `subject_delete_marker_ttl` - (optional) Enables placing markers when Max Age removes messages, duration specified in seconds. This field requires `allow_rollup_hdrs` to be set to true. (number)
174174

175175
## jetstream_consumer
176176

@@ -259,6 +259,7 @@ resource "jetstream_kv_bucket" "test" {
259259
* `max_value_size` - (optional) Maximum size of any value
260260
* `max_bucket_size` - (optional) The maximum size of all data in the bucket
261261
* `replicas` - (optional) How many replicas to keep on a JetStream cluster
262+
* `limit_marker_ttl` - (optional) Enables Per-Key TTLs and Limit Markers, specified in seconds
262263

263264
## jetstream_kv_entry
264265

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ go 1.23.6
55
require (
66
github.com/google/go-cmp v0.7.0
77
github.com/hashicorp/go-cty v1.5.0
8-
github.com/nats-io/jsm.go v0.2.1
9-
github.com/nats-io/jwt/v2 v2.7.3
10-
github.com/nats-io/nats-server/v2 v2.11.1
11-
github.com/nats-io/nats.go v1.41.0
8+
github.com/nats-io/jsm.go v0.2.3
9+
github.com/nats-io/jwt/v2 v2.7.4
10+
github.com/nats-io/nats-server/v2 v2.11.3
11+
github.com/nats-io/nats.go v1.42.0
1212
github.com/xeipuuv/gojsonschema v1.2.0
1313
)
1414

@@ -21,7 +21,7 @@ require (
2121
github.com/expr-lang/expr v1.17.2 // indirect
2222
github.com/fatih/color v1.18.0 // indirect
2323
github.com/golang/protobuf v1.5.4 // indirect
24-
github.com/google/go-tpm v0.9.3 // indirect
24+
github.com/google/go-tpm v0.9.4 // indirect
2525
github.com/hashicorp/errwrap v1.1.0 // indirect
2626
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
2727
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
@@ -51,7 +51,7 @@ require (
5151
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
5252
github.com/mitchellh/mapstructure v1.5.0 // indirect
5353
github.com/mitchellh/reflectwalk v1.0.2 // indirect
54-
github.com/nats-io/nkeys v0.4.10 // indirect
54+
github.com/nats-io/nkeys v0.4.11 // indirect
5555
github.com/nats-io/nuid v1.0.1 // indirect
5656
github.com/oklog/run v1.1.0 // indirect
5757
github.com/stretchr/testify v1.9.0 // indirect

go.sum

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
5252
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5353
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
5454
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
55-
github.com/google/go-tpm v0.9.3 h1:+yx0/anQuGzi+ssRqeD6WpXjW2L/V0dItUayO0i9sRc=
56-
github.com/google/go-tpm v0.9.3/go.mod h1:h9jEsEECg7gtLis0upRBQU+GhYVH6jMjrFxI8u6bVUY=
55+
github.com/google/go-tpm v0.9.4 h1:awZRf9FwOeTunQmHoDYSHJps3ie6f1UlhS1fOdPEt1I=
56+
github.com/google/go-tpm v0.9.4/go.mod h1:h9jEsEECg7gtLis0upRBQU+GhYVH6jMjrFxI8u6bVUY=
5757
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
5858
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5959
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
@@ -135,16 +135,16 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
135135
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
136136
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
137137
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
138-
github.com/nats-io/jsm.go v0.2.1 h1:YEulg5SXlsHSo4+b23tSRHzYb+R1Xh0hv84B2UUV3GA=
139-
github.com/nats-io/jsm.go v0.2.1/go.mod h1:hKs9syzIPRvAuYVjoMi1tonUd29FpO+PZppqEvaeofo=
140-
github.com/nats-io/jwt/v2 v2.7.3 h1:6bNPK+FXgBeAqdj4cYQ0F8ViHRbi7woQLq4W29nUAzE=
141-
github.com/nats-io/jwt/v2 v2.7.3/go.mod h1:GvkcbHhKquj3pkioy5put1wvPxs78UlZ7D/pY+BgZk4=
142-
github.com/nats-io/nats-server/v2 v2.11.1 h1:LwdauqMqMNhTxTN3+WFTX6wGDOKntHljgZ+7gL5HCnk=
143-
github.com/nats-io/nats-server/v2 v2.11.1/go.mod h1:leXySghbdtXSUmWem8K9McnJ6xbJOb0t9+NQ5HTRZjI=
144-
github.com/nats-io/nats.go v1.41.0 h1:PzxEva7fflkd+n87OtQTXqCTyLfIIMFJBpyccHLE2Ko=
145-
github.com/nats-io/nats.go v1.41.0/go.mod h1:wV73x0FSI/orHPSYoyMeJB+KajMDoWyXmFaRrrYaaTo=
146-
github.com/nats-io/nkeys v0.4.10 h1:glmRrpCmYLHByYcePvnTBEAwawwapjCPMjy2huw20wc=
147-
github.com/nats-io/nkeys v0.4.10/go.mod h1:OjRrnIKnWBFl+s4YK5ChQfvHP2fxqZexrKJoVVyWB3U=
138+
github.com/nats-io/jsm.go v0.2.3 h1:TmdS5JJaccBy/qpa5tXJa9sMOG4S8fYjWFAh4jolstE=
139+
github.com/nats-io/jsm.go v0.2.3/go.mod h1:wODCssHzwZdsHGql7cj46sH8RD0hbGhbAW1XvUyMi+k=
140+
github.com/nats-io/jwt/v2 v2.7.4 h1:jXFuDDxs/GQjGDZGhNgH4tXzSUK6WQi2rsj4xmsNOtI=
141+
github.com/nats-io/jwt/v2 v2.7.4/go.mod h1:me11pOkwObtcBNR8AiMrUbtVOUGkqYjMQZ6jnSdVUIA=
142+
github.com/nats-io/nats-server/v2 v2.11.3 h1:AbGtXxuwjo0gBroLGGr/dE0vf24kTKdRnBq/3z/Fdoc=
143+
github.com/nats-io/nats-server/v2 v2.11.3/go.mod h1:6Z6Fd+JgckqzKig7DYwhgrE7bJ6fypPHnGPND+DqgMY=
144+
github.com/nats-io/nats.go v1.42.0 h1:ynIMupIOvf/ZWH/b2qda6WGKGNSjwOUutTpWRvAmhaM=
145+
github.com/nats-io/nats.go v1.42.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g=
146+
github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0=
147+
github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE=
148148
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
149149
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
150150
github.com/oklog/run v1.1.0 h1:GEenZ1cK0+q0+wsJew9qUg/DyD8k3JzYsZAi5gYi2mA=

jetstream/resource_jetstream_stream.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ func resourceStream() *schema.Resource {
330330
},
331331
"subject_delete_marker_ttl": {
332332
Type: schema.TypeInt,
333-
Description: "When placing a marker, how long should it be valid, duration specified in seconds",
333+
Description: "When placing a marker, how long should it be valid, duration specified in seconds. allow_rollup_hdrs must be set to true when using this field.",
334334
ForceNew: false,
335335
Optional: true,
336336
},

jetstream/resource_jetstream_stream_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ resource "jetstream_stream" "test" {
3737
compression = "s2"
3838
allow_msg_ttl = true
3939
subject_delete_marker_ttl = 24*60*60
40+
allow_rollup_hdrs = true
4041
metadata = {
4142
foo = "bar"
4243
}

jetstream/resource_kv_bucket.go

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@
1414
package jetstream
1515

1616
import (
17+
"context"
1718
"fmt"
1819
"time"
1920

2021
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2122
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2223
"github.com/nats-io/jsm.go"
2324
"github.com/nats-io/nats.go"
25+
"github.com/nats-io/nats.go/jetstream"
2426
)
2527

2628
func resourceKVBucket() *schema.Resource {
@@ -100,6 +102,14 @@ func resourceKVBucket() *schema.Resource {
100102
ForceNew: true,
101103
ValidateFunc: validation.All(validation.IntAtLeast(1), validation.IntAtMost(5)),
102104
},
105+
"limit_marker_ttl": {
106+
Type: schema.TypeInt,
107+
Description: "Enables Per-Key TTLs and Limit Markers, duration specified in seconds",
108+
Optional: true,
109+
ForceNew: false,
110+
Default: 0,
111+
ValidateFunc: validation.IntAtLeast(0),
112+
},
103113
},
104114
}
105115
}
@@ -118,11 +128,12 @@ func resourceKVBucketCreate(d *schema.ResourceData, m any) error {
118128
maxB := d.Get("max_bucket_size").(int)
119129
replicas := d.Get("replicas").(int)
120130
descrption := d.Get("description").(string)
131+
limit_marker_ttl := d.Get("limit_marker_ttl").(int)
121132

122-
var placement *nats.Placement
133+
var placement *jetstream.Placement
123134
c, ok := d.GetOk("placement_cluster")
124135
if ok {
125-
placement = &nats.Placement{Cluster: c.(string)}
136+
placement = &jetstream.Placement{Cluster: c.(string)}
126137
pt, ok := d.GetOk("placement_tags")
127138
if ok {
128139
ts := pt.([]any)
@@ -142,25 +153,26 @@ func resourceKVBucketCreate(d *schema.ResourceData, m any) error {
142153
return fmt.Errorf("bucket %s already exist", name)
143154
}
144155

145-
js, err := nc.JetStream()
156+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
157+
defer cancel()
158+
159+
js, err := jetstream.New(nc)
146160
if err != nil {
147161
return err
148162
}
149163

150-
_, err = js.CreateKeyValue(&nats.KeyValueConfig{
151-
Bucket: name,
152-
Description: descrption,
153-
MaxValueSize: int32(maxV),
154-
History: uint8(history),
155-
TTL: time.Duration(ttl) * time.Second,
156-
MaxBytes: int64(maxB),
157-
Storage: nats.FileStorage,
158-
Replicas: replicas,
159-
Placement: placement,
164+
js.CreateKeyValue(ctx, jetstream.KeyValueConfig{
165+
Bucket: name,
166+
Description: descrption,
167+
MaxValueSize: int32(maxV),
168+
History: uint8(history),
169+
TTL: time.Duration(ttl) * time.Second,
170+
MaxBytes: int64(maxB),
171+
Storage: jetstream.FileStorage,
172+
Replicas: replicas,
173+
Placement: placement,
174+
LimitMarkerTTL: time.Duration(limit_marker_ttl) * time.Second,
160175
})
161-
if err != nil {
162-
return err
163-
}
164176

165177
d.SetId(fmt.Sprintf("JETSTREAM_KV_%s", name))
166178

@@ -179,19 +191,23 @@ func resourceKVBucketRead(d *schema.ResourceData, m any) error {
179191
}
180192
defer nc.Close()
181193

182-
js, err := nc.JetStream()
194+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
195+
defer cancel()
196+
197+
js, err := jetstream.New(nc)
183198
if err != nil {
184199
return err
185200
}
186-
bucket, err := js.KeyValue(name)
201+
202+
bucket, err := js.KeyValue(ctx, name)
187203
if err != nil {
188204
if err == nats.ErrBucketNotFound {
189205
d.SetId("")
190206
return nil
191207
}
192208
return err
193209
}
194-
status, err := bucket.Status()
210+
status, err := bucket.Status(ctx)
195211
if err != nil {
196212
return err
197213
}
@@ -200,7 +216,7 @@ func resourceKVBucketRead(d *schema.ResourceData, m any) error {
200216
d.Set("history", status.History())
201217
d.Set("ttl", status.TTL().Seconds())
202218

203-
jStatus := status.(*nats.KeyValueBucketStatus)
219+
jStatus := status.(*jetstream.KeyValueBucketStatus)
204220
si := jStatus.StreamInfo()
205221

206222
d.Set("max_value_size", si.Config.MaxMsgSize)
@@ -213,6 +229,8 @@ func resourceKVBucketRead(d *schema.ResourceData, m any) error {
213229
d.Set("placement_tags", si.Config.Placement.Tags)
214230
}
215231

232+
d.Set("limit_marker_ttl", si.Config.SubjectDeleteMarkerTTL.Seconds())
233+
216234
return nil
217235
}
218236

@@ -225,19 +243,22 @@ func resourceKVBucketUpdate(d *schema.ResourceData, m any) error {
225243
}
226244
defer nc.Close()
227245

228-
js, err := nc.JetStream()
246+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
247+
defer cancel()
248+
249+
js, err := jetstream.New(nc)
229250
if err != nil {
230251
return err
231252
}
232-
bucket, err := js.KeyValue(name)
253+
bucket, err := js.KeyValue(ctx, name)
233254
if err != nil {
234255
return err
235256
}
236-
status, err := bucket.Status()
257+
status, err := bucket.Status(ctx)
237258
if err != nil {
238259
return err
239260
}
240-
jStatus := status.(*nats.KeyValueBucketStatus)
261+
jStatus := status.(*jetstream.KeyValueBucketStatus)
241262

242263
str, err := mgr.LoadStream(jStatus.StreamInfo().Config.Name)
243264
if err != nil {
@@ -249,13 +270,15 @@ func resourceKVBucketUpdate(d *schema.ResourceData, m any) error {
249270
maxV := d.Get("max_value_size").(int)
250271
maxB := d.Get("max_bucket_size").(int)
251272
description := d.Get("description").(string)
273+
markerTTL := d.Get("limit_marker_ttl").(int)
252274

253275
cfg := str.Configuration()
254276
cfg.MaxAge = time.Duration(ttl) * time.Second
255277
cfg.MaxMsgSize = int32(maxV)
256278
cfg.MaxBytes = int64(maxB)
257279
cfg.MaxMsgsPer = int64(history)
258280
cfg.Description = description
281+
cfg.SubjectDeleteMarkerTTL = time.Duration(markerTTL) * time.Second
259282

260283
err = str.UpdateConfiguration(cfg)
261284
if err != nil {

jetstream/resource_kv_bucket_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ resource "jetstream_kv_bucket" "test" {
3434
history = 10
3535
max_value_size = 1024
3636
max_bucket_size = 10240
37+
limit_marker_ttl = 45
3738
}
3839
`
3940

@@ -65,6 +66,7 @@ func TestResourceKV(t *testing.T) {
6566
resource.TestCheckResourceAttr("jetstream_kv_bucket.test", "history", "10"),
6667
resource.TestCheckResourceAttr("jetstream_kv_bucket.test", "max_value_size", "1024"),
6768
resource.TestCheckResourceAttr("jetstream_kv_bucket.test", "max_bucket_size", "10240"),
69+
resource.TestCheckResourceAttr("jetstream_kv_bucket.test", "limit_marker_ttl", "45"),
6870
),
6971
},
7072
},

0 commit comments

Comments
 (0)