ironic.common.redfish.resource module¶
Lightweight asynchronous Redfish resource implementation.
- class ironic.common.redfish.resource.AsyncResource(connector, path, json_data=None)[source]¶
Bases:
objectLightweight asynchronous Redfish resource.
This is a dynamic alternative to sushy’s static field-based resources. Instead of declaring all fields upfront, it provides methods to access fields, execute actions, and navigate links on-demand.
Example usage:
system = AsyncResource(connector, '/redfish/v1/Systems/1') await system.refresh() # Get simple fields manufacturer = system.field('Manufacturer') # Get nested fields with slash notation boot_target = system.field('Boot/BootSourceOverrideTarget') # Apply adapters to field values power_state = system.field('PowerState', adapter=str.lower) # Execute actions await system.action('Reset', {'ResetType': 'ForceRestart'}) # PATCH resources await system.patch({'AssetTag': 'new-tag'}) # Navigate to subresources bios = await system.link('Bios') boot_mode = bios.field('BootMode')
- async action(name, payload=None)[source]¶
Execute a Redfish action on this resource.
- Parameters:
name – Action name (e.g., ‘Reset’, ‘ComputerSystem.Reset’)
payload – Optional dict payload for the action
- Returns:
Response from the action POST request
- Raises:
ValueError if action is not found or has no target
Example:
await system.action('Reset', {'ResetType': 'ForceRestart'}) payload = {'Image': 'http://example.com/image.iso'} await virtual_media.action('InsertMedia', payload)
- collection(name='Members', expand=False)[source]¶
Get a collection of resources from a collection field.
- Parameters:
name – Collection field name (e.g., ‘Members’, ‘Drives’)
expand – If True, append
$expand=.($levels=1)to each member path so sub-resources are fetched inline when the collection is iterated. Each member strips the query back off inAsyncResource.__init__, so it never reaches a PATCH target and is never appended twice.
- Returns:
AsyncResourceCollection instance
Example:
# Get all systems from a collection systems_collection_resource = await client.resource( '/redfish/v1/Systems' ) systems_collection = systems_collection_resource.collection() systems = await systems_collection.get_all() for system in systems: print(system.field('Manufacturer')) # Get all drives from a storage resource with expansion storage = await client.resource('/redfish/v1/Systems/1/Storage/1') drives_collection = storage.collection('Drives', expand=True) drives = await drives_collection.get_all()
- property etag¶
Get the cached ETag value.
- property expanded¶
Whether this resource is fetched with
$expandby default.
- field(name, adapter=None, default=None)[source]¶
Get a field value from the resource JSON.
Supports nested field access using slash notation.
- Parameters:
name – Field name or path (e.g., ‘Manufacturer’ or ‘Boot/BootSourceOverrideTarget’)
adapter – Optional callable to process the field value
default – Default value if field is not found
- Returns:
Field value, optionally processed by adapter
- Example:
manufacturer = resource.field(‘Manufacturer’) boot_target = resource.field(‘Boot/BootSourceOverrideTarget’) power_watts = resource.field(‘PowerWatts’, adapter=int)
- property json¶
Get the raw JSON data.
- async link(name, default=Ellipsis, expand=False)[source]¶
Get a linked subresource.
Navigates to a linked resource via @odata.id reference. Supports links in the ‘Links’ object or as direct properties.
- Parameters:
name – Link name (e.g., ‘Bios’, ‘Storage’, ‘Processors’)
default – Default value if link is not found (defaults to raising ValueError)
expand – If True, fetch the linked resource with
$expand=.($levels=1)to inline sub-resources in a single request. Mirrors sushy’schassis_expanded/storage_expandedproperties.
- Returns:
AsyncResource instance for the linked resource, or default
- Raises:
ValueError if link is not found and no default provided
Example:
bios = await system.link('Bios') boot_mode = bios.field('BootMode') storage_collection = await system.link('Storage', expand=True) members = storage_collection.field('Members', default=[]) # Optional link that may not exist thermal = await chassis.link('Thermal', default=None) if thermal: fans = thermal.field('Fans', default=[])
- async patch(payload, etag=None)[source]¶
PATCH this resource with the given payload.
The resource is deliberately not re-fetched afterwards. A BMC that is still settling after an update can answer the follow-up GET with a 5xx, and raising that here would report a PATCH that in fact succeeded as a failure, leaving the caller unable to tell the two apart. Instead, when the BMC returns the updated representation in the PATCH response it is adopted directly – the same shortcut sushy takes with
refresh(json_doc=...). Otherwisejsonkeeps the pre-PATCH document, and a caller that needs the new state callsrefresh()itself, where a failure is unambiguously a failure to read.- Parameters:
payload – Dict of fields to update
etag – Optional ETag for conditional updates. If not provided, uses the ETag from the last refresh (if available)
- Returns:
Response from the PATCH request
- Example:
await system.patch({‘AssetTag’: ‘new-tag’}) await system.patch({‘Boot’: {‘BootSourceOverrideTarget’: ‘Pxe’}})
- property path¶
Get the resource path.
- async refresh(expand=None)[source]¶
Refresh the resource data from the server.
- Parameters:
expand – Whether to request
$expand=.($levels=1)so that sub-resources are fetched inline (mirrors sushy’s_expandedproperties).None, the default, keeps whatever this resource was built with, so a member of a collection taken withexpand=Truestays expanded when it is refreshed.TrueorFalseoverride that and become the resource’s new default, keepingexpandedin step with the document injson.- Returns:
self for method chaining
- class ironic.common.redfish.resource.AsyncResourceCollection(connector, paths)[source]¶
Bases:
objectHelper for working with collections of resources.
This is returned when accessing collection links that contain multiple @odata.id references.
- Supports async iteration:
- async for resource in collection:
print(resource.field(‘Name’))
- Can be iterated multiple times:
- async for resource in collection:
# First iteration
- async for resource in collection:
# Second iteration (independent)
- async get(index)[source]¶
Get a single resource by index.
- Parameters:
index – Index in the collection
- Returns:
AsyncResource instance with refreshed data
- async get_all()[source]¶
Fetch all resources in the collection.
- Returns:
List of AsyncResource instances with refreshed data
- async one()[source]¶
Get the single resource from this collection.
- Returns:
AsyncResource instance with refreshed data
- Raises:
UnknownDefaultError if collection doesn’t have exactly 1 item
- Example:
# Get the only system (useful when you know there’s exactly one) systems = await client.collection(‘Systems’) system = await systems.one()
- property paths¶
Returns a list of all paths in the collection.