138 lines
4.2 KiB
Python
Raw Normal View History

2015-03-17 08:35:39 -04:00
# Copyright 2013 Nebula Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
"""OpenStackClient Plugin interface"""
import json
2015-03-17 08:35:39 -04:00
import logging
import uuid
import websocket
2015-03-17 08:35:39 -04:00
from openstackclient.common import utils
from openstackclient.identity import common as identity_common
2015-03-17 08:35:39 -04:00
LOG = logging.getLogger(__name__)
DEFAULT_TRIPLEOCLIENT_API_VERSION = '1'
2015-03-17 08:35:39 -04:00
# Required by the OSC plugin interface
API_NAME = 'tripleoclient'
API_VERSION_OPTION = 'os_tripleoclient_api_version'
API_VERSIONS = {
'1': 'tripleoclient.plugin'
}
2015-03-17 08:35:39 -04:00
def make_client(instance):
return ClientWrapper(instance)
2015-03-17 08:35:39 -04:00
# Required by the OSC plugin interface
def build_option_parser(parser):
"""Hook to add global options
Called from openstackclient.shell.OpenStackShell.__init__()
after the builtin parser has been initialized. This is
where a plugin can add global options such as an API version setting.
:param argparse.ArgumentParser parser: The parser object that has been
initialized by OpenStackShell.
"""
parser.add_argument(
'--os-tripleoclient-api-version',
metavar='<tripleoclient-api-version>',
2015-03-17 08:35:39 -04:00
default=utils.env(
'OS_TRIPLEOCLIENT_API_VERSION',
default=DEFAULT_TRIPLEOCLIENT_API_VERSION),
help='TripleO Client API version, default=' +
DEFAULT_TRIPLEOCLIENT_API_VERSION +
' (Env: OS_TRIPLEOCLIENT_API_VERSION)')
2015-03-17 08:35:39 -04:00
return parser
class WebsocketClient(object):
def __init__(self, instance, queue_name):
self._project_id = None
self._ws = None
self._websocket_client_id = None
self._queue_name = queue_name
endpoint = instance.get_endpoint_for_service_type(
'messaging')
token = instance.auth.get_token(instance.session)
self._project_id = identity_common.find_project(
instance.identity,
instance._project_name).id
self._websocket_client_id = str(uuid.uuid4())
# FIXME(dprince): add messaging-websocket to the keystone catalog
ws_url = endpoint.replace('http', 'ws').replace('8888', '9000')
LOG.debug('Instantiating messaging websocket client: %s', ws_url)
self._ws = websocket.create_connection(ws_url)
self.send('authenticate', extra_headers={'X-Auth-Token': token})
# create and subscribe to a queue
# NOTE: if the queue exists it will 204
self.send('queue_create', {'queue_name': queue_name})
self.send('subscription_create', {
'queue_name': queue_name,
'ttl': 10000
})
def cleanup(self):
self.send('queue_delete', {'queue_name': self._queue_name})
self._ws.close()
def send(self, action, body=None, extra_headers=None):
headers = {
'Client-ID': self._websocket_client_id,
'X-Project-ID': self._project_id
}
if extra_headers is not None:
headers.update(extra_headers)
msg = {'action': action, 'headers': headers}
if body:
msg['body'] = body
self._ws.send(json.dumps(msg))
data = self.recv()
if data['headers']['status'] not in (200, 201, 204):
raise RuntimeError(data)
return data
def recv(self):
return json.loads(self._ws.recv())
class ClientWrapper(object):
def __init__(self, instance):
self._instance = instance
self._messaging_websocket = None
def messaging_websocket(self, queue_name='tripleo'):
"""Returns a websocket for the messaging service"""
if self._messaging_websocket is not None:
return self._messaging_websocket
self._messaging_websocket = WebsocketClient(self._instance, queue_name)
return self._messaging_websocket