From 9cf0883e8145e107bdff55c551aafb0389110961 Mon Sep 17 00:00:00 2001 From: Nikita Beletskii Date: Mon, 17 Oct 2022 01:19:57 +0200 Subject: [PATCH 1/2] add scaling policy endpoint --- .github/workflows/main.yml | 2 +- .gitignore | 3 ++ nomad/__init__.py | 5 +++ nomad/api/__init__.py | 1 + nomad/api/scaling.py | 68 ++++++++++++++++++++++++++++++++++++++ tests/test_scaling.py | 11 ++++++ 6 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 nomad/api/scaling.py create mode 100644 tests/test_scaling.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 38a1a03..6a8f313 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,7 +20,7 @@ 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 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 4c0301c..242bab5 100644 --- a/nomad/__init__.py +++ b/nomad/__init__.py @@ -87,6 +87,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) @@ -161,6 +162,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") From 587cc63cded8da025a6a982268429dd16dd9c83d Mon Sep 17 00:00:00 2001 From: Nikita Beletskii Date: Mon, 17 Oct 2022 01:22:54 +0200 Subject: [PATCH 2/2] bump nomad version in tests --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6a8f313..65e768d 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,7 +26,7 @@ jobs: fail-fast: false matrix: python-version: ['2.7', '3.7'] - nomad-version: ['1.0.0', '1.1.4'] + nomad-version: ['1.0.0', '1.1.18'] steps: - uses: actions/checkout@v2