Skip to content

Commit

Permalink
Drop resources from HV props which return none
Browse files Browse the repository at this point in the history
These resources all return None for HVs, as Yoga+ expects us to use a
resource object instead.

Remove these since they will not work again, and any downstream usage
will fail loudly when they try to import the dead enum.
This will require a major version bump which will come in the next
commit adding the placement replacement.
  • Loading branch information
DavidFair committed Jan 3, 2025
1 parent ba9d83e commit 4b4a1df
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 343 deletions.
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
22 changes: 1 addition & 21 deletions openstackquery/mappings/hypervisor_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,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 +95,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,
)
47 changes: 1 addition & 46 deletions openstackquery/runners/hypervisor_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,49 +29,6 @@ def parse_meta_params(self, conn: OpenstackConnection, **kwargs):
logger.debug("HypervisorQuery has no meta-params available")
return super().parse_meta_params(conn, **kwargs)

def _populate_placement_info(
self, conn: OpenstackConnection, hypervisors: List
) -> List:
"""
Adds resource usage stats to the hypervisors
:param conn: Openstack connecion
:param hypervisors: List of hypervisors
:return: List of hypervisors with additional resource usage stats
"""
client = PlacementClient(
api_version="1.6",
session=conn.session,
ks_filter={"service_type": "placement"},
)

for hypervisor in hypervisors:
hypervisor.resources = self._get_usage_info(conn, client, hypervisor)

return hypervisors

def _get_usage_info(
self, conn: OpenstackConnection, client: PlacementClient, hypervisor: Hypervisor
) -> Dict:
"""
Get usage stats from the openstack placement api
:param conn: Openstack connection
:param client: osc_placement session client
:param hypervisor: Openstack hypervisor
:return: resource usage for the hypervisor
"""
resources = conn.placement.resource_provider_inventories(hypervisor.id)
usages = client.request("get", f"/resource_providers/{hypervisor.id}/usages")
usages = json.loads(usages.text).get("usages")
usage_info = {}
for i in resources:
usage_info[i.resource_class] = {
"total": i.total,
"usage": usages.get(i.resource_class),
"free": i.total - usages.get(i.resource_class),
}

return usage_info

