Add api support for jobs
removes distinction between actions and configs in the api The v1/jobs endpoint is the single point of access to the api to store, search and retrieve operating instruction about a single freezer run. optional scheduling informations can be added to a job to allow for future/recurrent job execution. removes api endpoints - v1/actions - v1/configs adds api endpoints: - v1/jobs Implements blueprint: freezer-api-jobs Change-Id: Ideeef14dfccd21ddd10b4faa438124c04d2e1ff8
This commit is contained in:

committed by
Fausto Marzi

parent
2f690e1ac5
commit
5d75e3c789
101
actions.py
101
actions.py
@@ -1,101 +0,0 @@
|
|||||||
"""
|
|
||||||
Copyright 2015 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 ActionManager(object):
|
|
||||||
|
|
||||||
def __init__(self, client):
|
|
||||||
self.client = client
|
|
||||||
self.endpoint = self.client.endpoint + '/v1/actions/'
|
|
||||||
|
|
||||||
@property
|
|
||||||
def headers(self):
|
|
||||||
return {'X-Auth-Token': self.client.auth_token}
|
|
||||||
|
|
||||||
def create(self, doc):
|
|
||||||
r = requests.post(self.endpoint,
|
|
||||||
data=json.dumps(doc),
|
|
||||||
headers=self.headers)
|
|
||||||
if r.status_code != 201:
|
|
||||||
raise exceptions.MetadataCreationFailure(
|
|
||||||
"[*] Error {0}: {1}".format(r.status_code, r.text))
|
|
||||||
action_id = r.json()['action_id']
|
|
||||||
return action_id
|
|
||||||
|
|
||||||
def delete(self, action_id):
|
|
||||||
endpoint = self.endpoint + action_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 action 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"},
|
|
||||||
{"backup_name": "mydata"},
|
|
||||||
...
|
|
||||||
],
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
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()['actions']
|
|
||||||
|
|
||||||
def get(self, action_id):
|
|
||||||
endpoint = self.endpoint + action_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))
|
|
||||||
|
|
||||||
def update(self, action_id, update_doc):
|
|
||||||
endpoint = self.endpoint + action_id
|
|
||||||
r = requests.patch(endpoint,
|
|
||||||
headers=self.headers,
|
|
||||||
data=json.dumps(update_doc))
|
|
||||||
if r.status_code != 200:
|
|
||||||
raise exceptions.MetadataUpdateFailure(
|
|
||||||
"[*] Error {0}: {1}".format(r.status_code, r.text))
|
|
||||||
return r.json()['version']
|
|
||||||
|
|
||||||
def set_status(self, action_id, new_status):
|
|
||||||
return self.update(action_id, {'status': new_status})
|
|
14
backups.py
14
backups.py
@@ -22,7 +22,7 @@ Hudson (tjh@cryptsoft.com).
|
|||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from freezer.apiclient import exceptions
|
import exceptions
|
||||||
|
|
||||||
|
|
||||||
class BackupsManager(object):
|
class BackupsManager(object):
|
||||||
@@ -40,8 +40,7 @@ class BackupsManager(object):
|
|||||||
data=json.dumps(backup_metadata),
|
data=json.dumps(backup_metadata),
|
||||||
headers=self.headers)
|
headers=self.headers)
|
||||||
if r.status_code != 201:
|
if r.status_code != 201:
|
||||||
raise exceptions.MetadataCreationFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
|
||||||
backup_id = r.json()['backup_id']
|
backup_id = r.json()['backup_id']
|
||||||
return backup_id
|
return backup_id
|
||||||
|
|
||||||
@@ -49,8 +48,7 @@ class BackupsManager(object):
|
|||||||
endpoint = self.endpoint + backup_id
|
endpoint = self.endpoint + backup_id
|
||||||
r = requests.delete(endpoint, headers=self.headers)
|
r = requests.delete(endpoint, headers=self.headers)
|
||||||
if r.status_code != 204:
|
if r.status_code != 204:
|
||||||
raise exceptions.MetadataDeleteFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
|
||||||
|
|
||||||
def list(self, limit=10, offset=0, search=None):
|
def list(self, limit=10, offset=0, search=None):
|
||||||
"""
|
"""
|
||||||
@@ -70,8 +68,7 @@ class BackupsManager(object):
|
|||||||
r = requests.get(self.endpoint, headers=self.headers,
|
r = requests.get(self.endpoint, headers=self.headers,
|
||||||
params=query, data=data)
|
params=query, data=data)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise exceptions.MetadataGetFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
|
||||||
|
|
||||||
return r.json()['backups']
|
return r.json()['backups']
|
||||||
|
|
||||||
@@ -82,5 +79,4 @@ class BackupsManager(object):
|
|||||||
return r.json()
|
return r.json()
|
||||||
if r.status_code == 404:
|
if r.status_code == 404:
|
||||||
return None
|
return None
|
||||||
raise exceptions.MetadataGetFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
|
||||||
|
83
client.py
83
client.py
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Copyright 2014 Hewlett-Packard
|
Copyright 2015 Hewlett-Packard
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
@@ -19,23 +19,30 @@ Hudson (tjh@cryptsoft.com).
|
|||||||
========================================================================
|
========================================================================
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import socket
|
||||||
import sys
|
|
||||||
|
|
||||||
from openstackclient.identity import client as os_client
|
from openstackclient.identity import client as os_client
|
||||||
|
|
||||||
possible_topdir = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
|
from backups import BackupsManager
|
||||||
os.pardir, os.pardir, os.pardir))
|
from registration import RegistrationManager
|
||||||
if os.path.exists(os.path.join(possible_topdir, 'freezer', '__init__.py')):
|
from jobs import JobManager
|
||||||
sys.path.insert(0, possible_topdir)
|
|
||||||
|
|
||||||
from freezer.apiclient.backups import BackupsManager
|
|
||||||
from freezer.apiclient.registration import RegistrationManager
|
|
||||||
from freezer.apiclient.actions import ActionManager
|
|
||||||
from freezer.apiclient.configs import ConfigsManager
|
|
||||||
import exceptions
|
import exceptions
|
||||||
|
|
||||||
|
|
||||||
|
class cached_property(object):
|
||||||
|
|
||||||
|
def __init__(self, func):
|
||||||
|
self.__doc__ = getattr(func, '__doc__')
|
||||||
|
self.func = func
|
||||||
|
|
||||||
|
def __get__(self, obj, cls):
|
||||||
|
if obj is None:
|
||||||
|
return self
|
||||||
|
value = obj.__dict__[self.func.__name__] = self.func(obj)
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
class Client(object):
|
class Client(object):
|
||||||
|
|
||||||
def __init__(self, version='1',
|
def __init__(self, version='1',
|
||||||
@@ -54,18 +61,19 @@ class Client(object):
|
|||||||
self.auth_url = auth_url
|
self.auth_url = auth_url
|
||||||
self._endpoint = endpoint
|
self._endpoint = endpoint
|
||||||
self.session = session
|
self.session = session
|
||||||
self._auth = None
|
|
||||||
self.backups = BackupsManager(self)
|
self.backups = BackupsManager(self)
|
||||||
self.registration = RegistrationManager(self)
|
self.registration = RegistrationManager(self)
|
||||||
self.actions = ActionManager(self)
|
self.jobs = JobManager(self)
|
||||||
self.configs = ConfigsManager(self)
|
|
||||||
|
|
||||||
def _update_api_endpoint(self):
|
@cached_property
|
||||||
|
def endpoint(self):
|
||||||
|
if self._endpoint:
|
||||||
|
return self._endpoint
|
||||||
services = self.auth.services.list()
|
services = self.auth.services.list()
|
||||||
try:
|
try:
|
||||||
freezer_service = next(x for x in services if x.name == 'freezer')
|
freezer_service = next(x for x in services if x.name == 'freezer')
|
||||||
except:
|
except:
|
||||||
raise exceptions.AuthFailure(
|
raise exceptions.ApiClientException(
|
||||||
'freezer service not found in services list')
|
'freezer service not found in services list')
|
||||||
endpoints = self.auth.endpoints.list()
|
endpoints = self.auth.endpoints.list()
|
||||||
try:
|
try:
|
||||||
@@ -73,40 +81,37 @@ class Client(object):
|
|||||||
next(x for x in endpoints
|
next(x for x in endpoints
|
||||||
if x.service_id == freezer_service.id)
|
if x.service_id == freezer_service.id)
|
||||||
except:
|
except:
|
||||||
raise exceptions.AuthFailure(
|
raise exceptions.ApiClientException(
|
||||||
'freezer endpoint not found in endpoint list')
|
'freezer endpoint not found in endpoint list')
|
||||||
self._endpoint = freezer_endpoint.publicurl
|
return freezer_endpoint.publicurl
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def auth(self):
|
def auth(self):
|
||||||
if self._auth is None:
|
if self.username and self.password:
|
||||||
if self.username and self.password:
|
_auth = os_client.IdentityClientv2(
|
||||||
self._auth = os_client.IdentityClientv2(
|
auth_url=self.auth_url,
|
||||||
auth_url=self.auth_url,
|
username=self.username,
|
||||||
username=self.username,
|
password=self.password,
|
||||||
password=self.password,
|
tenant_name=self.tenant_name)
|
||||||
tenant_name=self.tenant_name)
|
elif self.token:
|
||||||
elif self.token:
|
_auth = os_client.IdentityClientv2(
|
||||||
self._auth = os_client.IdentityClientv2(
|
endpoint=self.auth_url,
|
||||||
endpoint=self.auth_url,
|
token=self.token)
|
||||||
token=self.token)
|
else:
|
||||||
else:
|
raise exceptions.ApiClientException("Missing auth credentials")
|
||||||
raise exceptions.AuthFailure("Missing auth credentials")
|
return _auth
|
||||||
return self._auth
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def auth_token(self):
|
def auth_token(self):
|
||||||
return self.auth.auth_token
|
return self.auth.auth_token
|
||||||
|
|
||||||
@property
|
|
||||||
def endpoint(self):
|
|
||||||
if self._endpoint is None:
|
|
||||||
self._update_api_endpoint()
|
|
||||||
return self._endpoint
|
|
||||||
|
|
||||||
def api_exists(self):
|
def api_exists(self):
|
||||||
try:
|
try:
|
||||||
if self.endpoint is not None:
|
if self.endpoint is not None:
|
||||||
return True
|
return True
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def client_id(self):
|
||||||
|
return '{0}_{1}'.format(self.auth.project_id, socket.gethostname())
|
||||||
|
@@ -23,60 +23,48 @@ import json
|
|||||||
|
|
||||||
|
|
||||||
class ApiClientException(Exception):
|
class ApiClientException(Exception):
|
||||||
def __init__(self, r):
|
@staticmethod
|
||||||
|
def get_message_from_api_response(r):
|
||||||
|
"""
|
||||||
|
returns a message based on information from a api-formatted response
|
||||||
|
if available, otherwise None
|
||||||
|
api-formatted response should be:
|
||||||
|
{
|
||||||
|
title: "error title string",
|
||||||
|
description: "error description string"
|
||||||
|
}
|
||||||
|
|
||||||
|
:param r: response object
|
||||||
|
:return: string with error message or None if it fails
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
body = json.loads(r.text)
|
body = json.loads(r.text)
|
||||||
message = "[*] Error {0}: {1}".format(
|
message = "[*] Error {0}: {1}".format(
|
||||||
r.status_code,
|
r.status_code,
|
||||||
body['description'])
|
body['description'])
|
||||||
except:
|
except:
|
||||||
message = r
|
message = None
|
||||||
|
return message
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def get_message_from_response(r):
|
||||||
|
"""
|
||||||
|
composes the error message using information eventually present
|
||||||
|
in the response (http error code and the http response body)
|
||||||
|
|
||||||
|
:param r: response object
|
||||||
|
:return: string with error message or None if it fails
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
message = "[*] Error {0}: {1}".format(
|
||||||
|
r.status_code,
|
||||||
|
r.text)
|
||||||
|
except:
|
||||||
|
message = None
|
||||||
|
return message
|
||||||
|
|
||||||
|
def __init__(self, r):
|
||||||
|
message = self.get_message_from_api_response(r) or \
|
||||||
|
self.get_message_from_response(r) or \
|
||||||
|
str(r)
|
||||||
super(ApiClientException, self).__init__(message)
|
super(ApiClientException, self).__init__(message)
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return self.message
|
|
||||||
|
|
||||||
|
|
||||||
class MetadataCreationFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
|
||||||
|
|
||||||
class MetadataGetFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
|
||||||
|
|
||||||
class MetadataDeleteFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
|
||||||
|
|
||||||
class AuthFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
|
||||||
|
|
||||||
class MetadataUpdateFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigCreationFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigGetFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigDeleteFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigUpdateFailure(ApiClientException):
|
|
||||||
def __init__(self, r=''):
|
|
||||||
super(self.__class__, self).__init__(r)
|
|
||||||
|
@@ -1,5 +1,5 @@
|
|||||||
"""
|
"""
|
||||||
Copyright 2014 Hewlett-Packard
|
Copyright 2015 Hewlett-Packard
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
@@ -22,63 +22,76 @@ Hudson (tjh@cryptsoft.com).
|
|||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from freezer.apiclient import exceptions
|
import exceptions
|
||||||
|
|
||||||
|
|
||||||
class ConfigsManager(object):
|
class JobManager(object):
|
||||||
|
|
||||||
def __init__(self, client):
|
def __init__(self, client):
|
||||||
self.client = client
|
self.client = client
|
||||||
self.endpoint = self.client.endpoint + '/v1/configs/'
|
self.endpoint = self.client.endpoint + '/v1/jobs/'
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def headers(self):
|
def headers(self):
|
||||||
return {'X-Auth-Token': self.client.auth_token}
|
return {'X-Auth-Token': self.client.auth_token}
|
||||||
|
|
||||||
def create(self, config_file):
|
def create(self, doc, job_id=''):
|
||||||
r = requests.post(self.endpoint,
|
job_id = job_id or doc.get('job_id', '')
|
||||||
data=json.dumps(config_file),
|
endpoint = self.endpoint + job_id
|
||||||
|
doc['client_id'] = doc.get('client_id', '') or self.client.client_id
|
||||||
|
r = requests.post(endpoint,
|
||||||
|
data=json.dumps(doc),
|
||||||
headers=self.headers)
|
headers=self.headers)
|
||||||
if r.status_code != 201:
|
if r.status_code != 201:
|
||||||
raise exceptions.ConfigCreationFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
job_id = r.json()['job_id']
|
||||||
config_id = r.json()['config_id']
|
return job_id
|
||||||
return config_id
|
|
||||||
|
|
||||||
def delete(self, config_id):
|
def delete(self, job_id):
|
||||||
endpoint = self.endpoint + config_id
|
endpoint = self.endpoint + job_id
|
||||||
r = requests.delete(endpoint, headers=self.headers)
|
r = requests.delete(endpoint, headers=self.headers)
|
||||||
if r.status_code != 204:
|
if r.status_code != 204:
|
||||||
raise exceptions.ConfigDeleteFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
|
||||||
|
|
||||||
def list(self, limit=10, offset=0, search=None):
|
def list_all(self, limit=10, offset=0, search=None):
|
||||||
data = json.dumps(search) if search else None
|
data = json.dumps(search) if search else None
|
||||||
query = {'limit': int(limit), 'offset': int(offset)}
|
query = {'limit': int(limit), 'offset': int(offset)}
|
||||||
r = requests.get(self.endpoint, headers=self.headers,
|
r = requests.get(self.endpoint, headers=self.headers,
|
||||||
params=query, data=data)
|
params=query, data=data)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise exceptions.ConfigGetFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
return r.json()['jobs']
|
||||||
|
|
||||||
return r.json()['configs']
|
def list(self, limit=10, offset=0, search={}, client_id=None):
|
||||||
|
client_id = client_id or self.client.client_id
|
||||||
|
new_search = search.copy()
|
||||||
|
new_search['match'] = search.get('match', [])
|
||||||
|
new_search['match'].append({'client_id': client_id})
|
||||||
|
return self.list_all(limit, offset, new_search)
|
||||||
|
|
||||||
def get(self, config_id):
|
def get(self, job_id):
|
||||||
endpoint = self.endpoint + config_id
|
endpoint = self.endpoint + job_id
|
||||||
r = requests.get(endpoint, headers=self.headers)
|
r = requests.get(endpoint, headers=self.headers)
|
||||||
if r.status_code == 200:
|
if r.status_code == 200:
|
||||||
return r.json()
|
return r.json()
|
||||||
if r.status_code == 404:
|
if r.status_code == 404:
|
||||||
return None
|
return None
|
||||||
raise exceptions.ConfigGetFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
|
||||||
|
|
||||||
def update(self, config_id, update_doc):
|
def update(self, job_id, update_doc):
|
||||||
endpoint = self.endpoint + config_id
|
endpoint = self.endpoint + job_id
|
||||||
r = requests.patch(endpoint,
|
r = requests.patch(endpoint,
|
||||||
headers=self.headers,
|
headers=self.headers,
|
||||||
data=json.dumps(update_doc))
|
data=json.dumps(update_doc))
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise exceptions.ConfigUpdateFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}: {1}".format(r.status_code, r.text))
|
|
||||||
return r.json()['version']
|
return r.json()['version']
|
||||||
|
|
||||||
|
def start_job(self, job_id):
|
||||||
|
return self.update(job_id, {'job_schedule': {'event': 'start'}})
|
||||||
|
|
||||||
|
def stop_job(self, job_id):
|
||||||
|
return self.update(job_id, {'job_schedule': {'event': 'stop'}})
|
||||||
|
|
||||||
|
def abort_job(self, job_id):
|
||||||
|
return self.update(job_id, {'job_schedule': {'event': 'abort'}})
|
@@ -22,7 +22,7 @@ Hudson (tjh@cryptsoft.com).
|
|||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from freezer.apiclient import exceptions
|
import exceptions
|
||||||
|
|
||||||
|
|
||||||
class RegistrationManager(object):
|
class RegistrationManager(object):
|
||||||
@@ -40,8 +40,7 @@ class RegistrationManager(object):
|
|||||||
data=json.dumps(client_info),
|
data=json.dumps(client_info),
|
||||||
headers=self.headers)
|
headers=self.headers)
|
||||||
if r.status_code != 201:
|
if r.status_code != 201:
|
||||||
raise exceptions.MetadataCreationFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}: {1}".format(r.status_code, r.text))
|
|
||||||
client_id = r.json()['client_id']
|
client_id = r.json()['client_id']
|
||||||
return client_id
|
return client_id
|
||||||
|
|
||||||
@@ -49,8 +48,7 @@ class RegistrationManager(object):
|
|||||||
endpoint = self.endpoint + client_id
|
endpoint = self.endpoint + client_id
|
||||||
r = requests.delete(endpoint, headers=self.headers)
|
r = requests.delete(endpoint, headers=self.headers)
|
||||||
if r.status_code != 204:
|
if r.status_code != 204:
|
||||||
raise exceptions.MetadataDeleteFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
|
||||||
|
|
||||||
def list(self, limit=10, offset=0, search=None):
|
def list(self, limit=10, offset=0, search=None):
|
||||||
"""
|
"""
|
||||||
@@ -74,8 +72,7 @@ class RegistrationManager(object):
|
|||||||
r = requests.get(self.endpoint, headers=self.headers,
|
r = requests.get(self.endpoint, headers=self.headers,
|
||||||
params=query, data=data)
|
params=query, data=data)
|
||||||
if r.status_code != 200:
|
if r.status_code != 200:
|
||||||
raise exceptions.MetadataGetFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}: {1}".format(r.status_code, r.text))
|
|
||||||
return r.json()['clients']
|
return r.json()['clients']
|
||||||
|
|
||||||
def get(self, client_id):
|
def get(self, client_id):
|
||||||
@@ -85,5 +82,4 @@ class RegistrationManager(object):
|
|||||||
return r.json()
|
return r.json()
|
||||||
if r.status_code == 404:
|
if r.status_code == 404:
|
||||||
return None
|
return None
|
||||||
raise exceptions.MetadataGetFailure(
|
raise exceptions.ApiClientException(r)
|
||||||
"[*] Error {0}".format(r.status_code))
|
|
||||||
|
Reference in New Issue
Block a user