Ejemplo n.º 1
0
    def test_validate_limit(self):
        limit = utils.validate_limit(10)
        self.assertEqual(10, 10)

        # max limit
        limit = utils.validate_limit(999999999)
        self.assertEqual(CONF.api.max_limit, limit)

        # negative
        self.assertRaises(wsme.exc.ClientSideError, utils.validate_limit, -1)

        # zero
        self.assertRaises(wsme.exc.ClientSideError, utils.validate_limit, 0)
Ejemplo n.º 2
0
    def test_validate_limit(self):
        limit = utils.validate_limit(10)
        self.assertEqual(10, 10)

        # max limit
        limit = utils.validate_limit(999999999)
        self.assertEqual(CONF.api.max_limit, limit)

        # negative
        self.assertRaises(wsme.exc.ClientSideError, utils.validate_limit, -1)

        # zero
        self.assertRaises(wsme.exc.ClientSideError, utils.validate_limit, 0)
Ejemplo n.º 3
0
    def _get_audits_collection(self, marker, limit,
                               sort_key, sort_dir, expand=False,
                               resource_url=None, audit_template=None):
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.Audit.get_by_uuid(pecan.request.context,
                                                   marker)

        filters = {}
        if audit_template:
            if utils.is_uuid_like(audit_template):
                filters['audit_template_uuid'] = audit_template
            else:
                filters['audit_template_name'] = audit_template

        if sort_key == 'audit_template_uuid':
            sort_db_key = None
        else:
            sort_db_key = sort_key

        audits = objects.Audit.list(pecan.request.context,
                                    limit,
                                    marker_obj, sort_key=sort_db_key,
                                    sort_dir=sort_dir, filters=filters)

        return AuditCollection.convert_with_links(audits, limit,
                                                  url=resource_url,
                                                  expand=expand,
                                                  sort_key=sort_key,
                                                  sort_dir=sort_dir)
Ejemplo n.º 4
0
    def _get_goals_collection(self,
                              limit,
                              sort_key,
                              sort_dir,
                              expand=False,
                              resource_url=None,
                              goal_name=None):

        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        goals = []

        if not goal_name and goal_name in CONF.watcher_goals.goals.keys():
            goals.append({'name': goal_name, 'strategy': goals[goal_name]})
        else:
            for name, strategy in CONF.watcher_goals.goals.items():
                goals.append({'name': name, 'strategy': strategy})

        return GoalCollection.convert_with_links(goals[:limit],
                                                 limit,
                                                 url=resource_url,
                                                 expand=expand,
                                                 sort_key=sort_key,
                                                 sort_dir=sort_dir)
Ejemplo n.º 5
0
    def _get_scoring_engines_collection(self, marker, limit,
                                        sort_key, sort_dir, expand=False,
                                        resource_url=None):

        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.ScoringEngine.get_by_uuid(
                pecan.request.context, marker)

        filters = {}

        sort_db_key = sort_key

        scoring_engines = objects.ScoringEngine.list(
            context=pecan.request.context,
            limit=limit,
            marker=marker_obj,
            sort_key=sort_db_key,
            sort_dir=sort_dir,
            filters=filters)

        return ScoringEngineCollection.convert_with_links(
            scoring_engines,
            limit,
            url=resource_url,
            expand=expand,
            sort_key=sort_key,
            sort_dir=sort_dir)
Ejemplo n.º 6
0
    def _get_strategies_collection(self, filters, marker, limit, sort_key,
                                   sort_dir, expand=False, resource_url=None):
        additional_fields = ["goal_uuid", "goal_name"]

        api_utils.validate_sort_key(
            sort_key, list(objects.Strategy.fields) + additional_fields)
        api_utils.validate_search_filters(
            filters, list(objects.Strategy.fields) + additional_fields)
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.Strategy.get_by_uuid(
                pecan.request.context, marker)

        need_api_sort = api_utils.check_need_api_sort(sort_key,
                                                      additional_fields)
        sort_db_key = (sort_key if not need_api_sort
                       else None)

        strategies = objects.Strategy.list(
            pecan.request.context, limit, marker_obj, filters=filters,
            sort_key=sort_db_key, sort_dir=sort_dir)

        strategies_collection = StrategyCollection.convert_with_links(
            strategies, limit, url=resource_url, expand=expand,
            sort_key=sort_key, sort_dir=sort_dir)

        if need_api_sort:
            api_utils.make_api_sort(strategies_collection.strategies,
                                    sort_key, sort_dir)

        return strategies_collection
