blob: 4e1f68685a3f78a5e13fe35fe1c94195bcaebe11 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
#
# Copyright 2013 Cloudbase Solutions Srl
# 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.
"""
Utility class for network related operations.
"""
import sys
import uuid
if sys.platform == 'win32':
import wmi
from nova.virt.hyperv import vmutils
class NetworkUtils(object):
def __init__(self):
if sys.platform == 'win32':
self._conn = wmi.WMI(moniker='//./root/virtualization')
def get_external_vswitch(self, vswitch_name):
if vswitch_name:
vswitches = self._conn.Msvm_VirtualSwitch(ElementName=vswitch_name)
else:
# Find the vswitch that is connected to the first physical nic.
ext_port = self._conn.Msvm_ExternalEthernetPort(IsBound='TRUE')[0]
port = ext_port.associators(wmi_result_class='Msvm_SwitchPort')[0]
vswitches = port.associators(wmi_result_class='Msvm_VirtualSwitch')
if not len(vswitches):
raise vmutils.HyperVException(_('vswitch "%s" not found')
% vswitch_name)
return vswitches[0].path_()
def create_vswitch_port(self, vswitch_path, port_name):
switch_svc = self._conn.Msvm_VirtualSwitchManagementService()[0]
#Create a port on the vswitch.
(new_port, ret_val) = switch_svc.CreateSwitchPort(
Name=str(uuid.uuid4()),
FriendlyName=port_name,
ScopeOfResidence="",
VirtualSwitch=vswitch_path)
if ret_val != 0:
raise vmutils.HyperVException(_("Failed to create vswitch port "
"%(port_name)s on switch "
"%(vswitch_path)s") % locals())
return new_port
|