def _nic_backing_from_obj(self, network_obj): rv = None if isinstance(network_obj, vim.dvs.DistributedVirtualPortgroup): rv = vim.VirtualEthernetCardDistributedVirtualPortBackingInfo( port=vim.DistributedVirtualSwitchPortConnection( portgroupKey=network_obj.key, switchUuid=network_obj.config.distributedVirtualSwitch.uuid )) elif isinstance(network_obj, vim.OpaqueNetwork): rv = vim.vm.device.VirtualEthernetCard.OpaqueNetworkBackingInfo( opaqueNetworkType='nsx.LogicalSwitch', opaqueNetworkId=network_obj.summary.opaqueNetworkId) elif isinstance(network_obj, vim.Network): rv = vim.vm.device.VirtualEthernetCard.NetworkBackingInfo( deviceName=network_obj.name, network=network_obj) return rv
def _reassign_ports(self, sg_aggr): """ Reassigns VM to a dvportgroup based on its port's security group set """ if not sg_aggr.ports_to_assign: return ports = sg_aggr.ports_to_assign sg_aggr.ports_to_assign = [] port_keys_to_drop = defaultdict(list) for port in ports: sg_set = sg_util.security_group_set(port) if not sg_set: LOG.debug("Port {} has no security group set, skipping reassignment.".format(port['id'])) continue port_desc = port['port_desc'] if port_desc.port_group_key == sg_aggr.pg.key: # Existing ports can enter the reassignment queue # on agent boot before the pg_key has been set # on the sg_aggr object. Filter them here. continue dvs_uuid = port_desc.dvs_uuid dvs = self.v_center.get_dvs_by_uuid(dvs_uuid) # Configure the backing to the required dvportgroup port_connection = vim.DistributedVirtualSwitchPortConnection() port_connection.switchUuid = dvs_uuid port_connection.portgroupKey = sg_aggr.pg_key port_backing = vim.VirtualEthernetCardDistributedVirtualPortBackingInfo() port_backing.port = port_connection # Specify the device that we are going to edit virtual_device = getattr(vim, port_desc.device_type)() virtual_device.key = port_desc.device_key virtual_device.backing = port_backing virtual_device.addressType = "manual" virtual_device.macAddress = port_desc.mac_address # Create an edit spec for an existing virtual device virtual_device_config_spec = vim.VirtualDeviceConfigSpec() virtual_device_config_spec.operation = "edit" virtual_device_config_spec.device = virtual_device # Create a config spec for applying the update to the virtual machine vm_config_spec = vim.VirtualMachineConfigSpec() vm_config_spec.deviceChange = [virtual_device_config_spec] # Queue the update vm_ref = vim.VirtualMachine(port_desc.vmobref) vm_ref._stub = self.dvs.connection._stub if not CONF.AGENT.dry_run: self._green.spawn_n(reconfig_vm, vm_ref, vm_config_spec) else: LOG.debug("Reassign: %s", vm_config_spec) # Store old port keys of reassigned VMs port_keys_to_drop[dvs_uuid].append(port_desc.port_key) # Remove obsolete port binding specs """ This should be fixed in the design instead of adding corrective code! Still, it is a cheap fix and saves unnecessary API calls. """ for dvs_uuid, port_keys in six.iteritems(port_keys_to_drop): dvs = self.v_center.get_dvs_by_uuid(dvs_uuid) dvs.filter_update_specs(lambda x: x.key not in port_keys) eventlet.sleep(0) # yield to allow VM network reassignments to take place