Return details in baremetal_node_info when iterating over all machines

Without any parameters supplied openstack.cloud.baremetal_node_info is
supposed to gather and return information about all nodes.
This is done with a call to cloud.list_machines(), which itself calls
self.baremetal.nodes().
Unfortunately this will not return detailed information about each
machine as the details parameter of self.baremetal.nodes() defaults to
false.
This commit rewrites the module to use the baremetal service proxy of
openstacksdk to get machines with details and converts them using the
`to_dict()` method.

Story: 2010017
Task: 45207
Change-Id: Ib06aea5f59f799d6ed81b30264c8a168301c1a9b
This commit is contained in:
Jan Horstmann
2022-04-28 16:39:34 +02:00
parent e85a0b809a
commit 9b62cd7734

View File

@@ -58,7 +58,7 @@ baremetal_nodes:
returned: always, but can be null returned: always, but can be null
type: complex type: complex
contains: contains:
allocation_uuid: allocation_id:
description: The UUID of the allocation associated with the node. description: The UUID of the allocation associated with the node.
If not null, will be the same as instance_uuid (the If not null, will be the same as instance_uuid (the
opposite is not always true). Unlike instance_uuid, opposite is not always true). Unlike instance_uuid,
@@ -66,25 +66,20 @@ baremetal_nodes:
to remove allocations. to remove allocations.
returned: success returned: success
type: str type: str
automated_clean:
description: Indicates whether the node will perform automated
clean or not.
returned: success
type: bool
bios_interface: bios_interface:
description: The bios interface to be used for this node. description: The bios interface to be used for this node.
returned: success returned: success
type: str type: str
boot_interface: boot_interface:
description: The boot interface for a Node, e.g. "pxe". description: The boot interface for a node, e.g. "pxe".
returned: success returned: success
type: str type: str
boot_mode: boot_mode:
description: The boot mode for a node, either "uefi" or "bios" description: The boot mode for a node, either "uefi" or "bios"
returned: success returned: success
type: str type: str
chassis_uuid: chassis_id:
description: UUID of the chassis associated with this Node. May be description: UUID of the chassis associated with this node. May be
empty or None. empty or None.
returned: success returned: success
type: str type: str
@@ -103,11 +98,6 @@ baremetal_nodes:
-, and .. -, and ..
returned: success returned: success
type: str type: str
console_enabled:
description: Indicates whether console access is enabled or
disabled on this node.
returned: success
type: bool
console_interface: console_interface:
description: The console interface for a node, e.g. "no-console". description: The console interface for a node, e.g. "no-console".
returned: success returned: success
@@ -130,13 +120,13 @@ baremetal_nodes:
type: str type: str
driver_info: driver_info:
description: All the metadata required by the driver to manage this description: All the metadata required by the driver to manage this
Node. List of fields varies between drivers, and can node. List of fields varies between drivers, and can
be retrieved from the be retrieved from the
/v1/drivers/<DRIVER_NAME>/properties resource. /v1/drivers/<DRIVER_NAME>/properties resource.
returned: success returned: success
type: dict type: dict
driver_internal_info: driver_internal_info:
description: Internal metadata set and stored by the Node's driver. description: Internal metadata set and stored by the node's driver.
returned: success returned: success
type: dict type: dict
extra: extra:
@@ -146,7 +136,7 @@ baremetal_nodes:
type: dict type: dict
fault: fault:
description: The fault indicates the active fault detected by description: The fault indicates the active fault detected by
ironic, typically the Node is in "maintenance mode". ironic, typically the node is in "maintenance mode".
None means no fault has been detected by ironic. None means no fault has been detected by ironic.
"power failure" indicates ironic failed to retrieve "power failure" indicates ironic failed to retrieve
power state from this node. There are other possible power state from this node. There are other possible
@@ -162,27 +152,32 @@ baremetal_nodes:
description: The interface used for node inspection. description: The interface used for node inspection.
returned: success returned: success
type: str type: str
instance_id:
description: UUID of the Nova instance associated with this node.
returned: success
type: str
instance_info: instance_info:
description: Information used to customize the deployed image. May description: Information used to customize the deployed image. May
include root partition size, a base 64 encoded config include root partition size, a base 64 encoded config
drive, and other metadata. Note that this field is drive, and other metadata. Note that this field is
erased automatically when the instance is deleted erased automatically when the instance is deleted
(this is done by requesting the Node provision state (this is done by requesting the node provision state
be changed to DELETED). be changed to DELETED).
returned: success returned: success
type: dict type: dict
instance_uuid: is_automated_clean_enabled:
description: UUID of the Nova instance associated with this Node. description: Indicates whether the node will perform automated
clean or not.
returned: success returned: success
type: str type: bool
last_error: is_console_enabled:
description: Any error from the most recent (last) transaction that description: Indicates whether console access is enabled or
started but failed to finish. disabled on this node.
returned: success returned: success
type: str type: bool
maintenance: is_maintenance:
description: Whether or not this Node is currently in "maintenance description: Whether or not this node is currently in "maintenance
mode". Setting a Node into maintenance mode removes it mode". Setting a node into maintenance mode removes it
from the available resource pool and halts some from the available resource pool and halts some
internal automation. This can happen manually (eg, via internal automation. This can happen manually (eg, via
an API request) or automatically when Ironic detects a an API request) or automatically when Ironic detects a
@@ -190,8 +185,35 @@ baremetal_nodes:
machine. machine.
returned: success returned: success
type: bool type: bool
is_protected:
description: Whether the node is protected from undeploying,
rebuilding and deletion.
returned: success
type: bool
is_retired:
description: Whether the node is retired and can hence no longer be
provided, i.e. move from manageable to available, and
will end up in manageable after cleaning (rather than
available).
returned: success
type: bool
is_secure_boot:
description: Indicates whether node is currently booted with
secure_boot turned on.
returned: success
type: bool
last_error:
description: Any error from the most recent (last) transaction that
started but failed to finish.
returned: success
type: str
links:
description: A list of relative links, including self and bookmark
links.
returned: success
type: list
maintenance_reason: maintenance_reason:
description: User-settable description of the reason why this Node description: User-settable description of the reason why this node
was placed into maintenance mode was placed into maintenance mode
returned: success returned: success
type: str type: str
@@ -200,27 +222,27 @@ baremetal_nodes:
returned: success returned: success
type: str type: str
name: name:
description: Human-readable identifier for the Node resource. May description: Human-readable identifier for the node resource. May
be undefined. Certain words are reserved. be undefined. Certain words are reserved.
returned: success returned: success
type: str type: str
network_interface: network_interface:
description: Which Network Interface provider to use when plumbing description: Which Network Interface provider to use when plumbing
the network connections for this Node. the network connections for this node.
returned: success returned: success
type: str type: str
owner: owner:
description: A string or UUID of the tenant who owns the object. description: A string or UUID of the tenant who owns the object.
returned: success returned: success
type: str type: str
portgroups: ports:
description: List of ironic portgroups on this node. description: List of ironic ports on this node.
returned: success returned: success
type: list type: list
elements: dict elements: dict
contains: contains:
address: address:
description: Physical hardware address of this Portgroup, description: Physical hardware address of this network port,
typically the hardware MAC address. typically the hardware MAC address.
returned: success returned: success
type: str type: str
@@ -239,7 +261,76 @@ baremetal_nodes:
returned: success returned: success
type: str type: str
internal_info: internal_info:
description: Internal metadata set and stored by the Portgroup. description: Internal metadata set and stored by the port. This
field is read-only.
returned: success
type: dict
is_pxe_enabled:
description: Indicates whether PXE is enabled or disabled on
the port.
returned: success
type: str
links:
description: A list of relative links, including self and bookmark
links.
returned: success
type: list
local_link_connection:
description: The port binding profile. If specified, must
contain switch_id (only a MAC address or an
OpenFlow based datapath_id of the switch are
accepted in this field) and port_id (identifier of
the physical port on the switch to which node's
port is connected to) fields. switch_info is an
optional string field to be used to store any
vendor-specific information.
returned: success
type: dict
node_id:
description: UUID of the node this resource belongs to.
returned: success
type: str
physical_network:
description: The name of the physical network to which a port
is connected. May be empty.
returned: success
type: str
port_group_id:
description: UUID of the port group this resource belongs to.
returned: success
type: str
updated_at:
description: The UTC date and time when the resource was
updated, ISO 8601 format. May be "null".
returned: success
type: str
port_groups:
description: List of ironic port groups on this node.
returned: success
type: list
elements: dict
contains:
address:
description: Physical hardware address of this port group,
typically the hardware MAC address.
returned: success
type: str
created_at:
description: The UTC date and time when the resource was
created, ISO 8601 format.
returned: success
type: str
extra:
description: A set of one or more arbitrary metadata key and
value pairs.
returned: success
type: dict
id:
description: The UUID for the resource.
returned: success
type: str
internal_info:
description: Internal metadata set and stored by the port group.
This field is read-only. This field is read-only.
returned: success returned: success
type: dict type: dict
@@ -248,6 +339,11 @@ baremetal_nodes:
port group can be used as stand-alone ports. port group can be used as stand-alone ports.
returned: success returned: success
type: bool type: bool
links:
description: A list of relative links, including self and bookmark
links.
returned: success
type: list
mode: mode:
description: Mode of the port group. For possible values, refer description: Mode of the port group. For possible values, refer
to https://www.kernel.org/doc/Documentation/networking/bonding.txt. to https://www.kernel.org/doc/Documentation/networking/bonding.txt.
@@ -259,37 +355,22 @@ baremetal_nodes:
returned: success returned: success
type: str type: str
name: name:
description: Human-readable identifier for the Portgroup description: Human-readable identifier for the port group
resource. May be undefined. resource. May be undefined.
returned: success returned: success
type: str type: str
node_id: node_id:
description: UUID of the Node this resource belongs to. description: UUID of the node this resource belongs to.
returned: success returned: success
type: str type: str
ports: ports:
description: List of port UUID's of ports belonging to this description: List of ports belonging to this port group.
portgroup.
returned: success
type: list
properties:
description: Key/value properties related to the port group's
configuration.
returned: success
type: dict
updated_at:
description: The UTC date and time when the resource was
updated, ISO 8601 format. May be "null".
returned: success
type: str
ports:
description: List of ironic ports on this node.
returned: success returned: success
type: list type: list
elements: dict elements: dict
contains: contains:
address: address:
description: Physical hardware address of this network Port, description: Physical hardware address of this network port,
typically the hardware MAC address. typically the hardware MAC address.
returned: success returned: success
type: str type: str
@@ -308,12 +389,22 @@ baremetal_nodes:
returned: success returned: success
type: str type: str
internal_info: internal_info:
description: Internal metadata set and stored by the Port. This description: Internal metadata set and stored by the port. This
field is read-only. field is read-only.
returned: success returned: success
type: dict type: dict
is_pxe_enabled:
description: Indicates whether PXE is enabled or disabled on
the port.
returned: success
type: str
links:
description: A list of relative links, including self and bookmark
links.
returned: success
type: list
local_link_connection: local_link_connection:
description: The Port binding profile. If specified, must description: The port binding profile. If specified, must
contain switch_id (only a MAC address or an contain switch_id (only a MAC address or an
OpenFlow based datapath_id of the switch are OpenFlow based datapath_id of the switch are
accepted in this field) and port_id (identifier of accepted in this field) and port_id (identifier of
@@ -323,12 +414,8 @@ baremetal_nodes:
vendor-specific information. vendor-specific information.
returned: success returned: success
type: dict type: dict
name: node_id:
description: The name of the resource. description: UUID of the node this resource belongs to.
returned: success
type: str
node_uuid:
description: UUID of the Node this resource belongs to.
returned: success returned: success
type: str type: str
physical_network: physical_network:
@@ -336,13 +423,8 @@ baremetal_nodes:
is connected. May be empty. is connected. May be empty.
returned: success returned: success
type: str type: str
portgroup_uuid: port_group_id:
description: UUID of the Portgroup this resource belongs to. description: UUID of the port group this resource belongs to.
returned: success
type: str
pxe_enabled:
description: Indicates whether PXE is enabled or disabled on
the Port.
returned: success returned: success
type: str type: str
updated_at: updated_at:
@@ -350,8 +432,14 @@ baremetal_nodes:
updated, ISO 8601 format. May be "null". updated, ISO 8601 format. May be "null".
returned: success returned: success
type: str type: str
uuid: properties:
description: The UUID for the resource. description: Key/value properties related to the port group's
configuration.
returned: success
type: dict
updated_at:
description: The UTC date and time when the resource was
updated, ISO 8601 format. May be "null".
returned: success returned: success
type: str type: str
power_interface: power_interface:
@@ -360,29 +448,24 @@ baremetal_nodes:
returned: success returned: success
type: str type: str
power_state: power_state:
description: The current power state of this Node. Usually, "power description: The current power state of this node. Usually, "power
on" or "power off", but may be "None" if Ironic is on" or "power off", but may be "None" if Ironic is
unable to determine the power state (eg, due to unable to determine the power state (eg, due to
hardware failure). hardware failure).
returned: success returned: success
type: str type: str
properties: properties:
description: Physical characteristics of this Node. Populated by description: Physical characteristics of this node. Populated by
ironic-inspector during inspection. May be edited via ironic-inspector during inspection. May be edited via
the REST API at any time. the REST API at any time.
returned: success returned: success
type: dict type: dict
protected:
description: Whether the node is protected from undeploying,
rebuilding and deletion.
returned: success
type: bool
protected_reason: protected_reason:
description: The reason the node is marked as protected. description: The reason the node is marked as protected.
returned: success returned: success
type: str type: str
provision_state: provision_state:
description: The current provisioning state of this Node. description: The current provisioning state of this node.
returned: success returned: success
type: str type: str
raid_config: raid_config:
@@ -406,27 +489,19 @@ baremetal_nodes:
type: str type: str
resource_class: resource_class:
description: A string which can be used by external schedulers to description: A string which can be used by external schedulers to
identify this Node as a unit of a specific type of identify this node as a unit of a specific type of
resource. For more details, see resource. For more details, see
https://docs.openstack.org/ironic/latest/install/configure-nova-flavors.html https://docs.openstack.org/ironic/latest/install/configure-nova-flavors.html
returned: success returned: success
type: str type: str
retired:
description: Whether the node is retired and can hence no longer be
provided, i.e. move from manageable to available, and
will end up in manageable after cleaning (rather than
available).
returned: success
type: bool
retired_reason: retired_reason:
description: The reason the node is marked as retired. description: The reason the node is marked as retired.
returned: success returned: success
type: str type: str
secure_boot: states:
description: Indicates whether node is currently booted with description: Links to the collection of states.
secure_boot turned on.
returned: success returned: success
type: bool type: list
storage_interface: storage_interface:
description: Interface used for attaching and detaching volumes on description: Interface used for attaching and detaching volumes on
this node, e.g. "cinder". this node, e.g. "cinder".
@@ -441,17 +516,17 @@ baremetal_nodes:
target_provision_state: target_provision_state:
description: If a provisioning action has been requested, this description: If a provisioning action has been requested, this
field represents the requested (ie, "target") state. field represents the requested (ie, "target") state.
Note that a Node may go through several states during Note that a node may go through several states during
its transition to this target state. For instance, its transition to this target state. For instance,
when requesting an instance be deployed to an when requesting an instance be deployed to an
AVAILABLE Node, the Node may go through the following AVAILABLE node, the node may go through the following
state change progression, AVAILABLE -> DEPLOYING -> state change progression, AVAILABLE -> DEPLOYING ->
DEPLOYWAIT -> DEPLOYING -> ACTIVE DEPLOYWAIT -> DEPLOYING -> ACTIVE
returned: success returned: success
type: str type: str
target_raid_config: target_raid_config:
description: Represents the requested RAID configuration of the description: Represents the requested RAID configuration of the
node, which will be applied when the Node next node, which will be applied when the node next
transitions through the CLEANING state. Introduced transitions through the CLEANING state. Introduced
with the cleaning feature. with the cleaning feature.
returned: success returned: success
@@ -464,10 +539,6 @@ baremetal_nodes:
description: Bare Metal node updated at timestamp. description: Bare Metal node updated at timestamp.
returned: success returned: success
type: str type: str
uuid:
description: The UUID for the resource.
returned: success
type: str
vendor_interface: vendor_interface:
description: Interface for vendor-specific functionality on this description: Interface for vendor-specific functionality on this
node, e.g. "no-vendor". node, e.g. "no-vendor".
@@ -486,31 +557,19 @@ from ansible_collections.openstack.cloud.plugins.module_utils.openstack import (
) )
def cleanup_node_properties(machine, cloud):
# states are links, not useful
machine.pop('states', None)
for port in machine.ports:
# links are not useful
port.pop('links', None)
# redundant, location is in on machine as well
port.pop('location', None)
for portgroup in machine.portgroups:
# links are not useful
portgroup.pop('links', None)
# redundant, location is in on machine as well
portgroup.pop('location', None)
# links to ports are not useful, replace with list of port uuid's
portgroup['ports'] = [x.id for x in list(
cloud.baremetal.ports(portgroup=portgroup['id']))]
def get_ports_and_portgroups(cloud, machine): def get_ports_and_portgroups(cloud, machine):
machine.ports = cloud.list_nics_for_machine(machine.uuid) machine['ports'] = [nic.to_dict(computed=False)
machine.portgroups = [dict(x) for x in for nic in cloud.baremetal.ports(
list(cloud.baremetal.port_groups(node=machine.uuid, details=True, node_id=machine['id'])]
details=True))]
machine['port_groups'] = [grp.to_dict(computed=False) for grp in
cloud.baremetal.port_groups(node=machine['id'],
details=True)]
# links to ports are not useful, replace with list of ports
for port_group in machine['port_groups']:
port_group['ports'] = [port for port in machine['ports']
if port['port_group_id'] == port_group['id']]
def main(): def main():
@@ -529,22 +588,25 @@ def main():
sdk, cloud = openstack_cloud_from_module(module) sdk, cloud = openstack_cloud_from_module(module)
try: try:
if module.params['node']: if module.params['node']:
machine = cloud.get_machine(module.params['node']) machine = cloud.baremetal.find_node(module.params['node'])
elif module.params['mac']: elif module.params['mac']:
machine = cloud.get_machine_by_mac(module.params['mac']) nic = next(cloud.baremetal.ports(address=module.params['mac'],
fields=['node_id']), None)
if nic:
machine = cloud.baremetal.find_node(nic['node_id'])
# Fail if node not found # Fail if node not found
if (module.params['node'] or module.params['mac']) and not machine: if (module.params['node'] or module.params['mac']) and not machine:
module.fail_json(msg='The baremetal node was not found') module.fail_json(msg='The baremetal node was not found')
if machine: if machine:
machines.append(machine) machines.append(machine.to_dict(computed=False))
else: else:
machines = cloud.list_machines() machines = [machine.to_dict(computed=False)
for machine in cloud.baremetal.nodes(details=True)]
for machine in machines: for machine in machines:
get_ports_and_portgroups(cloud, machine) get_ports_and_portgroups(cloud, machine)
cleanup_node_properties(machine, cloud)
module.exit_json(changed=False, baremetal_nodes=machines) module.exit_json(changed=False, baremetal_nodes=machines)
except sdk.exceptions.OpenStackCloudException as e: except sdk.exceptions.OpenStackCloudException as e: