vmware-nsx/quantum/plugins/plumgrid/plumgrid_nos_plugin/rest_connection.py
Maru Newby a13295b9a8 Enable automatic validation of many HACKING rules.
* Add hacking to the tox build - a set of flake8 plugins that
   perform automatic validation of many HACKING.rst rules.
 * This patch configures hacking in the tox build and performs
   the mechanical cleanup required to allow the checks to pass.
 * See https://pypi.python.org/pypi/hacking

Change-Id: Ie9b3eb5627a9b6dd181f79225abae422e6cf3777
2013-04-11 19:48:28 +00:00

98 lines
3.7 KiB
Python

# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 PLUMgrid, Inc. All Rights Reserved.
#
# 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.
#
# @author: Edgar Magana, emagana@plumgrid.com, PLUMgrid, Inc.
# @author: Brenden Blanco, bblanco@plumgrid.com, PLUMgrid, Inc.
"""
Quantum PLUMgrid Plug-in for PLUMgrid Virtual Technology
This plugin will forward authenticated REST API calls
to the Network Operating System by PLUMgrid called NOS
"""
import httplib
import urllib2
from quantum.openstack.common import jsonutils as json
from quantum.openstack.common import log as logging
from quantum.plugins.plumgrid.common import exceptions as plum_excep
LOG = logging.getLogger(__name__)
class RestConnection(object):
"""REST Connection to PLUMgrid NOS Server."""
def __init__(self, server, port, timeout):
LOG.debug(_('QuantumPluginPLUMgrid Status: REST Connection Started'))
self.server = server
self.port = port
self.timeout = timeout
def nos_rest_conn(self, nos_url, action, data, headers):
self.nos_url = nos_url
body_data = json.dumps(data)
if not headers:
headers = {}
headers['Content-type'] = 'application/json'
headers['Accept'] = 'application/json'
LOG.debug(_("PLUMgrid_NOS_Server: %(server)s %(port)s %(action)s"),
dict(server=self.server, port=self.port, action=action))
conn = httplib.HTTPConnection(self.server, self.port,
timeout=self.timeout)
if conn is None:
LOG.error(_('PLUMgrid_NOS_Server: Could not establish HTTP '
'connection'))
return
try:
LOG.debug(_("PLUMgrid_NOS_Server Sending Data: %(nos_url)s "
"%(body_data)s %(headers)s"),
dict(
nos_url=nos_url,
body_data=body_data,
headers=headers,
))
conn.request(action, nos_url, body_data, headers)
resp = conn.getresponse()
resp_str = resp.read()
LOG.debug(_("PLUMgrid_NOS_Server Connection Data: %(resp)s, "
"%(resp_str)s"), dict(resp=resp, resp_str=resp_str))
if resp.status is httplib.OK:
try:
respdata = json.loads(resp_str)
LOG.debug(_("PLUMgrid_NOS_Server Connection RESP: %s"),
respdata)
pass
except ValueError:
err_message = _("PLUMgrid HTTP Connection Failed: ")
LOG.Exception(err_message)
raise plum_excep.PLUMgridException(err_message)
ret = (resp.status, resp.reason, resp_str)
except urllib2.HTTPError:
LOG.error(_('PLUMgrid_NOS_Server: %(action)s failure, %(e)r'))
ret = 0, None, None, None
conn.close()
LOG.debug(_("PLUMgrid_NOS_Server: status=%(status)d, "
"reason=%(reason)r, ret=%(ret)s"),
{'status': ret[0], 'reason': ret[1], 'ret': ret[2]})
return ret