diff --git a/nomad/api/job.py b/nomad/api/job.py index 03288db..b8beba8 100644 --- a/nomad/api/job.py +++ b/nomad/api/job.py @@ -1,7 +1,9 @@ """Nomad job: https://developer.hashicorp.com/nomad/api-docs/jobs""" -import nomad.api.exceptions + +from typing import Union from nomad.api.base import Requester +import nomad.api.exceptions class Job(Requester): @@ -81,33 +83,58 @@ def get_versions(self, id_): """ return self.request(id_, "versions", method="get").json() - def get_allocations(self, id_): + def get_allocations( + self, + id_: str, + all_: Union[bool, None] = None, + namespace: Union[str, None] = None, + ): """Query the allocations belonging to a single job. https://www.nomadproject.io/docs/http/job.html arguments: - - id_ + - id_ + - all (bool optional) + - namespace (str) optional. + Specifies the target namespace. If ACL is enabled, this value + must match a namespace that the token is allowed to access. + This is specified as a query string parameter. returns: list raises: - - nomad.api.exceptions.BaseNomadException - - nomad.api.exceptions.URLNotFoundNomadException + - nomad.api.exceptions.BaseNomadException + - nomad.api.exceptions.URLNotFoundNomadException """ - return self.request(id_, "allocations", method="get").json() - - def get_evaluations(self, id_): + params = { + "all": all_, + "namespace": namespace, + } + return self.request(id_, "allocations", params=params, method="get").json() + + def get_evaluations( + self, + id_: str, + namespace: Union[str, None] = None, + ): """Query the evaluations belonging to a single job. https://www.nomadproject.io/docs/http/job.html arguments: - - id_ + - id_ + - namespace (str) optional. + Specifies the target namespace. If ACL is enabled, this value + must match a namespace that the token is allowed to access. + This is specified as a query string parameter. returns: dict raises: - - nomad.api.exceptions.BaseNomadException - - nomad.api.exceptions.URLNotFoundNomadException + - nomad.api.exceptions.BaseNomadException + - nomad.api.exceptions.URLNotFoundNomadException """ - return self.request(id_, "evaluations", method="get").json() + params = { + "namespace": namespace, + } + return self.request(id_, "evaluations", params=params, method="get").json() def get_deployments(self, id_): """This endpoint lists a single job's deployments @@ -252,7 +279,11 @@ def revert_job(self, id_, version, enforce_prior_version=None): - nomad.api.exceptions.BaseNomadException - nomad.api.exceptions.URLNotFoundNomadException """ - revert_json = {"JobID": id_, "JobVersion": version, "EnforcePriorVersion": enforce_prior_version} + revert_json = { + "JobID": id_, + "JobVersion": version, + "EnforcePriorVersion": enforce_prior_version, + } return self.request(id_, "revert", json=revert_json, method="post").json() def stable_job(self, id_, version, stable): @@ -272,28 +303,47 @@ def stable_job(self, id_, version, stable): revert_json = {"JobID": id_, "JobVersion": version, "Stable": stable} return self.request(id_, "stable", json=revert_json, method="post").json() - def deregister_job(self, id_, purge=None): + def deregister_job( + self, + id_: str, + eval_priority: Union[int, None] = None, + global_: Union[bool, None] = None, + namespace: Union[str, None] = None, + purge: Union[bool, None] = None, + ): # pylint: disable=too-many-arguments """Deregisters a job, and stops all allocations part of it. https://www.nomadproject.io/docs/http/job.html arguments: - - id_ - - purge (bool), optionally specifies whether the job should be - stopped and purged immediately (`purge=True`) or deferred to the - Nomad garbage collector (`purge=False`). + - id_ + - eval_priority (int) optional. + Override the priority of the evaluations produced as a result + of this job deregistration. By default, this is set to the + priority of the job. + - global_ (bool) optional. + Stop a multi-region job in all its regions. By default, job + stop will stop only a single region at a time. Ignored for + single-region jobs. + - purge (bool) optional. + Specifies that the job should be stopped and purged immediately. + This means the job will not be queryable after being stopped. + If not set, the job will be purged by the garbage collector. + - namespace (str) optional. + Specifies the target namespace. If ACL is enabled, this value + must match a namespace that the token is allowed to access. + This is specified as a query string parameter. returns: dict raises: - - nomad.api.exceptions.BaseNomadException - - nomad.api.exceptions.URLNotFoundNomadException - - nomad.api.exceptions.InvalidParameters + - nomad.api.exceptions.BaseNomadException + - nomad.api.exceptions.URLNotFoundNomadException + - nomad.api.exceptions.InvalidParameters """ - params = None - if purge is not None: - if not isinstance(purge, bool): - raise nomad.api.exceptions.InvalidParameters( - "purge is invalid " f"(expected type {type(bool())} but got {type(purge)})" - ) - params = {"purge": purge} + params = { + "eval_priority": eval_priority, + "global": global_, + "namespace": namespace, + "purge": purge, + } return self.request(id_, params=params, method="delete").json()