【vSphere | Python】vSphere Automation SDK for Python Ⅴ—— vCenter Folder & Datastore & Cluster APIs

2023-05-16

目录

  • 6. vCenter Folder APIs
    • 6.1 List folder
  • 7. vCenter Datastore APIs
    • 7.1 List datastore
    • 7.2 Get datastore
  • 8. vCenter Cluster APIs
    • 8.1 List cluster
    • 8.2 Get cluster
  • 参考资料

6. vCenter Folder APIs

文件夹服务(Folder service)提供操作 vCenter Server 文件夹的操作。

6.1 List folder

关键方法Folder.list()

方法说明:返回有关 vCenter 中最多 1000 个可见文件夹的信息。

方法返回值

  • Floder ID:文件夹ID,文件夹的标识符。

  • Name:vCenter Server 文件夹的名称。

  • Type:类型。

    • DATACENTER :可以包含数据中心的文件夹。
    • DATASTORE :可以包含数据存储的文件夹。
    • HOST :可以包含计算资源(主机和集群)的文件夹。
    • NETWORK :可以包含网络的文件夹。
    • VIRTUAL_MACHINE :可以包含虚拟机的文件夹。

    可能的值有: DATACENTERDATASTOREHOSTNETWORKVIRTUAL_MACHINE

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client
start_time = time.time()
try:
    list_folder = vsphere_client.vcenter.Folder.list()
    print("Floder ID".ljust(40),"Type".ljust(50),"Name")
    for i in list_folder:
        print(i.folder.ljust(40),
               i.name.ljust(50),
               i.type,
              )
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)

脚本效果图:

在这里插入图片描述

7. vCenter Datastore APIs

数据存储服务(Datastore service)提供操作数据存储的操作。

7.1 List datastore

关键方法Datastore.list()

方法说明:列出数据存储 返回有关 vCenter 中最多 2500 个可见数据存储的信息。

方法返回值

  • datastore:数据存储的标识符。

  • name:数据存储名

  • type:枚举类型定义了vCenter数据存储的支持类型。

    • VMFS : VMware文件系统(仅ESX服务器)。
    • NFS: 网络文件系统v3(仅Linux和ESX服务器)。
    • NFS41:网络文件系统v4.1(仅Linux和ESX服务器)。
    • CIFS : 通用互联网文件系统。
    • VSAN : 虚拟SAN(仅ESX服务器)。
    • VFFS :Flash Read Cache(仅 ESX 服务器)。
    • VVOL : vSphere虚拟卷(仅ESX服务器)。

    枚举: vmfs, nfs, nfs41, cifs, vsan, vffs, vvol

  • capacity:数据存储的容量,以字节为单位。服务器会定期更新该值。

  • free_space:数据存储的可用空间,以字节为单位。服务器会定期更新该值。

脚本:

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client,byte_to_gb
start_time = time.time()
try:
      ds = vsphere_client.vcenter.Datastore.list()
      if ds == []:
          print("---------Empty---------")
      else:
          for i in ds:
              print("Datastore ID:".ljust(30),i.datastore,
                    "\nDatastore Name:".ljust(28),i.name,
                    "\nDatastore Type:".ljust(29),i.type,
                    "\nDatastore Free Space:".ljust(25),byte_to_gb(i.free_space),  #Byte
                    "\nDatastore Capacity".ljust(26), byte_to_gb(i.capacity),         #Byte
                    "\n=======================")
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:", run_time)

脚本效果图:

在这里插入图片描述

7.2 Get datastore

关键方法vcenter.Datastore.get('datastore-ID')

方法说明:检索指定数据存储的信息。

方法返回值

  • accessible:数据存储是否可访问。布尔值。

  • name:数据存储名。

  • thin_provisioning_supported:数据存储是否支持基于每个文件的精简制备。使用自动精简制备时,会延迟分配后备存储。布尔值。

  • type:枚举类型定义了vCenter数据存储的支持类型。

    • VMFS : VMware文件系统(仅ESX服务器)。
    • NFS: 网络文件系统v3(仅Linux和ESX服务器)。
    • NFS41:网络文件系统v4.1(仅Linux和ESX服务器)。
    • CIFS : 通用互联网文件系统。
    • VSAN : 虚拟SAN(仅ESX服务器)。
    • VFFS :Flash Read Cache(仅 ESX 服务器)。
    • VVOL : vSphere虚拟卷(仅ESX服务器)。

    枚举: vmfs, nfs, nfs41, cifs, vsan, vffs, vvol

  • free_space:数据存储的可用空间,以字节为单位。服务器会定期更新该值。

