Skip to content

Commit

Permalink
Merge pull request #133 from nikita-b/add_namespace_agrs
Browse files Browse the repository at this point in the history
Add namespace agrs for deployment and allocations endpoints
  • Loading branch information
jonathanrcross authored Oct 10, 2022
2 parents a09434e + 1796a61 commit b04f724
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['2.7', '3.7']
python-version: ['2.7', '3.7', '3.10']
nomad-version: ['1.0.0', '1.1.4']

steps:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ NOMAD_CLIENT_KEY=/path/to/tls/client.key
* can either use the Vagrantfile for local integration testing or create environment variables `NOMAD_IP` and `NOMAD_PORT` that are assigned to a nomad binary that is running

```
virutalenv venv
source venv/bin/activate
virtualenv .venv
source .venv/bin/activate
pip install -r requirements-dev.txt
```

Expand Down
7 changes: 6 additions & 1 deletion nomad/api/allocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,22 @@ def __iter__(self):
response = self.get_allocations()
return iter(response)

def get_allocations(self, prefix=None):
def get_allocations(self, prefix=None, namespace=None):
""" Lists all the allocations.
https://www.nomadproject.io/docs/http/allocs.html
arguments:
- prefix :(str) optional, specifies a string to filter allocations on based on an prefix.
This is specified as a querystring parameter.
- namespace :(str) optional, specifies the target namespace. Specifying * would return all jobs.
This is specified as a querystring parameter.
returns: list
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
params = {"prefix": prefix}
if namespace:
params["namespace"] = namespace

return self.request(method="get", params=params).json()
7 changes: 6 additions & 1 deletion nomad/api/deployments.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,24 @@ def __getitem__(self, item):
except nomad.api.exceptions.URLNotFoundNomadException:
raise KeyError

def get_deployments(self, prefix=""):
def get_deployments(self, prefix="", namespace=None):
""" This endpoint lists all deployments.
https://www.nomadproject.io/docs/http/deployments.html
optional_arguments:
- prefix, (default "") Specifies a string to filter deployments on based on an index prefix.
This is specified as a querystring parameter.
- namespace :(str) optional, specifies the target namespace. Specifying * would return all jobs.
This is specified as a querystring parameter.
returns: list of dicts
raises:
- nomad.api.exceptions.BaseNomadException
- nomad.api.exceptions.URLNotFoundNomadException
"""
params = {"prefix": prefix}
if namespace:
params["namespace"] = namespace

return self.request(params=params, method="get").json()
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.26.0
requests==2.27.1
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
],
keywords='nomad hashicorp client',
)
12 changes: 12 additions & 0 deletions tests/test_allocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,15 @@ def test_get_allocations_with_namespace(nomad_setup_with_namespace):
json=[{"ID": "a8198d79-cfdb-6593-a999-1e9adabcba2e","EvalID": "5456bd7a-9fc0-c0dd-6131-cbee77f57577","Namespace": common.NOMAD_NAMESPACE, "Name": "example.cache[0]","NodeID": "fb2170a8-257d-3c64-b14d-bc06cc94e34c","PreviousAllocation": "516d2753-0513-cfc7-57ac-2d6fac18b9dc","NextAllocation": "cd13d9b9-4f97-7184-c88b-7b451981616b"}]
)
assert common.NOMAD_NAMESPACE in nomad_setup_with_namespace.allocations.get_allocations()[0]["Namespace"]

@responses.activate
def test_get_allocations_with_namespace_override_namespace_declared_on_create(nomad_setup_with_namespace):
override_namespace_name = "namespace=override-namespace"
responses.add(
responses.GET,
"http://{ip}:{port}/v1/allocations?prefix=a8198d79-cfdb-6593-a999-1e9adabcba2e&namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace=override_namespace_name),
status=200,
json=[{"ID": "a8198d79-cfdb-6593-a999-1e9adabcba2e","EvalID": "5456bd7a-9fc0-c0dd-6131-cbee77f57577","Namespace": override_namespace_name, "Name": "example.cache[0]","NodeID": "fb2170a8-257d-3c64-b14d-bc06cc94e34c","PreviousAllocation": "516d2753-0513-cfc7-57ac-2d6fac18b9dc","NextAllocation": "cd13d9b9-4f97-7184-c88b-7b451981616b"}]
)

nomad_setup_with_namespace.allocations.get_allocations("a8198d79-cfdb-6593-a999-1e9adabcba2e", namespace=override_namespace_name)
12 changes: 12 additions & 0 deletions tests/test_deployment.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,15 @@ def test_get_deployment_with_namespace(nomad_setup_with_namespace):
json={"ID": "70638f62-5c19-193e-30d6-f9d6e689ab8e","JobID": "example", "JobVersion": 1, "JobModifyIndex": 17, "JobSpecModifyIndex": 17, "JobCreateIndex": 7,"Namespace": common.NOMAD_NAMESPACE, "Name": "example.cache[0]"}
)
assert common.NOMAD_NAMESPACE in nomad_setup_with_namespace.deployment.get_deployment("a8198d79-cfdb-6593-a999-1e9adabcba2e")["Namespace"]

@responses.activate
def test_get_deployments_with_namespace_override_namespace_declared_on_create(nomad_setup_with_namespace):
override_namespace_name = "override-namespace"
responses.add(
responses.GET,
"http://{ip}:{port}/v1/deployments?prefix=a8198d79-cfdb-6593-a999-1e9adabcba2e&namespace={namespace}".format(ip=common.IP, port=common.NOMAD_PORT, namespace=override_namespace_name),
status=200,
json={"ID": "70638f62-5c19-193e-30d6-f9d6e689ab8e","JobID": "example", "JobVersion": 1, "JobModifyIndex": 17, "JobSpecModifyIndex": 17, "JobCreateIndex": 7,"Namespace": override_namespace_name, "Name": "example.cache[0]"}
)

nomad_setup_with_namespace.deployments.get_deployments("a8198d79-cfdb-6593-a999-1e9adabcba2e", namespace=override_namespace_name)

0 comments on commit b04f724

Please sign in to comment.