def create(self, req, body): """Creates a new plan.""" 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) context.notification = notification.KarborPlanCreate(context, request=req) parameters = plan.get("parameters", None) 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) with StartNotification(context, name=plan.get('name', None)): 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
def checkpoints_create(self, req, provider_id, body): """Creates a new checkpoint.""" context = req.environ['karbor.context'] context.notification = notification.KarborCheckpointCreate(context, request=req) 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: reserve_opts = {'checkpoints': 1} reservations = QUOTAS.reserve(context, **reserve_opts) except exception.OverQuota as e: quota.process_reserve_over_quota(context, e, resource='checkpoints') else: checkpoint_id = None try: with StartNotification( context, checkpoint_properties=checkpoint_properties): checkpoint_id = self.protection_api.protect( context, plan, checkpoint_properties) QUOTAS.commit(context, reservations) except Exception as error: if not checkpoint_id: QUOTAS.rollback(context, reservations) 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
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