脚本:

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client,byte_to_gb
start_time = time.time()
try:
      get_ds = vsphere_client.vcenter.Datastore.get('datastore-68')
      print("Datastore Name:".ljust(38),get_ds.name,
      "\nDatastore Type:".ljust(40),get_ds.type,
      "\nAccessible:".ljust(44),get_ds.accessible,
      "\nDatastore Free Space:".ljust(37),byte_to_gb(get_ds.free_space) ,
      "\nMultiple Host Access:".ljust(37),get_ds.multiple_host_access,
      "\nThin Provisioning Supported:".ljust(33),get_ds.thin_provisioning_supported
            )
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)

脚本效果图:

在这里插入图片描述

8. vCenter Cluster APIs

集群服务(Cluster service)提供了在 vCenter Server 中管理集群的操作。

8.1 List cluster

关键方法Cluster.list()

方法说明:列出集群 返回有关 vCenter 中的最多 1000 个可见集群的信息。

方法返回值

  • cluster:集群的标识符。
  • drs_enabled:指示是否为群集启用 vSphere DRS 服务的标志。布尔值。
  • ha_enabled:指示是否为群集启用 vSphere HA 功能的标志。布尔值。
  • name:集群名
import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client

print("Cluster ID".ljust(33),"HA Enabled".ljust(20),"DRS Enabled".ljust(17),"Cluster Name")
start_time = time.time()
try:
    list_cluster = vsphere_client.vcenter.Cluster.list()
    for i in list_cluster:
        if i.ha_enabled == True:
            i.ha_enabled = "True"
        else:
            i.ha_enabled = "False"
        if i.drs_enabled == True:
            i.drs_enabled = "True"
        else:
            i.drs_enabled = "False"
        print(i.cluster.ljust(25),
              i.ha_enabled.ljust(25),
              i.drs_enabled.ljust(25),
              i.name)

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(30), run_time)

脚本效果图:

在这里插入图片描述

8.2 Get cluster

关键方法Cluster.get(cluster="domain-ID")

方法说明:检索指定集群对应的集群的信息。

方法返回值

  • name:集群名
  • resource_pool:集群的根资源池的标识符。

脚本:

import time
from vSphere_Automation_SDK.Connect_to_vCenter_Server import vsphere_client

start_time = time.time()
try:
    get_cluster = vsphere_client.vcenter.Cluster.get(cluster="domain-c13239852")
    print("Name:".ljust(30),get_cluster.name,
          "\nResource Pool:".ljust(27),get_cluster.resource_pool)

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(30), run_time)

脚本效果图:

在这里插入图片描述

参考资料

vCenter REST APIs v7.0U3
vSphere-Python-Automation-Scripts/v1/

