Beispiel #1
0
    def put(self, request, provider_uuid, identity_uuid, volume_id):
        """
        Updates DB values for volume
        """
        user = request.user
        data = request.DATA

        #Ensure volume exists
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        if not esh_driver:
            return invalid_creds(provider_uuid, identity_uuid)
        try:
            esh_volume = esh_driver.get_volume(volume_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_volume:
            return volume_not_found(volume_id)
        core_volume = convert_esh_volume(esh_volume, provider_uuid,
                                         identity_uuid, user)
        serializer = VolumeSerializer(core_volume,
                                      data=data,
                                      context={'request': request})
        if serializer.is_valid():
            serializer.save()
            update_volume_metadata(esh_driver, esh_volume, data)
            response = Response(serializer.data)
            return response
        else:
            failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
Beispiel #2
0
 def put(self, request, provider_id, identity_id, volume_id):
     """
     Updates DB values for volume
     """
     user = request.user
     data = request.DATA
     #Ensure volume exists
     esh_driver = prepare_driver(request, provider_id, identity_id)
     if not esh_driver:
         return invalid_creds(provider_id, identity_id)
     esh_volume = esh_driver.get_volume(volume_id)
     if not esh_volume:
         return volume_not_found(volume_id)
     core_volume = convert_esh_volume(esh_volume, provider_id,
                                      identity_id, user)
     serializer = VolumeSerializer(core_volume, data=data,
                                   context={'request': request})
     if serializer.is_valid():
         serializer.save()
         response = Response(serializer.data)
         return response
     else:
         failure_response(
             status.HTTP_400_BAD_REQUEST,
             serializer.errors)
Beispiel #3
0
    def put(self, request, provider_uuid, identity_uuid, volume_id):
        """
        Updates DB values for volume
        """
        user = request.user
        data = request.DATA

        #Ensure volume exists
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        if not esh_driver:
            return invalid_creds(provider_uuid, identity_uuid)
        esh_volume = esh_driver.get_volume(volume_id)
        if not esh_volume:
            return volume_not_found(volume_id)
        core_volume = convert_esh_volume(esh_volume, provider_uuid,
                                         identity_uuid, user)
        serializer = VolumeSerializer(core_volume,
                                      data=data,
                                      context={'request': request})
        if serializer.is_valid():
            serializer.save()
            update_volume_metadata(esh_driver, esh_volume, data)
            response = Response(serializer.data)
            return response
        else:
            failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
Beispiel #4
0
 def get(self, request, provider_uuid, identity_uuid, volume_id):
     """
     """
     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_volume = esh_driver.get_volume(volume_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_volume:
         try:
             source = InstanceSource.objects.get(
                 identifier=volume_id, provider__uuid=provider_uuid)
             source.end_date = datetime.now()
             source.save()
         except CoreVolume.DoesNotExist:
             pass
         return volume_not_found(volume_id)
     core_volume = convert_esh_volume(esh_volume, provider_uuid,
                                      identity_uuid, user)
     serialized_data = VolumeSerializer(core_volume,
                                        context={
                                            'request': request
                                        }).data
     response = Response(serialized_data)
     return response
Beispiel #5
0
    def get(self, request, provider_uuid, identity_uuid):
        """
        Retrieves list of volumes and updates the DB
        """
        user = request.user
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        if not esh_driver:
            return invalid_creds(provider_uuid, identity_uuid)
        volume_list_method = esh_driver.list_volumes

        if AccountProvider.objects.filter(identity__uuid=identity_uuid):
            # Instance list method changes when using the OPENSTACK provider
            volume_list_method = esh_driver.list_all_volumes
        try:
            esh_volume_list = volume_list_method()
        except MalformedResponseError:
            return malformed_response(provider_id, identity_id)
        except InvalidCredsError:
            return invalid_creds(provider_id, identity_id)

        core_volume_list = [
            convert_esh_volume(volume, provider_uuid, identity_uuid, user)
            for volume in esh_volume_list
        ]
        serializer = VolumeSerializer(core_volume_list,
                                      context={'request': request},
                                      many=True)
        response = Response(serializer.data)
        return response
Beispiel #6
0
 def delete(self, request, provider_id, identity_id, volume_id):
     """
     Destroys the volume and updates the DB
     """
     user = request.user
     #Ensure volume exists
     esh_driver = prepare_driver(request, provider_id, identity_id)
     if not esh_driver:
         return invalid_creds(provider_id, identity_id)
     esh_volume = esh_driver.get_volume(volume_id)
     if not esh_volume:
         return volume_not_found(volume_id)
     core_volume = convert_esh_volume(esh_volume, provider_id, identity_id,
                                      user)
     #Delete the object, update the DB
     esh_driver.destroy_volume(esh_volume)
     core_volume.end_date = datetime.now()
     core_volume.save()
     #Return the object
     serialized_data = VolumeSerializer(
         core_volume,
         context={
             'user': request.user
         },
     ).data
     response = Response(serialized_data)
     return response
Beispiel #7
0
 def delete(self, request, provider_uuid, identity_uuid, volume_id):
     """
     Destroys the volume and updates the DB
     """
     user = request.user
     #Ensure volume exists
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     try:
         esh_volume = esh_driver.get_volume(volume_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_volume:
         return volume_not_found(volume_id)
     core_volume = convert_esh_volume(esh_volume, provider_uuid,
                                      identity_uuid, user)
     #Delete the object, update the DB
     esh_driver.destroy_volume(esh_volume)
     core_volume.end_date = now()
     core_volume.save()
     #Return the object
     serialized_data = VolumeSerializer(core_volume,
                                        context={
                                            'request': request
                                        }).data
     response = Response(serialized_data)
     return response
Beispiel #8
0
 def get(self, request, provider_uuid, identity_uuid, volume_id):
     """
     """
     user = request.user
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     esh_volume = esh_driver.get_volume(volume_id)
     if not esh_volume:
         try:
             core_volume = CoreVolume.objects.get(
                 identifier=volume_id, provider__uuid=provider_uuid)
             core_volume.end_date = datetime.now()
             core_volume.save()
         except CoreVolume.DoesNotExist:
             pass
         return volume_not_found(volume_id)
     core_volume = convert_esh_volume(esh_volume, provider_uuid,
                                      identity_uuid, user)
     serialized_data = VolumeSerializer(core_volume,
                                        context={
                                            'request': request
                                        }).data
     response = Response(serialized_data)
     return response
Beispiel #9
0
 def put(self, request, provider_id, identity_id, volume_id):
     """
     Updates DB values for volume
     """
     user = request.user
     data = request.DATA
     #Ensure volume exists
     esh_driver = prepare_driver(request, identity_id)
     esh_volume = esh_driver.get_volume(volume_id)
     if not esh_volume:
         errorObj = failureJSON([{'code': 404,
                                  'message': 'Volume does not exist'}])
         return Response(errorObj, status=status.HTTP_404_NOT_FOUND)
     core_volume = convert_esh_volume(esh_volume, provider_id, identity_id, user)
     serializer = VolumeSerializer(core_volume, data=data)
     if serializer.is_valid():
         serializer.save()
         response = Response(serializer.data)
         return response
     else:
         return Response(serializer.errors, status=400)
Beispiel #10
0
    def put(self, request, provider_uuid, identity_uuid, volume_id):
        """
        Updates DB values for volume
        """
        user = request.user
        data = request.DATA

        #Ensure volume exists
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        if not esh_driver:
            return invalid_creds(provider_uuid, identity_uuid)
        try:
            esh_volume = esh_driver.get_volume(volume_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_volume:
            return volume_not_found(volume_id)
        core_volume = convert_esh_volume(esh_volume, provider_uuid,
                                         identity_uuid, user)
        serializer = VolumeSerializer(core_volume, data=data,
                                      context={'request': request})
        if serializer.is_valid():
            serializer.save()
            update_volume_metadata(
                    esh_driver, esh_volume, data)
            response = Response(serializer.data)
            return response
        else:
            failure_response(
                status.HTTP_400_BAD_REQUEST,
                serializer.errors)
Beispiel #11
0
 def get(self, request):
     """
     """
     user = request.user
     volumes = user.volume_set.filter(only_active(),
                                      provider__active=True,
                                      projects=None)
     serialized_data = VolumeSerializer(volumes,
                                        many=True,
                                        context={
                                            "request": request
                                        }).data
     response = Response(serialized_data)
     return response
Beispiel #12
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)
     volumes = projects.volumes.filter(only_current)
     serialized_data = VolumeSerializer(volumes,
                                        many=True,
                                        context={
                                            "user": request.user
                                        }).data
     response = Response(serialized_data)
     return response
Beispiel #13
0
 def get(self, request, provider_id, identity_id, volume_id):
     """
     """
     user = request.user
     esh_driver = prepare_driver(request, provider_id, identity_id)
     if not esh_driver:
         return invalid_creds(provider_id, identity_id)
     esh_volume = esh_driver.get_volume(volume_id)
     if not esh_volume:
         return volume_not_found(volume_id)
     core_volume = convert_esh_volume(esh_volume, provider_id,
                                      identity_id, user)
     serialized_data = VolumeSerializer(core_volume).data
     response = Response(serialized_data)
     return response
Beispiel #14
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)
     volumes = project.volumes.filter(only_current(), provider__active=True)
     serialized_data = VolumeSerializer(volumes,
                                        many=True,
                                        context={
                                            "request": request
                                        }).data
     response = Response(serialized_data)
     return response
Beispiel #15
0
        except OverQuotaError, oqe:
            return over_quota(oqe)
        except ConnectionFailure:
            return connection_failure(provider_uuid, identity_uuid)
        except MalformedResponseError:
            return malformed_response(provider_uuid, identity_uuid)
        except InvalidCredsError:
            return invalid_creds(provider_uuid, identity_uuid)
        if not success:
            return failure_response(status.HTTP_500_INTERNAL_SERVER_ERROR,
                                    'Volume creation failed. Contact support')
        # Volume creation succeeded
        core_volume = convert_esh_volume(esh_volume, provider_uuid,
                                         identity_uuid, user)
        serialized_data = VolumeSerializer(core_volume,
                                           context={
                                               'request': request
                                           }).data
        return Response(serialized_data, status=status.HTTP_201_CREATED)


class Volume(APIView):
    """Details of specific volume on Identity."""
    permission_classes = (ApiAuthRequired, )

    def get(self, request, provider_uuid, identity_uuid, volume_id):
        """
        """
        user = request.user
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        if not esh_driver:
            return invalid_creds(provider_uuid, identity_uuid)
Beispiel #16
0
    def post(self, request, provider_id, identity_id, instance_id):
        """Authentication Required, Attempt a specific instance action, including necessary parameters.
        """
        #Service-specific call to action
        action_params = request.DATA
        if not action_params.get('action', None):
            return failure_response(
                status.HTTP_400_BAD_REQUEST,
                'POST request to /action require a BODY with \'action\'.')
        result_obj = None
        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 failure_response(
                status.HTTP_400_BAD_REQUEST,
                'Instance %s no longer exists' % (instance_id, ))
        action = action_params['action']
        try:
            if 'volume' in action:
                volume_id = action_params.get('volume_id')
                if 'attach_volume' == action:
                    mount_location = action_params.get('mount_location', None)
                    if mount_location == 'null' or mount_location == 'None':
                        mount_location = None
                    device = action_params.get('device', None)
                    if device == 'null' or device == 'None':
                        device = None
                    task.attach_volume_task(esh_driver, esh_instance.alias,
                                            volume_id, device, mount_location)
                elif 'detach_volume' == action:
                    (result, error_msg) = task.detach_volume_task(
                        esh_driver, esh_instance.alias, volume_id)
                    if not result and error_msg:
                        #Return reason for failed detachment
                        return failure_response(status.HTTP_400_BAD_REQUEST,
                                                error_msg)
                #Task complete, convert the volume and return the object
                esh_volume = esh_driver.get_volume(volume_id)
                core_volume = convert_esh_volume(esh_volume, provider_id,
                                                 identity_id, user)
                result_obj = VolumeSerializer(core_volume,
                                              context={
                                                  "request": request
                                              }).data
            elif 'resize' == action:
                size_alias = action_params.get('size', '')
                if type(size_alias) == int:
                    size_alias = str(size_alias)
                resize_instance(esh_driver, esh_instance, size_alias,
                                provider_id, identity_id, user)
            elif 'confirm_resize' == action:
                confirm_resize(esh_driver, esh_instance, provider_id,
                               identity_id, user)
            elif 'revert_resize' == action:
                esh_driver.revert_resize_instance(esh_instance)
            elif 'redeploy' == action:
                redeploy_init(esh_driver, esh_instance, countdown=None)
            elif 'resume' == action:
                resume_instance(esh_driver, esh_instance, provider_id,
                                identity_id, user)
            elif 'suspend' == action:
                suspend_instance(esh_driver, esh_instance, provider_id,
                                 identity_id, user)
            elif 'start' == action:
                start_instance(esh_driver, esh_instance, provider_id,
                               identity_id, user)
            elif 'stop' == action:
                stop_instance(esh_driver, esh_instance, provider_id,
                              identity_id, user)
            elif 'reset_network' == action:
                esh_driver.reset_network(esh_instance)
            elif 'console' == action:
                result_obj = esh_driver._connection.ex_vnc_console(
                    esh_instance)
            elif 'reboot' == action:
                reboot_type = action_params.get('reboot_type', 'SOFT')
                reboot_instance(esh_driver, esh_instance, reboot_type)
            elif 'rebuild' == action:
                machine_alias = action_params.get('machine_alias', '')
                machine = esh_driver.get_machine(machine_alias)
                esh_driver.rebuild_instance(esh_instance, machine)
            else:
                return failure_response(
                    status.HTTP_400_BAD_REQUEST,
                    'Unable to to perform action %s.' % (action))
            #ASSERT: The action was executed successfully
            api_response = {
                'result':
                'success',
                'message':
                'The requested action <%s> was run successfully' %
                action_params['action'],
                'object':
                result_obj,
            }
            response = Response(api_response, status=status.HTTP_200_OK)
            return response
        ### Exception handling below..
        except HypervisorCapacityError, hce:
            return over_capacity(hce)
Beispiel #17
0
        description = data.get('description')
        try:
            success, esh_volume = create_volume(esh_driver, identity_id,
                                                name, size, description)
        except OverQuotaError, oqe:
            return over_quota(oqe)
        except InvalidCredsError:
            return invalid_creds(provider_id, identity_id)
        if not success:
            return failure_response(
                status.HTTP_500_INTERNAL_SERVER_ERROR,
                'Volume creation failed. Contact support')
        # Volume creation succeeded
        core_volume = convert_esh_volume(esh_volume, provider_id,
                                         identity_id, user)
        serialized_data = VolumeSerializer(core_volume).data
        return Response(serialized_data, status=status.HTTP_201_CREATED)


class Volume(APIView):
    """
    List all volumes
    """
    @api_auth_token_required
    def get(self, request, provider_id, identity_id, volume_id):
        """
        """
        user = request.user
        esh_driver = prepare_driver(request, provider_id, identity_id)
        if not esh_driver:
            return invalid_creds(provider_id, identity_id)
Beispiel #18
0
    def post(self, request, provider_uuid, identity_uuid):
        """
        Updates DB values for volume
        """
        user = request.user
        data = request.DATA

        missing_keys = valid_snapshot_post_data(data)
        if missing_keys:
            return keys_not_found(missing_keys)
        #Required
        size = data.get('size')
        volume_id = data.get('volume_id')
        display_name = data.get('display_name')
        #Optional
        description = data.get('description')
        metadata = data.get('metadata')
        snapshot_id = data.get('snapshot_id')

        #STEP 0 - Existence tests
        esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
        if not esh_driver:
            return invalid_creds(provider_uuid, identity_uuid)
        try:
            esh_volume = esh_driver.get_volume(volume_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))
        #TODO: Put quota tests at the TOP so we dont over-create resources!
        #STEP 1 - Reuse/Create snapshot
        if snapshot_id:
            snapshot = esh_driver._connection.get_snapshot(snapshot_id)
            if not snapshot:
                return failure_response(
                    status.HTTP_400_BAD_REQUEST,
                    "Snapshot %s not found. Process aborted." % snapshot_id)
        else:
            #Normal flow, create a snapshot from the volume
            if not esh_volume:
                return volume_not_found(volume_id)
            if esh_volume.extra['status'].lower() != 'available':
                return failure_response(
                    status.HTTP_400_BAD_REQUEST,
                    "Volume status must be 'available'. "
                    "Did you detach the volume?")

            snapshot = esh_driver._connection.ex_create_snapshot(
                esh_volume, display_name, description)
            if not snapshot:
                return failure_response(
                    status.HTTP_400_BAD_REQUEST,
                    "Snapshot not created. Process aborted.")
        #STEP 2 - Create volume from snapshot
        try:
            success, esh_volume = create_volume(esh_driver,
                                                identity_uuid,
                                                display_name,
                                                size,
                                                description,
                                                metadata,
                                                snapshot=snapshot)
            if not success:
                return failure_response(
                    status.HTTP_500_INTERNAL_SERVER_ERROR,
                    'Volume creation failed. Contact support')
            # Volume creation succeeded
            core_volume = convert_esh_volume(esh_volume, provider_uuid,
                                             identity_uuid, user)
            serialized_data = VolumeSerializer(core_volume,
                                               context={
                                                   'request': request
                                               }).data
            return Response(serialized_data, status=status.HTTP_201_CREATED)
        except OverQuotaError, oqe:
            return over_quota(oqe)
Beispiel #19
0
        description = data.get('description')
        try:
            success, esh_volume = create_volume(esh_driver, identity_id, name,
                                                size, description)
        except OverQuotaError, oqe:
            return over_quota(oqe)
        except InvalidCredsError:
            return invalid_creds(provider_id, identity_id)
        if not success:
            return failure_response(status.HTTP_500_INTERNAL_SERVER_ERROR,
                                    'Volume creation failed. Contact support')
        # Volume creation succeeded
        core_volume = convert_esh_volume(esh_volume, provider_id, identity_id,
                                         user)
        serialized_data = VolumeSerializer(core_volume,
                                           context={
                                               'user': request.user
                                           }).data
        return Response(serialized_data, status=status.HTTP_201_CREATED)


class Volume(APIView):
    """Details of specific volume on Identity."""
    permission_classes = (ApiAuthRequired, )

    def get(self, request, provider_id, identity_id, volume_id):
        """
        """
        user = request.user
        esh_driver = prepare_driver(request, provider_id, identity_id)
        if not esh_driver:
            return invalid_creds(provider_id, identity_id)