-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcenterApi.py
55 lines (51 loc) · 1.68 KB
/
vcenterApi.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
from pyVim.connect import SmartConnectNoSSL
from pyVmomi import vim
class vCenterAPIRequest:
def __init__(self, host, user, password):
try:
service_instance = SmartConnectNoSSL(
host=host,
user=user,
pwd=password
)
self.content = service_instance.RetrieveContent()
except Exception as e:
print(f"Failed to connect to vCenter: {str(e)}")
return None
def get_all_vms(self):
container = self.content.viewManager.CreateContainerView(
self.content.rootFolder,
[vim.VirtualMachine],
True
)
vms = container.view
container.Destroy()
return vms
def get_vms_from_site(self,site_name):
if not hasattr(self, 'sites'):
self.sites = self.get_sites()
# Find the specified site
site = next((s for s in self.sites if s.name == site_name), None)
if not site:
print(f"Site {site_name} not found")
return None
container = site.vmFolder
view_type = [vim.VirtualMachine]
recursive = True
vm_view = self.content.viewManager.CreateContainerView(
container, view_type, recursive
)
vms = vm_view.view
vm_view.Destroy()
return vms
def get_sites(self):
container = self.content.rootFolder
view_type = [vim.Datacenter]
recursive = True
dc_view = self.content.viewManager.CreateContainerView(
container, view_type, recursive
)
sites = dc_view.view
dc_view.Destroy()
self.sites = sites
return sites