def schedule_get_by_id(schedule_id): if schedule_id not in DATA['schedules']: raise exception.NotFound() schedule = copy.deepcopy(DATA['schedules'][schedule_id]) schedule['schedule_metadata'] = \ copy.deepcopy(schedule_meta_get_all(schedule_id)) return schedule
def job_update(job_id, job_values): global DATA values = job_values.copy() if job_id not in DATA['jobs']: raise exception.NotFound() metadata = None if 'job_metadata' in values: metadata = values['job_metadata'] del values['job_metadata'] if len(values) > 0: job = DATA['jobs'][job_id] #NOTE(ameade): This must come before update specified values since # we may be trying to manually set updated_at job['updated_at'] = timeutils.utcnow() job['version_id'] = str(uuid.uuid4()) job.update(values) if metadata is not None: DATA['job_metadata'][job_id] = {} for metadatum in metadata: job_meta_create(job_id, metadatum) return job_get_by_id(job_id)
def _check_job_meta_exists(job_id, key): if (DATA['job_metadata'].get(job_id) is None or DATA['job_metadata'][job_id].get(key) is None): msg = (_('Meta %(key)s could not be found for Job %(job_id)s ') % {'key': key, 'job_id': job_id}) raise exception.NotFound(message=msg)
def _schedule_meta_get(schedule_id, key, session=None): session = session or get_session() try: _schedule_get_by_id(schedule_id, session) except exception.NotFound: msg = _('Schedule %s could not be found') % schedule_id raise exception.NotFound(message=msg) try: meta = session.query(models.ScheduleMetadata)\ .filter_by(schedule_id=schedule_id)\ .filter_by(key=key)\ .one() except sa_orm.exc.NoResultFound: raise exception.NotFound() return meta
def schedule_test_and_set_next_run(schedule_id, expected_next_run, next_run): global DATA schedule = DATA['schedules'].get(schedule_id) if not schedule: raise exception.NotFound() if expected_next_run: expected_next_run = expected_next_run.replace(tzinfo=None) current_next_run = schedule.get('next_run') if current_next_run: current_next_run = current_next_run.replace(tzinfo=None) if expected_next_run != current_next_run: raise exception.NotFound() if next_run: next_run = next_run.replace(tzinfo=None) schedule['next_run'] = next_run
def _check_meta_exists(schedule_id, key): _check_schedule_exists(schedule_id) if (DATA['schedule_metadata'].get(schedule_id) is None or DATA['schedule_metadata'][schedule_id].get(key) is None): msg = (_('Meta %(key)s could not be found ' 'for Schedule %(schedule_id)s ') % {'key': key, 'schedule_id': schedule_id}) raise exception.NotFound(message=msg)
def _job_meta_get_all_by_job_id(job_id): session = get_session() try: meta = session.query(models.JobMetadata)\ .filter_by(job_id=job_id)\ .all() except sa_orm.exc.NoResultFound: raise exception.NotFound() return meta
def job_get_by_id(job_id): if job_id not in DATA['jobs']: raise exception.NotFound() job = copy.deepcopy(DATA['jobs'][job_id]) job['job_metadata'] = \ job_meta_get_all_by_job_id(job_id) return job
def schedule_metadata_update(schedule_id, values): global DATA if DATA['schedules'].get(schedule_id) is None: msg = _('Schedule %s could not be found') % schedule_id raise exception.NotFound(message=msg) DATA['schedule_metadata'][schedule_id] = {} for metadatum in values: schedule_meta_create(schedule_id, metadatum) return copy.deepcopy(DATA['schedule_metadata'][schedule_id].values())
def _job_get_by_id(job_id, session=None): session = session or get_session() try: job = session.query(models.Job)\ .options(sa_orm.subqueryload('job_metadata'))\ .filter_by(id=job_id)\ .one() except sa_orm.exc.NoResultFound: raise exception.NotFound() return job
def _schedule_get_by_id(schedule_id, session=None): session = session or get_session() try: schedule = session.query(models.Schedule)\ .options(sa_orm.joinedload_all('schedule_metadata'))\ .filter_by(id=schedule_id)\ .one() except sa_orm.exc.NoResultFound: raise exception.NotFound() return schedule
def schedule_test_and_set_next_run(schedule_id, expected_next_run, next_run): session = get_session() if expected_next_run: query = session.query(models.Schedule).filter_by(id=schedule_id)\ .filter_by(next_run=expected_next_run)\ .update(dict(next_run=next_run)) else: query = session.query(models.Schedule).filter_by(id=schedule_id)\ .update(dict(next_run=next_run)) if not query: raise exception.NotFound()
def schedule_meta_create(schedule_id, values): global DATA if DATA['schedules'].get(schedule_id) is None: msg = _('Schedule %s could not be found') % schedule_id raise exception.NotFound(message=msg) _schedule_meta_init(schedule_id) try: _check_meta_exists(schedule_id, values['key']) except exception.NotFound: pass else: raise exception.Duplicate() meta = {} values['schedule_id'] = schedule_id meta.update(values) meta.update(_gen_base_attributes()) DATA['schedule_metadata'][schedule_id][values['key']] = meta return copy.deepcopy(meta)
def schedule_update(schedule_id, schedule_values): global DATA values = schedule_values.copy() if schedule_id not in DATA['schedules']: raise exception.NotFound() metadata = None if 'schedule_metadata' in values: metadata = values['schedule_metadata'] del values['schedule_metadata'] if len(values) > 0: schedule = DATA['schedules'][schedule_id] schedule['updated_at'] = timeutils.utcnow() schedule.update(values) if metadata is not None: DATA['schedule_metadata'][schedule_id] = {} for metadatum in metadata: schedule_meta_create(schedule_id, metadatum) return schedule_get_by_id(schedule_id)
def _do_pagination(items, marker, limit): """ This method mimics the behavior of sqlalchemy paginate_query. It takes items and pagination parameters - 'limit' and 'marker' to filter out the items to be returned. Items are sorted in lexicographical order based on the sort key - 'id'. """ items = sorted(items, key=itemgetter('id')) start = 0 end = -1 if marker is None: start = 0 else: for i, item in enumerate(items): if item['id'] == marker: start = i + 1 break else: msg = _('Marker %s not found') % marker raise exception.NotFound(explanation=msg) end = start + limit if limit is not None else None return items[start:end]
def schedule_delete(schedule_id): global DATA if schedule_id not in DATA['schedules']: raise exception.NotFound() del DATA['schedules'][schedule_id]
def _check_job_exists(job_id): if not job_id in DATA['jobs']: msg = _('Job %s could not be found') % job_id raise exception.NotFound(message=msg)
def job_delete(job_id): global DATA if job_id not in DATA['jobs']: raise exception.NotFound() del DATA['jobs'][job_id]
def _check_schedule_exists(schedule_id): if DATA['schedules'].get(schedule_id) is None: msg = _('Schedule %s could not be found') % schedule_id raise exception.NotFound(message=msg)
def worker_get_by_id(worker_id): if worker_id not in DATA['workers']: raise exception.NotFound() return copy.deepcopy(DATA['workers'][worker_id])
def worker_delete(worker_id): global DATA if worker_id not in DATA['workers']: raise exception.NotFound() del DATA['workers'][worker_id]