diff --git a/linstor/linstorapi.py b/linstor/linstorapi.py index cab8b47..7261cc0 100644 --- a/linstor/linstorapi.py +++ b/linstor/linstorapi.py @@ -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 @@ -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" } @@ -3525,7 +3528,7 @@ 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 @@ -3533,9 +3536,10 @@ def __init__(self, ctrl_host_list, timeout=300, keep_alive=False): :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): diff --git a/linstor/resource.py b/linstor/resource.py index 6f0cbf0..3b38e84 100644 --- a/linstor/resource.py +++ b/linstor/resource.py @@ -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): @@ -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. @@ -254,6 +255,7 @@ 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 """ @@ -261,7 +263,7 @@ def from_resource_group(cls, uri, resource_group_name, resource_name, vlm_sizes, 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( @@ -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' diff --git a/linstor/resourcegroup.py b/linstor/resourcegroup.py index 0578207..c762cb6 100644 --- a/linstor/resourcegroup.py +++ b/linstor/resourcegroup.py @@ -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 @@ -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): @@ -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):