Skip to content

RFC: Blocking queries index param #89

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 15 additions & 6 deletions nomad/api/acl.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,45 +37,54 @@ def generate_bootstrap(self):
"""
return self.request("bootstrap", method="post").json()

def get_tokens(self):
def get_tokens(self, index=None):
""" Get a list of tokens.

https://www.nomadproject.io/api/acl-tokens.html

optional_arguments:
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index

returns: list

raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""

return self.request("tokens", method="get").json()
return self.request("tokens", method="get", index=index).json()

def get_token(self, id):
def get_token(self, id, index=None):
""" Retrieve specific token.

https://www.nomadproject.io/api/acl-tokens.html

optional_arguments:
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index

returns: dict

raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
return self.request("token", id, method="get").json()
return self.request("token", id, method="get", index=index).json()

def get_self_token(self):
def get_self_token(self, index=None):
""" Retrieve self token used for auth.

https://www.nomadproject.io/api/acl-tokens.html

optional_arguments:
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index

returns: dict

raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
return self.request("token", "self", method="get").json()
return self.request("token", "self", method="get", index=index).json()

def create_token(self, token):
""" Create token.
Expand Down
7 changes: 5 additions & 2 deletions nomad/api/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,17 @@ def __getitem__(self, item):
except nomad.api.exceptions.URLNotFoundNomadException:
raise KeyError

def get_allocation(self, id):
def get_allocation(self, id, index=None):
""" Query a specific allocation.

https://www.nomadproject.io/docs/http/alloc.html

arguments:
- id
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index
returns: dict
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
return self.request(id, method="get").json()
return self.request(id, method="get", index=index).json()
5 changes: 3 additions & 2 deletions nomad/api/allocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ def __iter__(self):
response = self.get_allocations()
return iter(response)

def get_allocations(self, prefix=None):
def get_allocations(self, prefix=None, index=None):
""" Lists all the allocations.

https://www.nomadproject.io/docs/http/allocs.html
arguments:
- prefix :(str) optional, specifies a string to filter allocations on based on an prefix.
This is specified as a querystring parameter.
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index
returns: list
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
params = {"prefix": prefix}
return self.request(method="get", params=params).json()
return self.request(method="get", params=params, index=index).json()
16 changes: 12 additions & 4 deletions nomad/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ class Requester(object):

ENDPOINT = ""

def __init__(self, address=None, uri='http://127.0.0.1', port=4646, namespace=None, token=None, timeout=5, version='v1', verify=False, cert=(), region=None, **kwargs):
def __init__(self, address=None, uri='http://127.0.0.1', port=4646, namespace=None, token=None, timeout=5, blocking_timeout=360, version='v1', verify=False, cert=(), region=None, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we want to add this to nomad/__init__.py to pass this when creating all the endpoint objects so the blocking_timeout could be adjusted on the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we would! That was purely an oversight on my behalf after setting a default which was usable for my purposes.

self.uri = uri
self.port = port
self.namespace = namespace
self.token = token
self.timeout = timeout
self.blocking_timeout = blocking_timeout
self.version = version
self.verify = verify
self.cert = cert
Expand Down Expand Up @@ -79,12 +80,13 @@ def request(self, *args, **kwargs):
data=kwargs.get("data", None),
json=kwargs.get("json", None),
headers=kwargs.get("headers", None),
allow_redirects=kwargs.get("allow_redirects", False)
allow_redirects=kwargs.get("allow_redirects", False),
index=kwargs.get("index", None)
)

return response

def _request(self, method, endpoint, params=None, data=None, json=None, headers=None, allow_redirects=None):
def _request(self, method, endpoint, params=None, data=None, json=None, headers=None, allow_redirects=None, index=None):
url = self._url_builder(endpoint)
qs = self._query_string_builder(endpoint)

Expand All @@ -99,16 +101,20 @@ def _request(self, method, endpoint, params=None, data=None, json=None, headers=
except TypeError:
headers = {"X-Nomad-Token": self.token}

if index and index.get("X-Nomad-Index"):
params["index"] = index["X-Nomad-Index"]

response = None

try:
method = method.lower()
if method == "get":
timeout = self.blocking_timeout if index else self.timeout
response = self.session.get(
url=url,
params=params,
headers=headers,
timeout=self.timeout,
timeout=timeout,
verify=self.verify,
cert=self.cert,
allow_redirects=allow_redirects
Expand Down Expand Up @@ -148,6 +154,8 @@ def _request(self, method, endpoint, params=None, data=None, json=None, headers=
)

if response.ok:
if index is not None:
index["X-Nomad-Index"] = response.headers["X-Nomad-Index"]
return response
elif response.status_code == 400:
raise nomad.api.exceptions.BadRequestNomadException(response)
Expand Down
10 changes: 6 additions & 4 deletions nomad/api/deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,33 +43,35 @@ def __getitem__(self, item):
except nomad.api.exceptions.URLNotFoundNomadException:
raise KeyError

def get_deployment(self, id):
def get_deployment(self, id, index=None):
""" This endpoint reads information about a specific deployment by ID.