Ejemplo n.º 7
0
    def _get_actions_collection(self, marker, limit,
                                sort_key, sort_dir, expand=False,
                                resource_url=None,
                                action_plan_uuid=None, audit_uuid=None):
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.Action.get_by_uuid(pecan.request.context,
                                                    marker)

        filters = {}
        if action_plan_uuid:
            filters['action_plan_uuid'] = action_plan_uuid

        if audit_uuid:
            filters['audit_uuid'] = audit_uuid

        if sort_key == 'next_uuid':
            sort_db_key = None
        else:
            sort_db_key = sort_key

        actions = objects.Action.list(pecan.request.context,
                                      limit,
                                      marker_obj, sort_key=sort_db_key,
                                      sort_dir=sort_dir,
                                      filters=filters)

        return ActionCollection.convert_with_links(actions, limit,
                                                   url=resource_url,
                                                   expand=expand,
                                                   sort_key=sort_key,
                                                   sort_dir=sort_dir)
Ejemplo n.º 8
0
 def test_validate_limit(self):
     cfg.CONF.set_override("max_limit",
                           self.max_limit,
                           group="api",
                           enforce_type=True)
     actual_limit = v1_utils.validate_limit(self.limit)
     self.assertEqual(self.expected, actual_limit)
Ejemplo n.º 9
0
    def _get_goals_collection(self,
                              marker,
                              limit,
                              sort_key,
                              sort_dir,
                              expand=False,
                              resource_url=None):
        api_utils.validate_sort_key(sort_key, list(objects.Goal.fields))
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.Goal.get_by_uuid(pecan.request.context,
                                                  marker)

        sort_db_key = (sort_key if sort_key in objects.Goal.fields else None)

        goals = objects.Goal.list(pecan.request.context,
                                  limit,
                                  marker_obj,
                                  sort_key=sort_db_key,
                                  sort_dir=sort_dir)

        return GoalCollection.convert_with_links(goals,
                                                 limit,
                                                 url=resource_url,
                                                 expand=expand,
                                                 sort_key=sort_key,
                                                 sort_dir=sort_dir)
Ejemplo n.º 10
0
    def _get_audits_collection(self, marker, limit,
                               sort_key, sort_dir, expand=False,
                               resource_url=None, audit_template=None):
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.Audit.get_by_uuid(pecan.request.context,
                                                   marker)

        filters = {}
        if audit_template:
            if utils.is_uuid_like(audit_template):
                filters['audit_template_uuid'] = audit_template
            else:
                filters['audit_template_name'] = audit_template

        if sort_key == 'audit_template_uuid':
            sort_db_key = None
        else:
            sort_db_key = sort_key

        audits = objects.Audit.list(pecan.request.context,
                                    limit,
                                    marker_obj, sort_key=sort_db_key,
                                    sort_dir=sort_dir, filters=filters)

        return AuditCollection.convert_with_links(audits, limit,
                                                  url=resource_url,
                                                  expand=expand,
                                                  sort_key=sort_key,
                                                  sort_dir=sort_dir)
Ejemplo n.º 11
0
    def _get_audit_templates_collection(self, filters, marker, limit,
                                        sort_key, sort_dir, expand=False,
                                        resource_url=None):
        api_utils.validate_search_filters(
            filters, objects.audit_template.AuditTemplate.fields.keys())
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.AuditTemplate.get_by_uuid(
                pecan.request.context,
                marker)

        audit_templates = objects.AuditTemplate.list(
            pecan.request.context,
            filters,
            limit,
            marker_obj, sort_key=sort_key,
            sort_dir=sort_dir)

        return AuditTemplateCollection.convert_with_links(audit_templates,
                                                          limit,
                                                          url=resource_url,
                                                          expand=expand,
                                                          sort_key=sort_key,
                                                          sort_dir=sort_dir)
