|
| 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() |
0 commit comments