Merge "Reuse the existing zunclient in websocketclient"

This commit is contained in:
Jenkins 2017-04-19 16:56:12 +00:00 committed by Gerrit Code Review
commit 4ed06d73bb
4 changed files with 15 additions and 64 deletions

View File

@ -32,7 +32,6 @@ import tty
import websocket
from zunclient.common.websocketclient import exceptions
from zunclient.v1 import client
LOG = logging.getLogger(__name__)
@ -43,54 +42,13 @@ DEFAULT_SERVICE_TYPE = 'container'
class WebSocketClient(object):
def __init__(self, host_url, id, escape='~',
def __init__(self, zunclient, host_url, id, escape='~',
close_wait=0.5):
self.id = id
self.escape = escape
self.close_wait = close_wait
self.host_url = host_url
self.cs = None
def init_httpclient(self):
"""Initialize the httpclient
Websocket client need to call httpclient to send the resize
command to Zun API server
"""
os_username = os.environ.get('OS_USERNAME')
os_password = os.environ.get('OS_PASSWORD')
os_project_name = os.environ.get('OS_PROJECT_NAME')
os_project_id = os.environ.get('OS_PROJECT_ID')
os_user_domain_id = os.environ.get('OS_USER_DOMAIN_ID')
os_user_domain_name = os.environ.get('OS_USER_DOMAIN_NAME')
os_project_domain_id = os.environ.get('OS_PROJECT_DOMAIN_ID')
os_project_domain_name = os.environ.get('OS_PROJECT_DOMAIN_NAME')
os_auth_url = os.environ.get('OS_AUTH_URL')
endpoint_type = os.environ.get('ENDPOINT_TYPE')
service_type = os.environ.get('SERVICE_TYPE')
os_region_name = os.environ.get('OS_REGION_NAME')
bypass_url = os.environ.get('BYPASS_URL')
insecure = os.environ.get('INSECURE')
if not endpoint_type:
endpoint_type = DEFAULT_ENDPOINT_TYPE
if not service_type:
service_type = DEFAULT_SERVICE_TYPE
self.cs = client.Client(username=os_username,
api_key=os_password,
project_id=os_project_id,
project_name=os_project_name,
user_domain_id=os_user_domain_id,
user_domain_name=os_user_domain_name,
project_domain_id=os_project_domain_id,
project_domain_name=os_project_domain_name,
auth_url=os_auth_url,
service_type=service_type,
region_name=os_region_name,
zun_url=bypass_url,
endpoint_type=endpoint_type,
insecure=insecure)
self.cs = zunclient
def connect(self):
url = self.host_url
@ -330,12 +288,12 @@ class WINCHHandler(object):
signal.signal(signal.SIGWINCH, self.original_handler)
def do_attach(url, container, escape, close_wait):
def do_attach(zunclient, url, container, escape, close_wait):
if url.startswith("ws://"):
try:
wscls = WebSocketClient(host_url=url, id=container,
escape=escape, close_wait=close_wait)
wscls.init_httpclient()
wscls = WebSocketClient(zunclient=zunclient, host_url=url,
id=container, escape=escape,
close_wait=close_wait)
wscls.connect()
wscls.handle_resize()
wscls.start_loop()

View File

@ -616,7 +616,8 @@ class RunContainer(command.ShowOne):
time.sleep(1)
if ready_for_attach is True:
response = client.containers.attach(container_uuid)
websocketclient.do_attach(response, container_uuid, "~", 0.5)
websocketclient.do_attach(client, response, container_uuid,
"~", 0.5)
else:
raise exceptions.InvalidWebSocketLink(container_uuid)
@ -736,7 +737,8 @@ class AttachContainer(command.Command):
def take_action(self, parsed_args):
client = _get_client(self, parsed_args)
response = client.containers.attach(parsed_args.container)
websocketclient.do_attach(response, parsed_args.container, "~", 0.5)
websocketclient.do_attach(client, response, parsed_args.container,
"~", 0.5)
class CopyContainer(command.Command):

View File

@ -30,7 +30,9 @@ WAIT_TIME = 0.5
class WebSocketClientTest(testtools.TestCase):
def test_websocketclient_variables(self):
wsclient = websocketclient.WebSocketClient(host_url=URL,
mock_client = mock.Mock()
wsclient = websocketclient.WebSocketClient(zunclient=mock_client,
host_url=URL,
id=CONTAINER_ID,
escape=ESCAPE_FLAG,
close_wait=WAIT_TIME)
@ -38,14 +40,3 @@ class WebSocketClientTest(testtools.TestCase):
self.assertEqual(wsclient.id, CONTAINER_ID)
self.assertEqual(wsclient.escape, ESCAPE_FLAG)
self.assertEqual(wsclient.close_wait, WAIT_TIME)
@mock.patch('zunclient.v1.client.Client')
def test_init_httpclient(self, mock_client):
wsclient = websocketclient.WebSocketClient(host_url=URL,
id=CONTAINER_ID,
escape=ESCAPE_FLAG,
close_wait=WAIT_TIME)
mock_client.return_value = 'Client Object'
wsclient.init_httpclient()
self.assertEqual(wsclient.cs, 'Client Object')
self.assertTrue(mock_client.called)

View File

@ -437,7 +437,7 @@ def do_run(cs, args):
time.sleep(1)
if ready_for_attach is True:
response = cs.containers.attach(container_uuid)
websocketclient.do_attach(response, container_uuid, "~", 0.5)
websocketclient.do_attach(cs, response, container_uuid, "~", 0.5)
else:
raise exceptions.InvalidWebSocketLink(container_uuid)
@ -480,7 +480,7 @@ def do_update(cs, args):
def do_attach(cs, args):
"""Attach to a running container."""
response = cs.containers.attach(args.container)
websocketclient.do_attach(response, args.container, "~", 0.5)
websocketclient.do_attach(cs, response, args.container, "~", 0.5)
@utils.arg('container',