https://www.nomadproject.io/docs/http/deployments.html

arguments:
- id
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index
returns: dict
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
return self.request(id, method="get").json()
return self.request(id, method="get", index=index).json()

def get_deployment_allocations(self, id):
def get_deployment_allocations(self, id, index=None):
""" This endpoint lists the allocations created or modified for the given deployment.

https://www.nomadproject.io/docs/http/deployments.html

arguments:
- id
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index
returns: list of dicts
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
return self.request("allocations", id, method="get").json()
return self.request("allocations", id, method="get", index=index).json()

def fail_deployment(self, id):
""" This endpoint is used to mark a deployment as failed. This should be done to force the scheduler to stop
Expand Down
4 changes: 3 additions & 1 deletion nomad/api/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ def __getitem__(self, item):
except nomad.api.exceptions.URLNotFoundNomadException:
raise KeyError

def get_deployments(self, prefix=""):
def get_deployments(self, prefix="", index=None):
""" This endpoint lists all deployments.

https://www.nomadproject.io/docs/http/deployments.html

optional_arguments:
- prefix, (default "") Specifies a string to filter deployments on based on an index prefix.
This is specified as a querystring parameter.
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index


returns: list of dicts
raises:
Expand Down
8 changes: 5 additions & 3 deletions nomad/api/evaluation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,32 @@ def __getitem__(self, item):
except nomad.api.exceptions.URLNotFoundNomadException:
raise KeyError

def get_evaluation(self, id):
def get_evaluation(self, id, index=None):
""" Query a specific evaluation.

https://www.nomadproject.io/docs/http/eval.html

arguments:
- id
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index
returns: dict
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
return self.request(id, method="get").json()

def get_allocations(self, id):
def get_allocations(self, id, index=None):
""" Query the allocations created or modified by an evaluation.

https://www.nomadproject.io/docs/http/eval.html

arguments:
- id
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index
returns: list
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
return self.request(id, "allocations", method="get").json()
return self.request(id, "allocations", method="get", index=index).json()
5 changes: 3 additions & 2 deletions nomad/api/evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ def __iter__(self):
evaluations = self.get_evaluations()
return iter(evaluations)

def get_evaluations(self, prefix=None):
def get_evaluations(self, prefix=None, index=None):
""" Lists all the evaluations.

https://www.nomadproject.io/docs/http/evals.html
arguments:
- prefix :(str) optional, specifies a string to filter evaluations on based on an prefix.
This is specified as a querystring parameter.
- index :(dict) optional, provides a dictionary for keeping track of x-nomad-index
returns: list
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
params = {"prefix": prefix}
return self.request(method="get", params=params).json()
return self.request(method="get", params=params, index=index).json()
Loading