720f40f418
Add two tests with network templates: * multiroles (controller/compute + cinder) and network template with almost default configuration (only interfaces mapping is changed); * multiroles (controller/compute + ceph) and network template with changed network roles assignment ('ceph/public', 'mgmt/memcache' and 'mgmt/database' roles are moved to 'storage' network) and bonding. Related blueprint templates-for-networking Change-Id: If8fcabde52755f69a848051ebc2e2812851a08db
134 lines
4.8 KiB
Python
134 lines
4.8 KiB
Python
# Copyright 2013 Mirantis, 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.
|
|
|
|
import json
|
|
import traceback
|
|
import urllib2
|
|
|
|
from keystoneclient.v2_0 import Client as keystoneclient
|
|
from keystoneclient import exceptions
|
|
from fuelweb_test import logger
|
|
|
|
|
|
class HTTPClient(object):
|
|
"""HTTPClient.""" # TODO documentation
|
|
|
|
def __init__(self, url, keystone_url, credentials, **kwargs):
|
|
logger.info('Initiate HTTPClient with url %s', url)
|
|
self.url = url
|
|
self.keystone_url = keystone_url
|
|
self.creds = dict(credentials, **kwargs)
|
|
self.keystone = None
|
|
self.opener = urllib2.build_opener(urllib2.HTTPHandler)
|
|
|
|
def authenticate(self):
|
|
try:
|
|
logger.info('Initialize keystoneclient with url %s',
|
|
self.keystone_url)
|
|
self.keystone = keystoneclient(
|
|
auth_url=self.keystone_url, **self.creds)
|
|
# it depends on keystone version, some versions doing auth
|
|
# explicitly some dont, but we are making it explicitly always
|
|
self.keystone.authenticate()
|
|
logger.debug('Authorization token is successfully updated')
|
|
except exceptions.AuthorizationFailure:
|
|
logger.warning(
|
|
'Cant establish connection to keystone with url %s',
|
|
self.keystone_url)
|
|
|
|
@property
|
|
def token(self):
|
|
if self.keystone is not None:
|
|
try:
|
|
return self.keystone.auth_token
|
|
except exceptions.AuthorizationFailure:
|
|
logger.warning(
|
|
'Cant establish connection to keystone with url %s',
|
|
self.keystone_url)
|
|
except exceptions.Unauthorized:
|
|
logger.warning("Keystone returned unauthorized error, trying "
|
|
"to pass authentication.")
|
|
self.authenticate()
|
|
return self.keystone.auth_token
|
|
return None
|
|
|
|
def get(self, endpoint):
|
|
req = urllib2.Request(self.url + endpoint)
|
|
return self._open(req)
|
|
|
|
def post(self, endpoint, data=None, content_type="application/json"):
|
|
if not data:
|
|
data = {}
|
|
req = urllib2.Request(self.url + endpoint, data=json.dumps(data))
|
|
req.add_header('Content-Type', content_type)
|
|
return self._open(req)
|
|
|
|
def put(self, endpoint, data=None, content_type="application/json"):
|
|
if not data:
|
|
data = {}
|
|
req = urllib2.Request(self.url + endpoint, data=json.dumps(data))
|
|
req.add_header('Content-Type', content_type)
|
|
req.get_method = lambda: 'PUT'
|
|
return self._open(req)
|
|
|
|
def delete(self, endpoint):
|
|
req = urllib2.Request(self.url + endpoint)
|
|
req.get_method = lambda: 'DELETE'
|
|
return self._open(req)
|
|
|
|
def _open(self, req):
|
|
try:
|
|
return self._get_response(req)
|
|
except urllib2.HTTPError as e:
|
|
if e.code == 401:
|
|
logger.warning('Authorization failure: {0}'.format(e.read()))
|
|
self.authenticate()
|
|
return self._get_response(req)
|
|
else:
|
|
raise
|
|
|
|
def _get_response(self, req):
|
|
if self.token is not None:
|
|
try:
|
|
logger.debug('Set X-Auth-Token to {0}'.format(self.token))
|
|
req.add_header("X-Auth-Token", self.token)
|
|
except exceptions.AuthorizationFailure:
|
|
logger.warning('Failed with auth in http _get_response')
|
|
logger.warning(traceback.format_exc())
|
|
return self.opener.open(req)
|
|
|
|
|
|
class HTTPClientZabbix(object):
|
|
"""HTTPClientZabbix.""" # TODO documentation
|
|
|
|
def __init__(self, url):
|
|
self.url = url
|
|
self.opener = urllib2.build_opener(urllib2.HTTPHandler)
|
|
|
|
def get(self, endpoint=None, cookie=None):
|
|
req = urllib2.Request(self.url + endpoint)
|
|
if cookie:
|
|
req.add_header('cookie', cookie)
|
|
return self.opener.open(req)
|
|
|
|
def post(self, endpoint=None, data=None, content_type="text/css",
|
|
cookie=None):
|
|
if not data:
|
|
data = {}
|
|
req = urllib2.Request(self.url + endpoint, data=json.dumps(data))
|
|
req.add_header('Content-Type', content_type)
|
|
if cookie:
|
|
req.add_header('cookie', cookie)
|
|
return self.opener.open(req)
|