def replace_resources(self, context, resources, ori_res, ori_dep): LOG.info("replace resources with values: %s", resources) if not isinstance(resources, list): msg = "'resources' argument must be a list." LOG.error(msg) raise exception.PlanUpdateError(message=msg) # Verify resources for res in resources: if not isinstance(res, dict): msg = "Every resource to be replaced must be a dict." LOG.error(msg) raise exception.PlanUpdateError(message=msg) # Simply parse value. for k, v in res.items(): if v == 'true': res[k] = True elif v == 'false': res[k] = False elif isinstance(v, six.string_types): try: new_value = eval(v) if type(new_value) in (dict, list, numbers.Number): res[k] = new_value except Exception: pass return self.resource_rpcapi.replace_resources(context, resources, ori_res, ori_dep)
def _do_update_plan(self, context, plan_id, values): LOG.info("Update plan <%s> with values: %s", plan_id, values) allowed_properties = [ 'task_status', 'plan_status', 'stack_id', 'updated_resources', 'sys_clone', 'copy_data' ] # Verify the keys and values for k, v in values.items(): if k not in allowed_properties: msg = ("Update plan failed. %s field " "not found or unsupported to update." % k) LOG.error(msg) raise exception.PlanUpdateError(message=msg) elif k == 'plan_status' and v not in p_status.PLAN_STATUS: msg = "Update plan failed. '%s' plan_status unsupported." % v LOG.error(msg) raise exception.PlanUpdateError(message=msg) # If values contain updated_resources, set update time. if 'updated_resources' in values: values['updated_at'] = timeutils.utcnow() # Update in database plan_cls.update_plan_to_db(context, plan_id, values) LOG.info("Update plan with id of %s succeed!", plan_id)
def update_plan(self, context, plan_id, values): if not isinstance(values, dict): msg = "Update plan failed. 'values' attribute must be a dict." LOG.error(msg) raise exception.PlanUpdateError(message=msg) allowed_status = (p_status.INITIATING, p_status.CREATING, p_status.AVAILABLE, p_status.FINISHED) try: plan = db_api.plan_get(context, plan_id) if 'updated_resources' in values.keys() \ and plan['plan_status'] not in allowed_status: msg = ("Plan are not allowed to be updated in %s status." % plan['plan_status']) LOG.error(msg) raise exception.PlanUpdateError(message=msg) except exception.PlanNotFoundInDb: LOG.error('Plan <%s> could not be found.', plan_id) raise exception.PlanNotFound(plan_id=plan_id) LOG.info("Update plan <%s> with values: %s", plan_id, values) return self.plan_rpcapi.update_plan(context, plan_id, values)
def update_plan_resources(self, context, plan_id, resources): LOG.info("Update resources of plan <%s> with values: %s", plan_id, resources) if not isinstance(resources, list): msg = "'resources' argument must be a list." LOG.error(msg) raise exception.PlanUpdateError(message=msg) # Verify plan allowed_status = (p_status.INITIATING, p_status.CREATING, p_status.AVAILABLE, p_status.FINISHED) try: plan = db_api.plan_get(context, plan_id) if plan['plan_status'] not in allowed_status: msg = ("Plan are not allowed to be updated in %s status." % plan['plan_status']) LOG.error(msg) raise exception.PlanUpdateError(message=msg) except exception.PlanNotFoundInDb: LOG.error('The plan <%s> could not be found.', plan_id) raise exception.PlanNotFound(plan_id=plan_id) # Verify resources for res in resources: if not isinstance(res, dict): msg = "Every resource to be updated must be a dict." LOG.error(msg) raise exception.PlanUpdateError(message=msg) action = res.get('action') if not action or action not in ('add', 'edit', 'delete'): msg = "%s action is unsupported." % action LOG.error(msg) raise exception.PlanUpdateError(message=msg) if action == 'add' and ('id' not in res.keys() or 'resource_type' not in res.keys()): msg = ("'id' and 'resource_type' of new resource " "must be provided when adding new resources.") LOG.error(msg) raise exception.PlanUpdateError(message=msg) elif action == 'edit' and (len(res) < 2 or 'resource_id' not in res.keys()): msg = ("'resource_id' and the fields to be edited " "must be provided when editing resources.") LOG.error(msg) raise exception.PlanUpdateError(message=msg) elif action == 'delete' and 'resource_id' not in res.keys(): msg = "'resource_id' must be provided when " \ "deleting resources." LOG.error(msg) raise exception.PlanUpdateError(message=msg) # Simply parse value. for k, v in res.items(): if v == 'true': res[k] = True elif v == 'false': res[k] = False elif isinstance(v, six.string_types): try: new_value = eval(v) if type(new_value) in (dict, list, numbers.Number): res[k] = new_value except Exception: pass return self.plan_rpcapi.update_plan_resources(context, plan_id, resources)