数据中心服务(Datacenter service)提供管理 vCenter Server 中数据中心的操作。
关键方法:Datacenter.create(spec)
spec是DatacenterCreateSpec,创建新数据中心有2个必填参数:
在 vCenter 清单中创建一个新的数据中心。
脚本:
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中可以看到有任务在执行。
关键方法:Datacenter.list()
返回有关 vCenter 中最多 1000 个可见(受权限检查)数据中心的信息(可以获取DC ID)。
脚本:
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)
脚本效果图:
关键方法:Datacenter.get("DC-ID")
检索有关数据中心对应的数据中心的信息。可以获得以下ID,DC-ID通过List DC获得:
脚本:
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)
脚本效果图:
关键方法:Datacenter.delete("DC-ID")
从 vCenter Server 中删除空数据中心。DC-ID通过List DC获得。
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自动化