def update_session(user_id, session_id, patch_doc, project_id=None): if CONF.enable_v1_api: valid_patch = utilsv1.SessionDoc.create_patch(patch_doc) else: valid_patch = utilsv2.SessionDoc.create_patch(patch_doc) sessiont = get_session(project_id=project_id, user_id=user_id, session_id=session_id) if not sessiont: raise freezer_api_exc.DocumentNotFound( message='Session not register with ID' ' {0}'.format(session_id)) values = {} for key in valid_patch.keys(): if key == 'jobs': jobintable = sessiont.get('jobs', None) jobinpatch = valid_patch.get('jobs', None) if jobintable: jobintable.update(jobinpatch) jobinpatch.update(jobintable) values['job'] = json_utils.json_encode(valid_patch.get(key, None)) elif key == 'schedule': values[key] = json_utils.json_encode(valid_patch.get(key, None)) else: values[key] = valid_patch.get(key, None) update_tuple(tablename=models.Session, user_id=user_id, tuple_id=session_id, tuple_values=values, project_id=project_id) return 0
def add_backup(user_id, user_name, doc, project_id=None): if CONF.enable_v1_api: metadatadoc = utilsv1.BackupMetadataDoc(user_id, user_name, doc) else: metadatadoc = utilsv2.BackupMetadataDoc(project_id, user_id, user_name, doc) if not metadatadoc.is_valid(): raise freezer_api_exc.BadDataFormat(message='Bad Data Format') backup_id = metadatadoc.backup_id backupjson = metadatadoc.serialize() backup_metadata = backupjson.get('backup_metadata') existing = get_backup(project_id=project_id, user_id=user_id, backup_id=backup_id) if existing: raise freezer_api_exc.DocumentExists( message='Backup already registered with ID' ' {0}'.format(backup_id)) backup = models.Backup() backupvalue = {} backupvalue['project_id'] = project_id backupvalue['id'] = backup_id backupvalue['user_id'] = user_id backupvalue['user_name'] = user_name backupvalue['job_id'] = backup_metadata.get('job_id') # The field backup_metadata is json, including : # hostname , backup_name , container etc backupvalue['backup_metadata'] = json_utils.json_encode(backup_metadata) backup.update(backupvalue) add_tuple(tuple=backup) LOG.info('Backup registered, backup_id: {0}'.format(backup_id)) return backup_id
def replace_action(user_id, action_id, doc, project_id=None): if CONF.enable_v1_api: valid_doc = utilsv1.ActionDoc.update(doc, user_id, action_id) else: valid_doc = utilsv2.ActionDoc.update(doc, user_id, action_id, project_id) values = {} keyt = [ 'action', 'backup_name', 'container', 'src_file', 'timeout', 'priority', 'mandatory', 'log_file' ] freezer_action = valid_doc.get('freezer_action', {}) values['project_id'] = project_id values['id'] = action_id values['user_id'] = user_id values['max_retries'] = valid_doc.get('max_retries', None) values['max_retries_interval'] = valid_doc.\ get('max_retries_interval', None) for key in freezer_action.keys(): if key in keyt: values[key] = freezer_action.get(key) values['actionmode'] = freezer_action.get('mode', None) values['backup_metadata'] = json_utils.json_encode(freezer_action) replace_tuple(tablename=models.Action, user_id=user_id, tuple_id=action_id, tuple_values=values, project_id=project_id) LOG.info('action replaced, action_id: {0}'.format(action_id)) return action_id
def add_session(user_id, doc, project_id=None): if CONF.enable_v1_api: session_doc = utilsv1.SessionDoc.create(doc=doc, user_id=user_id) else: session_doc = utilsv2.SessionDoc.create(doc=doc, user_id=user_id, project_id=project_id) session_id = session_doc['session_id'] schedulingjson = session_doc.get('schedule') existing = get_session(project_id=project_id, user_id=user_id, session_id=session_id) if existing: raise freezer_api_exc.DocumentExists( message='Session already registered with ID' ' {0}'.format(session_id)) sessiont = models.Session() sessionvalue = {} sessionvalue['project_id'] = project_id sessionvalue['id'] = session_id sessionvalue['user_id'] = user_id sessionvalue['description'] = session_doc.get('description', None) sessionvalue['hold_off'] = session_doc.get('hold_off', 30) sessionvalue['session_tag'] = session_doc.get('session_tag', 0) sessionvalue['status'] = session_doc.get('status') sessionvalue['time_ended'] = session_doc.get('time_ended', -1) sessionvalue['time_started'] = session_doc.get('time_started', -1) sessionvalue['time_end'] = session_doc.get('time_end', -1) sessionvalue['time_start'] = session_doc.get('time_start', -1) sessionvalue['result'] = session_doc.get('result', None) # The field scheduling is json sessionvalue['schedule'] = json_utils.json_encode(schedulingjson) sessiont.update(sessionvalue) add_tuple(tuple=sessiont) LOG.info('Session registered, session_id: {0}'.format(session_id)) return session_id
def update_action(user_id, action_id, patch_doc, project_id=None): # changes in user_id or action_id are not allowed if CONF.enable_v1_api: valid_patch = utilsv1.ActionDoc.create_patch(patch_doc) else: valid_patch = utilsv2.ActionDoc.create_patch(patch_doc) keyt = [ 'action', 'backup_name', 'container', 'src_file', 'timeout', 'priority', 'mandatory', 'log_file' ] values = {} freezer_action = valid_patch.get('freezer_action', {}) values['project_id'] = project_id values['id'] = action_id values['user_id'] = user_id values['max_retries'] = valid_patch.get('max_retries', None) values['max_retries_interval'] = valid_patch.\ get('max_retries_interval', None) for key in freezer_action.keys(): if key in keyt: values[key] = freezer_action.get(key) values['actionmode'] = freezer_action.get('mode', None) values['backup_metadata'] = json_utils.json_encode(freezer_action) update_tuple(tablename=models.Action, user_id=user_id, tuple_id=action_id, tuple_values=values, project_id=project_id) return action_id
def update_job(user_id, job_id, patch_doc, project_id=None): if CONF.enable_v1_api: valid_patch = utilsv1.JobDoc.create_patch(patch_doc) else: valid_patch = utilsv2.JobDoc.create_patch(patch_doc) values = {} for key in valid_patch.keys(): if key == 'job_schedule': values['schedule'] = json_utils.\ json_encode(valid_patch.get(key, None)) elif key == 'job_actions': values[key] = json_utils.json_encode(valid_patch.get(key, None)) else: values[key] = valid_patch.get(key, None) update_tuple(tablename=models.Job, user_id=user_id, tuple_id=job_id, tuple_values=values, project_id=project_id) return 0
def replace_session(user_id, session_id, doc, project_id=None): if CONF.enable_v1_api: valid_doc = utilsv1.SessionDoc.update(doc, user_id, session_id) else: valid_doc = utilsv2.SessionDoc.update(doc, user_id, session_id, project_id) values = {} valuesnew = {} values['id'] = session_id values['project_id'] = project_id values['user_id'] = user_id values['schedule'] = json_utils.\ json_encode(valid_doc.get('schedule', None)) values['job'] = json_utils.json_encode(valid_doc.get('jobs', None)) values['session_tag'] = valid_doc.get('session_tag', None) values['description'] = valid_doc.get('description', None) values['status'] = valid_doc.get('status') values['time_ended'] = valid_doc.get('time_ended', None) values['time_started'] = valid_doc.get('time_started', None) values['time_end'] = valid_doc.get('time_end', None) values['time_start'] = valid_doc.get('time_start', None) values['result'] = valid_doc.get('result', None) values['hold_off'] = valid_doc.get('hold_off', None) for key in values: if values[key] is not None: valuesnew[key] = values[key] replace_tuple(tablename=models.Session, user_id=user_id, tuple_id=session_id, tuple_values=valuesnew, project_id=project_id) LOG.info('session replaced, session_id: {0}'.format(session_id)) return session_id
def add_action(user_id, doc, project_id=None): if CONF.enable_v1_api: action_doc = utilsv1.ActionDoc.create(doc, user_id) else: action_doc = utilsv2.ActionDoc.create(doc, user_id, project_id) keyt = [ 'action', 'backup_name', 'container', 'src_file', 'timeout', 'priority', 'mandatory', 'log_file' ] freezer_action = action_doc.get('freezer_action', {}) action_id = action_doc.get('action_id') existing = get_action(project_id=project_id, user_id=user_id, action_id=action_id) if existing: raise freezer_api_exc.DocumentExists( message='Action already registered with ID' ' {0}'.format(action_id)) action = models.Action() actionvalue = {} actionreportvalue = {} actionvalue['project_id'] = project_id actionvalue['id'] = action_id actionvalue['user_id'] = user_id actionvalue['max_retries'] = action_doc.get('max_retries', 5) actionvalue['max_retries_interval'] = action_doc.\ get('max_retries_interval', 6) actionvalue['actionmode'] = freezer_action.get('mode', None) for key in freezer_action.keys(): if key in keyt: actionvalue[key] = freezer_action.get(key) actionreportvalue['result'] = freezer_action.get('result', None) actionreportvalue['time_elapsed'] = freezer_action.\ get('time_elapsed', None) actionreportvalue['report_date'] = freezer_action.\ get('report_date', None) actionvalue['backup_metadata'] = json_utils.json_encode(freezer_action) action.update(actionvalue) add_tuple(tuple=action) LOG.info('Action registered, action_id: {0}'.format(action_id)) actionReport = models.ActionReport() actionreportvalue['project_id'] = project_id actionreportvalue['id'] = action_id actionreportvalue['user_id'] = user_id actionReport.update(actionreportvalue) add_tuple(tuple=actionReport) LOG.info('Action Reports registered, action_id: {0}'.format(action_id)) return action_id