# Copyright 2026 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A :term:`Storage Switch` represents a single physical storage switch in the
FICON storage configuration associated with a DPM-enabled CPC.
A storage switch belongs to exactly one storage site and one storage fabric.
It can be moved between sites and fabrics using dedicated operations.
The Storage Switch object APIs provide access to the set of storage switches
within the FICON configuration associated with a CPC that is enabled for DPM.
APIs exist to define and undefine storage switches, list storage switches,
query storage switch properties, update selected properties, and move a switch
to a different storage site or storage fabric.
The :term:`Storage Switch` resources are accessible via the
:attr:`~zhmcclient.Console.storage_switches` property of the
:class:`~zhmcclient.Console` object.
"""
import copy
from ._manager import BaseManager
from ._resource import BaseResource
from ._logging import logged_api_call
from ._utils import RC_STORAGE_SWITCH
__all__ = ['StorageSwitchManager', 'StorageSwitch']
[docs]
class StorageSwitchManager(BaseManager):
"""
Manager providing access to the :term:`storage switches <storage switch>`
of the HMC.
Derived from :class:`~zhmcclient.BaseManager`; see there for common
methods and attributes.
Objects of this class are not directly created by the user; they are
accessible via the following instance variable:
* :attr:`~zhmcclient.Console.storage_switches` of a
:class:`~zhmcclient.Console` object.
"""
def __init__(self, console):
# This function should not go into the docs.
# Parameters:
# console (:class:`~zhmcclient.Console`):
# Console defining the scope for this manager.
# Resource properties that are supported as filter query parameters.
query_props = [
'name',
'domain-id',
]
super().__init__(
resource_class=StorageSwitch,
class_name=RC_STORAGE_SWITCH,
session=console.manager.session,
parent=console,
base_uri='/api/storage-switches', # Not for List operation!
oid_prop='object-id',
uri_prop='object-uri',
name_prop='name',
query_props=query_props,
)
@property
def console(self):
"""
:class:`~zhmcclient.Console`: The Console object representing the HMC.
"""
return self._parent
[docs]
@logged_api_call
def list(self, full_properties=False, filter_args=None,
additional_properties=None):
"""
List the storage switches defined in the HMC.
If the authenticated user does not have task permission to the
"Configure Storage – System Programmer" or
"Configure Storage – Storage Administrator" tasks, or if no objects are
returned due to filtering, the method succeeds and an empty list is
returned.
The properties in the returned resource objects are by default those
returned by the
"List Storage Switches of a Storage Site" or
"List Storage Switches of a Storage Fabric"
HMC operations defined in the :term:`HMC API` book and can be expanded
with the "full_properties" and "additional_properties" parameters of
this method.
Any resource property may be specified in a filter argument. For
details about filter arguments, see :ref:`Filtering`.
When using filters to get a single resource object of this type, the
``name`` property alone is not sufficient, because the name of storage
switch objects is unique within the containing storage site object and
within the containing storage fabric object. The name can thus be used
for another storage switch object (usually representing the same
real-life storage switch) on a different CPC.
In that case, specify the ``storage-site-uri`` or ``storage-fabric-uri``
(or both) properties in addition to the ``name`` property in the filter.
The listing of resources is handled by performing the "List Storage
Switches of a Storage Site" and/or "List Storage Switches of a Storage
Fabric" HMC operations depending on which of the ``storage-site-uri``
and ``storage-fabric-uri`` properties are specified in the filter
arguments.
HMC/SE version requirements:
* :ref:`firmware feature <firmware features>` "dpm-storage-management"
Authorization requirements:
* Task permission to the "Configure Storage – System Programmer" or
"Configure Storage – Storage Administrator" tasks.
Parameters:
full_properties (bool):
If `True`, the set of properties in the returned resource objects
will be expanded to the full set of readable properties defined in
the data model for
Storage Switch objects
in the :term:`HMC API` book.
filter_args (dict):
Filter arguments that narrow the list of returned resources to
those that match the specified filter arguments. For details, see
:ref:`Filtering`.
`None` causes no filtering to happen.
additional_properties (list of string):
A list of property names.
If specified, the set of properties in the returned resource objects
will be expanded with the specified properties.
`None` is treated the same as an empty list.
Returns:
: A list of :class:`~zhmcclient.StorageSwitch` objects.
Raises:
:exc:`~zhmcclient.HTTPError`
:exc:`~zhmcclient.ParseError`
:exc:`~zhmcclient.AuthError`
:exc:`~zhmcclient.ConnectionError`
:exc:`~zhmcclient.FilterConversionError`
"""
result_prop = 'storage-switches'
if filter_args is None or "storage-site-uri" not in filter_args and \
"storage-fabric-uri" not in filter_args:
sites = self.console.storage_sites.list()
result_switches = []
for site in sites:
list_uri = site.uri + "/storage-switches"
switches = self._list_with_operation(
list_uri, result_prop, full_properties, filter_args,
additional_properties)
result_switches.extend(switches)
return result_switches
switches_of_site = None
switches_of_fabric = None
if filter_args and "storage-site-uri" in filter_args:
filter_args_ = dict(filter_args)
list_uri = filter_args_.pop("storage-site-uri") + \
"/storage-switches"
switches_of_site = self._list_with_operation(
list_uri, result_prop, full_properties, filter_args_,
additional_properties)
if filter_args and "storage-fabric-uri" in filter_args:
filter_args_ = dict(filter_args)
list_uri = filter_args_.pop("storage-fabric-uri") + \
"/storage-switches"
switches_of_fabric = self._list_with_operation(
list_uri, result_prop, full_properties, filter_args_,
additional_properties)
if switches_of_site is None:
# "storage-fabric-uri" has been specified as a filter
return switches_of_fabric
if switches_of_fabric is None:
# "storage-site-uri" has been specified as a filter
return switches_of_site
# Both "storage-fabric-uri" and "storage-site-uri" have been specified
# as a filter
switch_uris_of_site = \
{obj.properties["object-uri"] for obj in switches_of_site}
result_switches = []
for obj in switches_of_fabric:
uri = obj.properties["object-uri"]
if uri in switch_uris_of_site:
result_switches.append(obj)
return result_switches
[docs]
@logged_api_call
def define(self, properties):
"""
Define a new storage switch with the specified properties.
This calls the "Define Storage Switch" operation on the HMC console
(``POST /api/console/operations/define-storage-switch``).
Authorization requirements:
* Task permission to the "Configure Storage – System Programmer" or
"Configure Storage – Storage Administrator" tasks.
Parameters:
properties (dict): Initial property values.
Allowable properties are defined in section 'Request body
contents' in section 'Define Storage Switch' in the
:term:`HMC API` book.
Required fields: ``domain-id``, ``storage-fabric-uri``,
``storage-site-uri``.
Optional fields: ``name``, ``description``, ``port-count``.
Returns:
:class:`~zhmcclient.StorageSwitch`: The new storage switch.
Raises:
:exc:`~zhmcclient.HTTPError`
:exc:`~zhmcclient.ParseError`
:exc:`~zhmcclient.AuthError`
:exc:`~zhmcclient.ConnectionError`
"""
result = self.session.post(
uri='/api/console/operations/define-storage-switch',
body=properties,
)
uri = result['object-uri']
name = properties.get(self._name_prop)
props = copy.deepcopy(properties)
props[self._uri_prop] = uri
storage_switch = StorageSwitch(self, uri, name, props)
self._name_uri_cache.update(name, uri)
return storage_switch
[docs]
class StorageSwitch(BaseResource):
"""
Representation of a :term:`storage switch`.
Derived from :class:`~zhmcclient.BaseResource`; see there for common
methods and attributes.
Objects of this class are not directly created by the user; they are
returned from creation or list functions on their manager object
(in this case, :class:`~zhmcclient.StorageSwitchManager`).
"""
def __init__(self, manager, uri, name=None, properties=None):
# This function should not go into the docs.
# manager (:class:`~zhmcclient.StorageSwitchManager`):
# Manager object for this resource object.
# uri (string):
# Canonical URI path of the resource.
# name (string):
# Name of the resource.
# properties (dict):
# Properties to be set for this resource object. May be `None` or
# empty.
assert isinstance(manager, StorageSwitchManager), (
"StorageSwitch init: Expected manager type "
f"{StorageSwitchManager}, got {type(manager)}"
)
super().__init__(manager, uri, name, properties)
self._storage_site = None
self._storage_fabric = None
@property
def storage_site(self):
"""
:class:`~zhmcclient.StorageSite`: The :term:`storage site` that contains
this storage switch.
The returned :class:`~zhmcclient.StorageSite` has only a minimal set of
properties populated.
"""
# We do here some lazy loading.
if not self._storage_site:
site_uri = self.get_property('storage-site-uri')
site_mgr = self.manager.console.storage_sites
self._storage_site = site_mgr.resource_object(site_uri)
return self._storage_site
@property
def storage_fabric(self):
"""
:class:`~zhmcclient.StorageFabric`: The :term:`storage fabric` that
contains this storage switch.
The returned :class:`~zhmcclient.StorageFabric` has only a minimal set
of properties populated.
"""
# We do here some lazy loading.
if not self._storage_fabric:
fabric_uri = self.get_property('storage-fabric-uri')
fabric_mgr = self.manager.console.storage_fabrics
self._storage_fabric = fabric_mgr.resource_object(fabric_uri)
return self._storage_fabric
[docs]
@logged_api_call
def undefine(self):
"""
Undefine (delete) this storage switch.
This calls the "Undefine Storage Switch" operation
(``POST /api/storage-switches/{id}/operations/undefine``).
If the storage switch contains switch ports, they are removed as well.
Authorization requirements:
* Task permission to the "Configure Storage – System Programmer" or
"Configure Storage – Storage Administrator" tasks.
Raises:
:exc:`~zhmcclient.HTTPError`
:exc:`~zhmcclient.ParseError`
:exc:`~zhmcclient.AuthError`
:exc:`~zhmcclient.ConnectionError`
"""
self.manager.session.post(
uri=self.uri + '/operations/undefine',
resource=self,
body=None,
)
# pylint: disable=protected-access
self.manager._name_uri_cache.delete(
self.get_properties_local(self.manager._name_prop, None)
)
self.cease_existence_local()
[docs]
@logged_api_call
def update_properties(self, properties):
"""
Update writeable properties of this storage switch.
This calls the "Update Storage Switch Properties" operation
(``POST /api/storage-switches/{id}``).
This method serializes with other methods that access or change
properties on the same Python object.
Authorization requirements:
* Task permission to the "Configure Storage – System Programmer" or
"Configure Storage – Storage Administrator" tasks.
Parameters:
properties (dict): New values for the properties to be updated.
Properties not to be updated are omitted.
Allowable properties are the writeable properties of the storage
switch resource defined in the :term:`HMC API` book.
Writeable properties: ``name``, ``description``, ``domain-id``,
``port-count``.
Raises:
:exc:`~zhmcclient.HTTPError`
:exc:`~zhmcclient.ParseError`
:exc:`~zhmcclient.AuthError`
:exc:`~zhmcclient.ConnectionError`
"""
self.manager.session.post(self.uri, resource=self, body=properties)
# pylint: disable=protected-access
is_rename = self.manager._name_prop in properties
if is_rename:
self.manager._name_uri_cache.delete(self.name)
self.update_properties_local(copy.deepcopy(properties))
if is_rename:
self.manager._name_uri_cache.update(self.name, self.uri)
[docs]
@logged_api_call
def move_to_storage_site(self, storage_site_uri):
"""
Move this storage switch to a different storage site.
This calls the "Move Storage Switch to Storage Site" operation
(``POST /api/storage-switches/{id}/operations/move-storage-site``).
Authorization requirements:
* Task permission to the "Configure Storage – System Programmer" or
"Configure Storage – Storage Administrator" tasks.
Parameters:
storage_site_uri (str): The canonical URI path of the target storage
site.
Raises:
:exc:`~zhmcclient.HTTPError`
:exc:`~zhmcclient.ParseError`
:exc:`~zhmcclient.AuthError`
:exc:`~zhmcclient.ConnectionError`
"""
body = {'storage-site-uri': storage_site_uri}
self.manager.session.post(
uri=self.uri + '/operations/move-storage-site',
resource=self,
body=body,
)
self.update_properties_local({'storage-site-uri': storage_site_uri})
[docs]
@logged_api_call
def move_to_storage_fabric(self, storage_fabric_uri):
"""
Move this storage switch to a different storage fabric.
This calls the "Move Storage Switch to Storage Fabric" operation
(``POST /api/storage-switches/{id}/operations/move-storage-fabric``).
Authorization requirements:
* Task permission to the "Configure Storage – System Programmer" or
"Configure Storage – Storage Administrator" tasks.
Parameters:
storage_fabric_uri (str): The canonical URI path of the target
storage fabric.
Raises:
:exc:`~zhmcclient.HTTPError`
:exc:`~zhmcclient.ParseError`
:exc:`~zhmcclient.AuthError`
:exc:`~zhmcclient.ConnectionError`
"""
body = {'storage-fabric-uri': storage_fabric_uri}
self.manager.session.post(
uri=self.uri + '/operations/move-storage-fabric',
resource=self,
body=body,
)
self.update_properties_local(
{'storage-fabric-uri': storage_fabric_uri})
[docs]
def dump(self):
"""
Dump this StorageSwitch resource with its properties as a resource
definition.
The returned resource definition has the following format::
{
# Resource properties:
"properties": {...},
}
Returns:
dict: Resource definition of this resource.
"""
resource_dict = super().dump()
return resource_dict