Ejemplo n.º 12
0
    def _get_scoring_engines_collection(self,
                                        marker,
                                        limit,
                                        sort_key,
                                        sort_dir,
                                        expand=False,
                                        resource_url=None):

        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.ScoringEngine.get_by_uuid(
                pecan.request.context, marker)

        filters = {}

        sort_db_key = sort_key

        scoring_engines = objects.ScoringEngine.list(
            context=pecan.request.context,
            limit=limit,
            marker=marker_obj,
            sort_key=sort_db_key,
            sort_dir=sort_dir,
            filters=filters)

        return ScoringEngineCollection.convert_with_links(scoring_engines,
                                                          limit,
                                                          url=resource_url,
                                                          expand=expand,
                                                          sort_key=sort_key,
                                                          sort_dir=sort_dir)
Ejemplo n.º 13
0
    def _get_services_collection(self,
                                 marker,
                                 limit,
                                 sort_key,
                                 sort_dir,
                                 expand=False,
                                 resource_url=None):
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        sort_db_key = (sort_key
                       if sort_key in objects.Service.fields else None)

        marker_obj = None
        if marker:
            marker_obj = objects.Service.get(pecan.request.context, marker)

        services = objects.Service.list(pecan.request.context,
                                        limit,
                                        marker_obj,
                                        sort_key=sort_db_key,
                                        sort_dir=sort_dir)

        return ServiceCollection.convert_with_links(services,
                                                    limit,
                                                    url=resource_url,
                                                    expand=expand,
                                                    sort_key=sort_key,
                                                    sort_dir=sort_dir)
Ejemplo n.º 14
0
    def _get_actions_collection(self, marker, limit,
                                sort_key, sort_dir, expand=False,
                                resource_url=None,
                                action_plan_uuid=None, audit_uuid=None):
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.Action.get_by_uuid(pecan.request.context,
                                                    marker)

        filters = {}
        if action_plan_uuid:
            filters['action_plan_uuid'] = action_plan_uuid

        if audit_uuid:
            filters['audit_uuid'] = audit_uuid

        if sort_key == 'next_uuid':
            sort_db_key = None
        else:
            sort_db_key = sort_key

        actions = objects.Action.list(pecan.request.context,
                                      limit,
                                      marker_obj, sort_key=sort_db_key,
                                      sort_dir=sort_dir,
                                      filters=filters)

        return ActionCollection.convert_with_links(actions, limit,
                                                   url=resource_url,
                                                   expand=expand,
                                                   sort_key=sort_key,
                                                   sort_dir=sort_dir)
Ejemplo n.º 15
0
    def _get_action_plans_collection(self,
                                     marker,
                                     limit,
                                     sort_key,
                                     sort_dir,
                                     expand=False,
                                     resource_url=None,
                                     audit_uuid=None,
                                     strategy=None):
        additional_fields = ['audit_uuid', 'strategy_uuid', 'strategy_name']

        api_utils.validate_sort_key(
            sort_key,
            list(objects.ActionPlan.fields) + additional_fields)
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.ActionPlan.get_by_uuid(
                pecan.request.context, marker)

        filters = {}
        if audit_uuid:
            filters['audit_uuid'] = audit_uuid

        if strategy:
            if utils.is_uuid_like(strategy):
                filters['strategy_uuid'] = strategy
            else:
                filters['strategy_name'] = strategy

        need_api_sort = api_utils.check_need_api_sort(sort_key,
                                                      additional_fields)
        sort_db_key = (sort_key if not need_api_sort else None)

        action_plans = objects.ActionPlan.list(pecan.request.context,
                                               limit,
                                               marker_obj,
                                               sort_key=sort_db_key,
                                               sort_dir=sort_dir,
                                               filters=filters)

        action_plans_collection = ActionPlanCollection.convert_with_links(
            action_plans,
            limit,
            url=resource_url,
            expand=expand,
            sort_key=sort_key,
            sort_dir=sort_dir)

        if need_api_sort:
            api_utils.make_api_sort(action_plans_collection.action_plans,
                                    sort_key, sort_dir)

        return action_plans_collection
