Add logging
Make all of the parameters in create_order() and create_secret() optional except for mime_type
This commit is contained in:
@@ -4,26 +4,38 @@ eventlet.monkey_patch(socket=True, select=True)
|
||||
import json
|
||||
import requests
|
||||
|
||||
|
||||
from barbicanclient.common import config
|
||||
from barbicanclient.secrets import Secret
|
||||
from barbicanclient.orders import Order
|
||||
from barbicanclient.common.auth import authenticate
|
||||
from barbicanclient.common import auth
|
||||
from barbicanclient.openstack.common import log
|
||||
from barbicanclient.common.exceptions import ClientException
|
||||
from barbicanclient.openstack.common.gettextutils import _
|
||||
from openstack.common.timeutils import parse_isotime
|
||||
from urlparse import urljoin
|
||||
|
||||
|
||||
config.parse_args()
|
||||
log.setup('barbicanclient')
|
||||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
class Connection(object):
|
||||
SECRETS_PATH = 'secrets'
|
||||
ORDERS_PATH = 'orders'
|
||||
|
||||
def __init__(self, auth_endpoint, user, key, tenant, **kwargs):
|
||||
def __init__(self, auth_endpoint, user, key, tenant,
|
||||
authenticate=None, token=None, **kwargs):
|
||||
"""
|
||||
:param auth_endpoint: The auth URL to authenticate against
|
||||
:param user: The user to authenticate as
|
||||
:param key: The API key or passowrd to auth with
|
||||
:param key: The API key or password to auth with
|
||||
"""
|
||||
|
||||
LOG.debug(_("Creating Connection object"))
|
||||
|
||||
self._auth_endpoint = auth_endpoint
|
||||
self.authenticate = authenticate or auth.authenticate
|
||||
self._user = user
|
||||
self._key = key
|
||||
self._tenant = tenant
|
||||
@@ -31,7 +43,7 @@ class Connection(object):
|
||||
or 'https://barbican.api.rackspacecloud.com/v1/')
|
||||
self._cacert = kwargs.get('cacert')
|
||||
|
||||
self.connect()
|
||||
self.connect(token=token)
|
||||
|
||||
@property
|
||||
def _conn(self):
|
||||
@@ -57,6 +69,9 @@ class Connection(object):
|
||||
token will be used for this connection and auth will
|
||||
not happen.
|
||||
"""
|
||||
|
||||
LOG.debug(_("Establishing connection"))
|
||||
|
||||
self._session = requests.Session()
|
||||
|
||||
#headers = {"Client-Id": self._client_id}
|
||||
@@ -64,9 +79,11 @@ class Connection(object):
|
||||
self._session.verify = True
|
||||
|
||||
if token:
|
||||
LOG.warn(_("Bypassing authentication - using provided token"))
|
||||
self.auth_token = token
|
||||
else:
|
||||
self._endpoint, self.auth_token = authenticate(
|
||||
LOG.debug(_("Authenticating token"))
|
||||
self._endpoint, self.auth_token = self.authenticate(
|
||||
self._auth_endpoint,
|
||||
self._user,
|
||||
self._key,
|
||||
@@ -91,8 +108,11 @@ class Connection(object):
|
||||
"""
|
||||
Returns the list of secrets for the auth'd tenant
|
||||
"""
|
||||
LOG.debug(_("Listing secrets"))
|
||||
href = "{0}/{1}?limit=100".format(self._tenant, self.SECRETS_PATH)
|
||||
LOG.debug("href: {}".format(href))
|
||||
hdrs, body = self._perform_http(href=href, method='GET')
|
||||
LOG.debug("Response - headers: {0}\nbody: {1}".format(hdrs, body))
|
||||
|
||||
secrets_dict = body['secrets']
|
||||
secrets = []
|
||||
@@ -102,44 +122,59 @@ class Connection(object):
|
||||
return secrets
|
||||
|
||||
def create_secret(self,
|
||||
name,
|
||||
mime_type,
|
||||
algorithm,
|
||||
bit_length,
|
||||
cypher_type,
|
||||
plain_text,
|
||||
expiration):
|
||||
plain_text=None,
|
||||
name=None,
|
||||
algorithm=None,
|
||||
bit_length=None,
|
||||
cypher_type=None,
|
||||
expiration=None):
|
||||
LOG.debug(_("Creating secret of mime_type {}".format(mime_type)))
|
||||
href = "{0}/{1}".format(self._tenant, self.SECRETS_PATH)
|
||||
LOG.debug("href: {}".format(href))
|
||||
secret_dict = {}
|
||||
secret_dict['name'] = name
|
||||
secret_dict['mime_type'] = mime_type
|
||||
secret_dict['algorithm'] = algorithm
|
||||
secret_dict['bit_length'] = int(bit_length)
|
||||
secret_dict['cypher_type'] = cypher_type
|
||||
secret_dict['plain_text'] = plain_text
|
||||
secret_dict['name'] = name
|
||||
secret_dict['algorithm'] = algorithm
|
||||
secret_dict['cypher_type'] = cypher_type
|
||||
if bit_length is not None:
|
||||
secret_dict['bit_length'] = int(bit_length)
|
||||
if expiration is not None:
|
||||
secret_dict['expiration'] = parse_isotime(expiration)
|
||||
for k in secret_dict.keys():
|
||||
if secret_dict[k] is None:
|
||||
secret_dict.pop(k)
|
||||
LOG.debug("Request body: ".format(secret_dict))
|
||||
hdrs, body = self._perform_http(href=href,
|
||||
method='POST',
|
||||
request_body=json.dumps(secret_dict))
|
||||
LOG.debug("Response - headers: {0}\nbody: {1}".format(hdrs, body))
|
||||
|
||||
return body['secret_ref']
|
||||
#return Secret(self, body)
|
||||
|
||||
def delete_secret_by_id(self, secret_id):
|
||||
href = "{0}/{1}/{2}".format(self._tenant, self.SECRETS_PATH, secret_id)
|
||||
LOG.info(_("Deleting secret - Secret ID: {}".format(secret_id)))
|
||||
return self.delete_secret(href)
|
||||
|
||||
def delete_secret(self, href):
|
||||
hdrs, body = self._perform_http(href=href, method='DELETE')
|
||||
LOG.debug("Response - headers: {0}\nbody: {1}".format(hdrs, body))
|
||||
|
||||
def get_secret_by_id(self, secret_id):
|
||||
LOG.debug(_("Getting secret - Secret ID: {}".format(secret_id)))
|
||||
href = "{0}/{1}/{2}".format(self._tenant, self.SECRETS_PATH, secret_id)
|
||||
return self.get_secret(href)
|
||||
|
||||
def get_secret(self, href):
|
||||
hdrs, body = self._perform_http(href=href, method='GET')
|
||||
LOG.debug("Response - headers: {0}\nbody: {1}".format(hdrs, body))
|
||||
return Secret(self._conn, body)
|
||||
|
||||
def get_raw_secret_by_id(self, secret_id, mime_type):
|
||||
LOG.debug(_("Getting raw secret - Secret ID: {0}".format(secret_id)))
|
||||
href = "{0}/{1}/{2}".format(self._tenant, self.SECRETS_PATH, secret_id)
|
||||
return self.get_raw_secret(href, mime_type)
|
||||
|
||||
@@ -147,14 +182,18 @@ class Connection(object):
|
||||
hdrs = {"Accept": mime_type}
|
||||
hdrs, body = self._perform_http(href=href, method='GET', headers=hdrs,
|
||||
parse_json=False)
|
||||
LOG.debug("Response - headers: {0}\nbody: {1}".format(hdrs, body))
|
||||
return body
|
||||
|
||||
def list_orders(self):
|
||||
"""
|
||||
Returns the list of orders
|
||||
"""
|
||||
LOG.debug(_("Listing orders"))
|
||||
href = "{0}/{1}?limit=100".format(self._tenant, self.ORDERS_PATH)
|
||||
LOG.debug("href: {}".format(href))
|
||||
hdrs, body = self._perform_http(href=href, method='GET')
|
||||
LOG.debug("Response - headers: {0}\nbody: {1}".format(hdrs, body))
|
||||
|
||||
orders_dict = body['orders']
|
||||
orders = []
|
||||
@@ -164,36 +203,46 @@ class Connection(object):
|
||||
return orders
|
||||
|
||||
def create_order(self,
|
||||
name,
|
||||
mime_type,
|
||||
algorithm,
|
||||
bit_length,
|
||||
cypher_type):
|
||||
name=None,
|
||||
algorithm=None,
|
||||
bit_length=None,
|
||||
cypher_type=None):
|
||||
LOG.debug(_("Creating order of mime_type {}".format(mime_type)))
|
||||
href = "{0}/{1}".format(self._tenant, self.ORDERS_PATH)
|
||||
LOG.debug("href: {}".format(href))
|
||||
order_dict = {'secret': {}}
|
||||
order_dict['secret']['name'] = name
|
||||
order_dict['secret']['mime_type'] = mime_type
|
||||
order_dict['secret']['algorithm'] = algorithm
|
||||
order_dict['secret']['bit_length'] = bit_length
|
||||
order_dict['secret']['cypher_type'] = cypher_type
|
||||
for k in order_dict['secret'].keys():
|
||||
if order_dict['secret'][k] is None:
|
||||
order_dict['secret'].pop(k)
|
||||
LOG.debug("Request body: {}".format(order_dict['secret']))
|
||||
hdrs, body = self._perform_http(href=href,
|
||||
method='POST',
|
||||
request_body=json.dumps(order_dict))
|
||||
return body['order_ref']
|
||||
|
||||
def delete_order_by_id(self, order_id):
|
||||
LOG.info(_("Deleting order - Order ID: {}".format(order_id)))
|
||||
href = "{0}/{1}/{2}".format(self._tenant, self.ORDERS_PATH, order_id)
|
||||
return self.delete_order(href)
|
||||
|
||||
def delete_order(self, href):
|
||||
hdrs, body = self._perform_http(href=href, method='DELETE')
|
||||
LOG.debug("Response - headers: {0}\nbody: {1}".format(hdrs, body))
|
||||
|
||||
def get_order_by_id(self, order_id):
|
||||
LOG.debug(_("Getting order - Order ID: {}".format(order_id)))
|
||||
href = "{0}/{1}/{2}".format(self._tenant, self.ORDERS_PATH, order_id)
|
||||
return self.get_order(href)
|
||||
|
||||
def get_order(self, href):
|
||||
hdrs, body = self._perform_http(href=href, method='GET')
|
||||
LOG.debug("Response - headers: {0}\nbody: {1}".format(hdrs, body))
|
||||
return Order(self._conn, body)
|
||||
|
||||
def _perform_http(self, method, href, request_body='', headers={},
|
||||
@@ -218,6 +267,7 @@ class Connection(object):
|
||||
|
||||
# Check if the status code is 2xx class
|
||||
if not response.ok:
|
||||
LOG.error('Response status code was bad')
|
||||
raise ClientException(href=href, method=method,
|
||||
http_status=response.status_code,
|
||||
http_response_content=response.content)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[DEFAULT]
|
||||
|
||||
# The list of modules to copy from openstack-common
|
||||
modules=setup,jsonutils,timeutils,version
|
||||
modules=gettextutils,setup,jsonutils,timeutils,version,log
|
||||
|
||||
# The base module to hold the copy of openstack.common
|
||||
base=barbicanclient
|
||||
@@ -2,4 +2,5 @@ httplib2>=0.7.7
|
||||
argparse>=1.2.1
|
||||
python-keystoneclient>=0.2.3
|
||||
eventlet>=0.12.1
|
||||
oslo.config>=1.1.0
|
||||
iso8601>=0.1.4
|
||||
Reference in New Issue
Block a user