-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from anish-mudaraddi/openstack-query/flavor-q…
…uery ENH: add flavor query
- Loading branch information
Showing
18 changed files
with
706 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
from enum import auto | ||
from enums.query.props.prop_enum import PropEnum | ||
from exceptions.parse_query_error import ParseQueryError | ||
from exceptions.query_property_mapping_error import QueryPropertyMappingError | ||
|
||
|
||
class FlavorProperties(PropEnum): | ||
""" | ||
An enum class for all flavor properties | ||
""" | ||
|
||
FLAVOR_DESCRIPTION = auto() | ||
FLAVOR_DISK = auto() | ||
FLAVOR_EPHEMERAL = auto() | ||
FLAVOR_ID = auto() | ||
FLAVOR_IS_DISABLED = auto() | ||
FLAVOR_IS_PUBLIC = auto() | ||
FLAVOR_NAME = auto() | ||
FLAVOR_RAM = auto() | ||
FLAVOR_SWAP = auto() | ||
FLAVOR_VCPU = auto() | ||
|
||
@staticmethod | ||
def from_string(val: str): | ||
""" | ||
Converts a given string in a case-insensitive way to the enum values | ||
""" | ||
try: | ||
return FlavorProperties[val.upper()] | ||
except KeyError as err: | ||
raise ParseQueryError( | ||
f"Could not find Flavor Property {val}. " | ||
f"Available properties are {','.join([prop.name for prop in FlavorProperties])}" | ||
) from err | ||
|
||
@staticmethod | ||
def get_prop_mapping(prop): | ||
""" | ||
Method that returns the property function if function mapping exists for a given FlavorProperty Enum | ||
how to get specified property from an openstacksdk Flavor object is documented here: | ||
https://docs.openstack.org/openstacksdk/latest/user/resources/compute/v2/flavor.html#openstack.compute.v2.flavor.Flavor | ||
:param prop: A FlavorProperty Enum for which a function may exist for | ||
""" | ||
mapping = { | ||
FlavorProperties.FLAVOR_DESCRIPTION: lambda a: a["description"], | ||
FlavorProperties.FLAVOR_DISK: lambda a: a["disk"], | ||
FlavorProperties.FLAVOR_EPHEMERAL: lambda a: a["ephemeral"], | ||
FlavorProperties.FLAVOR_ID: lambda a: a["id"], | ||
FlavorProperties.FLAVOR_IS_DISABLED: lambda a: a["is_disabled"], | ||
FlavorProperties.FLAVOR_IS_PUBLIC: lambda a: a["is_public"], | ||
FlavorProperties.FLAVOR_NAME: lambda a: a["name"], | ||
FlavorProperties.FLAVOR_RAM: lambda a: a["ram"], | ||
FlavorProperties.FLAVOR_SWAP: lambda a: a["swap"], | ||
FlavorProperties.FLAVOR_VCPU: lambda a: a["vcpus"], | ||
} | ||
try: | ||
return mapping[prop] | ||
except KeyError as exp: | ||
raise QueryPropertyMappingError( | ||
f"Error: failed to get property mapping, property {prop.name} is not supported in FlavorProperties" | ||
) from exp | ||
|
||
@staticmethod | ||
def get_marker_prop_func(): | ||
""" | ||
A getter method to return marker property function for pagination | ||
""" | ||
return FlavorProperties.get_prop_mapping(FlavorProperties.FLAVOR_ID) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
from typing import Type | ||
from structs.query.query_client_side_handlers import QueryClientSideHandlers | ||
|
||
from enums.query.props.flavor_properties import FlavorProperties | ||
from enums.query.query_presets import ( | ||
QueryPresetsGeneric, | ||
QueryPresetsInteger, | ||
QueryPresetsString, | ||
) | ||
|
||
from openstack_query.handlers.server_side_handler import ServerSideHandler | ||
from openstack_query.handlers.client_side_handler_generic import ( | ||
ClientSideHandlerGeneric, | ||
) | ||
from openstack_query.handlers.client_side_handler_integer import ( | ||
ClientSideHandlerInteger, | ||
) | ||
from openstack_query.handlers.client_side_handler_string import ClientSideHandlerString | ||
|
||
from openstack_query.mappings.mapping_interface import MappingInterface | ||
from openstack_query.runners.flavor_runner import FlavorRunner | ||
|
||
|
||
class FlavorMapping(MappingInterface): | ||
""" | ||
Mapping class for querying Openstack Flavor objects | ||
Define property mappings, kwarg mappings and filter function mappings, and runner mapping related to flavors here | ||
""" | ||
|
||
@staticmethod | ||
def get_runner_mapping() -> Type[FlavorRunner]: | ||
""" | ||
Returns a mapping to associated Runner class for the Query (FlavorRunner) | ||
""" | ||
return FlavorRunner | ||
|
||
@staticmethod | ||
def get_prop_mapping() -> Type[FlavorProperties]: | ||
""" | ||
Returns a mapping of valid presets for server side attributes (FlavorProperties) | ||
""" | ||
return FlavorProperties | ||
|
||
@staticmethod | ||
def get_server_side_handler() -> ServerSideHandler: | ||
""" | ||
method to configure a server handler which can be used to get 'filter' keyword arguments that | ||
can be passed to openstack function conn.compute.flavors() to filter results for a valid preset-property pair | ||
valid filters documented here: | ||
https://docs.openstack.org/openstacksdk/latest/user/proxies/compute.html | ||
https://docs.openstack.org/api-ref/compute/?expanded=list-servers-detail#show-flavor-details | ||
""" | ||
return ServerSideHandler( | ||
{ | ||
QueryPresetsGeneric.EQUAL_TO: { | ||
FlavorProperties.FLAVOR_IS_PUBLIC: lambda value: { | ||
"is_public": value | ||
} | ||
}, | ||
QueryPresetsGeneric.NOT_EQUAL_TO: { | ||
FlavorProperties.FLAVOR_IS_PUBLIC: lambda value: { | ||
"is_public": not value | ||
} | ||
}, | ||
QueryPresetsInteger.LESS_THAN_OR_EQUAL_TO: { | ||
FlavorProperties.FLAVOR_DISK: lambda value: {"minDisk": int(value)}, | ||
FlavorProperties.FLAVOR_RAM: lambda value: {"minRam": int(value)}, | ||
}, | ||
} | ||
) | ||
|
||
@staticmethod | ||
def get_client_side_handlers() -> QueryClientSideHandlers: | ||
""" | ||
method to configure a set of client-side handlers which can be used to get local filter functions | ||
corresponding to valid preset-property pairs. These filter functions can be used to filter results after | ||
listing all servers. | ||
""" | ||
|
||
integer_prop_list = [ | ||
FlavorProperties.FLAVOR_RAM, | ||
FlavorProperties.FLAVOR_DISK, | ||
FlavorProperties.FLAVOR_EPHEMERAL, | ||
FlavorProperties.FLAVOR_SWAP, | ||
FlavorProperties.FLAVOR_VCPU, | ||
] | ||
|
||
return QueryClientSideHandlers( | ||
# set generic query preset mappings | ||
generic_handler=ClientSideHandlerGeneric( | ||
{ | ||
QueryPresetsGeneric.EQUAL_TO: ["*"], | ||
QueryPresetsGeneric.NOT_EQUAL_TO: ["*"], | ||
QueryPresetsGeneric.ANY_IN: ["*"], | ||
QueryPresetsGeneric.NOT_ANY_IN: ["*"], | ||
} | ||
), | ||
# set string query preset mappings | ||
string_handler=ClientSideHandlerString( | ||
{QueryPresetsString.MATCHES_REGEX: [FlavorProperties.FLAVOR_NAME]} | ||
), | ||
# set datetime query preset mappings | ||
datetime_handler=None, | ||
# set integer query preset mappings | ||
integer_handler=ClientSideHandlerInteger( | ||
{ | ||
QueryPresetsInteger.LESS_THAN: integer_prop_list, | ||
QueryPresetsInteger.LESS_THAN_OR_EQUAL_TO: integer_prop_list, | ||
QueryPresetsInteger.GREATER_THAN: integer_prop_list, | ||
QueryPresetsInteger.GREATER_THAN_OR_EQUAL_TO: integer_prop_list, | ||
} | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Optional, List | ||
import logging | ||
|
||
from openstack.compute.v2.flavor import Flavor | ||
from exceptions.parse_query_error import ParseQueryError | ||
|
||
from openstack_api.openstack_connection import OpenstackConnection | ||
from openstack_query.runners.runner_wrapper import RunnerWrapper | ||
|
||
from custom_types.openstack_query.aliases import ServerSideFilters | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# pylint:disable=too-few-public-methods | ||
|
||
|
||
class FlavorRunner(RunnerWrapper): | ||
""" | ||
Runner class for openstack flavor resource | ||
FlavorRunner encapsulates running any openstacksdk Flavor commands | ||
""" | ||
|
||
RESOURCE_TYPE = Flavor | ||
|
||
def _parse_meta_params(self, _: OpenstackConnection, **__): | ||
logger.error( | ||
"FlavorQuery doesn't take any meta-params, if you think it should," | ||
"please raise an issue with the repo maintainers" | ||
) | ||
raise ParseQueryError("FlavorQuery has no meta-params available") | ||
|
||
def _run_query( | ||
self, | ||
conn: OpenstackConnection, | ||
filter_kwargs: Optional[ServerSideFilters] = None, | ||
**_, | ||
) -> List[Flavor]: | ||
""" | ||
This method runs the query by running openstacksdk commands | ||
For FlavorQuery, this command finds all flavors that match a given set of filter_kwargs | ||
:param conn: An OpenstackConnection object - used to connect to openstacksdk | ||
:param filter_kwargs: An Optional list of filter kwargs to pass to conn.compute.flavors() | ||
to limit the flavors being returned. | ||
- see https://docs.openstack.org/api-ref/compute/#list-flavors-with-details | ||
""" | ||
if not filter_kwargs: | ||
# return all info | ||
filter_kwargs = {"details": True} | ||
logger.debug( | ||
"running openstacksdk command conn.compute.flavors(%s)", | ||
",".join(f"{key}={value}" for key, value in filter_kwargs.items()), | ||
) | ||
return self._run_paginated_query(conn.compute.flavors, filter_kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.