目录
- 5. vCenter Datacenter APIs
- 操作
- 5.1 Create Datacenter
- 5.2 List Datacenter
- 5.3 Get Datacenter
- 5.4 Delete Datacenter
- 参考资料
5. vCenter Datacenter APIs
数据中心服务(Datacenter service)提供管理 vCenter Server 中数据中心的操作。
操作
- Create Datacenter:在 vCenter 清单中创建一个新的数据中心。
- List Datacenter:返回有关 vCenter 中最多 1000 个可见数据中心的信息。
- Get Datacenter:检索有关数据中心对应的数据中心的信息。
- Delete Datacenter:从 vCenter Server 中删除空数据中心。
5.1 Create Datacenter
关键方法:Datacenter.create(spec)
spec是DatacenterCreateSpec,创建新数据中心有2个必填参数:
- folder:类型为数据中心的文件夹ID(Floder ID ),该值每个vCenter中都会有一个,通过List Floder方法查看。
- name:新数据中心名。
在 vCenter 清单中创建一个新的数据中心。
脚本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : CreateDatacenter.py
# @Author: Yao Zhang
# @Date : 2023/4/2 16:37
# @Desc :
# @Contact : nowshut@qq.com
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
spec ={
"folder": "group-d1", #通过list folder查询Type为Datacenters的folder ID
"name": "DC3"
}
start_time = time.time()
try:
create_dc = vsphere_client.vcenter.Datacenter.create(spec)
print("Created successfully!")
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(43), run_time)
在vSphere Client中可以看到有任务在执行。
5.2 List Datacenter
关键方法:Datacenter.list()
返回有关 vCenter 中最多 1000 个可见(受权限检查)数据中心的信息(可以获取DC ID)。
脚本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : ListDatacenter.py
# @Author: Yao Zhang
# @Date : 2023/4/2 18:36
# @Desc :
# @Contact : nowshut@qq.com
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
print("Datacenter ID".ljust(35),"Name")
start_time = time.time()
try:
list_dc = vsphere_client.vcenter.Datacenter.list()
if list_dc == []:
print("---------Empty---------")
else:
for i in list_dc:
print(i.datacenter.ljust(34),
i.name)
print("-----------End-----------")
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(37), run_time)
脚本效果图:
5.3 Get Datacenter
关键方法:Datacenter.get("DC-ID")
检索有关数据中心对应的数据中心的信息。可以获得以下ID,DC-ID通过List DC获得:
- Datacenter Folder
- Host Folder
- Network Folder
- VM Folder
脚本:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : GetDatacenter.py
# @Author: Yao Zhang
# @Date : 2023/4/2 18:36
# @Desc :
# @Contact : nowshut@qq.com
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
start_time = time.time()
try:
get_dc = vsphere_client.vcenter.Datacenter.get("datacenter-3")
print("Name:".ljust(35),get_dc.name,
"\nDatacenter Folder:".ljust(30),get_dc.datastore_folder,
"\nHost Folder:".ljust(34),get_dc.host_folder,
"\nNetwork Folder:".ljust(31),get_dc.network_folder,
"\nVM Folder:".ljust(34),get_dc.vm_folder)
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(34), run_time)
脚本效果图:
5.4 Delete Datacenter
关键方法:Datacenter.delete("DC-ID")
从 vCenter Server 中删除空数据中心。DC-ID通过List DC获得。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : DeleteDatacenter.py
# @Author: Yao Zhang
# @Date : 2023/4/2 18:35
# @Desc :
# @Contact : nowshut@qq.com
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
start_time = time.time()
try:
delete_dc = vsphere_client.vcenter.Datacenter.delete("datacenter-1001")
print("Deleted successfully!")
except Exception as err:
for i in err.messages:
id = i.id,
default_message = i.default_message
args = i.args
params = i.params
localized = i.localized
print("\033[1;31m Encountered an error, Please see the following information \033[0m",
"\n\tError Class:", id,
"\n\tMessage:", default_message,
"\n\tArgs:", args,
"\n\tParams:", params,
"\n\tLocalized:", localized,
"\nError Data:", err.data,
"\nError Type:", err.error_type
)
end_time = time.time()
run_time = end_time - start_time
print("Used Time:".ljust(43), run_time)
在vSphere Client中可以看到有在执行删除数据中心的任务,此时DC3已经不在清单中。
参考资料
vCenter REST APIs v7.0U3
vSphere-Python-Automation-Scripts/v1/vCenter-Datacenter
关于本专栏其它博文,请关注专栏,会有更多关于vSphere Python自动化的内容:vSphere python自动化