# pylint: disable=unused-argument
def run_query(
self,
Expand All @@ -95,8 +52,6 @@ def run_query(
"running openstacksdk command conn.compute.hypervisors(%s)",
",".join(f"{key}={value}" for key, value in filter_kwargs.items()),
)
hypervisors = RunnerUtils.run_paginated_query(
return RunnerUtils.run_paginated_query(
conn.compute.hypervisors, self._page_marker_prop_func, filter_kwargs
)

return self._populate_placement_info(conn, hypervisors)
157 changes: 0 additions & 157 deletions tests/enums/props/test_hypervisor_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,66 +37,6 @@ def test_get_marker_prop_func(mock_get_prop_mapping):
assert val == mock_get_prop_mapping.return_value


@pytest.mark.parametrize(
"val",
[
"hypervisor_disk_free",
"Hypervisor_Disk_Free",
"HyPeRvIsOr_DiSk_FrEe",
"local_disk_free",
"free_disk_gb",
],
)
def test_hypervisor_disk_free_serialization(val):
"""
Tests that variants of HYPERVISOR_DISK_FREE can be serialized
"""
assert (
HypervisorProperties.from_string(val)
is HypervisorProperties.HYPERVISOR_DISK_FREE
)


@pytest.mark.parametrize(
"val",
[
"hypervisor_disk_size",
"Hypervisor_Disk_Size",
"HyPeRvIsOr_DiSk_SiZe",
"local_disk_size",
"local_gb",
],
)
def test_hypervisor_disk_size_serialization(val):
"""
Tests that variants of HYPERVISOR_DISK_SIZE can be serialized
"""
assert (
HypervisorProperties.from_string(val)
is HypervisorProperties.HYPERVISOR_DISK_SIZE
)


@pytest.mark.parametrize(
"val",
[
"hypervisor_disk_used",
"Hypervisor_Disk_Used",
"HyPeRvIsOr_DiSk_UsEd",
"local_disk_used",
"local_disk_used",
],
)
def test_hypervisor_disk_used_serialization(val):
"""
Tests that variants of HYPERVISOR_DISK_USED can be serialized
"""
assert (
HypervisorProperties.from_string(val)
is HypervisorProperties.HYPERVISOR_DISK_USED
)


@pytest.mark.parametrize(
"val",
["hypervisor_id", "Hypervisor_ID", "HyPeRvIsOr_Id", "id", "uuid", "host_id"],
Expand All @@ -119,66 +59,6 @@ def test_hypervisor_ip_serialization(val):
assert HypervisorProperties.from_string(val) is HypervisorProperties.HYPERVISOR_IP


@pytest.mark.parametrize(
"val",
[
"hypervisor_memory_free",
"Hypervisor_Memory_Free",
"HyPeRvIsOr_MeMoRy_FrEe",
"memory_free",
"free_ram_mb",
],
)
def test_hypervisor_memory_free_serialization(val):
"""
Tests that variants of HYPERVISOR_MEMORY_FREE can be serialized
"""
assert (
HypervisorProperties.from_string(val)
is HypervisorProperties.HYPERVISOR_MEMORY_FREE
)


@pytest.mark.parametrize(
"val",
[
"hypervisor_memory_size",
"Hypervisor_Memory_Size",
"HyPeRvIsOr_MeMoRy_SiZe",
"memory_size",
"memory_mb",
],
)
def test_hypervisor_memory_size_serialization(val):
"""
Tests that variants of HYPERVISOR_MEMORY_SIZE can be serialized
"""
assert (
HypervisorProperties.from_string(val)
is HypervisorProperties.HYPERVISOR_MEMORY_SIZE
)


@pytest.mark.parametrize(
"val",
[
"hypervisor_memory_used",
"Hypervisor_Memory_Used",
"HyPeRvIsOr_MeMoRy_UsEd",
"memory_used",
"memory_mb_used",
],
)
def test_hypervisor_memory_used_serialization(val):
"""
Tests that variants of HYPERVISOR_MEMORY_USED can be serialized
"""
assert (
HypervisorProperties.from_string(val)
is HypervisorProperties.HYPERVISOR_MEMORY_USED
)


@pytest.mark.parametrize(
"val",
["hypervisor_name", "Hypervisor_Name", "HyPeRvIsOr_NaMe", "name", "host_name"],
Expand Down Expand Up @@ -226,43 +106,6 @@ def test_hypervisor_status_serialization(val):
)


@pytest.mark.parametrize(
"val",
[
"hypervisor_vcpus",
"Hypervisor_VCPUs",
"HyPeRvIsOr_VcPuS",
"vcpus",
],
)
def test_hypervisor_vcpus_serialization(val):
"""
Tests that variants of HYPERVISOR_VCPUS can be serialized
"""
assert (
HypervisorProperties.from_string(val) is HypervisorProperties.HYPERVISOR_VCPUS
)


@pytest.mark.parametrize(
"val",
[
"hypervisor_vcpus_used",
"Hypervisor_VCPUs_Used",
"HyPeRvIsOr_VcPuS_uSeD",
"vcpus_used",
],
)
def test_hypervisor_vcpus_used_serialization(val):
"""
Tests that variants of HYPERVISOR_VCPUS_USED can be serialized
"""
assert (
HypervisorProperties.from_string(val)
is HypervisorProperties.HYPERVISOR_VCPUS_USED
)


@pytest.mark.parametrize(
"val",
[
Expand Down
25 changes: 0 additions & 25 deletions tests/mappings/test_hypervisor_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,6 @@ def test_client_side_handlers_datetime():
assert not handler


def test_client_side_handlers_integer(client_side_test_mappings):
"""
Tests client side handler mappings are correct, and line up to the expected
client side params for integer presets
"""
integer_prop_list = [
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,
]
handler = HypervisorMapping.get_client_side_handlers().integer_handler
mappings = {
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,
}
client_side_test_mappings(handler, mappings)


def test_get_chain_mappings():
"""
Tests get_chain_mapping outputs correctly
Expand Down
Loading

0 comments on commit 4b4a1df

Please sign in to comment.