示例#1
0
    def checkpoints_create(self, req, provider_id, body):
        """Creates a new checkpoint."""
        if not self.is_valid_body(body, 'checkpoint'):
            raise exc.HTTPUnprocessableEntity()

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

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

        check_policy(context, 'checkpoint_create')
        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")

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

        if not uuidutils.is_uuid_like(plan_id):
            msg = _("Invalid plan id provided.")
            raise exc.HTTPBadRequest(explanation=msg)

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

        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"),
            }
        }
        try:
            checkpoint_id = self.protection_api.protect(context, plan)
        except Exception as error:
            msg = _("Create checkpoint failed: %s") % error.msg
            raise exc.HTTPBadRequest(explanation=msg)
        checkpoint_properties['id'] = checkpoint_id

        returnval = self._checkpoint_view_builder.detail(
            req, checkpoint_properties)
        return returnval
示例#2
0
    def _plan_get(self, context, plan_id):
        if not uuidutils.is_uuid_like(plan_id):
            msg = _("Invalid plan id provided.")
            raise exc.HTTPBadRequest(explanation=msg)

        plan = objects.Plan.get_by_id(context, plan_id)
        try:
            context.can(plan_policy.GET_POLICY, target_obj=plan)
        except exception.PolicyNotAuthorized:
            # raise PlanNotFound instead to make sure karbor behaves
            # as it used to
            raise exception.PlanNotFound(plan_id=plan_id)
        LOG.info("Plan info retrieved successfully.", resource=plan)
        return plan
示例#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