def get_lpar_vnics(adapter): """Return a dict mapping LPAR wrappers to their VNIC feeds. :param adapter: The pypowervm.adapter.Adapter for REST API communication. :return: A dict of the form { LPAR: [VNIC, ...] }, where the keys are pypowervm.wrappers.logical_partition.LPAR and the values are lists of the pypowervm.wrappers.iocard.VNIC they own. """ return { lpar: card.VNIC.get(adapter, parent=lpar) for lpar in tpar.get_partitions(adapter, lpars=True, vioses=False) }
def find_orphaned_trunks(adapter, vswitch_name): """Returns all orphaned trunk adapters on a given vswitch. An orphaned trunk is a trunk adapter that does not have any associated CNAs. :param adapter: The pypowervm adapter to perform the search with. :param vswitch_name: The name of the vswitch to search for orphaned trunks on. :return: A list of trunk adapters that do not have any associated CNAs """ vswitch = pvm_net.VSwitch.search(adapter, parent_type=pvm_ms.System, one_result=True, name=vswitch_name) # May occur if the system does not host the vswitch passed in. if vswitch is None: return [] vswitch_id = vswitch.switch_id # VIOS and Management Partitions can host Trunk Adapters. host_wraps = partition.get_partitions(adapter, lpars=False, vioses=True, mgmt=True) # Get all the CNA wraps on the vswitch cna_wraps = _find_cna_wraps(adapter, vswitch_id=vswitch_id) # Find all trunk adapters on the vswitch. trunk_list = [] for host_wrap in host_wraps: trunks = _find_all_trunks_on_lpar(adapter, parent_wrap=host_wrap, vswitch_id=vswitch_id) trunk_list.extend(trunks) # Check if the trunk adapters are orphans orphaned_trunk_list = [] for trunk in trunk_list: if not find_cnas_on_trunk(trunk, cna_wraps=cna_wraps): orphaned_trunk_list.append(trunk) return orphaned_trunk_list
def find_trunks(adapter, cna_w): """Returns the Trunk Adapters associated with the CNA. :param adapter: The pypowervm adapter to perform the search with. :param cna_w: The Client Network Adapter to find the Trunk Adapters for. :return: A list of Trunk Adapters (sorted by Trunk Priority) that host the Client Network Adapter. """ # VIOS and Management Partitions can host Trunk Adapters. host_wraps = partition.get_partitions( adapter, lpars=False, vioses=True, mgmt=True) # Find the corresponding trunk adapters. trunk_list = [] for host_wrap in host_wraps: trunk = _find_trunk_on_lpar(adapter, host_wrap, cna_w) if trunk: trunk_list.append(trunk) # Sort by the trunk priority trunk_list.sort(key=lambda x: x.trunk_pri) return trunk_list
def list_vifs(adapter, vif_class, include_vios_and_mgmt=False): """Map of partition:[VIFs] for a specific VIF type (CNA, VNIC, etc.). VIOS trunk adapters are never included (even if include_vios_and_mgmt=True) :param adapter: The pypowervm adapter. :param vif_class: The pypowervm wrapper class for the VIF-ish type to be retrieved (CNA, VNIC, etc.). :param include_vios_and_mgmt: If True, the return includes VIFs belonging to the management partition; AND non-trunk VIFs belonging to VIOS partitions. If False, both of these types are excluded. :return: A map of {lpar_w: [vif_w, ...]} where each vif_w is a wrapper of the specified vif_class. The vif_w list may be empty for a given lpar_w. """ # Get the VMs to query for. LOG.info( "Gathering Virtual Machine wrappers for a list_vifs call. " "Include VIOS and management: %s", include_vios_and_mgmt) # Find the MGMT vswitch and the Novalink I/O vswitch (if configured) vs_exclu = [] vswitch_list = pvm_net.VSwitch.get(adapter, parent=pvm_ms.System.get(adapter)[0]) for vswitch in vswitch_list: if vswitch.name in NON_SEA_BRIDGES: vs_exclu.append(vswitch.switch_id) # Loop through the VMs total_vifs = {} for vm_wrap in pvm_par.get_partitions(adapter, lpars=True, vioses=include_vios_and_mgmt, mgmt=include_vios_and_mgmt): total_vifs[vm_wrap] = _find_vifs(adapter, vif_class, vm_wrap, vs_exclu) return total_vifs
def _find_cna_wraps(adapter, vswitch_id=None): """Returns all CNAs. :param adapter: The pypowervm adapter to perform the search with. :param vswitch_id: This param is optional. If specified, the method will only return CNAs associated with the given vswitch. :return: A list of CNAs that are optionally associated with the given vswitch_id. """ # All lpars should be searched, including VIOSes lpar_wraps = partition.get_partitions(adapter) cna_wraps = [] filtered_cna_wraps = [] for lpar_wrap in lpar_wraps: cna_wraps.extend(pvm_net.CNA.get(adapter, parent=lpar_wrap)) # If a vswitch_id is passed in then filter to only cnas on that vswitch if (vswitch_id): for cna in cna_wraps: if (cna.vswitch_id == vswitch_id): filtered_cna_wraps.append(cna) cna_wraps = filtered_cna_wraps return cna_wraps
def test_get_partitions(self, mock_vio_get, mock_lpar_get, mock_mgmt_get): adpt = mock.Mock() # Test with the MGMT as a VIOS mgmt = mock.Mock(uuid='1') vioses = [mock.Mock(uuid='2'), mgmt] lpars = [mock.Mock(uuid='3'), mock.Mock(uuid='4')] mock_mgmt_get.return_value = mgmt mock_vio_get.return_value = vioses mock_lpar_get.return_value = lpars # Basic case self.assertEqual(vioses + lpars, tpar.get_partitions(adpt)) # Different permutations self.assertEqual(lpars + [mgmt], tpar.get_partitions( adpt, vioses=False, mgmt=True)) self.assertEqual(vioses, tpar.get_partitions( adpt, lpars=False, mgmt=True)) # Now test with the MGMT as a LPAR vioses = [mock.Mock(uuid='2')] lpars = [mock.Mock(uuid='3'), mock.Mock(uuid='4'), mgmt] mock_vio_get.return_value = vioses mock_lpar_get.return_value = lpars # Basic case self.assertEqual(vioses + lpars, tpar.get_partitions(adpt)) # Different permutations self.assertEqual(lpars, tpar.get_partitions( adpt, vioses=False, mgmt=True)) self.assertEqual(vioses + [mgmt], tpar.get_partitions( adpt, lpars=False, mgmt=True))
def crt_trunk_with_free_vlan(adapter, host_uuid, src_io_host_uuids, vs_name, crt_vswitch=True, dev_name=None, ovs_bridge=None, ovs_ext_ids=None, configured_mtu=None): """Creates a trunk adapter(s) with a free VLAN on the system. :param adapter: The pypowervm adapter to perform the update through. :param host_uuid: Not used. :param src_io_host_uuids: The list of UUIDs of the LPARs that will host the Trunk Adapters. At least one UUID is required. Multiple will be supported, and the Trunk Priority will increment per adapter (in the order that the I/O hosts are specified). :param pvid: The port VLAN ID. :param vs_name: The name of the PowerVM Hypervisor Virtual Switch to create the p2p connection on. This is required because it is not recommended to create it on the default (ETHERNET0) virtual switch. :param crt_vswitch: (Optional, Default: True) A boolean to indicate that if the vSwitch can not be found, the system should attempt to create one (with the default parameters - ex: Veb mode). :param dev_name: (Optional, Default: None) The device name. Only valid if the src_io_host_uuids is a single entity and the uuid matches the mgmt lpar UUID. Otherwise leave as None. If set, the name of the trunk adapter created on the mgmt lpar will be set to this value. :param ovs_bridge: (Optional, Default: None) If hosting through mgmt partition, this attribute specifies which Open vSwitch to connect to. :param ovs_ext_ids: (Optional, Default: None) Comma-delimited list of key=value pairs that get set as external-id metadata attributes on the OVS port. Only valid if ovs_bridge is set. :param configured_mtu: (Optional, Default: None) Sets the MTU on the adapter. May only be valid if adapter is being created against mgmt partition. :return: The CNA Wrapper that was created. :return: The TrunkAdapters that were created. Match the order that the src_io_host_uuids were passed in. """ # Make sure we have the appropriate vSwitch vswitch_w = _find_or_create_vswitch(adapter, vs_name, crt_vswitch) # Find the free VLAN vlan = _find_free_vlan(adapter, vswitch_w) # Need to get the VIOS uuids to determine if the src_io_host_uuid is a VIOS iohost_wraps = partition.get_partitions(adapter, lpars=False, vioses=True, mgmt=True) io_uuid_to_wrap = { w.uuid: w for w in iohost_wraps if w.uuid in src_io_host_uuids } # Now create the corresponding Trunk trunk_adpts = [] trunk_pri = 1 for io_uuid in src_io_host_uuids: trunk_adpt = pvm_net.CNA.bld(adapter, vlan, vswitch_w.related_href, trunk_pri=trunk_pri, dev_name=dev_name, ovs_bridge=ovs_bridge, ovs_ext_ids=ovs_ext_ids, configured_mtu=configured_mtu) trunk_adpts.append(trunk_adpt.create(parent=io_uuid_to_wrap[io_uuid])) trunk_pri += 1 return trunk_adpts