def get(cls, context, efficacy_indicator_id): """Find an efficacy indicator object given its ID or UUID :param efficacy_indicator_id: the ID or UUID of an efficacy indicator. :returns: a :class:`EfficacyIndicator` object. """ if utils.is_int_like(efficacy_indicator_id): return cls.get_by_id(context, efficacy_indicator_id) elif utils.is_uuid_like(efficacy_indicator_id): return cls.get_by_uuid(context, efficacy_indicator_id) else: raise exception.InvalidIdentity(identity=efficacy_indicator_id)
def get(cls, context, action_plan_id): """Find a action_plan based on its id or uuid and return a Action object. :param action_plan_id: the id *or* uuid of a action_plan. :returns: a :class:`Action` object. """ if utils.is_int_like(action_plan_id): return cls.get_by_id(context, action_plan_id) elif utils.is_uuid_like(action_plan_id): return cls.get_by_uuid(context, action_plan_id) else: raise exception.InvalidIdentity(identity=action_plan_id)
def get(cls, context, action_plan_id, eager=False): """Find a action_plan based on its id or uuid and return a Action object. :param action_plan_id: the id *or* uuid of a action_plan. :param eager: Load object fields if True (Default: False) :returns: a :class:`Action` object. """ if utils.is_int_like(action_plan_id): return cls.get_by_id(context, action_plan_id, eager=eager) elif utils.is_uuid_like(action_plan_id): return cls.get_by_uuid(context, action_plan_id, eager=eager) else: raise exception.InvalidIdentity(identity=action_plan_id)
def _get_goal(self, value): if value == wtypes.Unset: return None goal = None try: if utils.is_uuid_like(value) or utils.is_int_like(value): goal = objects.Goal.get(pecan.request.context, value) else: goal = objects.Goal.get_by_name(pecan.request.context, value) except exception.GoalNotFound: pass if goal: self.goal_id = goal.id return goal
def launch_audits_periodically(self): audit_context = context.RequestContext(is_admin=True) audit_filters = { 'audit_type': objects.audit.AuditType.CONTINUOUS.value, 'state__in': (objects.audit.State.PENDING, objects.audit.State.ONGOING, objects.audit.State.SUCCEEDED) } audits = objects.Audit.list(audit_context, filters=audit_filters, eager=True) scheduler_job_args = [ job.args for job in self.scheduler.get_jobs() if job.name == 'execute_audit' ] for audit in audits: # if audit is not presented in scheduled audits yet. if audit.uuid not in [arg[0].uuid for arg in scheduler_job_args]: # if interval is provided with seconds if utils.is_int_like(audit.interval): # if audit has already been provided and we need # to restore it after shutdown if audit.next_run_time is not None: old_run_time = audit.next_run_time current = datetime.datetime.utcnow() if old_run_time < current: delta = datetime.timedelta( seconds=(int(audit.interval) - (current - old_run_time).seconds % int(audit.interval))) audit.next_run_time = current + delta next_run_time = audit.next_run_time # if audit is new one else: next_run_time = datetime.datetime.utcnow() self._add_job('interval', audit, audit_context, seconds=int(audit.interval), next_run_time=next_run_time) else: audit.next_run_time = self._next_cron_time(audit) self._add_job('date', audit, audit_context, run_date=audit.next_run_time) audit.save()
def execute_audit(cls, audit, request_context): self = cls() if not self._is_audit_inactive(audit): try: self.execute(audit, request_context) except Exception: raise finally: if utils.is_int_like(audit.interval): audit.next_run_time = ( datetime.datetime.utcnow() + datetime.timedelta(seconds=int(audit.interval))) else: audit.next_run_time = self._next_cron_time(audit) audit.save()
def _get_goal(self, value): if value == wtypes.Unset: return None goal = None try: if (common_utils.is_uuid_like(value) or common_utils.is_int_like(value)): goal = objects.Goal.get(pecan.request.context, value) else: goal = objects.Goal.get_by_name(pecan.request.context, value) except exception.GoalNotFound: pass if goal: self.goal_id = goal.id return goal
def _get_strategy(self, value): if value == wtypes.Unset: return None strategy = None try: if utils.is_uuid_like(value) or utils.is_int_like(value): strategy = objects.Strategy.get(pecan.request.context, value) else: strategy = objects.Strategy.get_by_name( pecan.request.context, value) except exception.StrategyNotFound: pass if strategy: self.strategy_id = strategy.id return strategy
def _get_audit_template(self, value): if value == wtypes.Unset: return None audit_template = None try: if utils.is_uuid_like(value) or utils.is_int_like(value): audit_template = objects.AuditTemplate.get( pecan.request.context, value) else: audit_template = objects.AuditTemplate.get_by_name( pecan.request.context, value) except exception.AuditTemplateNotFound: pass if audit_template: self.audit_template_id = audit_template.id return audit_template
def get_audit_scope(self, context, audit=None): scope = None try: if utils.is_uuid_like(audit) or utils.is_int_like(audit): audit = objects.Audit.get( context, audit) else: audit = objects.Audit.get_by_name( context, audit) except exception.AuditNotFound: raise exception.InvalidIdentity(identity=audit) if audit: scope = audit.scope else: scope = [] return scope
def add_identity_filter(query, value): """Adds an identity filter to a query. Filters results by ID, if supplied value is a valid integer. Otherwise attempts to filter results by UUID. :param query: Initial query to add filter to. :param value: Value for filtering results by. :return: Modified query. """ if utils.is_int_like(value): return query.filter_by(id=value) elif utils.is_uuid_like(value): return query.filter_by(uuid=value) else: raise exception.InvalidIdentity(identity=value)
def _get_strategy(self, value): if value == wtypes.Unset: return None strategy = None try: if utils.is_uuid_like(value) or utils.is_int_like(value): strategy = objects.Strategy.get( pecan.request.context, value) else: strategy = objects.Strategy.get_by_name( pecan.request.context, value) except exception.GoalNotFound: pass if strategy: self.strategy_id = strategy.id return strategy
def get_resource(resource, resource_id): """Get the resource from the uuid, id or logical name. :param resource: the resource type. :param resource_id: the UUID, ID or logical name of the resource. :returns: The resource. """ resource = getattr(objects, resource) if utils.is_int_like(resource_id): return resource.get(pecan.request.context, int(resource_id)) if uuidutils.is_uuid_like(resource_id): return resource.get_by_uuid(pecan.request.context, resource_id) return resource.get_by_name(pecan.request.context, resource_id)
def get(cls, context, service_id): """Find a service based on its id :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Service(context) :param service_id: the id of a service. :returns: a :class:`Service` object. """ if utils.is_int_like(service_id): db_service = cls.dbapi.get_service_by_id(context, service_id) service = Service._from_db_object(cls(context), db_service) return service else: raise exception.InvalidIdentity(identity=service_id)
def get(cls, context, audit_id): """Find a audit based on its id or uuid and return a Audit object. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Audit(context) :param audit_id: the id *or* uuid of a audit. :returns: a :class:`Audit` object. """ if utils.is_int_like(audit_id): return cls.get_by_id(context, audit_id) elif utils.is_uuid_like(audit_id): return cls.get_by_uuid(context, audit_id) else: raise exception.InvalidIdentity(identity=audit_id)
def get(cls, context, scoring_engine_id): """Find a scoring engine based on its id or uuid :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: ScoringEngine(context) :param scoring_engine_name: the name of a scoring_engine. :returns: a :class:`ScoringEngine` object. """ if utils.is_int_like(scoring_engine_id): return cls.get_by_id(context, scoring_engine_id) elif utils.is_uuid_like(scoring_engine_id): return cls.get_by_uuid(context, scoring_engine_id) else: raise exception.InvalidIdentity(identity=scoring_engine_id)
def get(cls, context, goal_id): """Find a goal based on its id or uuid :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Goal(context) :param goal_id: the id *or* uuid of a goal. :returns: a :class:`Goal` object. """ if utils.is_int_like(goal_id): return cls.get_by_id(context, goal_id) elif utils.is_uuid_like(goal_id): return cls.get_by_uuid(context, goal_id) else: raise exception.InvalidIdentity(identity=goal_id)
def get(cls, context, audit_id, eager=False): """Find a audit based on its id or uuid and return a Audit object. :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object, e.g.: Audit(context) :param audit_id: the id *or* uuid of a audit. :param eager: Load object fields if True (Default: False) :returns: a :class:`Audit` object. """ if utils.is_int_like(audit_id): return cls.get_by_id(context, audit_id, eager=eager) elif utils.is_uuid_like(audit_id): return cls.get_by_uuid(context, audit_id, eager=eager) else: raise exception.InvalidIdentity(identity=audit_id)
def get(cls, context, action_id): """Find a action description based on its id :param context: Security context. NOTE: This should only be used internally by the indirection_api. Unfortunately, RPC requires context as the first argument, even though we don't use it. A context should be set when instantiating the object :param action_id: the id of a action description. :returns: a :class:`ActionDescription` object. """ if utils.is_int_like(action_id): db_action = cls.dbapi.get_action_description_by_id( context, action_id) action = ActionDescription._from_db_object(cls(context), db_action) return action else: raise exception.InvalidIdentity(identity=action_id)
def get_resource(resource, resource_id, eager=False): """Get the resource from the uuid, id or logical name. :param resource: the resource type. :param resource_id: the UUID, ID or logical name of the resource. :returns: The resource. """ resource = getattr(objects, resource) _get = None if utils.is_int_like(resource_id): resource_id = int(resource_id) _get = resource.get elif uuidutils.is_uuid_like(resource_id): _get = resource.get_by_uuid else: _get = resource.get_by_name method_signature = reflection.get_signature(_get) if 'eager' in method_signature.parameters: return _get(pecan.request.context, resource_id, eager=eager) return _get(pecan.request.context, resource_id)
def validate(value): if not (utils.is_int_like(value) or utils.is_cron_like(value)): raise exception.InvalidIntervalOrCron(name=value) return value
def launch_audits_periodically(self): # if audit scheduler stop, restart it if not self.scheduler.running: self.scheduler.start() audit_context = context.RequestContext(is_admin=True) audit_filters = { 'audit_type': objects.audit.AuditType.CONTINUOUS.value, 'state__in': (objects.audit.State.PENDING, objects.audit.State.ONGOING), } audit_filters['hostname'] = None unscheduled_audits = objects.Audit.list(audit_context, filters=audit_filters, eager=True) for audit in unscheduled_audits: # If continuous audit doesn't have a hostname yet, # Watcher will set current CONF.host value. # TODO(alexchadin): Add scheduling of new continuous audits. audit.hostname = CONF.host audit.save() scheduler_job_args = [(job.args[0].uuid, job) for job in self.scheduler.get_jobs() if job.name == 'execute_audit'] scheduler_jobs = dict(scheduler_job_args) # if audit isn't in active states, audit's job should be removed jobs_to_remove = [] for job in scheduler_jobs.values(): if self._is_audit_inactive(job.args[0]): jobs_to_remove.append(job.args[0].uuid) for audit_uuid in jobs_to_remove: scheduler_jobs.pop(audit_uuid) audit_filters['hostname'] = CONF.host audits = objects.Audit.list(audit_context, filters=audit_filters, eager=True) for audit in audits: if self.check_audit_expired(audit): continue existing_job = scheduler_jobs.get(audit.uuid, None) # if audit is not presented in scheduled audits yet, # just add a new audit job. # if audit is already in the job queue, and interval has changed, # we need to remove the old job and add a new one. if (existing_job is None) or (existing_job and audit.interval != existing_job.args[0].interval): if existing_job: self.scheduler.remove_job(existing_job.id) # if interval is provided with seconds if utils.is_int_like(audit.interval): # if audit has already been provided and we need # to restore it after shutdown if audit.next_run_time is not None: old_run_time = audit.next_run_time current = datetime.datetime.utcnow() if old_run_time < current: delta = datetime.timedelta( seconds=(int(audit.interval) - (current - old_run_time).seconds % int(audit.interval))) audit.next_run_time = current + delta next_run_time = audit.next_run_time # if audit is new one else: next_run_time = datetime.datetime.utcnow() self._add_job('interval', audit, audit_context, seconds=int(audit.interval), next_run_time=next_run_time) else: audit.next_run_time = self._next_cron_time(audit) self._add_job('date', audit, audit_context, run_date=audit.next_run_time) audit.hostname = CONF.host audit.save()
def _get_object_by_value(context, class_name, value): if utils.is_uuid_like(value) or utils.is_int_like(value): return class_name.get(context, value) else: return class_name.get_by_name(context, value)