Skip to content

Commit

Permalink
Add kv txn get-not-exists operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
hashi-derek committed Sep 2, 2022
1 parent 25d272a commit d8f0131
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .changelog/14474.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:feature
http: Add new `get-not-exists` operation to the txn api. Refer to the [API docs](https://www.consul.io/api-docs/txn#kv-operations) for more information.
```
2 changes: 1 addition & 1 deletion agent/consul/kvs_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func kvsPreApply(logger hclog.Logger, srv *Server, authz resolver.Result, op api
return false, err
}

case api.KVGet, api.KVGetTree:
case api.KVGet, api.KVGetTree, api.KVGetNotExists:
// Filtering for GETs is done on the output side.

case api.KVCheckSession, api.KVCheckIndex:
Expand Down
8 changes: 7 additions & 1 deletion agent/consul/state/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ func (s *Store) txnKVS(tx WriteTxn, idx uint64, op *structs.TxnKVOp) (structs.Tx
err = fmt.Errorf("key %q doesn't exist", op.DirEnt.Key)
}

case api.KVGetNotExists:
_, entry, err = kvsGetTxn(tx, nil, op.DirEnt.Key, op.DirEnt.EnterpriseMeta)
if entry == nil && err == nil {
entry = &op.DirEnt
}

case api.KVGetTree:
var entries structs.DirEntries
_, entries, err = s.kvsListTxn(tx, nil, op.DirEnt.Key, op.DirEnt.EnterpriseMeta)
Expand Down Expand Up @@ -95,7 +101,7 @@ func (s *Store) txnKVS(tx WriteTxn, idx uint64, op *structs.TxnKVOp) (structs.Tx
// value (we have to clone so we don't modify the entry being used by
// the state store).
if entry != nil {
if op.Verb == api.KVGet {
if op.Verb == api.KVGet || op.Verb == api.KVGetNotExists {
result := structs.TxnResult{KV: entry}
return structs.TxnResults{&result}, nil
}
Expand Down
31 changes: 31 additions & 0 deletions agent/consul/state/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,22 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
&structs.TxnOp{
KV: &structs.TxnKVOp{
Verb: api.KVGetNotExists,
DirEnt: structs.DirEntry{
Key: "foo/update",
},
},
},
&structs.TxnOp{
KV: &structs.TxnKVOp{
Verb: api.KVGetNotExists,
DirEnt: structs.DirEntry{
Key: "foo/not-exists",
},
},
},
&structs.TxnOp{
KV: &structs.TxnKVOp{
Verb: api.KVCheckIndex,
Expand Down Expand Up @@ -702,6 +718,21 @@ func TestStateStore_Txn_KVS(t *testing.T) {
},
},
},
&structs.TxnResult{
KV: &structs.DirEntry{
Key: "foo/update",
Value: []byte("stale"),
RaftIndex: structs.RaftIndex{
CreateIndex: 5,
ModifyIndex: 5,
},
},
},
&structs.TxnResult{
KV: &structs.DirEntry{
Key: "foo/not-exists",
},
},
&structs.TxnResult{
KV: &structs.DirEntry{

Expand Down
1 change: 1 addition & 0 deletions api/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const (
KVLock KVOp = "lock"
KVUnlock KVOp = "unlock"
KVGet KVOp = "get"
KVGetNotExists KVOp = "get-not-exists"
KVGetTree KVOp = "get-tree"
KVCheckSession KVOp = "check-session"
KVCheckIndex KVOp = "check-index"
Expand Down
29 changes: 15 additions & 14 deletions website/content/api-docs/txn.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,21 @@ look like this:
The following tables summarize the available verbs and the fields that apply to
those operations ("X" means a field is required and "O" means it is optional):

| Verb | Operation | Key | Value | Flags | Index | Session |
| ------------------ | --------------------------------------- | :-: | :---: | :---: | :---: | :-----: |
| `set` | Sets the `Key` to the given `Value` | `x` | `x` | `o` | | |
| `cas` | Sets, but with CAS semantics | `x` | `x` | `o` | `x` | |
| `lock` | Lock with the given `Session` | `x` | `x` | `o` | | `x` |
| `unlock` | Unlock with the given `Session` | `x` | `x` | `o` | | `x` |
| `get` | Get the key, fails if it does not exist | `x` | | | | |
| `get-tree` | Gets all keys with the prefix | `x` | | | | |
| `check-index` | Fail if modify index != index | `x` | | | `x` | |
| `check-session` | Fail if not locked by session | `x` | | | | `x` |
| `check-not-exists` | Fail if key exists | `x` | | | | |
| `delete` | Delete the key | `x` | | | | |
| `delete-tree` | Delete all keys with a prefix | `x` | | | | |
| `delete-cas` | Delete, but with CAS semantics | `x` | | | `x` | |
| Verb | Operation | Key | Value | Flags | Index | Session |
| ------------------ | ---------------------------------------- | :-: | :---: | :---: | :---: | :-----: |
| `set` | Sets the `Key` to the given `Value` | `x` | `x` | `o` | | |
| `cas` | Sets, but with CAS semantics | `x` | `x` | `o` | `x` | |
| `lock` | Lock with the given `Session` | `x` | `x` | `o` | | `x` |
| `unlock` | Unlock with the given `Session` | `x` | `x` | `o` | | `x` |
| `get` | Get the key, fails if it does not exist | `x` | | | | |
| `get-not-exists` | Get the key, or nil if it does not exist | `x` | | | | |
| `get-tree` | Gets all keys with the prefix | `x` | | | | |
| `check-index` | Fail if modify index != index | `x` | | | `x` | |
| `check-session` | Fail if not locked by session | `x` | | | | `x` |
| `check-not-exists` | Fail if key exists | `x` | | | | |
| `delete` | Delete the key | `x` | | | | |
| `delete-tree` | Delete all keys with a prefix | `x` | | | | |
| `delete-cas` | Delete, but with CAS semantics | `x` | | | `x` | |

#### Node Operations

Expand Down

0 comments on commit d8f0131

Please sign in to comment.