Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code-style: reorganize the repository #62

Merged
merged 1 commit into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading