-
Notifications
You must be signed in to change notification settings - Fork 768
/
vsanapiutils.py
276 lines (251 loc) · 12 KB
/
vsanapiutils.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright (c) 2016-2024 Broadcom. All Rights Reserved.
The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
This module defines basic helper functions for vSAN-based development.
"""
__author__ = 'Broadcom, Inc'
import sys
import ssl
if (sys.version_info[0] == 3):
from urllib.request import urlopen
else:
from urllib2 import urlopen
from xml.dom import minidom
from pyVmomi import vim, vmodl, SoapStubAdapter, VmomiSupport
# Import the vSAN API python bindings
import pyVmomi
import vsanmgmtObjects
VSAN_API_VC_SERVICE_ENDPOINT = '/vsanHealth'
VSAN_API_ESXI_SERVICE_ENDPOINT = '/vsan'
VSAN_VMODL_VERSION = "vsan.version.version3"
# Construct a stub for vSAN API access using vCenter or ESXi sessions from
# existing stubs. Corresponding vCenter or ESXi service endpoint is required.
# vCenter service endpoint is used by default.
def valid_ipv6(addr):
import socket
try:
socket.inet_pton(socket.AF_INET6, addr)
except socket.error:
return False
return True
def _GetVsanStub(
stub, endpoint=VSAN_API_VC_SERVICE_ENDPOINT,
context=None, version='vim.version.version11'
):
index = stub.host.rfind(':')
if valid_ipv6(stub.host[:index][1:-1]):
hostname = stub.host[:index][1:-1]
else:
hostname = stub.host[:index]
port = int(stub.host.split(":")[-1])
vsanStub = SoapStubAdapter(
host=hostname,
port=port,
path=endpoint,
version=version,
sslContext=context
)
vsanStub.cookie = stub.cookie
return vsanStub
# Construct a stub for access vCenter side vSAN APIs.
def GetVsanVcStub(stub, context=None, version=VSAN_VMODL_VERSION):
return _GetVsanStub(stub, endpoint=VSAN_API_VC_SERVICE_ENDPOINT,
context=context, version=version)
# Construct a stub for access ESXi side vSAN APIs.
def GetVsanEsxStub(stub, context=None, version=VSAN_VMODL_VERSION):
return _GetVsanStub(stub, endpoint=VSAN_API_ESXI_SERVICE_ENDPOINT,
context=context, version=version)
# Construct a stub for access ESXi side vSAN APIs.
def GetVsanVcMos(vcStub, context=None, version=VSAN_VMODL_VERSION):
vsanStub = GetVsanVcStub(vcStub, context, version=version)
vcMos = {
'vsan-disk-management-system' : vim.cluster.VsanVcDiskManagementSystem(
'vsan-disk-management-system',
vsanStub
),
'vsan-stretched-cluster-system' : vim.cluster.VsanVcStretchedClusterSystem(
'vsan-stretched-cluster-system',
vsanStub
),
'vsan-cluster-config-system' : vim.cluster.VsanVcClusterConfigSystem(
'vsan-cluster-config-system',
vsanStub
),
'vsan-performance-manager' : vim.cluster.VsanPerformanceManager(
'vsan-performance-manager',
vsanStub
),
'vsan-cluster-health-system' : vim.cluster.VsanVcClusterHealthSystem(
'vsan-cluster-health-system',
vsanStub
),
'vsan-upgrade-systemex' : vim.VsanUpgradeSystemEx(
'vsan-upgrade-systemex',
vsanStub
),
'vsan-cluster-space-report-system' : vim.cluster.VsanSpaceReportSystem(
'vsan-cluster-space-report-system',
vsanStub
),
'vsan-cluster-object-system' : vim.cluster.VsanObjectSystem(
'vsan-cluster-object-system',
vsanStub
),
'vsan-cluster-iscsi-target-system' : vim.cluster.VsanIscsiTargetSystem(
'vsan-cluster-iscsi-target-system',
vsanStub
),
'vsan-vcsa-deployer-system' : vim.host.VsanVcsaDeployerSystem(
'vsan-vcsa-deployer-system',
vsanStub
),
'vsan-vds-system' : vim.vsan.VsanVdsSystem('vsan-vds-system', vsanStub),
'vsan-vc-capability-system' : vim.cluster.VsanCapabilitySystem(
'vsan-vc-capability-system', vsanStub),
'vsan-mass-collector' : vim.VsanMassCollector('vsan-mass-collector',
vsanStub),
'vsan-phonehome-system' : vim.VsanPhoneHomeSystem('vsan-phonehome-system',
vsanStub),
'vsan-vum-system' : vim.cluster.VsanVumSystem('vsan-vum-system', vsanStub),
'vsan-cluster-resource-check-system': vim.vsan.VsanResourceCheckSystem(
'vsan-cluster-resource-check-system',
vsanStub),
'cns-volume-manager': vim.cns.VolumeManager('cns-volume-manager',
vsanStub
),
'vsan-cluster-file-service-system': vim.vsan.VsanFileServiceSystem(
'vsan-cluster-file-service-system',
vsanStub
),
'vsan-remote-datastore-system': vim.cluster.VsanRemoteDatastoreSystem(
'vsan-remote-datastore-system',
vsanStub),
'vsan-cluster-ioinsight-manager': vim.cluster.VsanIoInsightManager(
'vsan-cluster-ioinsight-manager',
vsanStub),
'vsan-cluster-diagnostics-system': vim.cluster.VsanDiagnosticsSystem(
'vsan-cluster-diagnostics-system',
vsanStub),
'vsan-cluster-power-system': vim.cluster.VsanClusterPowerSystem(
'vsan-cluster-power-system',
vsanStub),
}
return vcMos
# Construct a stub for access ESXi side vSAN APIs.
def GetVsanEsxMos(esxStub, context=None, version=VSAN_VMODL_VERSION):
vsanStub = GetVsanEsxStub(esxStub, context, version=version)
esxMos = {
'vsan-performance-manager' : vim.cluster.VsanPerformanceManager(
'vsan-performance-manager',
vsanStub
),
'vsan-cluster-health-system' : vim.cluster.VsanVcClusterHealthSystem(
'vsan-cluster-health-system',
vsanStub
),
'ha-vsan-health-system' : vim.host.VsanHealthSystem(
'ha-vsan-health-system',
vsanStub
),
'vsan-object-system' : vim.cluster.VsanObjectSystem(
'vsan-object-system',
vsanStub
),
'vsan-vcsa-deployer-system' : vim.host.VsanVcsaDeployerSystem(
'vsan-vcsa-deployer-system',
vsanStub
),
'vsan-capability-system' : vim.cluster.VsanCapabilitySystem(
'vsan-capability-system', vsanStub),
'vsanSystem' : vim.host.VsanSystem('vsanSystem', vsanStub),
'vsanSystemEx' : vim.host.VsanSystemEx('vsanSystemEx', vsanStub),
'vsan-update-manager' : vim.host.VsanUpdateManager('vsan-update-manager',
vsanStub),
'vsan-cluster-iscsi-target-system' : vim.cluster.VsanIscsiTargetSystem(
'vsan-cluster-iscsi-target-system',
vsanStub
),
'vsan-host-ioinsight-manager': vim.cluster.VsanIoInsightManager(
'vsan-host-ioinsight-manager',
vsanStub),
}
return esxMos
# Convert a vSAN Task to a Task MO binding to vCenter service.
def ConvertVsanTaskToVcTask(vsanTask, vcStub):
vcTask = vim.Task(vsanTask._moId, vcStub)
return vcTask
# Wait for the vCenter task and returns after tasks are completed.
def WaitForTasks(tasks, si):
pc = si.content.propertyCollector
taskList = [str(task) for task in tasks]
# Create filter
objSpecs = [vmodl.query.PropertyCollector.ObjectSpec(obj=task)
for task in tasks]
propSpec = vmodl.query.PropertyCollector.PropertySpec(
type=vim.Task, pathSet=[], all=True)
filterSpec = vmodl.query.PropertyCollector.FilterSpec()
filterSpec.objectSet = objSpecs
filterSpec.propSet = [propSpec]
filter = pc.CreateFilter(filterSpec, True)
try:
version, state = None, None
# Loop looking for updates till the state moves to a completed state.
while len(taskList):
update = pc.WaitForUpdates(version)
for filterSet in update.filterSet:
for objSet in filterSet.objectSet:
task = objSet.obj
for change in objSet.changeSet:
if change.name == 'info':
state = change.val.state
elif change.name == 'info.state':
state = change.val
else:
continue
if not str(task) in taskList:
continue
if state == vim.TaskInfo.State.success:
# Remove task from taskList
taskList.remove(str(task))
elif state == vim.TaskInfo.State.error:
raise task.info.error
# Move to next version
version = update.version
finally:
if filter:
filter.Destroy()
def getVsanVersionFromNamespace(versionId, localVersion):
versionKey = "%s/%s" % ("vsan", str(versionId))
remoteVersion = VmomiSupport.versionMap.get(versionKey, None)
if not remoteVersion:
return None
if VmomiSupport.IsChildVersion(localVersion, remoteVersion):
return remoteVersion
# Get the VMODL version by checking the existence of vSAN namespace.
def GetLatestVmodlVersion(hostname, port=443):
try:
if valid_ipv6(hostname):
hostname = "[%s]" % hostname
vsanVmodlUrl = 'https://%s:%s/sdk/vsanServiceVersions.xml' % (hostname, str(port))
if (hasattr(ssl, '_create_unverified_context') and
hasattr(ssl, '_create_default_https_context')):
ssl._create_default_https_context = ssl._create_unverified_context
localVersion = VmomiSupport.newestVersions.GetName("vsan")
xmldoc = minidom.parse(urlopen(vsanVmodlUrl, timeout=5))
for element in xmldoc.getElementsByTagName('name'):
if (element.firstChild.nodeValue == "urn:vsan"):
versions = xmldoc.getElementsByTagName('version')
for ver in versions:
versionId = ver.firstChild.nodeValue
version = getVsanVersionFromNamespace(versionId, localVersion)
if version:
return version
return localVersion
else:
return VmomiSupport.newestVersions.GetName('vim')
except Exception as e:
# Any exception like failing to open the XML or failed to parse the
# the content should lead to the returning of namespace with vim.
return VmomiSupport.newestVersions.GetName('vim')