HEAD method to obtain whatever an entity exist or not (#19)

This commit is contained in:
Marx314 2016-05-30 17:13:05 -04:00 committed by Frédéric Guillot
parent 5c16300397
commit 87de8d1f0d
6 changed files with 200 additions and 122 deletions

View File

@ -277,6 +277,16 @@ def update_instance_entity(instance_id):
return result
@api.route("/entity/<entity_id>", methods=["HEAD"])
@authenticated
def entity_exists(entity_id):
logging.info("Does entity with id %s exists", entity_id)
response = Response('', 404)
if controller.entity_exists(entity_id=entity_id):
response = Response('', 200)
return response
@api.route("/volume_types", methods=["GET"])
@authenticated
@to_json

View File

@ -42,17 +42,6 @@ class Controller(object):
"active_entities": self.database_adapter.count_active_entities()}
}
def _fresher_entity_exists(self, entity_id, date):
try:
entity = self.database_adapter.get_active_entity(entity_id)
if entity and entity.last_event > date:
return True
except KeyError:
pass
except NotImplementedError:
pass
return False
def create_instance(self, instance_id, tenant_id, create_date, flavor, os_type, distro, version, name, metadata):
create_date = self._validate_and_parse_date(create_date)
logging.info("instance %s created in project %s (flavor %s; distro %s %s %s) on %s" % (
@ -133,6 +122,18 @@ class Controller(object):
logging.error("Instance '{0}' is not in the database yet.".format(instance_id))
raise e
def entity_exists(self, entity_id):
return self.database_adapter.count_entity_entries(entity_id=entity_id) >= 1
def attach_volume(self, volume_id, date, attachments):
date = self._validate_and_parse_date(date)
logging.info("volume %s attached to %s on %s" % (volume_id, attachments, date))
try:
self._volume_attach_instance(volume_id, date, attachments)
except KeyError as e:
logging.error("Trying to attach a volume with id '%s' not in the database yet." % volume_id)
raise e
def create_volume(self, volume_id, project_id, start, volume_type, size, volume_name, attached_to=None):
start = self._validate_and_parse_date(start)
logging.info("volume %s created in project %s to size %s on %s" % (volume_id, project_id, size, start))
@ -144,22 +145,6 @@ class Controller(object):
entity = Volume(volume_id, project_id, start, None, volume_type_name, size, start, volume_name, attached_to)
self.database_adapter.insert_entity(entity)
def _get_volume_type_name(self, volume_type_id):
if volume_type_id is None:
return None
volume_type = self.database_adapter.get_volume_type(volume_type_id)
return volume_type.volume_type_name
def attach_volume(self, volume_id, date, attachments):
date = self._validate_and_parse_date(date)
logging.info("volume %s attached to %s on %s" % (volume_id, attachments, date))
try:
self._volume_attach_instance(volume_id, date, attachments)
except KeyError as e:
logging.error("Trying to attach a volume with id '%s' not in the database yet." % volume_id)
raise e
def detach_volume(self, volume_id, date, attachments):
date = self._validate_and_parse_date(date)
logging.info("volume %s detached on %s" % (volume_id, date))
@ -169,6 +154,88 @@ class Controller(object):
logging.error("Trying to detach a volume with id '%s' not in the database yet." % volume_id)
raise e
def rename_volume(self, volume_id, volume_name):
try:
volume = self.database_adapter.get_active_entity(volume_id)
if volume and volume.name != volume_name:
logging.info("volume %s renamed from %s to %s" % (volume_id, volume.name, volume_name))
volume.name = volume_name
self.database_adapter.update_active_entity(volume)
except KeyError:
logging.error("Trying to update a volume with id '%s' not in the database yet." % volume_id)
def resize_volume(self, volume_id, size, update_date):
update_date = self._validate_and_parse_date(update_date)
try:
volume = self.database_adapter.get_active_entity(volume_id)
logging.info("volume %s updated in project %s to size %s on %s" % (volume_id, volume.project_id, size,
update_date))
self.database_adapter.close_active_entity(volume_id, update_date)
volume.size = size
volume.start = update_date
volume.end = None
volume.last_event = update_date
self.database_adapter.insert_entity(volume)
except KeyError as e:
logging.error("Trying to update a volume with id '%s' not in the database yet." % volume_id)
raise e
def delete_volume(self, volume_id, delete_date):
delete_date = self._localize_date(self._validate_and_parse_date(delete_date))
logging.info("volume %s deleted on %s" % (volume_id, delete_date))
try:
if self.database_adapter.count_entity_entries(volume_id) > 1:
volume = self.database_adapter.get_active_entity(volume_id)
if delete_date - volume.start < self.volume_existence_threshold:
self.database_adapter.delete_active_entity(volume_id)
return
self.database_adapter.close_active_entity(volume_id, delete_date)
except KeyError as e:
logging.error("Trying to delete a volume with id '%s' not in the database yet." % volume_id)
raise e
def create_volume_type(self, volume_type_id, volume_type_name):
logging.info("volume type %s with name %s created" % (volume_type_id, volume_type_name))
volume_type = VolumeType(volume_type_id, volume_type_name)
self.database_adapter.insert_volume_type(volume_type)
def list_entities(self, project_id, start, end):
return self.database_adapter.list_entities(project_id, start, end)
def list_instances(self, project_id, start, end):
return self.database_adapter.list_entities(project_id, start, end, Instance.TYPE)
def list_volumes(self, project_id, start, end):
return self.database_adapter.list_entities(project_id, start, end, Volume.TYPE)
def get_volume_type(self, type_id):
return self.database_adapter.get_volume_type(type_id)
def delete_volume_type(self, type_id):
self.database_adapter.delete_volume_type(type_id)
def list_volume_types(self):
return self.database_adapter.list_volume_types()
def _fresher_entity_exists(self, entity_id, date):
try:
entity = self.database_adapter.get_active_entity(entity_id)
if entity and entity.last_event > date:
return True
except KeyError:
pass
except NotImplementedError:
pass
return False
def _get_volume_type_name(self, volume_type_id):
if volume_type_id is None:
return None
volume_type = self.database_adapter.get_volume_type(volume_type_id)
return volume_type.volume_type_name
def _update_instance_object(self, instance, **kwargs):
for key, value in self._transform_attribute_to_match_entity_attribute(**kwargs).items():
setattr(instance, key, value)
@ -217,70 +284,6 @@ class Controller(object):
volume.end = None
self.database_adapter.insert_entity(volume)
def rename_volume(self, volume_id, volume_name):
try:
volume = self.database_adapter.get_active_entity(volume_id)
if volume and volume.name != volume_name:
logging.info("volume %s renamed from %s to %s" % (volume_id, volume.name, volume_name))
volume.name = volume_name
self.database_adapter.update_active_entity(volume)
except KeyError:
logging.error("Trying to update a volume with id '%s' not in the database yet." % volume_id)
def resize_volume(self, volume_id, size, update_date):
update_date = self._validate_and_parse_date(update_date)
try:
volume = self.database_adapter.get_active_entity(volume_id)
logging.info("volume %s updated in project %s to size %s on %s" % (volume_id, volume.project_id, size,
update_date))
self.database_adapter.close_active_entity(volume_id, update_date)
volume.size = size
volume.start = update_date
volume.end = None
volume.last_event = update_date
self.database_adapter.insert_entity(volume)
except KeyError as e:
logging.error("Trying to update a volume with id '%s' not in the database yet." % volume_id)
raise e
def delete_volume(self, volume_id, delete_date):
delete_date = self._localize_date(self._validate_and_parse_date(delete_date))
logging.info("volume %s deleted on %s" % (volume_id, delete_date))
try:
if self.database_adapter.count_entity_entries(volume_id) > 1:
volume = self.database_adapter.get_active_entity(volume_id)
if delete_date - volume.start < self.volume_existence_threshold:
self.database_adapter.delete_active_entity(volume_id)
return
self.database_adapter.close_active_entity(volume_id, delete_date)
except KeyError as e:
logging.error("Trying to delete a volume with id '%s' not in the database yet." % volume_id)
raise e
def create_volume_type(self, volume_type_id, volume_type_name):
logging.info("volume type %s with name %s created" % (volume_type_id, volume_type_name))
volume_type = VolumeType(volume_type_id, volume_type_name)
self.database_adapter.insert_volume_type(volume_type)
def list_instances(self, project_id, start, end):
return self.database_adapter.list_entities(project_id, start, end, Instance.TYPE)
def list_volumes(self, project_id, start, end):
return self.database_adapter.list_entities(project_id, start, end, Volume.TYPE)
def list_entities(self, project_id, start, end):
return self.database_adapter.list_entities(project_id, start, end)
def get_volume_type(self, type_id):
return self.database_adapter.get_volume_type(type_id)
def delete_volume_type(self, type_id):
self.database_adapter.delete_volume_type(type_id)
def list_volume_types(self):
return self.database_adapter.list_volume_types()
def _filter_metadata_with_whitelist(self, metadata):
return {key: value for key, value in metadata.items() if key in self.metadata_whitelist}

View File

@ -40,6 +40,12 @@ class AlmanachHelper(object):
headers=headers if headers else self._get_query_headers()
)
def head(self, url, headers=None, **params):
return requests.head(
url.format(url=self.base_url, **params),
headers=headers if headers else self._get_query_headers()
)
def post(self, url, data, **params):
return requests.post(
url.format(url=self.base_url, **params),

View File

@ -0,0 +1,19 @@
from hamcrest import assert_that, equal_to
from base_api_testcase import BaseApiTestCase
class ApiInstanceEntityTest(BaseApiTestCase):
def test_head_entity_by_id_with_entity_return_200(self):
instance_id = self._create_instance_entity()
response = self.almanachHelper.head(url="{url}/entity/{instance_id}",
instance_id=instance_id)
assert_that(response.status_code, equal_to(200))
def test_head_entity_by_id__without_entity_return_404(self):
instance_id = "some_uuid"
response = self.almanachHelper.head(url="{url}/entity/{instance_id}",
instance_id=instance_id)
assert_that(response.status_code, equal_to(404))

View File

@ -20,7 +20,7 @@ from datetime import datetime, timedelta
import pytz
from dateutil.parser import parse
from hamcrest import raises, calling, assert_that
from hamcrest import raises, calling, assert_that, equal_to
from flexmock import flexmock, flexmock_teardown
from nose.tools import assert_raises
@ -723,6 +723,24 @@ class ControllerTest(unittest.TestCase):
"2015-10-21T16:25:00.000000Z"
)
def test_entity_exists(self):
entity_id = "some_entity_id"
(flexmock(self.database_adapter)
.should_receive("count_entity_entries")
.with_args(entity_id=entity_id)
.and_return(1))
assert_that(True, equal_to(self.controller.entity_exists(entity_id)))
def test_entity_exists_not(self):
entity_id = "some_entity_id"
(flexmock(self.database_adapter)
.should_receive("count_entity_entries")
.with_args(entity_id=entity_id)
.and_return(0))
assert_that(False, equal_to(self.controller.entity_exists(entity_id)))
def test_rename_volume(self):
fake_volume = a(volume().with_display_name('old_volume_name'))

View File

@ -888,7 +888,6 @@ class ApiTest(TestCase):
"Format should be of yyyy-mm-ddThh:mm:ss.msZ, ex: 2015-01-31T18:24:34.1523Z"
}
))
assert_that(code, equal_to(400))
def test_rebuild_instance_wrong_authentication(self):
self.having_config('api_auth_token', 'some token value')
@ -897,35 +896,6 @@ class ApiTest(TestCase):
code, result = self.api_put('/instance/INSTANCE_ID/rebuild', headers={'X-Auth-Token': 'oops'})
assert_that(code, equal_to(401))
def api_get(self, url, query_string=None, headers=None, accept='application/json'):
return self._api_call(url, "get", None, query_string, headers, accept)
def api_post(self, url, data=None, query_string=None, headers=None, accept='application/json'):
return self._api_call(url, "post", data, query_string, headers, accept)
def api_put(self, url, data=None, query_string=None, headers=None, accept='application/json'):
return self._api_call(url, "put", data, query_string, headers, accept)
def api_delete(self, url, query_string=None, data=None, headers=None, accept='application/json'):
return self._api_call(url, "delete", data, query_string, headers, accept)
def _api_call(self, url, method, data=None, query_string=None, headers=None, accept='application/json'):
with self.app.test_client() as http_client:
if not headers:
headers = {}
headers['Accept'] = accept
result = getattr(http_client, method)(url, data=json.dumps(data), query_string=query_string, headers=headers)
return_data = json.loads(result.data) \
if result.headers.get('Content-Type') == 'application/json' \
else result.data
return result.status_code, return_data
@staticmethod
def having_config(key, value):
(flexmock(config)
.should_receive(key)
.and_return(value))
def test_update_active_instance_entity_with_wrong_attribute_exception(self):
errors = [
Invalid(message="error message1", path=["my_attribute1"]),
@ -958,6 +928,58 @@ class ApiTest(TestCase):
}))
assert_that(code, equal_to(400))
def test_entity_head_with_existing_entity(self):
entity_id = "entity_id"
self.having_config('api_auth_token', 'some token value')
self.controller.should_receive('entity_exists') \
.and_return(True)
code, result = self.api_head('/entity/{id}'.format(id=entity_id), headers={'X-Auth-Token': 'some token value'})
assert_that(code, equal_to(200))
def test_entity_head_with_nonexistent_entity(self):
entity_id = "entity_id"
self.having_config('api_auth_token', 'some token value')
self.controller.should_receive('entity_exists') \
.and_return(False)
code, result = self.api_head('/entity/{id}'.format(id=entity_id), headers={'X-Auth-Token': 'some token value'})
assert_that(code, equal_to(404))
def api_get(self, url, query_string=None, headers=None, accept='application/json'):
return self._api_call(url, "get", None, query_string, headers, accept)
def api_head(self, url, headers):
return self._api_call(url=url, method="head", headers=headers, accept='application/json')
def api_post(self, url, data=None, query_string=None, headers=None, accept='application/json'):
return self._api_call(url, "post", data, query_string, headers, accept)
def api_put(self, url, data=None, query_string=None, headers=None, accept='application/json'):
return self._api_call(url, "put", data, query_string, headers, accept)
def api_delete(self, url, query_string=None, data=None, headers=None, accept='application/json'):
return self._api_call(url, "delete", data, query_string, headers, accept)
def _api_call(self, url, method, data=None, query_string=None, headers=None, accept='application/json'):
with self.app.test_client() as http_client:
if not headers:
headers = {}
headers['Accept'] = accept
result = getattr(http_client, method)(url, data=json.dumps(data), query_string=query_string, headers=headers)
return_data = json.loads(result.data) \
if result.headers.get('Content-Type') == 'application/json' \
else result.data
return result.status_code, return_data
@staticmethod
def having_config(key, value):
(flexmock(config)
.should_receive(key)
.and_return(value))
class DateMatcher(object):
def __init__(self, date):