forked from vmware/pyvmomi-community-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_vm_nic_to_dvs.py
executable file
·170 lines (138 loc) · 5.03 KB
/
add_vm_nic_to_dvs.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
"""
Written by Luckylau
Github: https://github.com/Luckylau
Email: [email protected]
# Note: Example code For testing purposes only
"""
from pyVim.connect import SmartConnect, Disconnect
import atexit
from pyVmomi import vim
import sys
import argparse
import getpass
import ssl
def add_nic(vm, mac, port):
spec = vim.vm.ConfigSpec()
nic_changes = []
nic_spec = vim.vm.device.VirtualDeviceSpec()
nic_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
nic_spec.device = vim.vm.device.VirtualE1000()
nic_spec.device.deviceInfo = vim.Description()
nic_spec.device.deviceInfo.summary = 'vCenter API'
nic_spec.device.backing = \
vim.vm.device.VirtualEthernetCard.DistributedVirtualPortBackingInfo()
nic_spec.device.backing.port = vim.dvs.PortConnection()
nic_spec.device.backing.port.portgroupKey = port.portgroupKey
nic_spec.device.backing.port.switchUuid = port.dvsUuid
nic_spec.device.backing.port.portKey = port.key
nic_spec.device.connectable = vim.vm.device.VirtualDevice.ConnectInfo()
nic_spec.device.connectable.startConnected = True
nic_spec.device.connectable.allowGuestControl = True
nic_spec.device.connectable.connected = False
nic_spec.device.connectable.status = 'untried'
nic_spec.device.wakeOnLanEnabled = True
nic_spec.device.addressType = 'assigned'
nic_spec.device.macAddress = mac
nic_changes.append(nic_spec)
spec.deviceChange = nic_changes
e = vm.ReconfigVM_Task(spec=spec)
print("Nic card added success ...")
def get_obj(content, vimtype, 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 search_port(dvs, portgroupkey):
search_portkey = []
criteria = vim.dvs.PortCriteria()
criteria.connected = False
criteria.inside = True
criteria.portgroupKey = portgroupkey
ports = dvs.FetchDVPorts(criteria)
for port in ports:
search_portkey.append(port.key)
print(search_portkey)
return search_portkey[0]
def get_args():
parser = argparse.ArgumentParser(
description='Arguments for talking to vCenter')
parser.add_argument('-s', '--host',
required=True,
action='store',
help='vSphere service to connect to')
parser.add_argument('-o', '--port',
type=int,
default=443,
action='store',
help='Port to connect on')
parser.add_argument('-u', '--user',
required=True,
action='store',
help='User name to use')
parser.add_argument('-p', '--password',
required=False,
action='store',
help='Password to use')
parser.add_argument('-v', '--vm-name',
required=True,
action='store',
help='Name of the vm')
parser.add_argument('-pg', '--portgroup',
required=True,
action='store',
help='Port group to connect on')
parser.add_argument('-mac', '--macaddress',
required=True,
action='store',
help='Macadress of vm')
args = parser.parse_args()
if not args.password:
args.password = getpass.getpass(
prompt='Enter password')
return args
def port_find(dvs, key):
obj = None
ports = dvs.FetchDVPorts()
for c in ports:
if c.key == key:
obj = c
return obj
def main():
args = get_args()
context = None
if hasattr(ssl, "_create_unverified_context"):
context = ssl._create_unverified_context()
serviceInstance = SmartConnect(host=args.host,
user=args.user,
pwd=args.password,
port=args.port,
sslContext=context)
atexit.register(Disconnect, serviceInstance)
content = serviceInstance.RetrieveContent()
print("Search VDS PortGroup by Name ...")
portgroup = None
portgroup = get_obj(content,
[vim.dvs.DistributedVirtualPortgroup], args.portgroup)
if portgroup is None:
print("Portgroup not Found in DVS ...")
exit(0)
print("Search Available(Unused) port for VM...")
dvs = portgroup.config.distributedVirtualSwitch
portKey = search_port(dvs, portgroup.key)
port = port_find(dvs, portKey)
print("Search VM by Name ...")
vm = None
vm = get_obj(content, [vim.VirtualMachine], args.vm_name)
if vm:
print("Find Vm , Add Nic Card ...")
add_nic(vm, args.macaddress, port)
else:
print("Vm not Found ...")
if __name__ == '__main__':
sys.exit(main())