| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
from etcd3gw.utils import _decode |
| 14 |
|
| 15 |
|
| 16 |
class Lease: |
| 17 |
def __init__(self, id, client=None): |
| 18 |
"""Lease object for expiring keys |
| 19 |
|
| 20 |
:param id: |
| 21 |
:param client: |
| 22 |
""" |
| 23 |
self.id = id |
| 24 |
self.client = client |
| 25 |
|
| 26 |
def revoke(self): |
| 27 |
"""LeaseRevoke revokes a lease. |
| 28 |
|
| 29 |
All keys attached to the lease will expire and be deleted. |
| 30 |
This method makes a synchronous HTTP request by default. To make an |
| 31 |
asynchronous HTTP request, please define a `callback` function |
| 32 |
to be invoked when receiving the response. |
| 33 |
|
| 34 |
:return: |
| 35 |
""" |
| 36 |
self.client.post(self.client.get_url("/kv/lease/revoke"), |
| 37 |
json={"ID": self.id}) |
| 38 |
return True |
| 39 |
|
| 40 |
def ttl(self): |
| 41 |
"""LeaseTimeToLive retrieves lease information. |
| 42 |
|
| 43 |
This method makes a synchronous HTTP request by default. To make an |
| 44 |
asynchronous HTTP request, please define a `callback` function |
| 45 |
to be invoked when receiving the response. |
| 46 |
|
| 47 |
:return: |
| 48 |
""" |
| 49 |
result = self.client.post(self.client.get_url("/kv/lease/timetolive"), |
| 50 |
json={"ID": self.id}) |
| 51 |
return int(result['TTL']) |
| 52 |
|
| 53 |
def refresh(self): |
| 54 |
"""LeaseKeepAlive keeps the lease alive |
| 55 |
|
| 56 |
By streaming keep alive requests from the client to the server and |
| 57 |
streaming keep alive responses from the server to the client. |
| 58 |
This method makes a synchronous HTTP request by default. |
| 59 |
|
| 60 |
:return: returns new TTL for lease. If lease was already expired then |
| 61 |
TTL field is absent in response and the function returns -1 |
| 62 |
according to etcd documentation. |
| 63 |
https://etcd.io/docs/v3.5/dev-guide/apispec/swagger/rpc.swagger.json |
| 64 |
""" |
| 65 |
result = self.client.post(self.client.get_url("/lease/keepalive"), |
| 66 |
json={"ID": self.id}) |
| 67 |
return int(result['result'].get('TTL', -1)) |
| 68 |
|
| 69 |
def keys(self): |
| 70 |
"""Get the keys associated with this lease. |
| 71 |
|
| 72 |
:return: |
| 73 |
""" |
| 74 |
result = self.client.post(self.client.get_url("/kv/lease/timetolive"), |
| 75 |
json={"ID": self.id, |
| 76 |
"keys": True}) |
| 77 |
keys = result['keys'] if 'keys' in result else [] |
| 78 |
return [_decode(key) for key in keys] |