本文整理汇总了Python中api.serializers.InstanceSerializer类的典型用法代码示例。如果您正苦于以下问题:Python InstanceSerializer类的具体用法?Python InstanceSerializer怎么用?Python InstanceSerializer使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了InstanceSerializer类的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_instance_from_metadata
def set_instance_from_metadata(esh_driver, core_instance):
"""
NOT BEING USED ANYMORE.. DEPRECATED..
"""
# Fixes Dep. loop - Do not remove
from api.serializers import InstanceSerializer
# Breakout for drivers (Eucalyptus) that don't support metadata
if not hasattr(esh_driver._connection, 'ex_get_metadata'):
# logger.debug("EshDriver %s does not have function 'ex_get_metadata'"
# % esh_driver._connection.__class__)
return core_instance
try:
esh_instance = esh_driver.get_instance(core_instance.provider_alias)
if not esh_instance:
return core_instance
metadata = esh_driver._connection.ex_get_metadata(esh_instance)
except Exception:
logger.exception("Exception retrieving instance metadata for %s" %
core_instance.provider_alias)
return core_instance
# TODO: Match with actual instance launch metadata in service/instance.py
# TODO: Probably best to redefine serializer as InstanceMetadataSerializer
# TODO: Define a creator and their identity by the METADATA instead of
# assuming its the person who 'found' the instance
serializer = InstanceSerializer(core_instance, data=metadata,
partial=True)
if not serializer.is_valid():
logger.warn("Encountered errors serializing metadata:%s"
% serializer.errors)
return core_instance
core_instance = serializer.save()
core_instance.esh = esh_instance
return core_instance
示例2: put
def put(self, request, provider_id, identity_id, instance_id):
"""Authentication Required, update metadata about the instance"""
user = request.user
data = request.DATA
#Ensure item exists on the server first
esh_driver = prepare_driver(request, provider_id, identity_id)
if not esh_driver:
return invalid_creds(provider_id, identity_id)
esh_instance = esh_driver.get_instance(instance_id)
if not esh_instance:
return instance_not_found(instance_id)
#Gather the DB related item and update
core_instance = convert_esh_instance(esh_driver, esh_instance,
provider_id, identity_id, user)
serializer = InstanceSerializer(core_instance, data=data,
context={"request":request})
if serializer.is_valid():
logger.info('metadata = %s' % data)
update_instance_metadata(esh_driver, esh_instance, data)
serializer.save()
response = Response(serializer.data)
logger.info('data = %s' % serializer.data)
response['Cache-Control'] = 'no-cache'
return response
else:
return Response(serializer.errors, status=status.HTTP_400)
示例3: put
def put(self, request, provider_id, identity_id, instance_id):
"""
TODO:
Options for put
- Instance status change (suspend,resume,etc.)
- DB changes (Name, tags)
"""
user = request.user
data = request.DATA
#Ensure item exists on the server first
esh_driver = prepare_driver(request, identity_id)
try:
esh_instance = esh_driver.get_instance(instance_id)
except InvalidCredsError:
return invalid_creds(provider_id, identity_id)
if not esh_instance:
return instance_not_found(instance_id)
#Gather the DB related item and update
core_instance = convert_esh_instance(esh_driver, esh_instance,
provider_id, identity_id, user)
serializer = InstanceSerializer(core_instance, data=data)
if serializer.is_valid():
logger.info('metadata = %s' % data)
update_instance_metadata(esh_driver, esh_instance, data)
serializer.save()
response = Response(serializer.data)
logger.info('data = %s' % serializer.data)
response['Cache-Control'] = 'no-cache'
return response
else:
return Response(serializer.errors, status=status.HTTP_400)
示例4: patch
def patch(self, request, provider_id, identity_id, instance_id):
"""
"""
user = request.user
data = request.DATA
#Ensure item exists on the server first
esh_driver = prepare_driver(request, identity_id)
try:
esh_instance = esh_driver.get_instance(instance_id)
except InvalidCredsError:
return invalid_creds(provider_id, identity_id)
if not esh_instance:
return instance_not_found(instance_id)
#Gather the DB related item and update
core_instance = convert_esh_instance(esh_driver, esh_instance,
provider_id, identity_id, user)
serializer = InstanceSerializer(core_instance, data=data, partial=True)
if serializer.is_valid():
logger.info('metadata = %s' % data)
update_instance_metadata(esh_driver, esh_instance, data)
serializer.save()
response = Response(serializer.data)
logger.info('data = %s' % serializer.data)
response['Cache-Control'] = 'no-cache'
return response
else:
return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
示例5: put
def put(self, request, provider_uuid, identity_uuid, instance_id):
"""Authentication Required, update metadata about the instance"""
user = request.user
data = request.DATA
# Ensure item exists on the server first
esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
if not esh_driver:
return invalid_creds(provider_uuid, identity_uuid)
try:
esh_instance = esh_driver.get_instance(instance_id)
except ConnectionFailure:
return connection_failure(provider_uuid, identity_uuid)
except InvalidCredsError:
return invalid_creds(provider_uuid, identity_uuid)
except Exception as exc:
logger.exception("Encountered a generic exception. "
"Returning 409-CONFLICT")
return failure_response(status.HTTP_409_CONFLICT,
str(exc.message))
if not esh_instance:
return instance_not_found(instance_id)
# Gather the DB related item and update
core_instance = convert_esh_instance(esh_driver, esh_instance,
provider_uuid, identity_uuid,
user)
serializer = InstanceSerializer(core_instance, data=data,
context={"request": request})
if serializer.is_valid():
logger.info('metadata = %s' % data)
update_instance_metadata(esh_driver, esh_instance, data)
serializer.save()
new_instance = serializer.object
boot_scripts = data.pop('boot_scripts', [])
if boot_scripts:
new_instance = _save_scripts_to_instance(new_instance,
boot_scripts)
serializer = InstanceSerializer(
new_instance,
context={"request": request})
invalidate_cached_instances(
identity=Identity.objects.get(
uuid=identity_uuid))
response = Response(serializer.data)
logger.info('data = %s' % serializer.data)
response['Cache-Control'] = 'no-cache'
return response
else:
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
示例6: size_not_availabe
except SizeNotAvailable, snae:
return size_not_availabe(snae)
except SecurityGroupNotCreated:
return connection_failure(provider_id, identity_id)
except ConnectionFailure:
return connection_failure(provider_id, identity_id)
except InvalidCredsError:
return invalid_creds(provider_id, identity_id)
except Exception as exc:
logger.exception("Encountered a generic exception. "
"Returning 409-CONFLICT")
return failure_response(status.HTTP_409_CONFLICT,
str(exc.message))
serializer = InstanceSerializer(core_instance,
context={"request":request},
data=data)
#NEVER WRONG
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
else:
return Response(serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
def _sort_instance_history(history_instance_list, sort_by, descending=False):
#Using the 'sort_by' variable, sort the list:
if not sort_by or 'end_date' in sort_by:
return sorted(history_instance_list, key=lambda ish:
ish.end_date if ish.end_date else timezone.now(),
示例7: InstanceSerializer
try:
esh_instance = esh_driver.get_instance(core_instance.provider_alias)
if not esh_instance:
return core_instance
metadata = esh_driver._connection.ex_get_metadata(esh_instance)
except Exception, e:
logger.exception("Exception retrieving instance metadata for %s" %
core_instance.provider_alias)
return core_instance
#TODO: Match with actual instance launch metadata in service/instance.py
#TODO: Probably better to redefine serializer as InstanceMetadataSerializer
#TODO: Define a creator and their identity by the METADATA instead of
# assuming its the person who 'found' the instance
serializer = InstanceSerializer(core_instance, data=metadata,
partial=True)
if not serializer.is_valid():
logger.warn("Encountered errors serializing metadata:%s"
% serializer.errors)
return core_instance
core_instance = serializer.save()
core_instance.esh = esh_instance
return core_instance
def create_instance(provider_uuid, identity_uuid, provider_alias, instance_source,
ip_address, name, creator, create_stamp,
token=None, password=None):
#TODO: Define a creator and their identity by the METADATA instead of
# assuming its the person who 'found' the instance
identity = Identity.objects.get(uuid=identity_uuid)
new_inst = Instance.objects.create(name=name,