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
This commit is contained in:
Fabrizio Vanni 2015-04-27 17:23:13 +01:00
parent 1e101ddf1a
commit 0fb98abd05
3 changed files with 96 additions and 8 deletions

View File

@ -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):

View File

@ -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

86
registration.py Normal file
View File

@ -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))