2

我正在使用 Openstack Mitaka。在 Django 中,我试图为租户获取所有资源。(这没关系)之后我需要了解资源类型。例如,如果它是一个实例或浮动IP等。

def sync_resources():
    logger.info("Executing sync_resources")
    sync_tenants()
    tenants = Tenant.objects.all()
    managers = Manager.objects.filter(is_active=True)
    for manager in managers:
        services = manager.services.all()
        regions = manager.region_set.all()
        for region in regions:
            ceilometer_driver = CeilometerDriver(region_name=region.name, **manager.ceilometer_params)
            if ceilometer_driver.is_authenticated:
                for tenant in tenants:
                    queries = [ceilometer_driver.make_query("project_id", ceilometer_driver.EQUAL, tenant.tenant_id)]
                    resource_list = ceilometer_driver.get_resource_list(query=queries)

在这段代码中,我获得了租户的所有资源。但是我不需要所有这些。我将根据类型列表过滤它们。如“Instance、floating_ip、volume、snapshot等”

这是一个示例资源详细信息: 示例资源 floating_ip

从元数据中我可以看到它是一个浮动 IP。但是,我无法在其类型浮动 ip 的详细信息中看到某处。不同类型资源的元数据会发生变化。

我还从终端检查了

$ ceilometer resource-list
$ ceilometer resource-show id-of-resource 

同样,细节没有帮助。

所以我需要一种方法来轻松理解资源类型并将它们与“实例、浮动 ip、卷、快照等”相匹配。

附加信息:

经过一些研究,我意识到我可以从链接信息中获取类型。这不适用于我在 ceilometer_driver 中使用的“ceilometerclient”。当我直接从 REST API 调用时,我可以获得额外的链接信息。

这是调用单个项目资源的 URL:

http://192.168.101.11:8777/v2/resources?q.field=project_id&q.value=63c40f56e11a4d24981710fd46285233

例如,下面的结果是一个示例。我删除了大部分链接。但是一个链接显示它是一个实例。

{
"user_id": "ec0d0110f44f42608814106a1f921a16",
"links": [
    {
        "href": "http://192.168.101.11:8777/v2/resources/e4d30a8e-e339-4c47-b71f-ec7061aaf113",
        "rel": "self"
    },
    {
        "href": "http://192.168.101.11:8777/v2/meters/instance?q.field=resource_id&q.value=e4d30a8e-e339-4c47-b71f-ec7061aaf113",
        "rel": "instance"
    },
    ...
],
"resource_id": "e4d30a8e-e339-4c47-b71f-ec7061aaf113",
"source": "openstack",
"last_sample_timestamp": "2018-03-15T14:18:15.368000",
"first_sample_timestamp": "2017-09-20T11:58:06.704000",
"project_id": "63c40f56e11a4d24981710fd46285233",
"metadata": {
    "instance_host": "node-6.reg1.test.skyatlas.com",
    "ephemeral_gb": "0",
    "flavor.vcpus": "1",
    "flavor.ephemeral": "0",
    "instance_id": "e4d30a8e-e339-4c47-b71f-ec7061aaf113",
    "display_name": "fuel service plugin",
    "state": "active",
    "flavor.ram": "2048",
    "status": "active",
    "ramdisk_id": "None",
    "flavor.name": "m1.small",
    "disk_gb": "20",
    "kernel_id": "None",
    "image.id": "4905099d-7912-4b00-b5bd-d8e1698dee92",
    "flavor.id": "2",
    "host": "b26c476c766c058541258e0c9275a738b889e0242be2af62fb331422",
    "OS-EXT-AZ.availability_zone": "test-1",
    "image.name": "Ubuntu 16.04.3 LTS (Xenial Xerus)",
    "image_ref_url": "http://192.168.101.11:8774/images/4905099d-7912-4b00-b5bd-d8e1698dee92",
    "name": "instance-00000037",
    "flavor.disk": "20",
    "root_gb": "20",
    "image.links": "[{'href': 'http://192.168.101.11:8774/images/4905099d-7912-4b00-b5bd-d8e1698dee92', 'rel': 'bookmark'}]",
    "memory_mb": "2048",
    "instance_type": "m1.small",
    "vcpus": "1",
    "image_ref": "4905099d-7912-4b00-b5bd-d8e1698dee92",
    "flavor.links": "[{'href': 'http://192.168.101.11:8774/flavors/2', 'rel': 'bookmark'}]"
    }
}

这是另一个浮动ip:

{
"user_id": null,
"links": [
    {
        "href": "http://192.168.101.11:8777/v2/resources/f61e437d-dce3-4562-bd48-e14a6f0604f3",
        "rel": "self"
    },
    {
        "href": "http://192.168.101.11:8777/v2/meters/ip.floating?q.field=resource_id&q.value=f61e437d-dce3-4562-bd48-e14a6f0604f3",
        "rel": "ip.floating"
    },
    {
        "href": "http://192.168.101.11:8777/v2/meters/ip.floating.create?q.field=resource_id&q.value=f61e437d-dce3-4562-bd48-e14a6f0604f3",
        "rel": "ip.floating.create"
    }
],
"resource_id": "f61e437d-dce3-4562-bd48-e14a6f0604f3",
"source": "openstack",
"last_sample_timestamp": "2018-03-15T14:13:01.808000",
"first_sample_timestamp": "2018-02-14T06:33:51.663000",
"project_id": "63c40f56e11a4d24981710fd46285233",
"metadata": {
    "status": "DOWN",
    "router_id": "None",
    "floating_network_id": "3a15b9a4-8f93-4622-80e6-d57cfee53b43",
    "fixed_ip_address": "None",
    "floating_ip_address": "192.168.103.132",
    "port_id": "None"
    }
}

所以一个实例有一个与“rel”的链接:“instance”,一个浮动IP有一个与“rel”的链接:“ip.floating”

我想我可以通过 REST API 调用而不是使用驱动程序来解决它们。然而,在我看来,这种所谓的解决方案似乎不是正确的方法。所以我仍然在寻找更好的方法。欢迎所有的答案和意见。

4

0 回答 0