| 1 | from __future__ import annotations |
| 2 | |
| 3 | import json |
| 4 | from typing import Optional |
| 5 | |
| 6 | from consul.callback import CB |
| 7 | |
| 8 | |
| 9 | class Session: |
| 10 | def __init__(self, agent) -> None: |
| 11 | self.agent = agent |
| 12 | |
| 13 | def create( |
| 14 | self, |
| 15 | name: Optional[str] = None, |
| 16 | node=None, |
| 17 | checks=None, |
| 18 | lock_delay: int = 15, |
| 19 | behavior: str = "release", |
| 20 | ttl: Optional[int] = None, |
| 21 | dc=None, |
| 22 | token: str | None = None, |
| 23 | ): |
| 24 | """ |
| 25 | Creates a new session. There is more documentation for sessions |
| 26 | `here <https://consul.io/docs/internals/sessions.html>`_. |
| 27 | |
| 28 | *name* is an optional human readable name for the session. |
| 29 | |
| 30 | *node* is the node to create the session on. if not provided the |
| 31 | current agent's node will be used. |
| 32 | |
| 33 | *checks* is a list of checks to associate with the session. if not |
| 34 | provided it defaults to the *serfHealth* check. It is highly |
| 35 | recommended that, if you override this list, you include the |
| 36 | default *serfHealth*. |
| 37 | |
| 38 | *lock_delay* is an integer of seconds. |
| 39 | |
| 40 | *behavior* can be set to either 'release' or 'delete'. This |
| 41 | controls the behavior when a session is invalidated. By default, |
| 42 | this is 'release', causing any locks that are held to be released. |
| 43 | Changing this to 'delete' causes any locks that are held to be |
| 44 | deleted. 'delete' is useful for creating ephemeral key/value |
| 45 | entries. |
| 46 | |
| 47 | when *ttl* is provided, the session is invalidated if it is not |
| 48 | renewed before the TTL expires. If specified, it is an integer of |
| 49 | seconds. Currently it must be between 10 and 86400 seconds. |
| 50 | |
| 51 | *token* is an optional `ACL token` to apply to this request. ACL |
| 52 | required : session:write |
| 53 | |
| 54 | By default the session will be created in the current datacenter |
| 55 | but an optional *dc* can be provided. |
| 56 | |
| 57 | Returns the string *session_id* for the session. |
| 58 | """ |
| 59 | params = [] |
| 60 | dc = dc or self.agent.dc |
| 61 | if dc: |
| 62 | params.append(("dc", dc)) |
| 63 | data = {} |
| 64 | if name: |
| 65 | data["name"] = name |
| 66 | if node: |
| 67 | data["node"] = node |
| 68 | if checks is not None: |
| 69 | data["checks"] = checks |
| 70 | if lock_delay != 15: |
| 71 | data["lockdelay"] = f"{lock_delay}s" |
| 72 | assert behavior in ("release", "delete"), "behavior must be release or delete" |
| 73 | if behavior != "release": |
| 74 | data["behavior"] = behavior |
| 75 | if ttl: |
| 76 | assert 10 <= ttl <= 86400 |
| 77 | data["ttl"] = f"{ttl}s" |
| 78 | data_str = json.dumps(data) if data else "" |
| 79 | |
| 80 | headers = self.agent.prepare_headers(token) |
| 81 | return self.agent.http.put( |
| 82 | CB.json(is_id=True), "/v1/session/create", params=params, headers=headers, data=data_str |
| 83 | ) |
| 84 | |
| 85 | def destroy(self, session_id, dc=None, token: str | None = None): |
| 86 | """ |
| 87 | Destroys the session *session_id* |
| 88 | |
| 89 | *token* is an optional `ACL token` to apply to this request. ACL |
| 90 | required : session:write |
| 91 | |
| 92 | Returns *True* on success. |
| 93 | """ |
| 94 | params = [] |
| 95 | dc = dc or self.agent.dc |
| 96 | if dc: |
| 97 | params.append(("dc", dc)) |
| 98 | headers = self.agent.prepare_headers(token) |
| 99 | return self.agent.http.put(CB.boolean(), f"/v1/session/destroy/{session_id}", headers=headers, params=params) |
| 100 | |
| 101 | def list(self, index=None, wait=None, consistency=None, dc=None, token: str | None = None): |
| 102 | """ |
| 103 | Returns a tuple of (*index*, *sessions*) of all active sessions in |
| 104 | the *dc* datacenter. *dc* defaults to the current datacenter of |
| 105 | this agent. |
| 106 | |
| 107 | *index* is the current Consul index, suitable for making subsequent |
| 108 | calls to wait for changes since this query was last run. |
| 109 | |
| 110 | *wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 111 | a given index. this parameter is only applied if *index* is also |
| 112 | specified. the wait time by default is 5 minutes. |
| 113 | |
| 114 | *consistency* can be either 'default', 'consistent' or 'stale'. if |
| 115 | not specified *consistency* will the consistency level this client |
| 116 | was configured with. |
| 117 | |
| 118 | *token* is an optional `ACL token` to apply to this request. ACL |
| 119 | required : session:read |
| 120 | |
| 121 | The response looks like this:: |
| 122 | |
| 123 | (index, [ |
| 124 | { |
| 125 | "LockDelay": 1.5e+10, |
| 126 | "Checks": [ |
| 127 | "serfHealth" |
| 128 | ], |
| 129 | "Node": "foobar", |
| 130 | "ID": "adf4238a-882b-9ddc-4a9d-5b6758e4159e", |
| 131 | "CreateIndex": 1086449 |
| 132 | }, |
| 133 | ... |
| 134 | ]) |
| 135 | """ |
| 136 | params = [] |
| 137 | dc = dc or self.agent.dc |
| 138 | if dc: |
| 139 | params.append(("dc", dc)) |
| 140 | if index: |
| 141 | params.append(("index", index)) |
| 142 | if wait: |
| 143 | params.append(("wait", wait)) |
| 144 | consistency = consistency or self.agent.consistency |
| 145 | if consistency in ("consistent", "stale"): |
| 146 | params.append((consistency, "1")) |
| 147 | headers = self.agent.prepare_headers(token) |
| 148 | return self.agent.http.get(CB.json(index=True), "/v1/session/list", headers=headers, params=params) |
| 149 | |
| 150 | def node(self, node: str, index=None, wait=None, consistency=None, dc=None, token: str | None = None): |
| 151 | """ |
| 152 | Returns a tuple of (*index*, *sessions*) as per *session.list*, but |
| 153 | filters the sessions returned to only those active for *node*. |
| 154 | |
| 155 | *index* is the current Consul index, suitable for making subsequent |
| 156 | calls to wait for changes since this query was last run. |
| 157 | |
| 158 | *wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 159 | a given index. this parameter is only applied if *index* is also |
| 160 | specified. the wait time by default is 5 minutes. |
| 161 | |
| 162 | *consistency* can be either 'default', 'consistent' or 'stale'. if |
| 163 | not specified *consistency* will the consistency level this client |
| 164 | was configured with. |
| 165 | |
| 166 | *token* is an optional `ACL token` to apply to this request. ACL |
| 167 | required : session:read |
| 168 | """ |
| 169 | params = [] |
| 170 | dc = dc or self.agent.dc |
| 171 | if dc: |
| 172 | params.append(("dc", dc)) |
| 173 | if index: |
| 174 | params.append(("index", index)) |
| 175 | if wait: |
| 176 | params.append(("wait", wait)) |
| 177 | consistency = consistency or self.agent.consistency |
| 178 | if consistency in ("consistent", "stale"): |
| 179 | params.append((consistency, "1")) |
| 180 | headers = self.agent.prepare_headers(token) |
| 181 | return self.agent.http.get(CB.json(index=True), f"/v1/session/node/{node}", headers=headers, params=params) |
| 182 | |
| 183 | def info(self, session_id: str, index=None, wait=None, consistency=None, dc=None, token: str | None = None): |
| 184 | """ |
| 185 | Returns a tuple of (*index*, *session*) for the session |
| 186 | *session_id* in the *dc* datacenter. *dc* defaults to the current |
| 187 | datacenter of this agent. |
| 188 | |
| 189 | *index* is the current Consul index, suitable for making subsequent |
| 190 | calls to wait for changes since this query was last run. |
| 191 | |
| 192 | *wait* the maximum duration to wait (e.g. '10s') to retrieve |
| 193 | a given index. this parameter is only applied if *index* is also |
| 194 | specified. the wait time by default is 5 minutes. |
| 195 | |
| 196 | *consistency* can be either 'default', 'consistent' or 'stale'. if |
| 197 | not specified *consistency* will the consistency level this client |
| 198 | was configured with. |
| 199 | |
| 200 | *token* is an optional `ACL token` to apply to this request. ACL |
| 201 | required : session:read |
| 202 | """ |
| 203 | params = [] |
| 204 | dc = dc or self.agent.dc |
| 205 | if dc: |
| 206 | params.append(("dc", dc)) |
| 207 | if index: |
| 208 | params.append(("index", index)) |
| 209 | if wait: |
| 210 | params.append(("wait", wait)) |
| 211 | consistency = consistency or self.agent.consistency |
| 212 | if consistency in ("consistent", "stale"): |
| 213 | params.append((consistency, "1")) |
| 214 | headers = self.agent.prepare_headers(token) |
| 215 | return self.agent.http.get( |
| 216 | CB.json(index=True, one=True), f"/v1/session/info/{session_id}", headers=headers, params=params |
| 217 | ) |
| 218 | |
| 219 | def renew(self, session_id, dc=None, token: str | None = None): |
| 220 | """ |
| 221 | This is used with sessions that have a TTL, and it extends the |
| 222 | expiration by the TTL. |
| 223 | |
| 224 | *dc* is the optional datacenter that you wish to communicate with. |
| 225 | If None is provided, defaults to the agent's datacenter. |
| 226 | |
| 227 | *token* is an optional `ACL token` to apply to this request. ACL |
| 228 | required : session:write |
| 229 | |
| 230 | Returns the session. |
| 231 | """ |
| 232 | params = [] |
| 233 | dc = dc or self.agent.dc |
| 234 | if dc: |
| 235 | params.append(("dc", dc)) |
| 236 | headers = self.agent.prepare_headers(token) |
| 237 | return self.agent.http.put( |
| 238 | CB.json(one=True, allow_404=False), f"/v1/session/renew/{session_id}", headers=headers, params=params |
| 239 | ) |