Пример #1
0
    def create(self, req, body):
        """Creates a new restore."""
        if not self.is_valid_body(body, 'restore'):
            raise exc.HTTPUnprocessableEntity()

        LOG.debug('Create restore request body: %s', body)
        context = req.environ['karbor.context']
        check_policy(context, 'create')
        restore = body['restore']
        LOG.debug('Create restore request : %s', restore)

        if not restore.get("provider_id"):
            msg = _("provider_id must be provided when creating " "a restore.")
            raise exception.InvalidInput(reason=msg)

        if not restore.get("checkpoint_id"):
            msg = _("checkpoint_id must be provided when creating "
                    "a restore.")
            raise exception.InvalidInput(reason=msg)

        parameters = restore.get("parameters")
        if not isinstance(parameters, dict):
            msg = _("parameters must be a dict when creating" " a restore.")
            raise exception.InvalidInput(reason=msg)

        # restore_auth and restore_target are optional
        restore_auth = restore.get("restore_auth")
        if restore_auth is not None:
            if not isinstance(restore_auth, dict):
                msg = _("restore_auth must be a dict when creating"
                        " a restore.")
                raise exception.InvalidInput(reason=msg)

        restore_properties = {
            'project_id': context.project_id,
            'provider_id': restore.get('provider_id'),
            'checkpoint_id': restore.get('checkpoint_id'),
            'restore_target': restore.get('restore_target'),
            'parameters': parameters,
            'status': constants.RESTORE_STATUS_IN_PROGRESS,
        }

        restoreobj = objects.Restore(context=context, **restore_properties)
        restoreobj.create()
        LOG.debug('call restore RPC  : restoreobj:%s', restoreobj)

        # call restore rpc API of protection service
        try:
            self.protection_api.restore(context, restoreobj, restore_auth)
        except Exception:
            # update the status of restore
            update_dict = {"status": constants.RESTORE_STATUS_FAILURE}
            check_policy(context, 'update', restoreobj)
            restoreobj = self._restore_update(context, restoreobj.get("id"),
                                              update_dict)

        retval = self._view_builder.detail(req, restoreobj)

        return retval
Пример #2
0
 def test_destroy(self, restore_destroy):
     db_restore = fake_restore.fake_db_restore()
     restore = objects.Restore.\
         _from_db_object(self.context,
                         objects.Restore(), db_restore)
     restore.destroy()
     self.assertTrue(restore_destroy.called)
     admin_context = restore_destroy.call_args[0][0]
     self.assertTrue(admin_context.is_admin)
Пример #3
0
 def test_save(self, restore_update):
     db_restore = fake_restore.fake_db_restore()
     restore = objects.Restore.\
         _from_db_object(self.context,
                         objects.Restore(), db_restore)
     restore.status = 'FAILED'
     restore.save()
     restore_update.assert_called_once_with(self.context, restore.id,
                                            {'status': 'FAILED'})
Пример #4
0
    def create(self, req, body):
        """Creates a new restore."""

        LOG.debug('Create restore request body: %s', body)
        context = req.environ['karbor.context']
        context.can(restore_policy.CREATE_POLICY)
        context.notification = notification.KarborRestoreCreate(context,
                                                                request=req)
        restore = body['restore']
        LOG.debug('Create restore request : %s', restore)

        parameters = restore.get("parameters")
        restore_auth = restore.get("restore_auth", None)
        restore_properties = {
            'project_id': context.project_id,
            'provider_id': restore.get('provider_id'),
            'checkpoint_id': restore.get('checkpoint_id'),
            'restore_target': restore.get('restore_target'),
            'parameters': parameters,
            'status': constants.RESTORE_STATUS_IN_PROGRESS,
        }

        restoreobj = objects.Restore(context=context, **restore_properties)
        restoreobj.create()
        LOG.debug('call restore RPC  : restoreobj:%s', restoreobj)

        # call restore rpc API of protection service
        try:
            with StartNotification(context, parameters=parameters):
                self.protection_api.restore(context, restoreobj, restore_auth)
        except exception.AccessCheckpointNotAllowed as error:
            raise exc.HTTPForbidden(explanation=error.msg)
        except Exception:
            # update the status of restore
            update_dict = {"status": constants.RESTORE_STATUS_FAILURE}
            context.can(restore_policy.UPDATE_POLICY, restoreobj)
            restoreobj = self._restore_update(context, restoreobj.get("id"),
                                              update_dict)

        retval = self._view_builder.detail(req, restoreobj)

        return retval
Пример #5
0
 def test_obj_field_status(self):
     restore = objects.Restore(context=self.context,
                               status='FAILED')
     self.assertEqual('FAILED', restore.status)
Пример #6
0
 def test_create(self, restore_create):
     db_restore = fake_restore.fake_db_restore()
     restore_create.return_value = db_restore
     restore = objects.Restore(context=self.context)
     restore.create()
     self.assertEqual(db_restore['id'], restore.id)