예제 #1
0
 def patch(self, request, provider_uuid, identity_uuid, instance_id):
     """Authentication Required, update metadata about the instance"""
     user = request.user
     data = request.DATA
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     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_uuid, identity_uuid, user)
     serializer = InstanceSerializer(core_instance, data=data,
                                     context={"request":request}, partial=True)
     if serializer.is_valid():
         logger.info('metadata = %s' % data)
         update_instance_metadata(esh_driver, esh_instance, data,
                 replace=False)
         serializer.save()
         boot_scripts = data.pop('boot_scripts', [])
         if boot_scripts:
             _save_scripts_to_instance(serializer.object, boot_scripts)
         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)
예제 #2
0
    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)
예제 #3
0
def set_instance_from_metadata(esh_driver, core_instance):
    #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
    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)

    #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
    serializer.save()
    core_instance = serializer.object
    core_instance.esh = esh_instance
    return core_instance
예제 #4
0
 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)
예제 #5
0
 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, 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)
     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)
예제 #6
0
 def patch(self, request, provider_id, identity_id, instance_id):
     """
     """
     user = request.user
     data = request.DATA
     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, 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)
예제 #7
0
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
예제 #8
0
    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)
예제 #9
0
 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)
예제 #10
0
 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)
예제 #11
0
 def delete(self, request, provider_id, identity_id, instance_id):
     user = request.user
     esh_driver = prepare_driver(request, provider_id, identity_id)
     if not esh_driver:
         return invalid_creds(provider_id, identity_id)
     try:
         esh_instance = esh_driver.get_instance(instance_id)
         if not esh_instance:
             return instance_not_found(instance_id)
         task.destroy_instance_task(esh_instance, identity_id)
         existing_instance = esh_driver.get_instance(instance_id)
         if existing_instance:
             #Instance will be deleted soon...
             esh_instance = existing_instance
             if esh_instance.extra\
                and 'task' not in esh_instance.extra:
                 esh_instance.extra['task'] = 'queueing delete'
         core_instance = convert_esh_instance(esh_driver, esh_instance,
                                              provider_id, identity_id,
                                              user)
         if core_instance:
             core_instance.end_date_all()
         serialized_data = InstanceSerializer(core_instance).data
         response = Response(serialized_data, status=status.HTTP_200_OK)
         response['Cache-Control'] = 'no-cache'
         return response
     except InvalidCredsError:
         return invalid_creds(provider_id, identity_id)
예제 #12
0
 def get(self, request, provider_uuid, identity_uuid):
     """
     Returns a list of all instances
     """
     user = request.user
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     identity = Identity.objects.get(uuid=identity_uuid)
     try:
         esh_instance_list = get_cached_instances(identity=identity)
     except MalformedResponseError:
         return malformed_response(provider_uuid, identity_uuid)
     except InvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     core_instance_list = [convert_esh_instance(esh_driver,
                                                inst,
                                                provider_uuid,
                                                identity_uuid,
                                                user)
                           for inst in esh_instance_list]
     #TODO: Core/Auth checks for shared instances
     serialized_data = InstanceSerializer(core_instance_list,
                                          context={"request":request},
                                          many=True).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response
예제 #13
0
    def delete(self, request, provider_id, identity_id, instance_id):
        """Authentication Required, TERMINATE the instance.

        Be careful, there is no going back once you've deleted an instance.
        """
        user = request.user
        esh_driver = prepare_driver(request, provider_id, identity_id)
        if not esh_driver:
            return invalid_creds(provider_id, identity_id)
        try:
            esh_instance = esh_driver.get_instance(instance_id)
            if not esh_instance:
                return instance_not_found(instance_id)
            task.destroy_instance_task(esh_instance, identity_id)
            existing_instance = esh_driver.get_instance(instance_id)
            if existing_instance:
                #Instance will be deleted soon...
                esh_instance = existing_instance
                if esh_instance.extra\
                   and 'task' not in esh_instance.extra:
                    esh_instance.extra['task'] = 'queueing delete'
            core_instance = convert_esh_instance(esh_driver, esh_instance,
                                                 provider_id, identity_id,
                                                 user)
            if core_instance:
                core_instance.end_date_all()
            serialized_data = InstanceSerializer(core_instance,
                                                 context={
                                                     'user': request.user
                                                 }).data
            response = Response(serialized_data, status=status.HTTP_200_OK)
            response['Cache-Control'] = 'no-cache'
            return response
        except InvalidCredsError:
            return invalid_creds(provider_id, identity_id)
