Exemplo n.º 1
0
 def validate_alarm(alarm):
     project = v2_utils.get_auth_project(
         alarm.project_id if alarm.project_id != wtypes.Unset else None)
     for id in alarm.combination_rule.alarm_ids:
         alarms = list(pecan.request.alarm_storage_conn.get_alarms(
             alarm_id=id, project=project))
         if not alarms:
             raise base.AlarmNotFound(id, project)
Exemplo n.º 2
0
 def validate_alarm(alarm):
     project = v2_utils.get_auth_project(
         alarm.project_id if alarm.project_id != wtypes.Unset else None)
     for id in alarm.combination_rule.alarm_ids:
         alarms = list(pecan.request.storage.get_alarms(
             alarm_id=id, project=project))
         if not alarms:
             raise base.AlarmNotFound(id, project)
Exemplo n.º 3
0
    def validate_alarm(cls, alarm):
        super(AggregationMetricByResourcesLookupRule,
              cls).validate_alarm(alarm)

        rule = alarm.gnocchi_aggregation_by_resources_threshold_rule

        # check the query string is a valid json
        try:
            query = json.loads(rule.query)
        except ValueError:
            raise wsme.exc.InvalidInput('rule/query', rule.query)

        conf = pecan.request.cfg

        # Scope the alarm to the project id if needed
        auth_project = v2_utils.get_auth_project(alarm.project_id)
        if auth_project:

            perms_filter = {"=": {"created_by_project_id": auth_project}}

            external_project_owner = cls.get_external_project_owner()
            if external_project_owner:
                perms_filter = {"or": [
                    perms_filter,
                    {"and": [
                        {"=": {"created_by_project_id":
                               external_project_owner}},
                        {"=": {"project_id": auth_project}}]}
                ]}

            query = {"and": [perms_filter, query]}
            rule.query = json.dumps(query)

        gnocchi_client = client.Client(
            '1', keystone_client.get_session(conf),
            adapter_options={
                'interface': conf.service_credentials.interface,
                'region_name': conf.service_credentials.region_name})
        try:
            gnocchi_client.metric.aggregation(
                metrics=rule.metric,
                query=query,
                aggregation=rule.aggregation_method,
                needed_overlap=0,
                start="-1 day",
                stop="now",
                resource_type=rule.resource_type)
        except exceptions.ClientException as e:
            if e.code == 404:
                # NOTE(sileht): We are fine here, we just want to ensure the
                # 'query' payload is valid for Gnocchi If the metric
                # doesn't exists yet, it doesn't matter
                return
            raise base.ClientSideError(e.message, status_code=e.code)
        except Exception as e:
            raise GnocchiUnavailable(e)
Exemplo n.º 4
0
    def validate_alarm(cls, alarm):
        super(AggregationMetricByResourcesLookupRule,
              cls).validate_alarm(alarm)

        rule = alarm.gnocchi_aggregation_by_resources_threshold_rule

        # check the query string is a valid json
        try:
            query = jsonutils.loads(rule.query)
        except ValueError:
            raise wsme.exc.InvalidInput('rule/query', rule.query)

        # Scope the alarm to the project id if needed
        auth_project = v2_utils.get_auth_project(alarm.project_id)
        if auth_project:
            query = {
                "and": [{
                    "=": {
                        "created_by_project_id": auth_project
                    }
                }, query]
            }
            rule.query = jsonutils.dumps(query)

        conf = pecan.request.cfg
        gnocchi_client = client.Client(
            '1',
            keystone_client.get_session(conf),
            interface=conf.service_credentials.interface,
            region_name=conf.service_credentials.region_name,
            endpoint_override=conf.gnocchi_url)

        try:
            gnocchi_client.metric.aggregation(
                metrics=rule.metric,
                query=query,
                aggregation=rule.aggregation_method,
                needed_overlap=0,
                resource_type=rule.resource_type)
        except exceptions.ClientException as e:
            raise base.ClientSideError(e.message, status_code=e.code)
        except Exception as e:
            raise GnocchiUnavailable(e)
