You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
2.8 KiB
Python
116 lines
2.8 KiB
Python
#!/usr/bin/env python
|
|
|
|
import pyVmomi
|
|
from pyVmomi import vim
|
|
from pyVmomi import vmodl
|
|
from pyVim.connect import SmartConnect, Disconnect, SmartConnectNoSSL
|
|
import atexit
|
|
import argparse
|
|
import getpass
|
|
import sys
|
|
import time
|
|
import json
|
|
|
|
|
|
def get_args():
|
|
parser = argparse.ArgumentParser(description='Arguments for talking to vCenter')
|
|
|
|
# extending disk arguments
|
|
parser.add_argument('-H', '--host',
|
|
required=True,
|
|
action='store',
|
|
help='vsphere host 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('-S', '--skip-ssl',
|
|
action='store_true',
|
|
default=False)
|
|
|
|
args = parser.parse_args()
|
|
|
|
if not args.password:
|
|
args.password = getpass.getpass(prompt='Enter password: ')
|
|
|
|
return args
|
|
|
|
|
|
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 is_disk(device):
|
|
if isinstance(device, vim.vm.device.VirtualDisk):
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
def main():
|
|
args = get_args()
|
|
|
|
if args.skip_ssl == True:
|
|
si = SmartConnectNoSSL(
|
|
host=args.host,
|
|
user=args.user,
|
|
pwd=args.password,
|
|
port=args.port)
|
|
else:
|
|
si = SmartConnect(
|
|
host=args.host,
|
|
user=args.user,
|
|
pwd=args.password,
|
|
port=args.port)
|
|
|
|
atexit.register(Disconnect, si)
|
|
content = si.RetrieveContent()
|
|
|
|
vm = get_obj(content, [vim.VirtualMachine], args.vm_name)
|
|
virtualDiskManager = si.content.virtualDiskManager
|
|
|
|
vm_disks = []
|
|
disks = []
|
|
|
|
for device in vm.config.hardware.device:
|
|
if is_disk(device):
|
|
vm_disks.append(device)
|
|
|
|
for disk in vm_disks:
|
|
disks.append(dict(
|
|
label = disk.deviceInfo.label,
|
|
uuid = disk.backing.uuid,
|
|
fullpath = disk.backing.fileName,
|
|
size = disk.capacityInBytes
|
|
))
|
|
|
|
print(json.dumps(disks))
|
|
|
|
main()
|