Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hv placement switch #9

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions openstackquery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
ProjectQuery,
ImageQuery,
HypervisorQuery,
PlacementQuery,
)

# Create logger
Expand Down
8 changes: 8 additions & 0 deletions openstackquery/api/query_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from openstackquery.mappings.hypervisor_mapping import HypervisorMapping
from openstackquery.mappings.image_mapping import ImageMapping
from openstackquery.mappings.mapping_interface import MappingInterface
from openstackquery.mappings.placement_mapping import PlacementMapping
from openstackquery.mappings.project_mapping import ProjectMapping
from openstackquery.mappings.server_mapping import ServerMapping
from openstackquery.mappings.user_mapping import UserMapping
Expand Down Expand Up @@ -70,3 +71,10 @@
Simple helper function to setup a query using a factory
"""
return get_common(HypervisorMapping)


def PlacementQuery() -> "QueryAPI":
"""
Simple helper function to setup a query using a factory
"""
return get_common(PlacementMapping)

Check warning on line 80 in openstackquery/api/query_objects.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/api/query_objects.py#L80

Added line #L80 was not covered by tests
59 changes: 0 additions & 59 deletions openstackquery/enums/props/hypervisor_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@ class HypervisorProperties(PropEnum):
An enum class for all hypervisor properties
"""

# HYPERVISOR_CURRENT_WORKLOAD = auto()
HYPERVISOR_DISK_FREE = auto()
HYPERVISOR_DISK_SIZE = auto()
HYPERVISOR_DISK_USED = auto()
HYPERVISOR_ID = auto()
HYPERVISOR_IP = auto()
HYPERVISOR_MEMORY_FREE = auto()
HYPERVISOR_MEMORY_SIZE = auto()
HYPERVISOR_MEMORY_USED = auto()
HYPERVISOR_NAME = auto()
# HYPERVISOR_SERVER_COUNT = auto() # Deprecated, use server query
HYPERVISOR_STATE = auto()
HYPERVISOR_STATUS = auto()
HYPERVISOR_VCPUS = auto()
HYPERVISOR_VCPUS_USED = auto()
HYPERVISOR_DISABLED_REASON = auto()
HYPERVISOR_UPTIME_DAYS = auto()

