def post_create_port(self, port): if port.port_status == constants.PORT_STATUS_UP: device_id = port.vm_id _clu_mor, _clu_path, vds_name = self._find_cluster_switch_for_vm( device_id) pg_mor = network_util.get_portgroup_mor_by_name( self.session, vds_name, port.network_uuid) if pg_mor is None: msg = (_("Port group %(net_id)s not created on " "virtual switch %(vds)s."), { 'net_id': port.network_uuid, 'vds': vds_name }) raise error_util.RunTimeError(msg) vm_mor = resource_util.get_vm_mor_for_uuid(self.session, device_id) if vm_mor is None: msg = (_("Virtual machine %(id)s with " "port %(port)s not created."), { 'id': device_id, 'port': port.uuid }) raise error_util.RunTimeError(msg) (pg_key, port_key, swuuid) = self._wait_for_port_update_on_vm(vm_mor, pg_mor) if pg_key and port_key and swuuid: # enable the port on virtual switch. network_util.enable_disable_port(self.session, swuuid, pg_key, port_key, True)
def create_port_group(session, dvs_name, pg_name, net_id, vlan_id): """Creates a Portgroup on DVS with a vlan id.""" port_group_mor = get_portgroup_mor_by_net_id(session, dvs_name, pg_name, net_id) if port_group_mor: port_group_config = session._call_method( vim_util, "get_dynamic_property", port_group_mor, "DistributedVirtualPortgroup", "config") if vlan_id == port_group_config.defaultPortConfig.vlan.vlanId: LOG.debug("Portgroup %(pg)s with vlan id %(vid)s already exists", { 'pg': pg_name, 'vid': vlan_id }) return else: LOG.info( _LI("Portgroup %(pg)s already exists " "but with vlan id %(vid)s"), { 'pg': pg_name, 'vid': port_group_config.defaultPortConfig.vlan.vlanId }) raise error_util.RunTimeError( "Inconsistent vlan id for portgroup" " %s", pg_name) else: client_factory = session._get_vim().client.factory add_prt_grp_spec = _get_add_vswitch_port_group_spec( client_factory, pg_name, vlan_id) blocked = client_factory.create('ns0:BoolPolicy') blocked.value = False blocked.inherited = False add_prt_grp_spec.defaultPortConfig.blocked = blocked dvs_mor = get_dvs_mor_by_name(session, dvs_name) try: task_ref = session._call_method(session._get_vim(), "AddDVPortgroup_Task", dvs_mor, spec=add_prt_grp_spec) session.wait_for_task(task_ref) LOG.info( _LI("Successfully created portgroup " "%(pg)s with vlan id %(vid)s"), { 'pg': pg_name, 'vid': vlan_id }) except Exception as e: LOG.exception( _LE("Failed to create portgroup %(pg)s with " "vlan id %(vid)s on vCenter. Cause : %(err)s"), { 'pg': pg_name, 'vid': vlan_id, 'err': e }) raise error_util.RunTimeError("Failed to create portgroup %s " "with vlan id %s on vCenter.Cause" " : %s" % (pg_name, vlan_id, e))
def delete_port_group(session, dvs_name, pg_name): """Deletes a port group from DVS.""" port_group_mor = get_portgroup_mor_by_name(session, dvs_name, pg_name) if port_group_mor: try: destroy_task = session._call_method(session._get_vim(), "Destroy_Task", port_group_mor) session.wait_for_task(destroy_task) LOG.info( _LI("Successfully deleted portgroup %(pg)s from " "dvs %(dvs)s"), { 'pg': pg_name, 'dvs': dvs_name }) except Exception as e: LOG.exception( _LE("Failed to delete portgroup %(pg)s from " "dvs %(dvs)s .Cause : %(err)s"), { 'pg': pg_name, 'dvs': dvs_name, 'err': e }) raise error_util.RunTimeError("Failed to delete portgroup %s " "on dvs %s on vCenter.Cause" " : %s" % (pg_name, dvs_name, e)) else: LOG.info(_LI("portgroup %(pg)s not present on dvs %(dvs)s"), { 'pg': pg_name, 'dvs': dvs_name })
def test_post_create_port_excp3(self): vm_id = fake_vmware_api.Constants.VM_UUID network_uuid = fake_vmware_api.Constants.PORTGROUP_NAME port = model.Port(name=None, mac_address=None, ipaddresses=None, vm_id=vm_id, port_status=constants.PORT_STATUS_UP, network_uuid=network_uuid) with mock.patch.object(dvs_driver.DvsNetworkDriver, "_register_vm_for_updates", side_effect=error_util.RunTimeError( "Exception in registering vm for updates")): self.assertRaises(error_util.RunTimeError, self.vc_driver.post_create_port, port) self.assertFalse( fake_vmware_api.is_task_done("ReconfigureDVPort_Task"))
def wait_for_task(self, task_ref): task_info = self._call_method(vim_util, "get_dynamic_property", task_ref, "Task", "info") if task_info.state == "error": raise error_util.RunTimeError("Incorrect Parameter") return