diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 61ba0bf..f5414a3 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,13 +20,13 @@ jobs: env: NOMAD_IP: '127.0.0.1' NOMAD_PORT: '4646' - NOMAD_LATEST: '1.1.4' + NOMAD_LATEST: '1.1.18' strategy: fail-fast: false matrix: python-version: ['2.7', '3.7', '3.10'] - nomad-version: ['1.0.0', '1.1.4'] + nomad-version: ['1.0.0', '1.1.18'] steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 246fd60..a5c391f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ .vagrant .build .venv + +example.json +example.nomad \ No newline at end of file diff --git a/nomad/__init__.py b/nomad/__init__.py index f3a2485..b1ffe5e 100644 --- a/nomad/__init__.py +++ b/nomad/__init__.py @@ -92,6 +92,7 @@ def __init__(self, self._nodes = api.Nodes(**self.requester_settings) self._operator = api.Operator(**self.requester_settings) self._regions = api.Regions(**self.requester_settings) + self._scaling = api.Scaling(**self.requester_settings) self._sentinel = api.Sentinel(**self.requester_settings) self._status = api.Status(**self.requester_settings) self._system = api.System(**self.requester_settings) @@ -166,6 +167,10 @@ def deployment(self): def regions(self): return self._regions + @property + def scaling(self): + return self._scaling + @property def status(self): return self._status diff --git a/nomad/api/__init__.py b/nomad/api/__init__.py index 1f6e873..3cd9fd9 100644 --- a/nomad/api/__init__.py +++ b/nomad/api/__init__.py @@ -19,6 +19,7 @@ from nomad.api.nodes import Nodes from nomad.api.operator import Operator from nomad.api.regions import Regions +from nomad.api.scaling import Scaling from nomad.api.sentinel import Sentinel from nomad.api.status import Status from nomad.api.system import System diff --git a/nomad/api/scaling.py b/nomad/api/scaling.py new file mode 100644 index 0000000..56776a2 --- /dev/null +++ b/nomad/api/scaling.py @@ -0,0 +1,68 @@ +import nomad.api.exceptions + +from nomad.api.base import Requester + + +class Scaling(Requester): + """ + Endpoints are used to list and view scaling policies. + + https://developer.hashicorp.com/nomad/api-docs/scaling-policies + """ + ENDPOINT = "scaling" + + def __init__(self, **kwargs): + super(Scaling, self).__init__(**kwargs) + + def __str__(self): + return "{0}".format(self.__dict__) + + def __repr__(self): + return "{0}".format(self.__dict__) + + def __getattr__(self, item): + raise AttributeError + + def get_scaling_policies(self, job="", type=""): + """ + This endpoint returns the scaling policies from all jobs. + + https://developer.hashicorp.com/nomad/api-docs/scaling-policies#list-scaling-policies + + arguments: + - job + - type + returns: list of dicts + raises: + - nomad.api.exceptions.BaseNomadException + - nomad.api.exceptions.URLNotFoundNomadException + """ + type_of_scaling_policies = [ + "horizontal", + "vertical_mem", + "vertical_cpu", + "", + ] # we have only horizontal in OSS + + if type not in type_of_scaling_policies: + raise nomad.api.exceptions.InvalidParameters("type is invalid " + "(expected values are {} but got {})".format(type_of_scaling_policies, type)) + + params = {"job": job, "type": type} + + return self.request("policies", method="get", params=params).json() + + def get_scaling_policy(self, id): + """ + This endpoint reads a specific scaling policy. + + https://developer.hashicorp.com/nomad/api-docs/scaling-policies#read-scaling-policy + + arguments: + - id + returns: list of dicts + raises: + - nomad.api.exceptions.BaseNomadException + - nomad.api.exceptions.URLNotFoundNomadException + """ + return self.request("policy/{}".format(id), method="get").json() diff --git a/tests/test_scaling.py b/tests/test_scaling.py new file mode 100644 index 0000000..686ad3b --- /dev/null +++ b/tests/test_scaling.py @@ -0,0 +1,11 @@ +import pytest + +from nomad.api import exceptions + +def test_scaling_list(nomad_setup): + result = nomad_setup.scaling.get_scaling_policies() + assert not result + +def test_scaling_policy_not_exist(nomad_setup): + with pytest.raises(exceptions.URLNotFoundNomadException): + nomad_setup.scaling.get_scaling_policy("example")