Skip to content

Commit

Permalink
code-style: reorganize the repository
Browse files Browse the repository at this point in the history
While the diff is pretty large, this commit
is just about splitting the base.py and its associated
test file into various scope-restricted files.
The api folder is also introduced.

This will ease the development/reviews
  • Loading branch information
Mathias Brulatout committed Mar 20, 2024
1 parent 102167e commit 7f0c1a3
Show file tree
Hide file tree
Showing 38 changed files with 3,152 additions and 3,074 deletions.
3 changes: 2 additions & 1 deletion consul/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__version__ = "1.3.0"

from consul.base import ACLDisabled, ACLPermissionDenied, Check, ConsulException, NotFound, Timeout
from consul.check import Check
from consul.exceptions import ACLDisabled, ACLPermissionDenied, ConsulException, NotFound, Timeout
from consul.std import Consul
4 changes: 2 additions & 2 deletions consul/aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import aiohttp

from consul import base
from consul import Timeout, base

__all__ = ["Consul"]

Expand Down Expand Up @@ -31,7 +31,7 @@ async def _request(self, callback, method, uri, data=None, connections_timeout=N
resp = await self._session.request(method, uri, data=data, **session_kwargs)
body = await resp.text(encoding="utf-8")
if resp.status == 599:
raise base.Timeout
raise Timeout
r = base.Response(resp.status, resp.headers, body)
return callback(r)

Expand Down
Empty file added consul/api/__init__.py
Empty file.
124 changes: 124 additions & 0 deletions consul/api/acl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import json

from consul.callback import CB


class ACL:
def __init__(self, agent):
self.agent = agent

def list(self, token=None):
"""
Lists all the active ACL tokens. This is a privileged endpoint, and
requires a management token. *token* will override this client's
default token.
Requires a token with acl:read capability. ACLPermissionDenied raised otherwise
"""
params = []
token = token or self.agent.token
if token:
params.append(("token", token))
return self.agent.http.get(CB.json(), "/v1/acl/tokens", params=params)

def read(self, accessor_id, token=None):
"""
Returns the token information for *accessor_id*. Requires a token with acl:read capability.
:param accessor_id: The accessor ID of the token to read
:param token: token with acl:read capability
:return: selected token information
"""
params = []
token = token or self.agent.token
if token:
params.append(("token", token))
return self.agent.http.get(CB.json(), f"/v1/acl/token/{accessor_id}", params=params)

def delete(self, accessor_id, token=None):
"""
Deletes the token with *accessor_id*. This is a privileged endpoint, and requires a token with acl:write.
:param accessor_id: The accessor ID of the token to delete
:param token: token with acl:write capability
:return: True if the token was deleted
"""
params = []
token = token or self.agent.token
if token:
params.append(("token", token))
return self.agent.http.delete(CB.bool(), f"/v1/acl/token/{accessor_id}", params=params)

def clone(self, accessor_id, token=None, description=""):
"""
Clones the token identified by *accessor_id*. This is a privileged endpoint, and requires a token with acl:write.
:param accessor_id: The accessor ID of the token to clone
:param token: token with acl:write capability
:param description: Optional new token description
:return: The cloned token information
"""
params = []
token = token or self.agent.token
if token:
params.append(("token", token))

json_data = {"Description": description}
return self.agent.http.put(
CB.json(),
f"/v1/acl/token/{accessor_id}/clone",
params=params,
data=json.dumps(json_data),
)

def create(self, token=None, accessor_id=None, secret_id=None, description=""):
"""
Create a token (optionally identified by *secret_id* and *accessor_id*).
This is a privileged endpoint, and requires a token with acl:write.
:param token: token with acl:write capability
:param accessor_id: The accessor ID of the token to create
:param secret_id: The secret ID of the token to create
:param description: Optional new token description
:return: The cloned token information
"""
params = []
token = token or self.agent.token
if token:
params.append(("token", token))

json_data = {}
if accessor_id:
json_data["AccessorID"] = accessor_id
if secret_id:
json_data["SecretID"] = secret_id
if description:
json_data["Description"] = description
return self.agent.http.put(
CB.json(),
"/v1/acl/token",
params=params,
data=json.dumps(json_data),
)

def update(self, accessor_id, token=None, secret_id=None, description=""):
"""
Update a token (optionally identified by *secret_id* and *accessor_id*).
This is a privileged endpoint, and requires a token with acl:write.
:param accessor_id: The accessor ID of the token to update
:param token: token with acl:write capability
:param secret_id: Optional secret ID of the token to update
:param description: Optional new token description
:return: The updated token information
"""
params = []
token = token or self.agent.token
if token:
params.append(("token", token))

json_data = {"AccessorID": accessor_id}
if secret_id:
json_data["SecretID"] = secret_id
if description:
json_data["Description"] = description
return self.agent.http.put(
CB.json(),
f"/v1/acl/token/{accessor_id}",
params=params,
data=json.dumps(json_data),
)
Loading

0 comments on commit 7f0c1a3

Please sign in to comment.