Expand All @@ -36,33 +26,11 @@ def _get_aliases() -> Dict:
A method that returns all valid string alias mappings
"""
return {
# HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD: [
# "current_workload",
# "workload",
# ],
HypervisorProperties.HYPERVISOR_DISK_FREE: [
"local_disk_free",
"free_disk_gb",
],
HypervisorProperties.HYPERVISOR_DISK_SIZE: ["local_disk_size", "local_gb"],
HypervisorProperties.HYPERVISOR_DISK_USED: [
"local_disk_used",
"local_gb_used",
],
HypervisorProperties.HYPERVISOR_ID: ["id", "uuid", "host_id"],
HypervisorProperties.HYPERVISOR_IP: ["ip", "host_ip"],
HypervisorProperties.HYPERVISOR_MEMORY_FREE: ["memory_free", "free_ram_mb"],
HypervisorProperties.HYPERVISOR_MEMORY_SIZE: ["memory_size", "memory_mb"],
HypervisorProperties.HYPERVISOR_MEMORY_USED: [
"memory_used",
"memory_mb_used",
],
HypervisorProperties.HYPERVISOR_NAME: ["name", "host_name"],
# HypervisorProperties.HYPERVISOR_SERVER_COUNT: ["running_vms"],
HypervisorProperties.HYPERVISOR_STATE: ["state"],
HypervisorProperties.HYPERVISOR_STATUS: ["status"],
HypervisorProperties.HYPERVISOR_VCPUS: ["vcpus"],
HypervisorProperties.HYPERVISOR_VCPUS_USED: ["vcpus_used"],
HypervisorProperties.HYPERVISOR_DISABLED_REASON: ["disabled_reason"],
HypervisorProperties.HYPERVISOR_UPTIME_DAYS: ["uptime"],
}
Expand All @@ -76,39 +44,12 @@ def get_prop_mapping(prop) -> Optional[PropFunc]:
:param prop: A HypervisorProperty Enum for which a function may exist for
"""
mapping = {
# HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD: lambda a: a[
# "current_workload"
# ],
HypervisorProperties.HYPERVISOR_DISK_FREE: lambda a: a.resources["DISK_GB"][
"free"
],
HypervisorProperties.HYPERVISOR_DISK_SIZE: lambda a: a.resources["DISK_GB"][
"total"
],
HypervisorProperties.HYPERVISOR_DISK_USED: lambda a: a.resources["DISK_GB"][
"usage"
],
HypervisorProperties.HYPERVISOR_ID: lambda a: a["id"],
HypervisorProperties.HYPERVISOR_IP: lambda a: a["host_ip"],
HypervisorProperties.HYPERVISOR_MEMORY_FREE: lambda a: a.resources[
"MEMORY_MB"
]["free"],
HypervisorProperties.HYPERVISOR_MEMORY_SIZE: lambda a: a.resources[
"MEMORY_MB"
]["total"],
HypervisorProperties.HYPERVISOR_MEMORY_USED: lambda a: a.resources[
"MEMORY_MB"
]["usage"],
HypervisorProperties.HYPERVISOR_NAME: lambda a: a["name"],
# HypervisorProperties.HYPERVISOR_SERVER_COUNT: lambda a: a["runnning_vms"],
HypervisorProperties.HYPERVISOR_STATE: lambda a: a["state"],
HypervisorProperties.HYPERVISOR_STATUS: lambda a: a["status"],
HypervisorProperties.HYPERVISOR_VCPUS: lambda a: a.resources["VCPU"][
"total"
],
HypervisorProperties.HYPERVISOR_VCPUS_USED: lambda a: a.resources["VCPU"][
"usage"
],
HypervisorProperties.HYPERVISOR_DISABLED_REASON: lambda a: a["service"][
"disabled_reason"
],
Expand Down
79 changes: 79 additions & 0 deletions openstackquery/enums/props/placement_properties.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from enum import auto
from typing import Dict, Optional

from openstackquery.enums.props.prop_enum import PropEnum, PropFunc
from openstackquery.exceptions.query_property_mapping_error import (
QueryPropertyMappingError,
)


class PlacementProperties(PropEnum):
"""
An enum class for currently used placement properties
"""

RESOURCE_PROVIDER_ID = auto()
RESOURCE_PROVIDER_NAME = auto()
VCPUS_USED = auto()
VCPUS_AVAIL = auto()
MEMORY_MB_USED = auto()
MEMORY_MB_AVAIL = auto()
DISK_GB_USED = auto()
DISK_GB_AVAIL = auto()

@staticmethod
def _get_aliases() -> Dict:
"""
A method that returns all valid string alias mappings
"""
return {

Check warning on line 29 in openstackquery/enums/props/placement_properties.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/enums/props/placement_properties.py#L29

Added line #L29 was not covered by tests
PlacementProperties.RESOURCE_PROVIDER_ID: [
"resource_provider_id",
"resource_provider_uuid",
"id",
],
PlacementProperties.RESOURCE_PROVIDER_NAME: [
"resource_name",
"name",
"provider_name",
],
PlacementProperties.VCPUS_USED: ["vcpus_used"],
PlacementProperties.VCPUS_AVAIL: ["vcpus_avail"],
PlacementProperties.MEMORY_MB_USED: ["memory_mb_used"],
PlacementProperties.MEMORY_MB_AVAIL: ["memory_mb_avail"],
PlacementProperties.DISK_GB_USED: ["disk_gb_used"],
PlacementProperties.DISK_GB_AVAIL: ["disk_gb_avail"],
}

@staticmethod
def get_prop_mapping(prop) -> Optional[PropFunc]:
"""
Method that returns the property function if function mapping exists for a given Hypervisor Enum
how to get specified property from a ResourceProviderUsage object
:param prop: A HypervisorProperty Enum for which a function may exist for
"""
mapping = {

Check warning on line 55 in openstackquery/enums/props/placement_properties.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/enums/props/placement_properties.py#L55

Added line #L55 was not covered by tests
PlacementProperties.RESOURCE_PROVIDER_ID: lambda a: a["id"],
PlacementProperties.RESOURCE_PROVIDER_NAME: lambda a: a["name"],
PlacementProperties.VCPUS_AVAIL: lambda a: a["vcpu_avail"],
PlacementProperties.MEMORY_MB_AVAIL: lambda a: a["memory_mb_avail"],
PlacementProperties.DISK_GB_AVAIL: lambda a: a["disk_gb_avail"],
PlacementProperties.VCPUS_USED: lambda a: a["vcpu_used"],
PlacementProperties.MEMORY_MB_USED: lambda a: a["memory_mb_used"],
PlacementProperties.DISK_GB_USED: lambda a: a["disk_gb_used"],
}
try:
return mapping[prop]
except KeyError as exp:
raise QueryPropertyMappingError(

Check warning on line 68 in openstackquery/enums/props/placement_properties.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/enums/props/placement_properties.py#L65-L68

Added lines #L65 - L68 were not covered by tests
f"Error: failed to get property mapping, property {prop.name} is not supported in PlacementProperties"
) from exp

@staticmethod
def get_marker_prop_func():
"""
A getter method to return marker property function for pagination
"""
return PlacementProperties.get_prop_mapping(

Check warning on line 77 in openstackquery/enums/props/placement_properties.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/enums/props/placement_properties.py#L77

Added line #L77 was not covered by tests
PlacementProperties.RESOURCE_PROVIDER_ID
)
2 changes: 2 additions & 0 deletions openstackquery/handlers/server_side_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def get_filters(
try:
filters = filter_func(**params)
except (KeyError, TypeError) as err:
# Dev note: your lambda must take "value" as the lambda
# argument if you arrive here adding new mappings
raise QueryPresetMappingError(
"Preset Argument Error: failed to build server-side openstacksdk filters for preset:prop: "
f"'{preset.name}':'{prop.name}' "
Expand Down
26 changes: 1 addition & 25 deletions openstackquery/mappings/hypervisor_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
from openstackquery.enums.query_presets import (
QueryPresetsGeneric,
QueryPresetsString,
QueryPresetsInteger,
)
from openstackquery.handlers.client_side_handler_generic import (
ClientSideHandlerGeneric,
)
from openstackquery.handlers.client_side_handler_integer import (
ClientSideHandlerInteger,
)
from openstackquery.handlers.client_side_handler_string import ClientSideHandlerString
from openstackquery.handlers.server_side_handler import ServerSideHandler
from openstackquery.mappings.mapping_interface import MappingInterface
Expand Down Expand Up @@ -72,19 +68,6 @@ def get_client_side_handlers() -> QueryClientSideHandlers:
corresponding to valid preset-property pairs. These filter functions can be used to filter results after
listing all hypervisors.
"""
integer_props = [
HypervisorProperties.HYPERVISOR_DISK_USED,
HypervisorProperties.HYPERVISOR_DISK_FREE,
HypervisorProperties.HYPERVISOR_DISK_SIZE,
HypervisorProperties.HYPERVISOR_MEMORY_SIZE,
HypervisorProperties.HYPERVISOR_MEMORY_USED,
HypervisorProperties.HYPERVISOR_MEMORY_FREE,
HypervisorProperties.HYPERVISOR_VCPUS,
HypervisorProperties.HYPERVISOR_VCPUS_USED,
# HypervisorProperties.HYPERVISOR_SERVER_COUNT, # Deprecated, use server query
# HypervisorProperties.HYPERVISOR_CURRENT_WORKLOAD,
]

return QueryClientSideHandlers(
# set generic query preset mappings
generic_handler=ClientSideHandlerGeneric(
Expand All @@ -108,12 +91,5 @@ def get_client_side_handlers() -> QueryClientSideHandlers:
# set datetime query preset mappings
datetime_handler=None,
# set integer query preset mappings
integer_handler=ClientSideHandlerInteger(
{
QueryPresetsInteger.LESS_THAN: integer_props,
QueryPresetsInteger.GREATER_THAN: integer_props,
QueryPresetsInteger.LESS_THAN_OR_EQUAL_TO: integer_props,
QueryPresetsInteger.GREATER_THAN_OR_EQUAL_TO: integer_props,
}
),
integer_handler=None,
)
121 changes: 121 additions & 0 deletions openstackquery/mappings/placement_mapping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from typing import Type

from openstackquery.enums.props.hypervisor_properties import HypervisorProperties
from openstackquery.enums.props.placement_properties import PlacementProperties
from openstackquery.enums.props.prop_enum import PropEnum
from openstackquery.enums.query_presets import (
QueryPresetsGeneric,
QueryPresetsString,
QueryPresetsInteger,
)
from openstackquery.handlers.client_side_handler_generic import (
ClientSideHandlerGeneric,
)
from openstackquery.handlers.client_side_handler_integer import (
ClientSideHandlerInteger,
)
from openstackquery.handlers.client_side_handler_string import ClientSideHandlerString
from openstackquery.handlers.server_side_handler import ServerSideHandler
from openstackquery.mappings.mapping_interface import MappingInterface
from openstackquery.runners.placement_runner import PlacementRunner

from openstackquery.runners.runner_wrapper import RunnerWrapper
from openstackquery.structs.query_client_side_handlers import QueryClientSideHandlers


class PlacementMapping(MappingInterface):
"""
Mapping class for querying Openstack placement and resource objects
Define property mappings, kwarg mappings and filter function mappings,
and runner mapping related to placement and resources here
"""

@staticmethod
def get_chain_mappings():
"""
Should return a dictionary containing property pairs mapped to query mappings.
This is used to define how to chain results from this query to other possible queries
"""
return {

Check warning on line 39 in openstackquery/mappings/placement_mapping.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/mappings/placement_mapping.py#L39

Added line #L39 was not covered by tests
PlacementProperties.RESOURCE_PROVIDER_NAME: HypervisorProperties.HYPERVISOR_NAME
}

@staticmethod
def get_runner_mapping() -> Type[RunnerWrapper]:
"""
Returns a mapping to associated Runner class for the Query (placement and resourceRunner)
"""
return PlacementRunner

Check warning on line 48 in openstackquery/mappings/placement_mapping.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/mappings/placement_mapping.py#L48

Added line #L48 was not covered by tests

@staticmethod
def get_prop_mapping() -> Type[PropEnum]:
"""
Returns a mapping of valid presets for server side attributes (placement and resourceProperties)
"""
return PlacementProperties

Check warning on line 55 in openstackquery/mappings/placement_mapping.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/mappings/placement_mapping.py#L55

Added line #L55 was not covered by tests

@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.placement.resource_providers() to filter results
Valid filters documented here:
https://docs.openstack.org/openstacksdk/latest/user/proxies/placement.html
"""
return ServerSideHandler(

Check warning on line 65 in openstackquery/mappings/placement_mapping.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/mappings/placement_mapping.py#L65

Added line #L65 was not covered by tests
{
QueryPresetsGeneric.EQUAL_TO: {
PlacementProperties.RESOURCE_PROVIDER_ID: lambda value: {
"id": value
},
PlacementProperties.RESOURCE_PROVIDER_NAME: lambda value: {
"name": 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 placement and resources.
"""
integer_prop_list = [

Check warning on line 85 in openstackquery/mappings/placement_mapping.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/mappings/placement_mapping.py#L85

Added line #L85 was not covered by tests
PlacementProperties.VCPUS_AVAIL,
PlacementProperties.MEMORY_MB_AVAIL,
PlacementProperties.DISK_GB_AVAIL,
PlacementProperties.VCPUS_USED,
PlacementProperties.MEMORY_MB_USED,
PlacementProperties.DISK_GB_USED,
]

return QueryClientSideHandlers(

Check warning on line 94 in openstackquery/mappings/placement_mapping.py

View check run for this annotation

Codecov / codecov/patch

openstackquery/mappings/placement_mapping.py#L94

Added line #L94 was not covered by tests
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: [
PlacementProperties.RESOURCE_PROVIDER_ID,
PlacementProperties.RESOURCE_PROVIDER_NAME,
]
}
),
datetime_handler=None,
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,
}
),
)
Loading
Loading