|
| 1 | +from typing import Type |
| 2 | + |
| 3 | +from openstackquery.aliases import QueryChainMappings |
| 4 | +from openstackquery.enums.props.aggregate_properties import AggregateProperties |
| 5 | +from openstackquery.enums.query_presets import QueryPresets |
| 6 | +from openstackquery.handlers.client_side_handler import ClientSideHandler |
| 7 | +from openstackquery.handlers.server_side_handler import ServerSideHandler |
| 8 | +from openstackquery.mappings.mapping_interface import MappingInterface |
| 9 | +from openstackquery.runners.aggregate_runner import AggregateRunner |
| 10 | + |
| 11 | + |
| 12 | +class AggregateMapping(MappingInterface): |
| 13 | + """ |
| 14 | + Mapping class for querying Openstack Aggregate objects |
| 15 | + Define property mappings, kwarg mappings and filter function mappings, |
| 16 | + and runner mapping related to aggregates here |
| 17 | + """ |
| 18 | + |
| 19 | + @staticmethod |
| 20 | + def get_chain_mappings() -> QueryChainMappings: |
| 21 | + """ |
| 22 | + Should return a dictionary containing property pairs mapped to query mappings. |
| 23 | + This is used to define how to chain results from this query to other possible queries |
| 24 | + """ |
| 25 | + # TODO: find a way to map list of hostnames: |
| 26 | + # AggregateProperties.HOST_IPS to HypervisorProperties.HYPERVISOR_NAME |
| 27 | + return None |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def get_runner_mapping() -> Type[AggregateRunner]: |
| 31 | + """ |
| 32 | + Returns a mapping to associated Runner class for the Query (AggregateRunner) |
| 33 | + """ |
| 34 | + return AggregateRunner |
| 35 | + |
| 36 | + @staticmethod |
| 37 | + def get_prop_mapping() -> Type[AggregateProperties]: |
| 38 | + """ |
| 39 | + Returns a mapping of valid presets for server side attributes (HypervisorProperties) |
| 40 | + """ |
| 41 | + return AggregateProperties |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def get_server_side_handler() -> ServerSideHandler: |
| 45 | + """ |
| 46 | + method to configure a server handler which can be used to get 'filter' keyword arguments that |
| 47 | + can be passed to openstack function conn.compute.aggregates() to filter results for a valid preset-property |
| 48 | + pair |
| 49 | +
|
| 50 | + valid filters documented here: |
| 51 | + https://docs.openstack.org/openstacksdk/latest/user/proxies/compute.html |
| 52 | + https://docs.openstack.org/api-ref/compute/?expanded=list-hypervisors-detail#list-aggregates |
| 53 | + """ |
| 54 | + # No server-side filters for AggregateQuery |
| 55 | + return ServerSideHandler({}) |
| 56 | + |
| 57 | + @staticmethod |
| 58 | + def get_client_side_handlers() -> ClientSideHandler: |
| 59 | + """ |
| 60 | + method to configure a set of client-side handlers which can be used to get local filter functions |
| 61 | + corresponding to valid preset-property pairs. These filter functions can be used to filter results after |
| 62 | + listing all aggregates. |
| 63 | + """ |
| 64 | + date_prop_list = [ |
| 65 | + AggregateProperties.AGGREGATE_DELETED_AT, |
| 66 | + AggregateProperties.AGGREGATE_UPDATED_AT, |
| 67 | + AggregateProperties.AGGREGATE_CREATED_AT, |
| 68 | + ] |
| 69 | + |
| 70 | + string_prop_list = [ |
| 71 | + AggregateProperties.AGGREGATE_HOSTTYPE, |
| 72 | + AggregateProperties.AGGREGATE_HOST_IPS, |
| 73 | + AggregateProperties.AGGREGATE_METADATA, |
| 74 | + AggregateProperties.AGGREGATE_LOCAL_STORAGE_TYPE, |
| 75 | + ] |
| 76 | + |
| 77 | + return ClientSideHandler( |
| 78 | + { |
| 79 | + QueryPresets.EQUAL_TO: ["*"], |
| 80 | + QueryPresets.NOT_EQUAL_TO: ["*"], |
| 81 | + QueryPresets.ANY_IN: ["*"], |
| 82 | + QueryPresets.NOT_ANY_IN: ["*"], |
| 83 | + QueryPresets.MATCHES_REGEX: string_prop_list, |
| 84 | + QueryPresets.NOT_MATCHES_REGEX: string_prop_list, |
| 85 | + QueryPresets.YOUNGER_THAN: date_prop_list, |
| 86 | + QueryPresets.YOUNGER_THAN_OR_EQUAL_TO: date_prop_list, |
| 87 | + QueryPresets.OLDER_THAN: date_prop_list, |
| 88 | + QueryPresets.OLDER_THAN_OR_EQUAL_TO: date_prop_list, |
| 89 | + QueryPresets.LESS_THAN: [AggregateProperties.AGGREGATE_GPUNUM], |
| 90 | + QueryPresets.LESS_THAN_OR_EQUAL_TO: [ |
| 91 | + AggregateProperties.AGGREGATE_GPUNUM |
| 92 | + ], |
| 93 | + QueryPresets.GREATER_THAN: [AggregateProperties.AGGREGATE_GPUNUM], |
| 94 | + QueryPresets.GREATER_THAN_OR_EQUAL_TO: [ |
| 95 | + AggregateProperties.AGGREGATE_GPUNUM |
| 96 | + ], |
| 97 | + } |
| 98 | + ) |
0 commit comments