|
| 1 | +# |
| 2 | +# Copyright 2021 Red Hat, Inc. |
| 3 | +# |
| 4 | +# This program is free software: you can redistribute it and/or modify |
| 5 | +# it under the terms of the GNU Affero General Public License as |
| 6 | +# published by the Free Software Foundation, either version 3 of the |
| 7 | +# License, or (at your option) any later version. |
| 8 | +# |
| 9 | +# This program is distributed in the hope that it will be useful, |
| 10 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +# GNU Affero General Public License for more details. |
| 13 | +# |
| 14 | +# You should have received a copy of the GNU Affero General Public License |
| 15 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | +# |
| 17 | +"""GCP Tag Query Handling.""" |
| 18 | +import logging |
| 19 | +from copy import deepcopy |
| 20 | + |
| 21 | +from api.models import Provider |
| 22 | +from api.report.gcp.provider_map import GCPProviderMap |
| 23 | +from api.tags.queries import TagQueryHandler |
| 24 | +from reporting.models import GCPTagsSummary |
| 25 | +from reporting.provider.gcp.models import GCPTagsValues |
| 26 | + |
| 27 | + |
| 28 | +LOG = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +class GCPTagQueryHandler(TagQueryHandler): |
| 32 | + """Handles tag queries and responses for GCP.""" |
| 33 | + |
| 34 | + provider = Provider.PROVIDER_GCP |
| 35 | + data_sources = [{"db_table": GCPTagsSummary, "db_column_period": "cost_entry_bill__billing_period"}] |
| 36 | + TAGS_VALUES_SOURCE = [{"db_table": GCPTagsValues, "fields": ["key"]}] |
| 37 | + SUPPORTED_FILTERS = TagQueryHandler.SUPPORTED_FILTERS + ["account", "project"] |
| 38 | + |
| 39 | + def __init__(self, parameters): |
| 40 | + """Establish GCP report query handler. |
| 41 | +
|
| 42 | + Args: |
| 43 | + parameters (QueryParameters): parameter object for query |
| 44 | +
|
| 45 | + """ |
| 46 | + self._parameters = parameters |
| 47 | + if not hasattr(self, "_mapper"): |
| 48 | + self._mapper = GCPProviderMap(provider=self.provider, report_type=parameters.report_type) |
| 49 | + # super() needs to be called after _mapper is set |
| 50 | + super().__init__(parameters) |
| 51 | + |
| 52 | + @property |
| 53 | + def filter_map(self): |
| 54 | + """Establish which filter map to use based on tag API.""" |
| 55 | + filter_map = deepcopy(TagQueryHandler.FILTER_MAP) |
| 56 | + if self._parameters.get_filter("value"): |
| 57 | + filter_map.update( |
| 58 | + { |
| 59 | + "account": [ |
| 60 | + {"field": "account_ids", "operation": "icontains", "composition_key": "account_filter"} |
| 61 | + ], |
| 62 | + "project": [ |
| 63 | + {"field": "project_ids", "operation": "icontains", "composition_key": "project_filter"}, |
| 64 | + {"field": "project_names", "operation": "icontains", "composition_key": "project_filter"}, |
| 65 | + ], |
| 66 | + } |
| 67 | + ) |
| 68 | + else: |
| 69 | + filter_map.update( |
| 70 | + { |
| 71 | + "account": [ |
| 72 | + {"field": "account_id", "operation": "icontains", "composition_key": "account_filter"} |
| 73 | + ], |
| 74 | + "project": [ |
| 75 | + {"field": "project_id", "operation": "icontains", "composition_key": "project_filter"}, |
| 76 | + {"field": "project_name", "operation": "icontains", "composition_key": "project_filter"}, |
| 77 | + ], |
| 78 | + } |
| 79 | + ) |
| 80 | + return filter_map |
0 commit comments