| 1 | from __future__ import annotations |
| 2 | |
| 3 | from consul.callback import CB |
| 4 | |
| 5 | |
| 6 | class KV: |
| 7 | """ |
| 8 | The KV endpoint is used to expose a simple key/value store. This can be |
| 9 | used to store service configurations or other meta data in a simple |
| 10 | way. |
| 11 | """ |
| 12 | |
| 13 | def __init__(self, agent) -> None: |
| 14 | self.agent = agent |
| 15 | |
| 16 | def get( |
| 17 | self, |
| 18 | key, |
| 19 | index=None, |
| 20 | recurse: bool = False, |
| 21 | wait=None, |
| 22 | token: str | None = None, |
| 23 | consistency=None, |
| 24 | keys: bool = False, |
| 25 | separator=None, |
| 26 | dc=None, |
| 27 | connections_timeout=None, |
| 28 | ): |
| 29 | """ |
| 30 | Returns a tuple of (*index*, *value[s]*) |
| 31 | |
| 32 | *index* is the current Consul index, suitable for making subsequent |
| 33 | calls to wait for changes since this query was last run. |
| 34 | |
| 35 | *wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 36 | a given index. this parameter is only applied if *index* is also |
| 37 | specified. the wait time by default is 5 minutes. |
| 38 | |
| 39 | *token* is an optional `ACL token`_ to apply to this request. |
| 40 | |
| 41 | *keys* is a boolean which, if True, says to return a flat list of |
| 42 | keys without values or other metadata. *separator* can be used |
| 43 | with *keys* to list keys only up to a given separator character. |
| 44 | |
| 45 | *dc* is the optional datacenter that you wish to communicate with. |
| 46 | If None is provided, defaults to the agent's datacenter. |
| 47 | |
| 48 | The *value* returned is for the specified key, or if *recurse* is |
| 49 | True a list of *values* for all keys with the given prefix is |
| 50 | returned. |
| 51 | |
| 52 | Each *value* looks like this:: |
| 53 | |
| 54 | { |
| 55 | "CreateIndex": 100, |
| 56 | "ModifyIndex": 200, |
| 57 | "LockIndex": 200, |
| 58 | "Key": "foo", |
| 59 | "Flags": 0, |
| 60 | "Value": "bar", |
| 61 | "Session": "adf4238a-882b-9ddc-4a9d-5b6758e4159e" |
| 62 | } |
| 63 | |
| 64 | Note, if the requested key does not exists *(index, None)* is |
| 65 | returned. It's then possible to long poll on the index for when the |
| 66 | key is created. |
| 67 | """ |
| 68 | assert not key.startswith("/"), "keys should not start with a forward slash" |
| 69 | params = [] |
| 70 | if index: |
| 71 | params.append(("index", index)) |
| 72 | if wait: |
| 73 | params.append(("wait", wait)) |
| 74 | if recurse: |
| 75 | params.append(("recurse", "1")) |
| 76 | dc = dc or self.agent.dc |
| 77 | if dc: |
| 78 | params.append(("dc", dc)) |
| 79 | if keys: |
| 80 | params.append(("keys", True)) |
| 81 | if separator: |
| 82 | params.append(("separator", separator)) |
| 83 | consistency = consistency or self.agent.consistency |
| 84 | if consistency in ("consistent", "stale"): |
| 85 | params.append((consistency, "1")) |
| 86 | |
| 87 | one = False |
| 88 | decode: bool | str = False |
| 89 | |
| 90 | if not keys: |
| 91 | decode = "Value" |
| 92 | if not recurse and not keys: |
| 93 | one = True |
| 94 | http_kwargs = {} |
| 95 | if connections_timeout: |
| 96 | http_kwargs["connections_timeout"] = connections_timeout |
| 97 | |
| 98 | headers = self.agent.prepare_headers(token) |
| 99 | return self.agent.http.get( |
| 100 | CB.json(index=True, decode=decode, one=one), f"/v1/kv/{key}", params=params, headers=headers, **http_kwargs |
| 101 | ) |
| 102 | |
| 103 | def put( |
| 104 | self, |
| 105 | key, |
| 106 | value, |
| 107 | cas=None, |
| 108 | flags=None, |
| 109 | acquire=None, |
| 110 | release=None, |
| 111 | token: str | None = None, |
| 112 | dc=None, |
| 113 | connections_timeout=None, |
| 114 | ): |
| 115 | """ |
| 116 | Sets *key* to the given *value*. |
| 117 | |
| 118 | *value* can either be None (useful for marking a key as a |
| 119 | directory) or any string type, including binary data (e.g. a |
| 120 | msgpack'd data structure) |
| 121 | |
| 122 | The optional *cas* parameter is used to turn the PUT into a |
| 123 | Check-And-Set operation. This is very useful as it allows clients |
| 124 | to build more complex syncronization primitives on top. If the |
| 125 | index is 0, then Consul will only put the key if it does not |
| 126 | already exist. If the index is non-zero, then the key is only set |
| 127 | if the index matches the ModifyIndex of that key. |
| 128 | |
| 129 | An optional *flags* can be set. This can be used to specify an |
| 130 | unsigned value between 0 and 2^64-1. |
| 131 | |
| 132 | *acquire* is an optional session_id. if supplied a lock acquisition |
| 133 | will be attempted. |
| 134 | |
| 135 | *release* is an optional session_id. if supplied a lock release |
| 136 | will be attempted. |
| 137 | |
| 138 | *token* is an optional `ACL token`_ to apply to this request. If |
| 139 | the token's policy is not allowed to write to this key an |
| 140 | *ACLPermissionDenied* exception will be raised. |
| 141 | |
| 142 | *dc* is the optional datacenter that you wish to communicate with. |
| 143 | If None is provided, defaults to the agent's datacenter. |
| 144 | |
| 145 | The return value is simply either True or False. If False is |
| 146 | returned, then the update has not taken place. |
| 147 | """ |
| 148 | assert not key.startswith("/"), "keys should not start with a forward slash" |
| 149 | assert value is None or isinstance(value, (str, bytes)), "value should be None or a string / binary data" |
| 150 | |
| 151 | params = [] |
| 152 | if cas is not None: |
| 153 | params.append(("cas", cas)) |
| 154 | if flags is not None: |
| 155 | params.append(("flags", flags)) |
| 156 | if acquire: |
| 157 | params.append(("acquire", acquire)) |
| 158 | if release: |
| 159 | params.append(("release", release)) |
| 160 | dc = dc or self.agent.dc |
| 161 | if dc: |
| 162 | params.append(("dc", dc)) |
| 163 | http_kwargs = {} |
| 164 | if connections_timeout: |
| 165 | http_kwargs["connections_timeout"] = connections_timeout |
| 166 | headers = self.agent.prepare_headers(token) |
| 167 | return self.agent.http.put( |
| 168 | CB.json(), f"/v1/kv/{key}", params=params, headers=headers, data=value, **http_kwargs |
| 169 | ) |
| 170 | |
| 171 | def delete(self, key, recurse=None, cas=None, token: str | None = None, dc=None, connections_timeout=None): |
| 172 | """ |
| 173 | Deletes a single key or if *recurse* is True, all keys sharing a |
| 174 | prefix. |
| 175 | |
| 176 | *cas* is an optional flag is used to turn the DELETE into a |
| 177 | Check-And-Set operation. This is very useful as a building block |
| 178 | for more complex synchronization primitives. Unlike PUT, the index |
| 179 | must be greater than 0 for Consul to take any action: a 0 index |
| 180 | will not delete the key. If the index is non-zero, the key is only |
| 181 | deleted if the index matches the ModifyIndex of that key. |
| 182 | |
| 183 | *token* is an optional `ACL token`_ to apply to this request. If |
| 184 | the token's policy is not allowed to delete to this key an |
| 185 | *ACLPermissionDenied* exception will be raised. |
| 186 | |
| 187 | *dc* is the optional datacenter that you wish to communicate with. |
| 188 | If None is provided, defaults to the agent's datacenter. |
| 189 | """ |
| 190 | assert not key.startswith("/"), "keys should not start with a forward slash" |
| 191 | |
| 192 | params = [] |
| 193 | if recurse: |
| 194 | params.append(("recurse", "1")) |
| 195 | if cas is not None: |
| 196 | params.append(("cas", cas)) |
| 197 | dc = dc or self.agent.dc |
| 198 | if dc: |
| 199 | params.append(("dc", dc)) |
| 200 | http_kwargs = {} |
| 201 | if connections_timeout: |
| 202 | http_kwargs["connections_timeout"] = connections_timeout |
| 203 | headers = self.agent.prepare_headers(token) |
| 204 | return self.agent.http.delete(CB.json(), f"/v1/kv/{key}", params=params, headers=headers, **http_kwargs) |