Exemple #1
0
 def suspend(self, request, id):
     instance = self.get_instance()
     try:
         instance.suspend()
     except Exception as e:
         exceptions.handle(request, message=e)
     return Response({'detail': _('Suspend sent')})
Exemple #2
0
 def resume(self, request, id):
     instance = self.get_instance()
     try:
         instance.resume()
     except Exception as e:
         exceptions.handle(request, message=e)
     return Response({'detail': _('Resume sent')})
Exemple #3
0
 def perform_destroy(self, volume):
     volume = self.get_volume(volume=volume)
     if volume.status not in [
             'available',
             'error',
             'error_restoring',
             'error_extending',
             'error_deleting',
             'deleting',
             'creating',
     ]:
         raise UnavailableException(
             detail=_('Volume cannot be deleted now'))
     try:
         volume.delete()
         user = self.request.user
         staff_delete_volume.send(sender=__name__,
                                  user=user,
                                  user_id=user.id,
                                  volume_name=volume.volume.name,
                                  volume_id=volume.volume.id,
                                  username=user.username,
                                  request=self.request)
     except Exception as e:
         LOG.debug(str(e))
         handle(self.request, message=_('Unable to delete volume'))
Exemple #4
0
 def perform_create(self, serializer: StaffVolumeCreateSerializer):
     if not validate_cloud_objects_limit():
         raise APIBadRequest(
             _('Licence cloud objects limit reached. Please check your license.'
               ))
     os_api = OSApi.from_project_id(
         project_id=serializer.validated_data['project'].project_id)
     volume_source = serializer.validated_data.pop('source', {})
     source_type = volume_source.get('source_type')
     source = volume_source.get('source')
     source_id = source.pk if source else None
     try:
         os_api.volumes.create(
             name=serializer.validated_data['name'],
             size=serializer.validated_data['size'],
             region_name=serializer.validated_data['region'],
             type=serializer.validated_data.get('type', None),
             source_type=source_type,
             source_id=source_id)
     except EndpointNotFound as e:
         LOG.error(e)
         raise APIBadRequest(
             detail=_('Cinder endpoint not found for region {}').format(
                 serializer.validated_data['region'], ))
     except Unauthorized as e:
         LOG.error(e)
         raise OpenstackAuthError(_('Operation not allowed'))
     except Exception as e:
         LOG.error(e)
         handle(self.request, message=e)
Exemple #5
0
 def perform_update(self, serializer):
     os_volume_backup = self.get_openstack_volume_backup()
     if serializer.is_valid(raise_exception=True):
         try:
             os_volume_backup.update(
                 name=serializer.validated_data.get('name'),
                 description=serializer.validated_data.get('description'))
         except Exception as e:
             del e  # unused
             handle(self.request,
                    message=_('Unable to update volume backup'))
         except APIBadRequest as e:
             raise APIBadRequest(str(e))
         else:
             # no notifications received after update, we need to sync this volume backup
             activity_helper.start_generic_activity(
                 category_name='volume_backup',
                 activity_class=
                 'volume backup extra details synchronization',
                 backup_id=os_volume_backup.id)
             sync_volume_backup_extra_details.delay(
                 volume_backup_id=os_volume_backup.volume_backup.id,
                 after_update=True)
             activity_helper.end_activity()
     return Response({'detail': _('Updating started')})
Exemple #6
0
 def restore(self, request, pk):
     os_volume_backup = self.get_openstack_volume_backup()
     try:
         response = os_volume_backup.restore(
             name=request.data.get('name', None),
             volume_id=request.data.get('volume_id', None))
         if not request.data.get('volume_id', None):
             try:
                 Volume.objects.create(id=response.volume_id,
                                       status=VolumeStatus.RESTORING_BACKUP,
                                       size=0)
             except IntegrityError as e:
                 del e  # unused
         Operation.objects.create(operation_type=VolumeRestoration.name,
                                  primary_object_id=response.volume_id,
                                  params=json.dumps({
                                      'backup_id':
                                      response.backup_id,
                                      'volume_id':
                                      response.volume_id,
                                  }))
     except Exception as e:
         del e  # unused
         handle(self.request, message=_('Unable to restore volume backup'))
     return Response({'detail': _('Restoration started')})
Exemple #7
0
 def abort_migrate(self, request, id):
     instance = self.get_instance()
     try:
         instance.abort_migrate()
     except Exception as e:
         LOG.error(e)
         exceptions.handle(request, message=_('Can not abort migrate.'))
     return Response({'detail': _('Abort migrate in progress')})
Exemple #8
0
 def diagnostics(self, request, id):
     instance = self.get_instance()
     data = 'Unable to get diagnostics'
     try:
         data = instance.diagnostics()[1]
     except Exception as e:
         exceptions.handle(request, message=e)
     return Response(data)
Exemple #9
0
 def perform_destroy(self, volume_snapshot):
     os_volume_snapshot = self.get_openstack_volume_snapshot(
         volume_snapshot=volume_snapshot)
     try:
         os_volume_snapshot.delete()
     except Exception as e:
         handle(self.request,
                message=_('Unable to delete volume snapshot: {}').format(
                    str(e)))
Exemple #10
0
 def migrate(self, request, id):
     serializer = MigrateSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     instance = self.get_instance()
     try:
         instance.migrate(serializer.validated_data)
     except Exception as e:
         LOG.error(e)
         exceptions.handle(request, message=_('Can not migrate instance.'))
     return Response({'detail': _('Migrate in progress')})
Exemple #11
0
 def unlock(self, request, id):
     instance = self.get_instance()
     try:
         instance.unlock()
         # TODO: remove this when switching to versioned notifications as this will be redundant
         db_instance = instance.instance
         api_instance = instance.api_instance
         db_instance.locked = api_instance.locked
         db_instance.save()
     except Exception as e:
         exceptions.handle(request, message=e)
     return Response({'detail': _('Unlock sent')})
Exemple #12
0
 def perform_update(self, serializer):
     db_volume_snapshot = serializer.instance
     os_volume_snapshot = self.get_openstack_volume_snapshot(
         volume_snapshot=db_volume_snapshot)
     try:
         os_volume_snapshot.update(
             name=serializer.validated_data.get('name', None),
             description=serializer.validated_data.get('description', None))
     except Exception as e:
         handle(self.request,
                message=_('Unable to update volume snapshot: {}').format(
                    str(e)))
Exemple #13
0
 def perform_update(self, serializer):
     db_image = serializer.instance
     if not active_features.is_enabled('openstack.images.updatecreate'):
         raise ForbiddenException(_('Image editing not allowed'))
     try:
         self.os_api.images.update(image=db_image,
                                   **serializer.validated_data)
     except GlanceForbiddenException as e:
         LOG.exception(e)
         handle(self.request, message=_('Image update forbidden'))
     except Exception as e:
         LOG.exception(e)
         handle(self.request, message=_('Image update failed'))