openstack authentication api & curl

本文介绍如何使用 OpenStack API 进行身份验证并请求服务。通过 cURL 命令演示了获取认证令牌的过程,并展示了如何利用该令牌调用 Compute API 列出可用的实例规格(flavors)、调用 Glance API 列出镜像以及调用 Neutron API 列出网络。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2 Authentication and API request workflow

  1. Request an authentication token from the Identity endpoint that your cloud administrator gave you. Send a payload of credentials in the request as shown in Authenticate. If the request succeeds, the server returns an authentication token.
  2. Send API requests and include the token in the X-Auth-Token header. Continue to send API requests with that token until the service completes the request or the Unauthorized (401) error occurs.
  3. If the Unauthorized (401) error occurs, request another token.

3 Use OpenStack API through cURL

3.1 Request a token

Run this cURL command to request a token

curl -H "Content-type: application/json" -X 'POST' -d '{
    "auth": {
        "passwordCredentials": {
            "password": "admin",
            "username": "admin"
        },
        "tenantName": "admin"
    }
}' http://localhost:35357/v2.0/tokens | python -mjson.tool

and the output like the following:

{
    "access": {
        "metadata": {
            "is_admin": 0,
            "roles": [
                "afce989e7f1e4246820d29323f2c76e6"
            ]
        },
        "serviceCatalog": [
            {
                "endpoints": [
                    {
                        "adminURL": "http://192.168.0.82:8774/v2/183d952bcb5d4091b8eef32124e5e264",
                        "id": "52f25a2d300a45f18daff447e78067c8",
                        "internalURL": "http://192.168.0.82:8774/v2/183d952bcb5d4091b8eef32124e5e264",
                        "publicURL": "http://192.168.0.82:8774/v2/183d952bcb5d4091b8eef32124e5e264",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "nova",
                "type": "compute"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "http://192.168.0.82:9696",
                        "id": "75271cb2ef2a40898ab4802ec0ac8928",
                        "internalURL": "http://192.168.0.82:9696",
                        "publicURL": "http://192.168.0.82:9696",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "neutron",
                "type": "network"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "http://127.0.0.1:8774/v3",
                        "id": "168a33a7ff2f4e43856092f49cd6cc75",
                        "internalURL": "http://127.0.0.1:8774/v3",
                        "publicURL": "http://127.0.0.1:8774/v3",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "novav3",
                "type": "computev3"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "http://192.168.0.82:9292",
                        "id": "4810fc083d2d4c27831231e7b355f490",
                        "internalURL": "http://192.168.0.82:9292",
                        "publicURL": "http://192.168.0.82:9292",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "glance",
                "type": "image"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "http://192.168.0.82:8777",
                        "id": "1abb5859e80a4902b7a1c2632177d2f0",
                        "internalURL": "http://192.168.0.82:8777",
                        "publicURL": "http://192.168.0.82:8777",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "ceilometer",
                "type": "metering"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "http://192.168.0.82:8773/services/Admin",
                        "id": "3ceab16c395d4fe1b959bdd95056024f",
                        "internalURL": "http://192.168.0.82:8773/services/Cloud",
                        "publicURL": "http://192.168.0.82:8773/services/Cloud",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "nova_ec2",
                "type": "ec2"
            },
            {
                "endpoints": [
                    {
                        "adminURL": "http://192.168.0.82:35357/v2.0",
                        "id": "9033bd5012104c3eba54a6a10a08bc76",
                        "internalURL": "http://192.168.0.82:5000/v2.0",
                        "publicURL": "http://192.168.0.82:5000/v2.0",
                        "region": "RegionOne"
                    }
                ],
                "endpoints_links": [],
                "name": "keystone",
                "type": "identity"
            }
        ],
        "token": {
            "audit_ids": [
                "psuJvHNnThyzShOQPrxQSQ"
            ],
            "expires": "2016-05-10T04:52:53Z",
            "id": "6007a2d9d43d4aa2b260ff1c0c464941",
            "issued_at": "2016-05-10T03:52:53.345511",
            "tenant": {
                "description": "admin tenant",
                "enabled": true,
                "id": "183d952bcb5d4091b8eef32124e5e264",
                "name": "admin"
            }
        },
        "user": {
            "id": "773a82dc932142e882824888fbcc919a",
            "name": "admin",
            "roles": [
                {
                    "name": "admin"
                }
            ],
            "roles_links": [],
            "username": "admin"
        }
    }
}

3.2 Use the token and tenant to send API requests

3.2.1 Use the Compute API to list flavors
curl -H "X-Auth-Token:8767d3598eca41bda24d15ccf2bcfd7f" -H "Content-type: application/json" -X 'GET' \
> http://localhost:8774/v2/183d952bcb5d4091b8eef32124e5e264/flavors | python -mjson.tool

and the output:

{
    "flavors": [
        {
            "id": "1",
            "links": [
                {
                    "href": "http://localhost:8774/v2/183d952bcb5d4091b8eef32124e5e264/flavors/1",
                    "rel": "self"
                },
                {
                    "href": "http://localhost:8774/183d952bcb5d4091b8eef32124e5e264/flavors/1",
                    "rel": "bookmark"
                }
            ],
            "name": "m1.tiny"
        },
        {
            "id": "2",
            "links": [
                {
                    "href": "http://localhost:8774/v2/183d952bcb5d4091b8eef32124e5e264/flavors/2",
                    "rel": "self"
                },
                {
                    "href": "http://localhost:8774/183d952bcb5d4091b8eef32124e5e264/flavors/2",
                    "rel": "bookmark"
                }
            ],
            "name": "m1.small"
        },
        {
            "id": "3",
            "links": [
                {
                    "href": "http://localhost:8774/v2/183d952bcb5d4091b8eef32124e5e264/flavors/3",
                    "rel": "self"
                },
                {
                    "href": "http://localhost:8774/183d952bcb5d4091b8eef32124e5e264/flavors/3",
                    "rel": "bookmark"
                }
            ],
            "name": "m1.medium"
        },
        {
            "id": "4",
            "links": [
                {
                    "href": "http://localhost:8774/v2/183d952bcb5d4091b8eef32124e5e264/flavors/4",
                    "rel": "self"
                },
                {
                    "href": "http://localhost:8774/183d952bcb5d4091b8eef32124e5e264/flavors/4",
                    "rel": "bookmark"
                }
            ],
            "name": "m1.large"
        },
        {
            "id": "5",
            "links": [
                {
                    "href": "http://localhost:8774/v2/183d952bcb5d4091b8eef32124e5e264/flavors/5",
                    "rel": "self"
                },
                {
                    "href": "http://localhost:8774/183d952bcb5d4091b8eef32124e5e264/flavors/5",
                    "rel": "bookmark"
                }
            ],
            "name": "m1.xlarge"
        }
    ]
}
3.2.2 the Glane API to list images
curl -H "X-Auth-Token:8767d3598eca41bda24d15ccf2bcfd7f" -H "Content-type: application/json" \
-X 'GET' http://localhost:9292/v2/images | python -mjson.tool

and the output:

{
    "first": "/v2/images",
    "images": [
        {
            "checksum": "ee1eca47dc88f4879d8a229cc70a07c6",
            "container_format": "bare",
            "created_at": "2016-05-09T05:38:14Z",
            "disk_format": "qcow2",
            "file": "/v2/images/5cea5132-c93c-474a-92aa-e7872f2ec27c/file",
            "id": "5cea5132-c93c-474a-92aa-e7872f2ec27c",
            "min_disk": 0,
            "min_ram": 0,
            "name": "cirros",
            "owner": "183d952bcb5d4091b8eef32124e5e264",
            "protected": false,
            "schema": "/v2/schemas/image",
            "self": "/v2/images/5cea5132-c93c-474a-92aa-e7872f2ec27c",
            "size": 13287936,
            "status": "active",
            "tags": [],
            "updated_at": "2016-05-09T05:38:15Z",
            "virtual_size": null,
            "visibility": "public"
        }
    ],
    "schema": "/v2/schemas/images"
}
3.2.3 the Neutron API to list networks
curl -H "X-Auth-Token:8767d3598eca41bda24d15ccf2bcfd7f" -H "Content-type: application/json" \
-X 'GET' http://localhost:9696/v2.0/networks | python -mjson.tool

and the output:

{
    "networks": [
        {
            "admin_state_up": true,
            "id": "27a2d0a3-b5e6-4836-87a5-8a5211e54c57",
            "mtu": 0,
            "name": "test",
            "provider:network_type": "vlan",
            "provider:physical_network": "physnet0",
            "provider:segmentation_id": 58,
            "router:external": false,
            "shared": false,
            "status": "ACTIVE",
            "subnets": [
                "9d6d18e3-f402-4cb9-bbe0-60da1edd9cd7"
            ],
            "tenant_id": "183d952bcb5d4091b8eef32124e5e264"
        },
        {
            "admin_state_up": true,
            "id": "e0c6a1cc-0621-431e-abe0-b813763203fa",
            "mtu": 0,
            "name": "fred",
            "provider:network_type": "vlan",
            "provider:physical_network": "physnet0",
            "provider:segmentation_id": 300,
            "router:external": false,
            "shared": false,
            "status": "ACTIVE",
            "subnets": [],
            "tenant_id": "183d952bcb5d4091b8eef32124e5e264"
        }
    ]
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值