Exemplo n.º 5
0
    def validate_alarm(cls, alarm):
        super(AggregationMetricByResourcesLookupRule,
              cls).validate_alarm(alarm)

        rule = alarm.gnocchi_aggregation_by_resources_threshold_rule

        # check the query string is a valid json
        try:
            query = jsonutils.loads(rule.query)
        except ValueError:
            raise wsme.exc.InvalidInput('rule/query', rule.query)

        # Scope the alarm to the project id if needed
        auth_project = v2_utils.get_auth_project(alarm.project_id)
        if auth_project:
            rule.query = jsonutils.dumps({
                "and": [{"=": {"created_by_project_id": auth_project}},
                        query]})

        # Delegate the query validation to gnocchi
        ks_client = keystone_client.get_client(pecan.request.cfg)
        request = {
            'url': "%s/v1/aggregation/resource/%s/metric/%s" % (
                pecan.request.cfg.gnocchi_url,
                rule.resource_type,
                rule.metric),
            'headers': {'Content-Type': "application/json",
                        'X-Auth-Token': ks_client.auth_token},
            'params': {'aggregation': rule.aggregation_method,
                       'needed_overlap': 0},
            'data': rule.query,
        }

        try:
            r = requests.post(**request)
        except requests.ConnectionError as e:
            raise GnocchiUnavailable(e)
        if r.status_code // 200 != 1:
            raise base.ClientSideError(r.content, status_code=r.status_code)
Exemplo n.º 6
0
    def validate_alarm(cls, alarm):
        super(AggregationMetricByResourcesLookupRule,
              cls).validate_alarm(alarm)

        rule = alarm.gnocchi_aggregation_by_resources_threshold_rule

        # check the query string is a valid json
        try:
            query = jsonutils.loads(rule.query)
        except ValueError:
            raise wsme.exc.InvalidInput('rule/query', rule.query)

        # Scope the alarm to the project id if needed
        auth_project = v2_utils.get_auth_project(alarm.project_id)
        if auth_project:
            rule.query = jsonutils.dumps({
                "and": [{"=": {"created_by_project_id": auth_project}},
                        query]})

        # Delegate the query validation to gnocchi
        ks_client = keystone_client.get_client(pecan.request.cfg)
        request = {
            'url': "%s/v1/aggregation/resource/%s/metric/%s" % (
                pecan.request.cfg.gnocchi_url,
                rule.resource_type,
                rule.metric),
            'headers': {'Content-Type': "application/json",
                        'X-Auth-Token': ks_client.auth_token},
            'params': {'aggregation': rule.aggregation_method},
            'data': rule.query,
        }

        try:
            r = requests.post(**request)
        except requests.ConnectionError as e:
            raise GnocchiUnavailable(e)
        if r.status_code // 200 != 1:
            raise base.ClientSideError(r.content, status_code=r.status_code)
    def validate_alarm(cls, alarm):
        super(AggregationMetricByResourcesLookupRule,
              cls).validate_alarm(alarm)

        rule = alarm.gnocchi_aggregation_by_resources_threshold_rule

        # check the query string is a valid json
        try:
            query = jsonutils.loads(rule.query)
        except ValueError:
            raise wsme.exc.InvalidInput('rule/query', rule.query)

        # Scope the alarm to the project id if needed
        auth_project = v2_utils.get_auth_project(alarm.project_id)
        if auth_project:
            query = {"and": [{"=": {"created_by_project_id": auth_project}},
                             query]}
            rule.query = jsonutils.dumps(query)

        conf = pecan.request.cfg
        gnocchi_client = client.Client(
            '1', keystone_client.get_session(conf),
            interface=conf.service_credentials.interface,
            region_name=conf.service_credentials.region_name,
            endpoint_override=conf.gnocchi_url)

        try:
            gnocchi_client.metric.aggregation(
                metrics=rule.metric,
                query=query,
                aggregation=rule.aggregation_method,
                needed_overlap=0,
                resource_type=rule.resource_type)
        except exceptions.ClientException as e:
            raise base.ClientSideError(e.message, status_code=e.code)
        except Exception as e:
            raise GnocchiUnavailable(e)