From 0fb98abd059b53151dedd6f4383132dc48fc9447 Mon Sep 17 00:00:00 2001 From: Fabrizio Vanni Date: Mon, 27 Apr 2015 17:23:13 +0100 Subject: [PATCH] freezer api support for client registration Adds an endpoint to the api for the registration of freezer clients Change-Id: I1ca2a5e0021d383df70dfd001ab12967714c35bc Implements: blueprint freezerclient-registration --- backups.py | 2 +- client.py | 16 +++++---- registration.py | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 registration.py diff --git a/backups.py b/backups.py index 5f4628d..3cdca82 100644 --- a/backups.py +++ b/backups.py @@ -29,7 +29,7 @@ class BackupsManager(object): def __init__(self, client): self.client = client - self.endpoint = self.client.api_endpoint + '/v1/backups/' + self.endpoint = self.client.endpoint + '/v1/backups/' @property def headers(self): diff --git a/client.py b/client.py index 13e5ae5..4ec3afe 100644 --- a/client.py +++ b/client.py @@ -30,6 +30,7 @@ if os.path.exists(os.path.join(possible_topdir, 'freezer', '__init__.py')): sys.path.insert(0, possible_topdir) from freezer.apiclient.backups import BackupsManager +from freezer.apiclient.registration import RegistrationManager import exceptions @@ -42,17 +43,18 @@ class Client(object): tenant_name=None, auth_url=None, session=None, - api_endpoint=None): + endpoint=None): self.version = version self.token = token self.username = username self.tenant_name = tenant_name self.password = password self.auth_url = auth_url - self._api_endpoint = api_endpoint + self._endpoint = endpoint self.session = session self._auth = None self.backups = BackupsManager(self) + self.registration = RegistrationManager(self) def _update_api_endpoint(self): services = self.auth.services.list() @@ -69,7 +71,7 @@ class Client(object): except: raise exceptions.AuthFailure( 'freezer endpoint not found in endpoint list') - self._api_endpoint = freezer_endpoint.publicurl + self._endpoint = freezer_endpoint.publicurl @property def auth(self): @@ -93,14 +95,14 @@ class Client(object): return self.auth.auth_token @property - def api_endpoint(self): - if self._api_endpoint is None: + def endpoint(self): + if self._endpoint is None: self._update_api_endpoint() - return self._api_endpoint + return self._endpoint def api_exists(self): try: - if self.api_endpoint is not None: + if self.endpoint is not None: return True except: return False diff --git a/registration.py b/registration.py new file mode 100644 index 0000000..6a13d64 --- /dev/null +++ b/registration.py @@ -0,0 +1,86 @@ +""" +Copyright 2014 Hewlett-Packard + +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. + +This product includes cryptographic software written by Eric Young +(eay@cryptsoft.com). This product includes software written by Tim +Hudson (tjh@cryptsoft.com). +======================================================================== +""" + +import json +import requests + +from freezer.apiclient import exceptions + + +class RegistrationManager(object): + + def __init__(self, client): + self.client = client + self.endpoint = self.client.endpoint + 'clients/' + self.headers = {'X-Auth-Token': client.token} + + def create(self, client_info): + r = requests.post(self.endpoint, + data=json.dumps(client_info), + headers=self.headers) + if r.status_code != 201: + raise exceptions.MetadataCreationFailure( + "[*] Error {0}: {1}".format(r.status_code, r.text)) + client_id = r.json()['client_id'] + return client_id + + def delete(self, client_id): + endpoint = self.endpoint + client_id + r = requests.delete(endpoint, headers=self.headers) + if r.status_code != 204: + raise exceptions.MetadataDeleteFailure( + "[*] Error {0}".format(r.status_code)) + + def list(self, limit=10, offset=0, search=None): + """ + Retrieves a list of client info structures + + :param limit: number of result to return (optional, default 10) + :param offset: order of first document (optional, default 0) + :param search: structured query (optional) + can contain: + * "match": list of {field, value} + Example: + { "match": [ + {"description": "some search text here"}, + {"client_id": "giano"}, + ... + ], + } + """ + data = json.dumps(search) if search else None + query = {'limit': int(limit), 'offset': int(offset)} + r = requests.get(self.endpoint, headers=self.headers, + params=query, data=data) + if r.status_code != 200: + raise exceptions.MetadataGetFailure( + "[*] Error {0}: {1}".format(r.status_code, r.text)) + return r.json()['clients'] + + def get(self, client_id): + endpoint = self.endpoint + client_id + r = requests.get(endpoint, headers=self.headers) + if r.status_code == 200: + return r.json() + if r.status_code == 404: + return None + raise exceptions.MetadataGetFailure( + "[*] Error {0}".format(r.status_code))