Skip to content

Commit d8a54bd

Browse files
authored
Merge pull request #136 from nikita-b/add_scaling_policy
add scaling policy endpoint
2 parents 3c6af97 + dce5157 commit d8a54bd

6 files changed

Lines changed: 90 additions & 2 deletions

File tree

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ jobs:
2020
env:
2121
NOMAD_IP: '127.0.0.1'
2222
NOMAD_PORT: '4646'
23-
NOMAD_LATEST: '1.1.4'
23+
NOMAD_LATEST: '1.1.18'
2424

2525
strategy:
2626
fail-fast: false
2727
matrix:
2828
python-version: ['2.7', '3.7', '3.10']
29-
nomad-version: ['1.0.0', '1.1.4']
29+
nomad-version: ['1.0.0', '1.1.18']
3030

3131
steps:
3232
- uses: actions/checkout@v2

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
.vagrant
66
.build
77
.venv
8+
9+
example.json
10+
example.nomad

nomad/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ def __init__(self,
9292
self._nodes = api.Nodes(**self.requester_settings)
9393
self._operator = api.Operator(**self.requester_settings)
9494
self._regions = api.Regions(**self.requester_settings)
95+
self._scaling = api.Scaling(**self.requester_settings)
9596
self._sentinel = api.Sentinel(**self.requester_settings)
9697
self._status = api.Status(**self.requester_settings)
9798
self._system = api.System(**self.requester_settings)
@@ -166,6 +167,10 @@ def deployment(self):
166167
def regions(self):
167168
return self._regions
168169

170+
@property
171+
def scaling(self):
172+
return self._scaling
173+
169174
@property
170175
def status(self):
171176
return self._status

nomad/api/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from nomad.api.nodes import Nodes
2020
from nomad.api.operator import Operator
2121
from nomad.api.regions import Regions
22+
from nomad.api.scaling import Scaling
2223
from nomad.api.sentinel import Sentinel
2324
from nomad.api.status import Status
2425
from nomad.api.system import System

nomad/api/scaling.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import nomad.api.exceptions
2+
3+
from nomad.api.base import Requester
4+
5+
6+
class Scaling(Requester):
7+
"""
8+
Endpoints are used to list and view scaling policies.
9+
10+
https://developer.hashicorp.com/nomad/api-docs/scaling-policies
11+
"""
12+
ENDPOINT = "scaling"
13+
14+
def __init__(self, **kwargs):
15+
super(Scaling, self).__init__(**kwargs)
16+
17+
def __str__(self):
18+
return "{0}".format(self.__dict__)
19+
20+
def __repr__(self):
21+
return "{0}".format(self.__dict__)
22+
23+
def __getattr__(self, item):
24+
raise AttributeError
25+
26+
def get_scaling_policies(self, job="", type=""):
27+
"""
28+
This endpoint returns the scaling policies from all jobs.
29+
30+
https://developer.hashicorp.com/nomad/api-docs/scaling-policies#list-scaling-policies
31+
32+
arguments:
33+
- job
34+
- type
35+
returns: list of dicts
36+
raises:
37+
- nomad.api.exceptions.BaseNomadException
38+
- nomad.api.exceptions.URLNotFoundNomadException
39+
"""
40+
type_of_scaling_policies = [
41+
"horizontal",
42+
"vertical_mem",
43+
"vertical_cpu",
44+
"",
45+
] # we have only horizontal in OSS
46+
47+
if type not in type_of_scaling_policies:
48+
raise nomad.api.exceptions.InvalidParameters("type is invalid "
49+
"(expected values are {} but got {})".format(type_of_scaling_policies, type))
50+
51+
params = {"job": job, "type": type}
52+
53+
return self.request("policies", method="get", params=params).json()
54+
55+
def get_scaling_policy(self, id):
56+
"""
57+
This endpoint reads a specific scaling policy.
58+
59+
https://developer.hashicorp.com/nomad/api-docs/scaling-policies#read-scaling-policy
60+
61+
arguments:
62+
- id
63+
returns: list of dicts
64+
raises:
65+
- nomad.api.exceptions.BaseNomadException
66+
- nomad.api.exceptions.URLNotFoundNomadException
67+
"""
68+
return self.request("policy/{}".format(id), method="get").json()

tests/test_scaling.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import pytest
2+
3+
from nomad.api import exceptions
4+
5+
def test_scaling_list(nomad_setup):
6+
result = nomad_setup.scaling.get_scaling_policies()
7+
assert not result
8+
9+
def test_scaling_policy_not_exist(nomad_setup):
10+
with pytest.raises(exceptions.URLNotFoundNomadException):
11+
nomad_setup.scaling.get_scaling_policy("example")

0 commit comments

Comments
 (0)