【问题标题】:Openstack API not providing precise dataOpenstack API 未提供精确数据
【发布时间】:2022-10-13 01:24:54
【问题描述】:

我在 CentOS 7.9 中使用 Openstack - Stein

我正在使用 python 收集有关 openstack nova 性能的数据,例如 openstack 项目中的服务器名称和 id,我创建了 3 个实例(服务器),我可以在 openstack cli 中看到所有三个实例,但是当我连接到中提到的 api 时openstack,它不提供数据或提供更少的数据。

我参考了 openstack 文档here

[root@centos-vm1 kavin(keystone_admin)]# openstack server list
+--------------------------------------+-----------------+--------+----------------------------------------+-------+----------+
| ID                                   | Name            | Status | Networks                               | Image | Flavor   |
+--------------------------------------+-----------------+--------+----------------------------------------+-------+----------+
| 08cf6226-0303-4b4c-ba53-10af79b81dae | test_instance_3 | ACTIVE | test_networ_3=10.150.0.8               |       | m1.tiny  |
| 9986f205-82b3-4cbb-bcdc-fb32eab97c83 | test_instance_1 | ACTIVE | test_networ_2=10.100.0.5, x.x.x.x      |       | m1.small |
| d1c0f520-8540-432c-8fe1-554390fd79bf | test_instance_2 | ACTIVE | test_networ_1=10.50.0.8                |       | m1.small |
+--------------------------------------+-----------------+--------+----------------------------------------+-------+----------+

我的python代码:

import requests,json
from six.moves.urllib.parse import urljoin

identity = {
    "methods": ["password"],
    "password": {
    "user": {
          "name": "admin",
          "domain": { "id": "default" },
          "password": "xxxxxxxxxxxxxxx"
        }
    }
}

OS_AUTH_URL = 'http://x.x.x.x:5000/v3'
data = {'auth': {'identity': identity}}
HEADERS = {'Content-Type': 'application/json', 'scope': 'unscoped'}

r = requests.post(
 OS_AUTH_URL+'/auth/tokens',
  headers = HEADERS,
  json    = data,     
  verify  = False
 )
auth_token = r.headers['X-Subject-Token']  # i got auth token

# server list
NOVA_URL="http://x.x.x.x:8774/v2.1"
HEADERS = {"X-Auth-Token" : str(auth_token)}
r = requests.get(
    NOVA_URL+'/servers',
    headers = HEADERS,
 )
r.raise_for_status()
print(r.json())

输出 :

{'servers': []}

帮助我,使用 api 调用收集准确的数据,谢谢

【问题讨论】:

    标签: rest openstack openstack-nova openstack-api


    【解决方案1】:

    根据 api-refList Serversdoc,也许你应该添加project scope在请求中。

    默认情况下,使用与经过身份验证的请求关联的项目 ID 过滤服务器。

    在我看来,你可以使用openstacksdk执行操作,只需使用Connection对象和list_servers方法。

    import openstack
    conn = openstack.connect(
        region_name='example-region',
        auth_url='http://x.x.x.x:5000/v3/',
        username='amazing-user',
        password='super-secret-password',
        project_id='33...b5',
        domain_id='05...03'
    )
    servers = conn.list_servers()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-02
      • 2019-11-03
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多