Python OpenStack SDK 没有 return Hypervisor 主机名

Python OpenStack SDK does not return Hypervisor hostname

我写了一个客户端代码如下

# Get Hypervisor details
try:
    print(f"+----------------------------------------------------------+")
    print(f"| Hypervisor details                                       |")
    print(f"+----------------------------------------------------------+")
    for hypervisor in conn.list_hypervisors():
        print(f"Hypervisor is: {hypervisor}")
        print(f"Hypervisor status:      {hypervisor.status}")
        print(f"Hypervisor vcpus:       {hypervisor.vcpus_used}")
        print(f"Hypervisor type:        {hypervisor.hypervisor_type}")
        print(f"Hypervisor id:          {hypervisor.id}")
        print(f"Hypervisor state:       {hypervisor.state}")
        print(f"Hypervisor host IP:     {hypervisor.host_ip}")
        print(f"Hypervisor running VMs: {hypervisor.running_vms}")
        print(f"Hypervisor running VMs: {hypervisor.vcpus}")
        print(f"Hypervisor hostname:    {hypervisor.hypervisor_hostname}")

        print(f"")
except EndpointNotFound as exp:
    print("Error: {}".format(exp))
    print(f"")

当我打印 'hypervisor' 对象本身时,我看到了以下详细信息(我已经格式化以便更好地阅读)。

openstack.compute.v2.hypervisor.Hypervisor
(
  status=enabled, 
  service=
  {
    'host': 'cl3-Compute3', 
    'disabled_reason': None, 
    'id': '34db399d-25b9-4004-98b9-2d8bbce7b228'
  }, 
  vcpus_used=0, 
  hypervisor_type=QEMU, 
  local_gb_used=0, 
  vcpus=52, 
  hypervisor_hostname=cl3-Compute3, 
  memory_mb_used=25600, 
  memory_mb=262032, 
  current_workload=0, 
  state=up, 
  host_ip=192.168.10.116, 
  cpu_info=
  {
    'arch': 'x86_64', 
    'model': 'Broadwell', 
    'vendor': 'Intel', 
    'features': 
    [
      'pge', 'avx', 'xsaveopt', 'clflush', 'sep', 'rtm', 'tsc_adjust', 'tsc-deadline', 'dtes64', 'invpcid', 'tsc', 'fsgsbase', 'xsave', 'smap', 'vmx', 'erms', 'xtpr', 'cmov', 'hle', 'smep', 'pcid', 'est', 'pat', 'monitor', 'smx', 'pbe', 'lm', 'msr', 'adx', '3dnowprefetch', 'nx', 'fxsr', 'syscall', 'tm', 'sse4.1', 'pae', 'sse4.2', 'pclmuldq', 'acpi', 'fma', 'vme', 'popcnt', 'mmx', 'osxsave', 'cx8', 'mce', 'de', 'rdtscp', 'ht', 'dca', 'lahf_lm', 'abm', 'rdseed', 'pdcm', 'mca', 'pdpe1gb', 'apic', 'sse', 'f16c', 'pse', 'ds', 'invtsc', 'pni', 'tm2', 'avx2', 'aes', 'sse2', 'ss', 'ds_cpl', 'arat', 'bmi1', 'bmi2', 'ssse3', 'fpu', 'cx16', 'pse36', 'mtrr', 'movbe', 'rdrand', 'x2apic'
    ], 
    'topology': 
    {
      'cores': 14, 
      'cells': 2, 
      'threads': 2, 
      'sockets': 1
    }
  }, 
  running_vms=0, 
  free_disk_gb=852, 
  hypervisor_version=2012000, 
  disk_available_least=849, 
  local_gb=852, 
  free_ram_mb=236432, 
  id=1130407d-5afd-41f9-bc0c-2d7babda3ce7, 
  location=Munch
  (
    {
      'cloud': 'defaults', 'region_name': 'RegionOne', 'zone': None, 'project': Munch({'id': '6d4f1b107bbf4b94832f7ef72df61ef2', 'name': '5g', 'domain_id': None, 'domain_name': 'default'})
    }
  )
)

大多数值如下所示打印,但是当我尝试访问 'hypervisor.hypervisor_hostname' 时,它抛出异常。

