Пример #1
0
    def delete(self, context, provider_id, checkpoint_id):
        LOG.info(_LI("Starting protection service:delete action"))
        LOG.debug('provider_id :%s checkpoint_id:%s', provider_id,
                  checkpoint_id)
        provider = self.provider_registry.show_provider(provider_id)
        try:
            checkpoint_collection = provider.get_checkpoint_collection()
            checkpoint = checkpoint_collection.get(checkpoint_id)
        except Exception:
            LOG.error(_LE("get checkpoint failed, checkpoint_id:%s"),
                      checkpoint_id)
            raise exception.InvalidInput(
                reason=_("Invalid checkpoint_id or provider_id"))

        if checkpoint.status not in [
                constants.CHECKPOINT_STATUS_AVAILABLE,
                constants.CHECKPOINT_STATUS_ERROR,
        ]:
            raise exception.CheckpointNotBeDeleted(checkpoint_id=checkpoint_id)
        checkpoint.status = constants.CHECKPOINT_STATUS_DELETING
        checkpoint.commit()

        try:
            delete_checkpoint_flow = self.worker.get_delete_checkpoint_flow(
                context, constants.OPERATION_DELETE, checkpoint, provider)
        except Exception:
            LOG.exception(
                _LE("Failed to create delete checkpoint flow, checkpoint:%s."),
                checkpoint_id)
            raise exception.KarborException(
                _("Failed to create delete checkpoint flow."))
        try:
            self.worker.run_flow(delete_checkpoint_flow)
            return True
        except Exception:
            LOG.exception(_LE("Failed to run delete checkpoint flow"))
            raise
Пример #2
0
 def _validate_path(self, path):
     if path.find('..') >= 0:
         msg = (_("The path(%s) is invalid.") % path)
         raise exception.InvalidInput(msg)
Пример #3
0
    def checkpoints_create(self, req, provider_id, body):
        """Creates a new checkpoint."""

        context = req.environ['karbor.context']

        LOG.debug('Create checkpoint request '
                  'body: %s provider_id:%s', body, provider_id)

        context.can(provider_policy.CHECKPOINT_CREATE_POLICY)
        checkpoint = body['checkpoint']
        LOG.debug('Create checkpoint request checkpoint: %s', checkpoint)

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

        plan_id = checkpoint.get("plan_id")

        plan = objects.Plan.get_by_id(context, plan_id)
        if not plan:
            raise exception.PlanNotFound(plan_id=plan_id)

        # check the provider_id
        if provider_id != plan.get("provider_id"):
            msg = _("The parameter provider_id is not the same as "
                    "the value in the plan.")
            raise exception.InvalidPlan(reason=msg)

        extra_info = checkpoint.get("extra_info", None)
        if extra_info is not None:
            if not isinstance(extra_info, dict):
                msg = _("The extra_info in checkpoint must be a dict when "
                        "creating a checkpoint.")
                raise exception.InvalidInput(reason=msg)
            elif not all(
                    map(lambda s: isinstance(s, six.string_types),
                        extra_info.keys())):
                msg = _("Key of extra_info in checkpoint must be string when"
                        "creating a checkpoint.")
                raise exception.InvalidInput(reason=msg)
        else:
            extra_info = {'created_by': constants.MANUAL}

        checkpoint_extra_info = None
        if extra_info is not None:
            checkpoint_extra_info = jsonutils.dumps(extra_info)
        checkpoint_properties = {
            'project_id': context.project_id,
            'status': constants.CHECKPOINT_STATUS_PROTECTING,
            'provider_id': provider_id,
            "protection_plan": {
                "id": plan.get("id"),
                "name": plan.get("name"),
                "resources": plan.get("resources"),
            },
            "extra_info": checkpoint_extra_info
        }
        try:
            checkpoint_id = self.protection_api.protect(
                context, plan, checkpoint_properties)
        except Exception as error:
            msg = _("Create checkpoint failed: %s") % error
            raise exc.HTTPBadRequest(explanation=msg)

        checkpoint_properties['id'] = checkpoint_id

        LOG.info("Create the checkpoint successfully. checkpoint_id:%s",
                 checkpoint_id)
        returnval = self._checkpoint_view_builder.detail(
            req, checkpoint_properties)
        return returnval
