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

Drop storage/network ports for removed adapters #1770

Merged
merged 1 commit into from
Mar 1, 2025
Merged
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 changes/1764.fix.1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove unused network and storage port objects from export data, too.
56 changes: 38 additions & 18 deletions zhmcclient/_cpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2046,9 +2046,9 @@ def export_dpm_configuration(self, include_unused_adapters=False):
capacity groups, various storage and tape related resources, and
certificate objects.

By default, only those adapters and virtual switches of the CPC are
exported that are referenced by other DPM specific configuration
objects.
By default, only those adapters, virtual switches, network and
storage port objects of the CPC are exported that are referenced by
other DPM specific configuration objects.

This method performs the "Get Inventory" HMC operation and extracts
the relevant elements into the result.
Expand All @@ -2068,9 +2068,9 @@ def export_dpm_configuration(self, include_unused_adapters=False):
Parameters:

include_unused_adapters (bool):
Controls whether the full set of adapters should be returned,
vs. only those that are referenced by other DPM objects that
are part of the return data.
Controls whether the full set of adapters and corresponding
resources should be returned, vs. only those that are referenced
by other DPM objects that are part of the return data.

Returns:

Expand Down Expand Up @@ -2930,7 +2930,7 @@ def _convert_to_config(self, inventory_list, include_unused_adapters):
config_dict['certificates'] = certificates

if not include_unused_adapters:
_drop_unused_adapters_and_switches(config_dict)
_drop_unused_adapters_and_resources(config_dict)

sort_lists(config_dict)

Expand Down Expand Up @@ -3157,34 +3157,54 @@ def _add_encoded(console, certificates):
cert_dict.update(cert.get_encoded())


def _drop_unused_adapters_and_switches(dpm_config):
def _drop_unused_adapters_and_resources(dpm_config):
"""
Removes all adapters and virtual switch objects from dpm_config in place
that aren't referenced by actual DPM configuration elements.
Removes all adapters, virtual switch, storage/network port objects from
dpm_config in place that aren't referenced by actual DPM configuration
elements.
"""
_remove_unreferenced_elements(dpm_config,
'virtual-switches', ['adapters'])
_remove_unreferenced_elements(dpm_config, 'adapters',
['network-ports', 'storage-ports'])
_remove_unreferenced_keys(dpm_config, 'virtual-switches', ['adapters'])
removed_adapters = _remove_unreferenced_keys(dpm_config, 'adapters',
['network-ports',
'storage-ports'])
_remove_child_elements(dpm_config, 'network-ports', removed_adapters)
_remove_child_elements(dpm_config, 'storage-ports', removed_adapters)


def _remove_unreferenced_elements(dpm_config, key_to_update, keys_to_ignore):
def _remove_unreferenced_keys(dpm_config, key_to_update, keys_to_ignore):
"""
Creates a string representation of dpm_config, EXCLUDING keys key_to_update
and keys_to_ignore. Then iterates the list of key_to_update
entries within dpm_config to collect those that are referenced by their
object-id within that string representation. Then updates dpm_config
for key_to_update in place to that list of elements that are actually
referenced.
Returns a list of object-uri fields of all dropped objects.
"""
config = str(
{key: dpm_config[key] for key in dpm_config
if (key != key_to_update and key not in keys_to_ignore)})
referenced = []
referenced_keys = []
dropped_uris = []
for elem in dpm_config[key_to_update]:
if elem['object-id'] in config:
referenced.append(elem)
dpm_config[key_to_update] = referenced
referenced_keys.append(elem)
else:
dropped_uris.append(elem['object-uri'])
dpm_config[key_to_update] = referenced_keys
return dropped_uris


def _remove_child_elements(dpm_config, key_to_update, dropped_parents):
"""
Updates dpm_config for key_to_update in place removing all those elements
with a parent in the list of dropped_parents.
"""
retained = []
for elem in dpm_config[key_to_update]:
if elem['parent'] not in dropped_parents:
retained.append(elem)
dpm_config[key_to_update] = retained


def sort_lists(dpm_config):
Expand Down
Loading