예제 #14
0
    def get(self, request, provider_id, identity_id):
        """
        Returns a list of all instances
        """
        user = request.user
        esh_driver = prepare_driver(request, provider_id, identity_id)
        if not esh_driver:
            return invalid_creds(provider_id, identity_id)

        instance_list_method = esh_driver.list_instances

        if AccountProvider.objects.filter(identity__id=identity_id):
            # Instance list method changes when using the OPENSTACK provider
            instance_list_method = esh_driver.list_all_instances
        try:
            esh_instance_list = instance_list_method()
        except InvalidCredsError:
            return invalid_creds(provider_id, identity_id)

        core_instance_list = [
            convert_esh_instance(esh_driver, inst, provider_id, identity_id,
                                 user) for inst in esh_instance_list
        ]

        #TODO: Core/Auth checks for shared instances

        serialized_data = InstanceSerializer(core_instance_list,
                                             context={
                                                 'user': request.user
                                             },
                                             many=True).data
        response = Response(serialized_data)
        response['Cache-Control'] = 'no-cache'
        return response
예제 #15
0
 def get(self, request, provider_id, identity_id, instance_id):
     """
     Authentication Required, get instance details.
     """
     user = request.user
     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:
         try:
             core_inst = CoreInstance.objects.get(
                 provider_alias=instance_id,
                 provider_machine__provider__id=provider_id,
                 created_by_identity__id=identity_id)
             core_inst.end_date_all()
         except CoreInstance.DoesNotExist:
             pass
         return instance_not_found(instance_id)
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_id, identity_id, user)
     serialized_data = InstanceSerializer(core_instance,
                                          context={
                                              "request": request
                                          }).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response
예제 #16
0
class BootVolume(APIView):
    """Launch an instance using this volume as the source"""
    permission_classes = (ApiAuthRequired, )

    def _select_source_key(self, data):
        if 'image_id' in data:
            return "image_id"
        elif 'snapshot_id' in data:
            return "snapshot_id"
        elif 'volume_id' in data:
            return "volume_id"
        else:
            return None

    def post(self, request, provider_uuid, identity_uuid, volume_id=None):
        user = request.user
        data = request.DATA

        missing_keys = valid_launch_data(data)
        if missing_keys:
            return keys_not_found(missing_keys)
        source = None
        name = data.pop('name')
        size_id = data.pop('size')
        key_name = self._select_source_key(data)
        if not key_name:
            return failure_response(
                status.HTTP_400_BAD_REQUEST,
                'Source could not be acquired. Did you send: ['
                'snapshot_id/volume_id/image_id] ?')
        try:
            core_instance = create_bootable_volume(request.user,
                                                   provider_uuid,
                                                   identity_uuid,
                                                   name,
                                                   size_id,
                                                   source_alias,
                                                   source_hint=key_name,
                                                   **data)
        except Exception, exc:
            message = exc.message
            return failure_response(status.HTTP_409_CONFLICT, message)

        serialized_data = InstanceSerializer(core_instance,
                                             context={
                                                 'request': request
                                             }).data
        response = Response(serialized_data)
        return response
예제 #17
0
 def get(self, request):
     """
     """
     user = request.user
     instances = user.instance_set.filter(
         only_active(),
         provider_machine__provider__active=True,
         projects=None)
     serialized_data = InstanceSerializer(instances,
                                          many=True,
                                          context={
                                              "request": request
                                          }).data
     response = Response(serialized_data)
     return response
예제 #18
0
 def get(self, request, project_id):
     """
     """
     user = request.user
     group = get_user_group(user.username)
     #TODO: Check that you have permission!
     projects = group.projects.get(id=project_id)
     instances = projects.instances.filter(only_current)
     serialized_data = InstanceSerializer(instances,
                                          many=True,
                                          context={
                                              "user": request.user
                                          }).data
     response = Response(serialized_data)
     return response
예제 #19
0
    def delete(self, request, provider_uuid, identity_uuid, instance_id):
        """Authentication Required, TERMINATE the instance.

        Be careful, there is no going back once you've deleted an instance.
        """
        user = request.user
        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)
            if not esh_instance:
                return instance_not_found(instance_id)
            #Test that there is not an attached volume BEFORE we destroy
            _check_volume_attachment(esh_driver, esh_instance)
            task.destroy_instance_task(esh_instance, identity_uuid)
            invalidate_cached_instances(identity=Identity.objects.get(uuid=identity_uuid))
            existing_instance = esh_driver.get_instance(instance_id)
            if existing_instance:
                #Instance will be deleted soon...
                esh_instance = existing_instance
                if esh_instance.extra\
                   and 'task' not in esh_instance.extra:
                    esh_instance.extra['task'] = 'queueing delete'
            core_instance = convert_esh_instance(esh_driver, esh_instance,
                                                 provider_uuid, identity_uuid,
                                                 user)
            if core_instance:
                core_instance.end_date_all()
            else:
                logger.warn("Unable to find core instance %s." % (instance_id))
            serialized_data = InstanceSerializer(core_instance,
                                                 context={"request":request}).data
            response = Response(serialized_data, status=status.HTTP_200_OK)
            response['Cache-Control'] = 'no-cache'
            return response
        except (Identity.DoesNotExist) as exc:
            return failure_response(status.HTTP_400_BAD_REQUEST,
                                    "Invalid provider_uuid or identity_uuid.")
        except VolumeAttachConflict as exc:
            message = exc.message
            return failure_response(status.HTTP_409_CONFLICT, message)
        except ConnectionFailure:
            return connection_failure(provider_uuid, identity_uuid)
        except InvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