Пример #4
0
    def _get_operation(self, operation_type):
        if operation_type not in self._ops_map:
            msg = (_("Invalid operation type: %s") % operation_type)
            raise exception.InvalidInput(msg)

        return self._ops_map[operation_type]
Пример #5
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
        # Heat client can be initialized using current login tenant when the
        # restore_target and restore_auth is not provided.
        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
Пример #6
0
    def create(self, req, body):
        """Creates a new plan."""
        if not self.is_valid_body(body, 'plan'):
            raise exc.HTTPUnprocessableEntity()

        LOG.debug('Create plan request body: %s', body)
        context = req.environ['karbor.context']
        context.can(plan_policy.CREATE_POLICY)
        plan = body['plan']
        LOG.debug('Create plan request plan: %s', plan)

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

        parameters = plan.get("parameters", None)

        if parameters is None:
            msg = _("parameters must be provided when creating " "a plan.")
            raise exception.InvalidInput(reason=msg)

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

        self.validate_name_and_description(plan)
        self.validate_plan_resources(plan)
        self.validate_plan_parameters(context, plan)

        resources = plan.get('resources', None)
        if resources:
            for resource in resources:
                extra_info = resource.get('extra_info', None)
                if extra_info is not None:
                    resource['extra_info'] = jsonutils.dumps(extra_info)

        plan_properties = {
            'name': plan.get('name', None),
            'description': plan.get('description', None),
            'provider_id': plan.get('provider_id', None),
            'project_id': context.project_id,
            'status': constants.PLAN_STATUS_SUSPENDED,
            'resources': resources,
            'parameters': parameters,
        }

        try:
            reserve_opts = {'plans': 1}
            reservations = QUOTAS.reserve(context, **reserve_opts)
        except exception.OverQuota as e:
            quota.process_reserve_over_quota(context, e, resource='plans')
        try:
            plan = objects.Plan(context=context, **plan_properties)
            plan.create()
            QUOTAS.commit(context, reservations)
        except Exception:
            with excutils.save_and_reraise_exception():
                try:
                    if plan and 'id' in plan:
                        plan.destroy()
                finally:
                    QUOTAS.rollback(context, reservations)

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

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

        LOG.debug('Create copy request body: %s', body)
        context = req.environ['karbor.context']
        context.can(copy_policy.CREATE_POLICY)
        copy = body['copy']
        plan_id = copy.get("plan_id", None)

        if not uuidutils.is_uuid_like(plan_id):
            msg = _("Invalid plan id provided.")
            raise exception.InvalidInput(reason=msg)

        if not uuidutils.is_uuid_like(provider_id):
            msg = _("Invalid provider id provided.")
            raise exception.InvalidInput(reason=msg)

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

        try:
            plan = objects.Plan.get_by_id(context, plan_id)
        except exception.PlanNotFound as error:
            raise exc.HTTPNotFound(explanation=error.msg)

        if provider_id != plan.provider_id:
            msg = _("The provider id is not the same as the value "
                    "in the plan.")
            raise exception.InvalidInput(reason=msg)

        filters = {'plan_id': plan_id}
        checkpoints = self.protection_api.list_checkpoints(
            context, provider_id, marker=None, limit=None,
            sort_keys=None, sort_dirs=None, filters=filters, offset=None)

        if not checkpoints:
            msg = _("The plan has not been protected.")
            raise exception.InvalidInput(reason=msg)

        plan.parameters.update(parameters)
        try:
            checkpoint_copy = self.protection_api.copy(context, plan)
        except Exception:
            LOG.exception("Failed to create checkpoint copies.")
            raise

        copy = {
            'project_id': context.project_id,
            'provider_id': plan.provider_id,
            'plan_id': plan.id,
            'checkpoint_id': checkpoint_copy,
            'parameters': parameters
        }

        retval = self._view_builder.detail(req, copy)
        return retval