-
Notifications
You must be signed in to change notification settings - Fork 60
/
create_vswitch_and_portgroup.py
113 lines (81 loc) · 3.36 KB
/
create_vswitch_and_portgroup.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'''
Copyright 2014-2015 Reubenur Rahman
All Rights Reserved
@author: [email protected]
'''
import atexit
from pyVmomi import vim, vmodl
from pyVim import connect
from pyVim.connect import Disconnect
inputs = {'vcenter_ip': '15.22.10.11',
'vcenter_password': 'Password123',
'vcenter_user': 'Administrator',
'host_name': '15.22.11.6',
'switch_name': 'TestvSwitch',
'num_ports': 100,
'nic_name': 'vmnic1',
'port_group_name': 'TestPortGroup'
}
def get_obj(content, vimtype, name):
"""
Get the vsphere object associated with a given text name
"""
obj = None
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for c in container.view:
if c.name == name:
obj = c
break
return obj
def create_vswitch(host_network_system, vss_name, num_ports, nic_name):
vss_spec = vim.host.VirtualSwitch.Specification()
vss_spec.numPorts = num_ports
#vss_spec.bridge = vim.host.VirtualSwitch.SimpleBridge(nicDevice='pnic_key')
vss_spec.bridge = vim.host.VirtualSwitch.BondBridge(nicDevice=[nic_name])
host_network_system.AddVirtualSwitch(vswitchName=vss_name, spec=vss_spec)
print "Successfully created vSwitch ", vss_name
def create_port_group(host_network_system, pg_name, vss_name):
port_group_spec = vim.host.PortGroup.Specification()
port_group_spec.name = pg_name
port_group_spec.vlanId = 0
port_group_spec.vswitchName = vss_name
security_policy = vim.host.NetworkPolicy.SecurityPolicy()
security_policy.allowPromiscuous = True
security_policy.forgedTransmits = True
security_policy.macChanges = False
port_group_spec.policy = vim.host.NetworkPolicy(security=security_policy)
host_network_system.AddPortGroup(portgrp=port_group_spec)
print "Successfully created PortGroup ", pg_name
def add_virtual_nic(host_network_system, pg_name):
vnic_spec = vim.host.VirtualNic.Specification()
vnic_spec.ip = vim.host.IpConfig(dhcp=True)
vnic_spec.mac = '00:50:56:7d:5e:0b'
host_network_system.AddServiceConsoleVirtualNic(portgroup=pg_name, nic=vnic_spec)
def main():
try:
si = None
try:
print "Trying to connect to VCENTER SERVER . . ."
si = connect.Connect(inputs['vcenter_ip'], 443, inputs['vcenter_user'], inputs['vcenter_password'], version="vim.version.version8")
except IOError, e:
pass
atexit.register(Disconnect, si)
print "Connected to VCENTER SERVER !"
content = si.RetrieveContent()
host = get_obj(content, [vim.HostSystem], inputs['host_name'])
host_network_system = host.configManager.networkSystem
# for pnic in host.config.network.pnic:
# if pnic.device == inputs['nic_name']:
# pnic_key = pnic.key
create_vswitch(host_network_system, inputs['switch_name'], inputs['num_ports'], inputs['nic_name'])
create_port_group(host_network_system, inputs['port_group_name'], inputs['switch_name'])
#add_virtual_nic(host_network_system, inputs['port_group_name'])
except vmodl.MethodFault, e:
print "Caught vmodl fault: %s" % e.msg
return 1
except Exception, e:
print "Caught exception: %s" % str(e)
return 1
# Start program
if __name__ == "__main__":
main()