예제 #20
0
 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)
예제 #21
0
 def get(self, request, provider_id, identity_id, instance_id):
     """
     Authentication Required, get instance details.
     """
     user = request.user
     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)
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_id, identity_id, user)
     serialized_data = InstanceSerializer(core_instance,
                                          context={'user':request.user}).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response
예제 #22
0
 def get(self, request, provider_id, identity_id, instance_id):
     """
     Return the object belonging to this instance ID
     TODO: Filter out instances you shouldnt see (permissions..)
     """
     user = request.user
     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)
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_id, identity_id, user)
     serialized_data = InstanceSerializer(core_instance).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response
예제 #23
0
 def get(self, request, project_uuid):
     """
     """
     user = request.user
     group = get_user_group(user.username)
     project = get_group_project(group, project_uuid)
     if not project:
         return Response("Project with ID=%s does not exist" % project_uuid,
                         status=status.HTTP_400_BAD_REQUEST)
     instances = project.instances.filter(
         only_current(), provider_machine__provider__active=True)
     serialized_data = InstanceSerializer(instances,
                                          many=True,
                                          context={
                                              "request": request
                                          }).data
     response = Response(serialized_data)
     return response
예제 #24
0
파일: volume.py 프로젝트: eandhy/atmosphere
    def post(self, request, provider_id, identity_id, volume_id=None):
        user = request.user
        data = request.DATA

        missing_keys = valid_launch_data(data)
        if missing_keys:
            return keys_not_found(missing_keys)

        esh_driver = prepare_driver(request, provider_id, identity_id)
        if not esh_driver:
            return invalid_creds(provider_id, identity_id)

        source = None
        name = data.pop('name')
        size_id = data.pop('size')

        (source_type, get_source,
         source_id) = self._select_source(esh_driver, data)
        if not get_source:
            return failure_response(
                status.HTTP_400_BAD_REQUEST,
                'Source could not be acquired. Did you send: ['
                'snapshot_id/volume_id/image_id] ?')
        source = get_source(source_id)
        if not source:
            return failure_response(
                status.HTTP_404_NOT_FOUND,
                "%s %s does not exist" % (source_type.title(), source_id))
        size = esh_driver.get_size(size_id)
        if not size:
            return failure_response(status.HTTP_404_NOT_FOUND,
                                    "Size %s does not exist" % (size_id, ))

        esh_instance = boot_volume(esh_driver, identity_id, name, size, source,
                                   source_type, **data)
        core_instance = convert_esh_instance(esh_driver, esh_instance,
                                             provider_id, identity_id, user)
        serialized_data = InstanceSerializer(core_instance,
                                             context={
                                                 'request': request
                                             }).data
        response = Response(serialized_data)
        return response
예제 #25
0
 def get(self, request, provider_uuid, identity_uuid, instance_id):
     """
     Authentication Required, get instance details.
     """
     user = request.user
     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:
         try:
             core_inst = CoreInstance.objects.get(
                 provider_alias=instance_id,
                 source__provider__uuid=provider_uuid,
                 created_by_identity__uuid=identity_uuid)
             core_inst.end_date_all()
         except CoreInstance.DoesNotExist:
             pass
         return instance_not_found(instance_id)
     core_instance = convert_esh_instance(esh_driver, esh_instance,
                                          provider_uuid, identity_uuid,
                                          user)
     serialized_data = InstanceSerializer(core_instance,
                                          context={
                                              "request": request
                                          }).data
     response = Response(serialized_data)
     response['Cache-Control'] = 'no-cache'
     return response
예제 #26
0
        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(),
예제 #27
0
        #Pass these as args
        size_alias = data.pop('size_alias')
        machine_alias = data.pop('machine_alias')
        try:
            core_instance = launch_instance(user, provider_id, identity_id,
                                            size_alias, machine_alias, **data)
        except OverQuotaError, oqe:
            return over_quota(oqe)
        except OverAllocationError, oae:
            return over_quota(oae)
        except SizeNotAvailable, snae:
            return size_not_availabe(snae)
        except InvalidCredsError:
            return invalid_creds(provider_id, identity_id)

        serializer = InstanceSerializer(core_instance, 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)


class InstanceHistory(APIView):
    """
    An InstanceHistory provides instance history for an identity.

    GET - A chronologically ordered list of Instances.
    """
예제 #28
0
                **data)
        except OverQuotaError, oqe:
            return over_quota(oqe)
        except OverAllocationError, oae:
            return over_quota(oae)
        except SizeNotAvailable, snae:
            return size_not_availabe(snae)
        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, exc.message)

        serializer = InstanceSerializer(core_instance,
                                        context={'user': request.user},
                                        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)


class InstanceHistory(APIView):
    """List of instance history for specific instance."""

    permission_classes = (ApiAuthRequired, )
예제 #29
0
    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,
예제 #30
0
            return over_quota(oae)
        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
예제 #31
0
    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,