+----------------------------------------------------------+
| Hypervisor details                                       |
+----------------------------------------------------------+
Hypervisor is: openstack.compute.v2.hypervisor.Hypervisor(status=enabled, service={'host': 'cl3-Compute3', 'disabled_reason': None, 'id': '34db399d-25b9-4004-98b9-2d8bbce7b228'}, vcpus_used=0, hypervisor_type=QEMU, local_gb_used=0, vcpus=52, hypervisor_hostname=cl3-Compute3, memory_mb_used=25600, memory_mb=262032, current_workload=0, state=up, host_ip=192.168.10.116, cpu_info={'arch': 'x86_64', 'model': 'Broadwell', 'vendor': 'Intel', 'features': ['pge', 'avx', 'xsaveopt', 'clflush', 'sep', 'rtm', 'tsc_adjust', 'tsc-deadline', 'dtes64', 'invpcid', 'tsc', 'fsgsbase', 'xsave', 'smap', 'vmx', 'erms', 'xtpr', 'cmov', 'hle', 'smep', 'pcid', 'est', 'pat', 'monitor', 'smx', 'pbe', 'lm', 'msr', 'adx', '3dnowprefetch', 'nx', 'fxsr', 'syscall', 'tm', 'sse4.1', 'pae', 'sse4.2', 'pclmuldq', 'acpi', 'fma', 'vme', 'popcnt', 'mmx', 'osxsave', 'cx8', 'mce', 'de', 'rdtscp', 'ht', 'dca', 'lahf_lm', 'abm', 'rdseed', 'pdcm', 'mca', 'pdpe1gb', 'apic', 'sse', 'f16c', 'pse', 'ds', 'invtsc', 'pni', 'tm2', 'avx2', 'aes', 'sse2', 'ss', 'ds_cpl', 'arat', 'bmi1', 'bmi2', 'ssse3', 'fpu', 'cx16', 'pse36', 'mtrr', 'movbe', 'rdrand', 'x2apic'], 'topology': {'cores': 14, 'cells': 2, 'threads': 2, 'sockets': 1}}, running_vms=0, free_disk_gb=852, hypervisor_version=2012000, disk_available_least=849, local_gb=852, free_ram_mb=236432, id=1130407d-5afd-41f9-bc0c-2d7babda3ce7, location=Munch({'cloud': 'defaults', 'region_name': 'RegionOne', 'zone': None, 'project': Munch({'id': '6d4f1b107bbf4b94832f7ef72df61ef2', 'name': '5g', 'domain_id': None, 'domain_name': 'default'})}))
Hypervisor status: enabled
Hypervisor vcpus: 0
Hypervisor type:     QEMU
Hypervisor id: 1130407d-5afd-41f9-bc0c-2d7babda3ce7
Hypervisor state: up
Hypervisor host IP: 192.168.10.116
Hypervisor running VMs: 0
Hypervisor running VMs: 52
Traceback (most recent call last):
  File "/Users/workspace/testprogs/python/opsviz/ops_worker.py", line 321, in <module>
    main()
  File "/Users/workspace/testprogs/python/opsviz/ops_worker.py", line 317, in main
    print_servers()
  File "/Users/workspace/testprogs/python/opsviz/ops_worker.py", line 115, in print_servers
    print(f"Hypervisor hostname: {hypervisor.hypervisor_hostname}")
  File "/Users/inventory/lib/python3.7/site-packages/openstack/resource.py", line 588, in __getattribute__
    raise e
  File "/Users/inventory/lib/python3.7/site-packages/openstack/resource.py", line 582, in __getattribute__
    return object.__getattribute__(self, name)
AttributeError: 'Hypervisor' object has no attribute 'hypervisor_hostname'

我查看了python-openstacksdk的代码。在文件 openstack/compute/v2/hypervisor.py 的 class Hypervisor 中,class 的构造函数中有以下几行:

class Hypervisor(resource.Resource):
...
#: Name of hypervisor
name = resource.Body('hypervisor_hostname')

REST-API-响应的值 hypervisor_hostname 映射到 name,因此请尝试访问此值。

另见源代码:https://github.com/openstack/openstacksdk/blob/master/openstack/compute/v2/hypervisor.py#L39