当前位置: 首页 > article >正文

【vSphere | Python】vSphere Automation SDK for Python Ⅲ—— vCenter Datacenter APIs

目录

  • 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自动化


http://www.kler.cn/a/7584.html

相关文章:

  • 后端总指挥---文件接口
  • C++中特殊类设计/单例模式
  • 微信小程序navigateTo:fail webview count limit exceed
  • Python小游戏24——小恐龙躲避游戏
  • 卡尔曼滤波:从理论到应用的简介
  • element plus的表格内容自动滚动
  • 为什么无法跨centos、ubuntu、rocky linux 发行版本进行系统升级?
  • xinput1_3.dll缺失了如何去修复?xinput1_3.dll解决方法分享
  • 释放AIoT商业价值 | 2023高通广和通智能物联网技术开放日圆满落幕
  • srs流媒体录制视频
  • 22.SSM-JdbcTemplate总结
  • 贯穿设计模式第二话--开闭职责原则
  • 区块链学习笔记(3)BTC协议
  • 运算符重载
  • 亚马逊管理的14条领导力准则
  • C/C++协程编程:解锁并发编程新纪元
  • 【MySQL】了解MySQL的Explain,读这一篇够了( ̄∇ ̄)/
  • 【刷题笔记】笔记三
  • cuda学习4-6
  • Shell脚本之数组向函数传参
  • 理解 arp以及大致的原理 + 存在的安全隐患
  • 0115 用户管理
  • 关于TextureRender适配的解决方案
  • Sentinel入门使用
  • 第九章-DOM与CSS
  • Linux系统【centos7】怎么手动部署网站?