Skip to content

Commit

Permalink
Unity: support of HTTP header Application-Type (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
yong-huang authored Mar 18, 2021
1 parent e729361 commit 7254045
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
6 changes: 5 additions & 1 deletion storops/connection/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class UnityRESTConnector(object):

def __init__(self, host, port=443, user='admin', password='',
verify=False, retries=None, cache_interval=0,
connect_timeout=30):
connect_timeout=30, application_type=None):
base_url = 'https://{host}:{port}'.format(host=host, port=port)

insecure = False
Expand All @@ -64,6 +64,10 @@ def __init__(self, host, port=443, user='admin', password='',
insecure = not verify
else:
ca_cert_path = verify

if application_type:
self.HEADERS['Application-Type'] = application_type

self.http_client = client.HTTPClient(base_url=base_url,
headers=self.HEADERS,
auth=(user, password),
Expand Down
5 changes: 3 additions & 2 deletions storops/unity/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ def _wrap(*args, **kwargs):

class UnityClient(PerfManager):
def __init__(self, ip, username, password, port=443, verify=False,
retries=None, cache_interval=0):
retries=None, cache_interval=0, application_type=None):
super(UnityClient, self).__init__()
self.ip = ip
self._rest = UnityRESTConnector(ip, port=port, user=username,
password=password,
verify=verify,
retries=retries,
cache_interval=cache_interval)
cache_interval=cache_interval,
application_type=application_type)
self._system_version = None

@wrap_not_supported
Expand Down
5 changes: 3 additions & 2 deletions storops/unity/resource/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@
class UnitySystem(UnitySingletonResource):
def __init__(self, host=None, username=None, password=None,
port=443, cli=None, verify=False, retries=None,
cache_interval=0):
cache_interval=0, application_type=None):
super(UnitySystem, self).__init__(cli=cli)
if cli is None:
self._cli = UnityClient(host, username, password, port,
verify=verify, retries=retries,
cache_interval=cache_interval)
cache_interval=cache_interval,
application_type=application_type)
else:
self._cli = cli

Expand Down
27 changes: 23 additions & 4 deletions storops_test/connection/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import unittest

import mock
from hamcrest import assert_that, equal_to

from storops.connection import connector

Expand All @@ -27,7 +28,6 @@ class UnityRESTConnectorTest(unittest.TestCase):

@mock.patch('storops.connection.client.HTTPClient')
def test_new_connector_verify_false(self, mocked_httpclient):

connector.UnityRESTConnector('10.10.10.10',
verify=False)

Expand All @@ -44,7 +44,6 @@ def test_new_connector_verify_false(self, mocked_httpclient):

@mock.patch('storops.connection.client.HTTPClient')
def test_new_connector_verify_true(self, mocked_httpclient):

connector.UnityRESTConnector('10.10.10.10',
verify=True)

Expand All @@ -61,7 +60,6 @@ def test_new_connector_verify_true(self, mocked_httpclient):

@mock.patch('storops.connection.client.HTTPClient')
def test_new_connector_verify_path(self, mocked_httpclient):

connector.UnityRESTConnector('10.10.10.10',
verify='/tmp/ca_cert.crt')

Expand All @@ -78,7 +76,6 @@ def test_new_connector_verify_path(self, mocked_httpclient):

@mock.patch('storops.connection.client.HTTPClient')
def test_new_connector_connect_timeout(self, mocked_httpclient):

connector.UnityRESTConnector('10.10.10.10',
connect_timeout=99)

Expand All @@ -92,3 +89,25 @@ def test_new_connector_connect_timeout(self, mocked_httpclient):
cache_interval=0,
timeout=(99, None),
)

@mock.patch('storops.connection.client.HTTPClient')
def test_connector_with_application_type(self, mocked_httpclient):
application_type = 'testclient/0.1.0'

connector.UnityRESTConnector('10.10.10.10',
connect_timeout=99,
application_type=application_type)

headers = connector.UnityRESTConnector.HEADERS
assert_that(application_type, equal_to(headers['Application-Type']))

mocked_httpclient.assert_called_with(
base_url='https://10.10.10.10:443',
headers=headers,
auth=('admin', ''),
insecure=True,
retries=None,
ca_cert_path=None,
cache_interval=0,
timeout=(99, None),
)

0 comments on commit 7254045

Please sign in to comment.