Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
### Other changes
* Up `requests` lib version to 2.28.1
* Add migging parameters to allocations.get_allocations and jobs.get_jobs (#144). Thanks @Kamilcuk

* Add option for custom user agent
## 1.5.0
* Add `namespace` agrument support for `get_allocations` and `get_deployments` endpoints (#133)
* Add Python 3.10 support (#133)
Expand Down
37 changes: 22 additions & 15 deletions nomad/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Nomad Python library"""
import os
from typing import Optional

import requests

from nomad import api

Expand All @@ -11,18 +14,19 @@ class Nomad: # pylint: disable=too-many-public-methods,too-many-instance-attrib

def __init__( # pylint: disable=too-many-arguments
self,
host="127.0.0.1",
secure=False,
port=4646,
address=os.getenv("NOMAD_ADDR", None),
namespace=os.getenv("NOMAD_NAMESPACE", None),
token=os.getenv("NOMAD_TOKEN", None),
timeout=5,
region=os.getenv("NOMAD_REGION", None),
version="v1",
verify=False,
cert=(os.getenv("NOMAD_CLIENT_CERT", None), os.getenv("NOMAD_CLIENT_KEY", None)),
session=None,
host: str = "127.0.0.1",
secure: bool = False,
port: int = 4646,
address: Optional[str] = os.getenv("NOMAD_ADDR", None),
user_agent: Optional[str] = None,
namespace: Optional[str] = os.getenv("NOMAD_NAMESPACE", None),
token: Optional[str] = os.getenv("NOMAD_TOKEN", None),
timeout: int = 5,
region: Optional[str] = os.getenv("NOMAD_REGION", None),
version: str = "v1",
verify: bool = False,
cert: tuple = (os.getenv("NOMAD_CLIENT_CERT", None), os.getenv("NOMAD_CLIENT_KEY", None)),
session: requests.Session = None,
):
"""Nomad api client

Expand All @@ -31,18 +35,19 @@ def __init__( # pylint: disable=too-many-arguments
optional arguments:
- host (defaults 127.0.0.1), string ip or name of the nomad api server/agent that will be used.
- port (defaults 4646), integer port that will be used to connect.
- user_agent (defaults None), custom user agent for requests to Nomad.
- secure (defaults False), define if the protocol is secured or not (https or http)
- version (defaults v1), vesion of the api of nomad.
- version (defaults v1), version of the api of nomad.
- verify (defaults False), verify the certificate when tls/ssl is enabled
at nomad.
- cert (defaults empty), cert, or key and cert file to validate the certificate
configured at nomad.
- region (defaults None), version of the region to use. It will be used then
regions of the current agent of the connection.
- namespace (defaults to None), Specifies the enterpise namespace that will
- namespace (defaults to None), Specifies the enterprise namespace that will
be use to deploy or to ask info to nomad.
- token (defaults to None), Specifies to append ACL token to the headers to
make authentication on secured based nomad environemnts.
make authentication on secured based nomad environments.
- session (defaults to None), allows for injecting a prepared requests.Session object that
all requests to Nomad should use.
returns: Nomad api client object
Expand All @@ -56,6 +61,7 @@ def __init__( # pylint: disable=too-many-arguments
self.secure = secure
self.port = port
self.address = address
self.user_agent = user_agent
self.region = region
self.timeout = timeout
self.version = version
Expand All @@ -69,6 +75,7 @@ def __init__( # pylint: disable=too-many-arguments
"address": self.address,
"uri": self.get_uri(),
"port": self.port,
"user_agent": self.user_agent,
"namespace": self.__namespace,
"token": self.token,
"timeout": self.timeout,
Expand Down
29 changes: 18 additions & 11 deletions nomad/api/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Requester"""
from typing import Optional

import requests

import nomad.api.exceptions
Expand All @@ -13,20 +15,22 @@ class Requester: # pylint: disable=too-many-instance-attributes,too-few-public-

def __init__( # pylint: disable=too-many-arguments
self,
address=None,
uri="http://127.0.0.1",
port=4646,
namespace=None,
token=None,
timeout=5,
version="v1",
verify=False,
cert=(),
region=None,
session=None,
address: Optional[str] = None,
uri: Optional[str] = "http://127.0.0.1",
port: int = 4646,
user_agent: Optional[str] = None,
namespace: Optional[str] = None,
token: Optional[str] = None,
timeout: int = 5,
version: str = "v1",
verify: bool = False,
cert: tuple = (),
region: Optional[str] = None,
session: requests.Session = None,
):
self.uri = uri
self.port = port
self.user_agent = user_agent
self.namespace = namespace
self.token = token
self.timeout = timeout
Expand Down Expand Up @@ -140,6 +144,9 @@ def _request( # pylint: disable=too-many-arguments, too-many-branches
else:
headers = {"X-Nomad-Token": self.token}

if self.user_agent:
headers["User-Agent"] = self.user_agent

response = None

try:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,17 @@ def test_base_use_address_instead_on_host_port():
address=nomad_address, host=common.IP, port=common.NOMAD_PORT, verify=False, token=common.NOMAD_TOKEN
)
n.jobs.get_jobs()

@responses.activate
def test_use_custom_user_agent():
custom_agent_name = "custom_agent"
responses.add(responses.GET, "https://nomad.service.consul:4646/v1/jobs", status=200, json=[])

nomad_address = "https://nomad.service.consul:4646"
n = nomad.Nomad(
address=nomad_address, host=common.IP, port=common.NOMAD_PORT, verify=False,
token=common.NOMAD_TOKEN, user_agent=custom_agent_name
)
n.jobs.get_jobs()

assert responses.calls[0].request.headers["User-Agent"] == custom_agent_name