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

Add missing parameters, do not pass None #144

Merged
merged 5 commits into from
Jan 16, 2023
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
51 changes: 34 additions & 17 deletions nomad/api/allocations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Nomad allocation: https://developer.hashicorp.com/nomad/api-docs/allocations"""
from typing import Optional

from nomad.api.base import Requester


Expand All @@ -10,6 +12,7 @@ class Allocations(Requester):

https://www.nomadproject.io/docs/http/allocs.html
"""

ENDPOINT = "allocations"

def __init__(self, **kwargs):
Expand All @@ -32,22 +35,36 @@ def __iter__(self):
response = self.get_allocations()
return iter(response)

def get_allocations(self, prefix=None, namespace=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.
- namespace :(str) optional, specifies the target namespace. Specifying * would return all jobs.
This is specified as a querystring parameter.
returns: list
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
params = {"prefix": prefix}
if namespace:
params["namespace"] = namespace
def get_allocations( # pylint: disable=too-many-arguments
self,
prefix: Optional[str] = None,
filter_: Optional[str] = None,
namespace: Optional[str] = None,
resources: Optional[bool] = None,
task_states: Optional[bool] = 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.
- filter_ :(str) optional
Name has a trailing underscore not to conflict with builtin function.
- namespace :(str) optional, specifies the target namespace. Specifying * would return all jobs.
This is specified as a querystring parameter.
- resources :(bool) optional
- task_states :(bool) optional
returns: list
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
params = {
"prefix": prefix,
"filter": filter_,
"namespace": namespace,
"resources": resources,
"task_states": task_states,
}
return self.request(method="get", params=params).json()
7 changes: 5 additions & 2 deletions nomad/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ def _query_string_builder(self, endpoint, params=None):
if not isinstance(params, dict):
params = {}

# Remove parameters that are None
params = {key: val for key, val in params.items() if val is not None}

if ("namespace" not in params) and (self.namespace and self._required_namespace(endpoint)):
query_string["namespace"] = self.namespace

Expand Down Expand Up @@ -132,9 +135,9 @@ def _request( # pylint: disable=too-many-arguments, too-many-branches
params = query_string

if self.token:
try:
if headers is not None:
headers["X-Nomad-Token"] = self.token
except TypeError:
else:
headers = {"X-Nomad-Token": self.token}

response = None
Expand Down
47 changes: 30 additions & 17 deletions nomad/api/jobs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Nomad job: https://developer.hashicorp.com/nomad/api-docs/jobs"""
from typing import Optional
import nomad.api.exceptions

from nomad.api.base import Requester
Expand Down Expand Up @@ -62,24 +63,36 @@ def __iter__(self):
jobs = self.get_jobs()
return iter(jobs)

def get_jobs(self, prefix=None, namespace=None):
""" Lists all the jobs registered with Nomad.

https://www.nomadproject.io/docs/http/jobs.html
arguments:
- prefix :(str) optional, specifies a string to filter jobs on based on an prefix.
This is specified as a querystring parameter.
- namespace :(str) optional, specifies the target namespace. Specifying * would return all jobs.
This is specified as a querystring parameter.
returns: list
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
def get_jobs(
self,
prefix: Optional[str] = None,
namespace: Optional[str] = None,
filter_: Optional[str] = None,
meta: Optional[bool] = None,
):
"""Lists all the jobs registered with Nomad.

https://www.nomadproject.io/docs/http/jobs.html
arguments:
- prefix :(str) optional, specifies a string to filter jobs on based on an prefix.
This is specified as a querystring parameter.
- namespace :(str) optional, specifies the target namespace. Specifying * would return all jobs.
This is specified as a querystring parameter.
- filter_ :(str) optional, specifies the expression used to filter the result.
Name has a trailing underscore not to conflict with builtin function.
- meta :(bool) optional, if set, jobs returned will include a meta field containing
key-value pairs provided in the job specification's meta block.
returns: list
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
params = {"prefix": prefix}
if namespace:
params["namespace"] = namespace

params = {
"prefix": prefix,
"namespace": namespace,
"filter": filter_,
"meta": meta,
}
return self.request(method="get", params=params).json()

def register_job(self, job):
Expand Down