Working with Hosts, Clusters and Mappings

InfiniSDK provides an easy interface to query and manipulate volume mappings to hosts.

Creating Hosts

Creating hosts is the same like creating any other management object through InfiniSDK. Hosts are represented by the Host class:

>>> host = system.hosts.create(name='production01')
>>> host
<Host id=1008>
>>> print(host.get_name())
production01

Adding/Removing FC Ports

Adding and removing FC ports can be done with infinisdk.infinibox.host.Host.add_port() and infinisdk.infinibox.host.Host.remove_port(). The address should be an instance of the infi.dtypes.wwn.WWN class to denote an FC address:

>>> from infi.dtypes.wwn import WWN
>>> address = WWN('00:01:02:03:04:05:06:07')
>>> host.add_port(address)
>>> host.remove_port(address)

Adding/Removing iSCSI IQNs

Adding and removing iSCSI IQNs is done in a fashion similar to FC ports, only that the address in this case should be an instance of the infi.dtypes.iqn.iSCSIName class:

>>> from infi.dtypes.iqn import make_iscsi_name
>>> address = make_iscsi_name('iqn.1994-05.com.redhat:8f8dcc647276')
>>> host.add_port(address)
>>> host.remove_port(address)

Querying Host by a Defined Port

You can quickly check if a system has a host system.hosts.get_host_id_by_initiator_address, system.hosts.get_host_by_initiator_address and system.hosts.has_registered_initiator_address:

>>> system.hosts.has_registered_initiator_address(address)
False
>>> host.add_port(address)
>>> system.hosts.get_host_by_initiator_address(address) == host
True

Mapping and Unmapping Volumes and Snapshots

Given a volume object, we can easily map it to a host:

>>> lu = host.map_volume(volume)

The returned lu object represents the volume mapping to the specific host, and it can be used to retrieve information about the mapping:

>>> print(int(lu))
1

Unmapping can be done in several ways. The easiest would be to call Host.unmap_volume():

>>> host.unmap_volume(volume)

Which can also receive a specific LUN to unmap:

>>> lu = host.map_volume(volume, lun=2)

>>> host.unmap_volume(lun=2)

The LUN can also be deleted directly through its accessor object:

>>> lu = host.map_volume(volume)
>>> lu.unmap()

Querying Volume Mappings

Iterating over available mappings of a host is fairly simple:

>>> lu = host.map_volume(volume, lun=5)

>>> host.get_luns()
<LogicalUnitsContainer: [<LUN 5: <Host id=1008>-><Volume id=1007>>]>

>>> for lun in host.get_luns():
...     print("{0} is mapped to {1}".format(lun, lun.volume))
<LUN 5: <Host id=1008>-><Volume id=1007>> is mapped to <Volume id=1007>

There is also a shortcut to iterate over all mappings in the entire system:

>>> for lun in system.luns:
...     print("{0} belongs to {1} and is mapped to {2}".format(lun, lun.mapping_object, lun.volume))
<LUN 5: <Host id=1008>-><Volume id=1007>> belongs to <Host id=1008> and is mapped to <Volume id=1007>

Here is a code snippet to unmap all volumes in the system that contain ‘to remove’ in their names:

>>> import itertools

>>> volume.update_name('this is a volume to remove')

>>> for mapping_object in itertools.chain(system.host_clusters, system.hosts):
...     for lun in mapping_object.get_luns():
...         if 'to remove' in lun.volume.get_name():
...             print("Unmapping", lun.volume)
...             lun.unmap()
Unmapping <Volume id=1007>

Of course there is a much more convenient shortcut for unmapping a volume from all hosts, using the Volume.unmap() shortcut:

>>> lu = host.map_volume(volume)
>>> host.is_volume_mapped(volume)
True
>>> volume.unmap()
>>> host.invalidate_cache()
>>> host.is_volume_mapped(volume)
False

Clusters and Hosts

Manipulating clusters is done with the infinisdk.infinibox.host_cluster.HostCluster class:

>>> cluster = system.host_clusters.create()
>>> cluster.add_host(host)

>>> lu = cluster.map_volume(volume)

>>> host.invalidate_cache()
>>> [host_lu] = host.get_luns()

>>> host_lu
<LUN 11: <HostCluster id=1012>-><Volume id=1007>>

>>> host_lu.is_clustered()
True