Skip to content

Commit

Permalink
allow setting an additional user-agent string info
Browse files Browse the repository at this point in the history
  • Loading branch information
rp- committed Jun 14, 2021
1 parent 0435af7 commit 897568d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
14 changes: 9 additions & 5 deletions linstor/linstorapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class Linstor(object):
REST_PORT = 3370
REST_HTTPS_PORT = 3371

def __init__(self, ctrl_host, timeout=300, keep_alive=False):
def __init__(self, ctrl_host, timeout=300, keep_alive=False, agent_info=""):
self._ctrl_host = ctrl_host
self._logger = logging.getLogger('Linstor')
self._timeout = timeout
Expand All @@ -187,8 +187,11 @@ def __init__(self, ctrl_host, timeout=300, keep_alive=False):
self._allow_insecure = False
self._times_entered = 0

user_agent = "PythonLinstor/{v} (API{a})".format(v=VERSION, a=API_VERSION_MIN)
if agent_info:
user_agent += ": " + agent_info
self._http_headers = {
"User-Agent": "PythonLinstor/{v} (API{a})".format(v=VERSION, a=API_VERSION_MIN),
"User-Agent": user_agent,
"Connection": "keep-alive",
"Accept-Encoding": "gzip"
}
Expand Down Expand Up @@ -3525,17 +3528,18 @@ def exos_map(self):


class MultiLinstor(Linstor):
def __init__(self, ctrl_host_list, timeout=300, keep_alive=False):
def __init__(self, ctrl_host_list, timeout=300, keep_alive=False, agent_info=""):
"""A Linstor client that tries connecting to a list of controllers
This is intended to support high availability deployments with multiple Controllers, with only one controller
active at a time.
:param list[str] ctrl_host_list: The list of controller uris. See linstor.Linstor for the exact format
:param timeout: connection timeout. See linstor.Linstor
:param keep_alive: See linstor.Linstor
:param bool keep_alive: See linstor.Linstor
:param str agent_info: This string gets added to the user-agent info
"""
super(MultiLinstor, self).__init__(ctrl_host_list[0], timeout, keep_alive)
super(MultiLinstor, self).__init__(ctrl_host_list[0], timeout, keep_alive, agent_info)
self._ctrl_host_list = ctrl_host_list # type: List[str]

def connect(self):
Expand Down
11 changes: 7 additions & 4 deletions linstor/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ def to_unicode(cls, t):


class _Client(object):
def __init__(self, uris, timeout=300, keep_alive=False):
def __init__(self, uris, timeout=300, keep_alive=False, agent_info=""):
# external properties
self._uri_list = linstor.MultiLinstor.controller_uri_list(uris) # type: list[str]
self.timeout = timeout
self.keep_alive = keep_alive
self.agent_info = agent_info

@property
def uri_list(self):
Expand Down Expand Up @@ -241,7 +242,7 @@ def __repr__(self):

@classmethod
def from_resource_group(cls, uri, resource_group_name, resource_name, vlm_sizes,
timeout=300, keep_alive=False, definitions_only=False, existing_client=None):
timeout=300, keep_alive=False, definitions_only=False, existing_client=None, agent_info=""):
"""
Spawns a new resource definition from the given resource group.
Expand All @@ -254,14 +255,15 @@ def from_resource_group(cls, uri, resource_group_name, resource_name, vlm_sizes,
:param bool keep_alive: keep client connection alive
:param bool definitions_only: only spawn definitions
:param linstor.Linstor existing_client: Client to associate with the resource
:param str agent_info: Info string added to user-agent
:return: linstor.resource.Resource object of the newly created resource definition
:rtype: linstor.resource.Resource
"""
if existing_client:
client = existing_client
else:
c = _Client(uri)
client = linstor.MultiLinstor(c.uri_list, timeout, keep_alive)
client = linstor.MultiLinstor(c.uri_list, timeout, keep_alive, agent_info)

with client as lin:
result = lin.resource_group_spawn(
Expand All @@ -285,7 +287,8 @@ def from_resource_group(cls, uri, resource_group_name, resource_name, vlm_sizes,
def _get_connection(self):
if self._existing_client:
return self._existing_client
return linstor.MultiLinstor(self.client.uri_list, self.client.timeout, self.client.keep_alive)
return linstor.MultiLinstor(self.client.uri_list, self.client.timeout, self.client.keep_alive,
self.client.agent_info)

def _set_properties(self):
dp = 'yes' if self._allow_two_primaries else 'no'
Expand Down
10 changes: 6 additions & 4 deletions linstor/resourcegroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@


class ResourceGroup(object):
def __init__(self, name, uri='linstor://localhost', existing_client=None):
def __init__(self, name, uri='linstor://localhost', existing_client=None, agent_info=""):
self._name = name
self._uri = uri
self.client = _Client(uri)
self.client = _Client(uri, agent_info=agent_info)
self._existing_client = existing_client

self._description = None
Expand All @@ -33,7 +33,8 @@ def __init__(self, name, uri='linstor://localhost', existing_client=None):
def _get_connection(self):
if self._existing_client:
return self._existing_client
return linstor.MultiLinstor(self.client.uri_list, self.client.timeout, self.client.keep_alive)
return linstor.MultiLinstor(self.client.uri_list, self.client.timeout, self.client.keep_alive,
self.client.agent_info)

@property
def name(self):
Expand Down Expand Up @@ -272,9 +273,10 @@ def create_resource(self, resource_name, vlm_sizes):
"""
r = Resource.from_resource_group(self._uri, self._name, resource_name, vlm_sizes,
timeout=self.client.timeout, keep_alive=self.client.keep_alive,
existing_client=self._existing_client)
existing_client=self._existing_client, agent_info=self.client.agent_info)
r.client.keep_alive = self.client.keep_alive
r.client.timeout = self.client.timeout
r.client.agent_info = self.client.agent_info
return r

def query_max_volume_size(self):
Expand Down

0 comments on commit 897568d

Please sign in to comment.