Ejemplo n.º 16
0
    def _get_audits_collection(self,
                               marker,
                               limit,
                               sort_key,
                               sort_dir,
                               expand=False,
                               resource_url=None,
                               goal=None,
                               strategy=None):
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)
        marker_obj = None
        if marker:
            marker_obj = objects.Audit.get_by_uuid(pecan.request.context,
                                                   marker)

        filters = {}
        if goal:
            if utils.is_uuid_like(goal):
                filters['goal_uuid'] = goal
            else:
                # TODO(michaelgugino): add method to get goal by name.
                filters['goal_name'] = goal

        if strategy:
            if utils.is_uuid_like(strategy):
                filters['strategy_uuid'] = strategy
            else:
                # TODO(michaelgugino): add method to get goal by name.
                filters['strategy_name'] = strategy

        if sort_key == 'goal_uuid':
            sort_db_key = 'goal_id'
        elif sort_key == 'strategy_uuid':
            sort_db_key = 'strategy_id'
        else:
            sort_db_key = sort_key

        audits = objects.Audit.list(pecan.request.context,
                                    limit,
                                    marker_obj,
                                    sort_key=sort_db_key,
                                    sort_dir=sort_dir,
                                    filters=filters)

        return AuditCollection.convert_with_links(audits,
                                                  limit,
                                                  url=resource_url,
                                                  expand=expand,
                                                  sort_key=sort_key,
                                                  sort_dir=sort_dir)
Ejemplo n.º 17
0
    def _get_audits_collection(self, marker, limit,
                               sort_key, sort_dir, expand=False,
                               resource_url=None, goal=None,
                               strategy=None):
        additional_fields = ["goal_uuid", "goal_name", "strategy_uuid",
                             "strategy_name"]

        api_utils.validate_sort_key(
            sort_key, list(objects.Audit.fields) + additional_fields)
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        marker_obj = None
        if marker:
            marker_obj = objects.Audit.get_by_uuid(pecan.request.context,
                                                   marker)

        filters = {}
        if goal:
            if utils.is_uuid_like(goal):
                filters['goal_uuid'] = goal
            else:
                # TODO(michaelgugino): add method to get goal by name.
                filters['goal_name'] = goal

        if strategy:
            if utils.is_uuid_like(strategy):
                filters['strategy_uuid'] = strategy
            else:
                # TODO(michaelgugino): add method to get goal by name.
                filters['strategy_name'] = strategy

        need_api_sort = api_utils.check_need_api_sort(sort_key,
                                                      additional_fields)
        sort_db_key = (sort_key if not need_api_sort
                       else None)

        audits = objects.Audit.list(pecan.request.context,
                                    limit,
                                    marker_obj, sort_key=sort_db_key,
                                    sort_dir=sort_dir, filters=filters)

        audits_collection = AuditCollection.convert_with_links(
            audits, limit, url=resource_url, expand=expand,
            sort_key=sort_key, sort_dir=sort_dir)

        if need_api_sort:
            api_utils.make_api_sort(audits_collection.audits, sort_key,
                                    sort_dir)

        return audits_collection
Ejemplo n.º 18
0
    def _get_audits_collection(self, marker, limit,
                               sort_key, sort_dir, expand=False,
                               resource_url=None, goal=None,
                               strategy=None, host_aggregate=None):
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)
        marker_obj = None
        if marker:
            marker_obj = objects.Audit.get_by_uuid(pecan.request.context,
                                                   marker)

        filters = {}
        if goal:
            if utils.is_uuid_like(goal):
                filters['goal_uuid'] = goal
            else:
                # TODO(michaelgugino): add method to get goal by name.
                filters['goal_name'] = goal

        if strategy:
            if utils.is_uuid_like(strategy):
                filters['strategy_uuid'] = strategy
            else:
                # TODO(michaelgugino): add method to get goal by name.
                filters['strategy_name'] = strategy

        if sort_key == 'goal_uuid':
            sort_db_key = 'goal_id'
        elif sort_key == 'strategy_uuid':
            sort_db_key = 'strategy_id'
        else:
            sort_db_key = sort_key

        audits = objects.Audit.list(pecan.request.context,
                                    limit,
                                    marker_obj, sort_key=sort_db_key,
                                    sort_dir=sort_dir, filters=filters)

        return AuditCollection.convert_with_links(audits, limit,
                                                  url=resource_url,
                                                  expand=expand,
                                                  sort_key=sort_key,
                                                  sort_dir=sort_dir)