关于本专栏其它博文,请关注专栏,会有更多关于vSphere Python自动化的内容:vSphere python自动化

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【vSphere | Python】vSphere Automation SDK for Python Ⅴ—— vCenter Folder & Datastore & Cluster APIs 的相关文章

  • 通过 add_subplot 添加子图后如何共享轴?

    我有一个像这样的数据框 df pd DataFrame A 0 3 0 2 0 5 0 2 B 0 1 0 0 0 3 0 1 C 0 2 0 5 0 0 0 7 D 0 6 0 3 0 4 0 6 index list abcd A B
  • 键入的完整命令行

    我想获得输入时的完整命令行 This join sys argv 在这里不起作用 删除双引号 另外 我不想重新加入已解析和拆分的内容 有任何想法吗 你太迟了 当键入的命令到达 Python 时 您的 shell 已经发挥了它的魔力 例如 引
  • 无法在 virtualenv 中安装 libxml2

    我有一个问题libxml2蟒蛇模块 我正在尝试将其安装在python3 虚拟环境使用以下命令 pip install libxml2 python3 但它显示以下错误 Collecting libxml2 python3 Using cac
  • 查找正在导入哪些 python 模块

    从应用程序中使用的特定包中查找所有 python 模块的简单方法是什么 sys modules是将模块名称映射到模块的字典 您可以检查其键以查看导入的模块 See http docs python org library sys html
  • 如何从hdfs读取文件[重复]

    这个问题在这里已经有答案了 我在 project1目录下的hadoop文件系统中有一个文本文件名mr txt 我需要编写 python 代码来读取文本文件的第一行 而不将 mr txt 文件下载到本地 但我无法从 hdfs 打开 mr tx
  • 为什么 re.findall 在查找字符串中的三元组项时不具体。 Python

    所以我有四行代码 seq ATGGAAGTTGGATGAAAGTGGAGGTAAAGAGAAGACGTTTGA OR 0 re findall r ATG 9 TAA TAG TGA seq 首先让我解释一下我正在尝试做什么 如果这令人困惑
  • Py2exe - Pmw WindowsError:[错误 3]

    我正在尝试使用 Py2exe 构建独立的可执行文件 我已经导入了 Pmw 类 当我运行独立可执行文件时 出现以下错误 Traceback most recent call last File py line 9 in
  • 高级描述熊猫

    有没有像 pandas 那样更高级的功能 通常我会继续这样 r pd DataFrame np random randn 1000 columns A r describe 我会得到一份很好的总结 就像这样 A count 1000 000
  • 数据框中 .map(str) 和 .astype(str) 有什么区别

    我有一个数据框 其列名为 col1 和 col2 的整数类型条目 我想将 col1 和 col2 的条目以及其间的 点 连接起来 我搜索并发现添加两个列条目 df col df col1 map str df col2 map str 并添
  • 更改 Matplotlib 投影轴的背景颜色

    我正在尝试使用 Cartopy 创建一个图形 该图形需要在未投影的轴上绘制投影轴 这是一个尽可能简单的代码版本 它将轴上的内容替换为背景颜色 import matplotlib pyplot as plt import cartopy cr
  • 使用 if 语句的网格网格和用户定义函数的真值不明确

    假设我有一个函数f x y 足够光滑 然而 有些值仅在有限的意义上存在 以sin x x的价值x 0只存在于极限 x gt 0 中 在一般情况下 我用一个来处理这个问题if陈述 如果我在情节中使用它meshgrid我收到一条错误消息 Val
  • Python:在字典中查找具有唯一值的键?

    我收到一个字典作为输入 并且想要返回一个键列表 其中字典值在该字典的范围内是唯一的 我将用一个例子来澄清 假设我的输入是字典 a 构造如下 a dict a cat 1 a fish 1 a dog 2 lt unique a bat 3
  • 从 wxPython 事件处理程序中调用函数

    我正在努力寻找一种在 wxPython 事件处理函数中使用函数的方法 假设我有一个按钮 单击该按钮时 它会使用事件处理程序运行一个名为 OnRun 的函数 但是 用户忘记单击 OnRun 按钮之前的 RadionButton 我想弹出一个
  • Python 2.7 缩进错误[关闭]

    Closed 这个问题不符合堆栈溢出指南 help closed questions 目前不接受答案 这个问题是由拼写错误或无法再重现的问题引起的 虽然类似的问题可能是on topic help on topic在这里 这个问题的解决方式不
  • Python:如何在不先创建整个列表的情况下计算列表的总和?

    通常我们必须 1 声明一个列表 2 使用以下方法计算该列表的总和sum 但现在我希望指定一个以 1 开头 间隔为 4 100 个元素的列表 如下所示 1 5 9 13 17 21 25 29 33 37 我不想涉及数学公式 所以 1 如何在
  • 没有名为“turtle”的模块

    我正在学习并尝试用Python3制作贪吃蛇游戏 我正在进口海龟 我正在使用 Linux mint 19 PyCharm python37 python3 tk Traceback most recent call last File hom
  • pandas.read_fwf 忽略提供的数据类型

    我正在从文本文件导入数据框 我想指定列的数据类型 但 pandas 似乎忽略了dtype input 一个工作示例 from io import StringIO import pandas as pd string USAF WBAN S
  • 使用 MPI 的 Allreduce 对 Python 对象求和

    我正在使用使用 Python 中的字典和计数器构建的稀疏张量数组操作 我想让并行使用这个数组操作成为可能 最重要的是 我最终在每个节点上都有计数器 我想使用 MPI Allreduce 或另一个不错的解决方案 将其添加在一起 例如 使用计数
  • 为boost python编译的.so找不到模块

    我正在尝试将 C 代码包装到 python 中 只需一个类即可导出两个函数 我编译为map so 当我尝试时import map得到像噪音一样的错误 Traceback most recent call last File
  • 如何使用 Python/Django 在 Facebook 中获取(和使用)扩展权限

    我正在尝试编写一个简单的应用程序 让用户授予我的代码写入其页面的 Facebook 流的权限 据我了解 它应该很简单 让用户单击一个按钮 启动一个弹出窗口 其中包含我的 Facebook 应用程序中的页面 在该页面中 他们单击授予的内容流发

随机推荐