1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
# Copyright (c) 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.
from keystoneauth1 import adapter
from oslo_serialization import jsonutils
import requests
from blazarclient import exception
from blazarclient.i18n import _
class RequestManager(object):
"""Manager to create request from given Blazar URL and auth token."""
def __init__(self, blazar_url, auth_token, user_agent):
self.blazar_url = blazar_url
self.auth_token = auth_token
self.user_agent = user_agent
def get(self, url):
"""Sends get request to Blazar.
:param url: URL to the wanted Blazar resource.
:type url: str
"""
return self.request(url, 'GET')
def post(self, url, body):
"""Sends post request to Blazar.
:param url: URL to the wanted Blazar resource.
:type url: str
:param body: Values resource to be created from.
:type body: dict
"""
return self.request(url, 'POST', body=body)
def delete(self, url):
"""Sends delete request to Blazar.
:param url: URL to the wanted Blazar resource.
:type url: str
"""
return self.request(url, 'DELETE')
def put(self, url, body):
"""Sends update request to Blazar.
:param url: URL to the wanted Blazar resource.
:type url: str
:param body: Values resource to be updated from.
:type body: dict
"""
return self.request(url, 'PUT', body=body)
def patch(self, url, body):
"""Sends patch request to Blazar.
:param url: URL to the wanted Blazar resource.
:type url: str
"""
return self.request(url, 'PATCH', body=body)
def request(self, url, method, **kwargs):
"""Base request method.
Adds specific headers and URL prefix to the request.
:param url: Resource URL.
:type url: str
:param method: Method to be called (GET, POST, PUT, DELETE).
:type method: str
:returns: Response and body.
:rtype: tuple
"""
kwargs.setdefault('headers', kwargs.get('headers', {}))
kwargs['headers']['User-Agent'] = self.user_agent
kwargs['headers']['Accept'] = 'application/json'
kwargs['headers']['x-auth-token'] = self.auth_token
if 'body' in kwargs:
kwargs['headers']['Content-Type'] = 'application/json'
kwargs['data'] = jsonutils.dump_as_bytes(kwargs['body'])
del kwargs['body']
resp = requests.request(method, self.blazar_url + url, **kwargs)
try:
body = jsonutils.loads(resp.text)
except ValueError:
body = None
if resp.status_code >= 400:
if body is not None:
error_message = body.get('error_message', body)
else:
error_message = resp.text
body = _("ERROR: {0}").format(error_message)
raise exception.BlazarClientException(body, code=resp.status_code)
return resp, body
class SessionClient(adapter.LegacyJsonAdapter):
"""Manager to create request with keystoneauth1 session."""
def request(self, url, method, **kwargs):
resp, body = super(SessionClient, self).request(
url, method, raise_exc=False, **kwargs)
if resp.status_code >= 400:
if body is not None:
error_message = body.get('error_message', body)
else:
error_message = resp.text
msg = _("ERROR: {0}").format(error_message)
raise exception.BlazarClientException(msg, code=resp.status_code)
return resp, body
class BaseClientManager(object):
"""Base class for managing resources of Blazar."""
user_agent = 'python-blazarclient'
def __init__(self, blazar_url, auth_token, session, **kwargs):
self.blazar_url = blazar_url
self.auth_token = auth_token
self.session = session
if self.session:
self.request_manager = SessionClient(
session=self.session,
user_agent=self.user_agent,
**kwargs
)
elif self.blazar_url and self.auth_token:
self.request_manager = RequestManager(blazar_url=self.blazar_url,
auth_token=self.auth_token,
user_agent=self.user_agent)
else:
raise exception.InsufficientAuthInformation
|