Ejemplo n.º 19
0
    def _get_goals_collection(self, limit,
                              sort_key, sort_dir, expand=False,
                              resource_url=None, goal_name=None):

        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        goals = []

        if not goal_name and goal_name in CONF.watcher_goals.goals.keys():
            goals.append({'name': goal_name, 'strategy': goals[goal_name]})
        else:
            for name, strategy in CONF.watcher_goals.goals.items():
                goals.append({'name': name, 'strategy': strategy})

        return GoalCollection.convert_with_links(goals[:limit], limit,
                                                 url=resource_url,
                                                 expand=expand,
                                                 sort_key=sort_key,
                                                 sort_dir=sort_dir)
Ejemplo n.º 20
0
    def _get_goals_collection(self, marker, limit, sort_key, sort_dir,
                              expand=False, resource_url=None):
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        sort_db_key = (sort_key if sort_key in objects.Goal.fields.keys()
                       else None)

        marker_obj = None
        if marker:
            marker_obj = objects.Goal.get_by_uuid(
                pecan.request.context, marker)

        goals = objects.Goal.list(pecan.request.context, limit, marker_obj,
                                  sort_key=sort_db_key, sort_dir=sort_dir)

        return GoalCollection.convert_with_links(goals, limit,
                                                 url=resource_url,
                                                 expand=expand,
                                                 sort_key=sort_key,
                                                 sort_dir=sort_dir)
Ejemplo n.º 21
0
    def _get_strategies_collection(self, filters, marker, limit, sort_key,
                                   sort_dir, expand=False, resource_url=None):
        api_utils.validate_search_filters(
            filters, list(objects.strategy.Strategy.fields) +
            ["goal_uuid", "goal_name"])
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        sort_db_key = (sort_key if sort_key in objects.Strategy.fields
                       else None)

        marker_obj = None
        if marker:
            marker_obj = objects.Strategy.get_by_uuid(
                pecan.request.context, marker)

        strategies = objects.Strategy.list(
            pecan.request.context, limit, marker_obj, filters=filters,
            sort_key=sort_db_key, sort_dir=sort_dir)

        return StrategyCollection.convert_with_links(
            strategies, limit, url=resource_url, expand=expand,
            sort_key=sort_key, sort_dir=sort_dir)
Ejemplo n.º 22
0
    def _get_strategies_collection(self, filters, marker, limit, sort_key,
                                   sort_dir, expand=False, resource_url=None):
        api_utils.validate_search_filters(
            filters, list(objects.strategy.Strategy.fields.keys()) +
            ["goal_uuid", "goal_name"])
        limit = api_utils.validate_limit(limit)
        api_utils.validate_sort_dir(sort_dir)

        sort_db_key = (sort_key if sort_key in objects.Strategy.fields.keys()
                       else None)

        marker_obj = None
        if marker:
            marker_obj = objects.Strategy.get_by_uuid(
                pecan.request.context, marker)

        strategies = objects.Strategy.list(
            pecan.request.context, limit, marker_obj, filters=filters,
            sort_key=sort_db_key, sort_dir=sort_dir)

        return StrategyCollection.convert_with_links(
            strategies, limit, url=resource_url, expand=expand,
            sort_key=sort_key, sort_dir=sort_dir)
Ejemplo n.º 23
0
 def test_validate_limit(self):
     cfg.CONF.set_override("max_limit", self.max_limit, group="api",
                           enforce_type=True)
     actual_limit = v1_utils.validate_limit(self.limit)
     self.assertEqual(actual_limit, self.expected)