A22. openstack架构实战-openstack的api

使用openstack的api之前必须获取openstack的token:

id就是openstack的token:

方法一:

[root@controller01 ~]# openstack token issue

[root@controller01 ~]# token=`openstack token issue | awk "NR==5"'{print $4}'`
[root@controller01 ~]# echo "$token"
gAAAAABe3uPnlxcX1u14e0fSZVwCL-JKg3pgv3ezsoeoapZRCLZwzVJ0cNo-xqK6TAturyknfMgj0IvQnuS91UvxNfKoc0YGGCaLTWi-2__XEKyT0kqZ3cLDtuO7Ni0ylvrIF2UT-qsBBnxf_GhkdaYxbSoqhcSv7e7jJGRorSn2IJu08hEpOzc

 

方法二:

curl -i -X POST -H 'Content-Type: application/json' \
-d '{
"auth": {
"identity": {
"methods": ["password"],
"password": {
"user": {
"domain": {
"name": "default"
},
"name": "admin",
"password": "huayun"
}
}
},
"scope": {
"project": {
"domain": {
"name": "default"
},
"name": "admin"
}

}
}
}' http://10.100.201.201:5000/v3/auth/tokens

 

 

 

 

有关详细的api请查看:https://docs.openstack.org/api-ref/

 

使用token查看镜像列表:

curl -H "X-Auth-Token:$token" -H 'Content-Type: application/json' http://10.100.214.202:9292/v2/images

{
"images": [{
"status": "active",
"name": "centos7",
"tags": [],
"container_format": "bare",
"created_at": "2020-06-08T06:50:00Z",
"size": 935067648,
"disk_format": "qcow2",
"updated_at": "2020-06-08T06:50:13Z",
"visibility": "public",
"self": "/v2/images/c01b3549-5b8a-48b0-b53b-973f63095f94",
"min_disk": 0,
"protected": false,
"id": "c01b3549-5b8a-48b0-b53b-973f63095f94",
"file": "/v2/images/c01b3549-5b8a-48b0-b53b-973f63095f94/file",
"checksum": "e9d374643c670a70bb495aab87df61ec",
"owner": "f4228d6dfa07453c84996e5f2be19ca2",
"virtual_size": null,
"min_ram": 0,
"schema": "/v2/schemas/image"
}, {
"status": "active",
"name": "cirros",
"tags": [],
"container_format": "bare",
"created_at": "2020-05-24T07:26:06Z",
"size": 12716032,
"disk_format": "qcow2",
"updated_at": "2020-05-24T07:26:06Z",
"visibility": "public",
"self": "/v2/images/423fe51f-6c08-4ba5-9248-fb107788b555",
"min_disk": 0,
"protected": false,
"id": "423fe51f-6c08-4ba5-9248-fb107788b555",
"file": "/v2/images/423fe51f-6c08-4ba5-9248-fb107788b555/file",
"checksum": "443b7623e27ecf03dc9e01ee93f67afe",
"owner": "f4228d6dfa07453c84996e5f2be19ca2",
"virtual_size": null,
"min_ram": 0,
"schema": "/v2/schemas/image"
}],
"schema": "/v2/schemas/images",
"first": "/v2/images"
}

 

使用token删除镜像:

[root@controller01 ~]# curl -X DELETE -H "X-Auth-Token:$token" -H 'Content-Type: application/json' http://10.100.214.202:9292/v2/images/c01b3549-5b8a-48b0-b53b-973f63095f94

[root@controller01 ~]# openstack image list
+--------------------------------------+--------+--------+
| ID | Name | Status |
+--------------------------------------+--------+--------+
| 423fe51f-6c08-4ba5-9248-fb107788b555 | cirros | active |
+--------------------------------------+--------+--------+

 

使用token删除一个实例:

curl -X DELETE -H "X-Auth-Token:$token" -H 'Content-Type: application/json' http://10.100.201.201/8774/v2.1/${项目id}/servers/${实例id}

创建实例:

curl -X POST  -H "X-Auth-Token:$token" -H  'Content-Type: application/json'

-d '{

    "server" : {
        "adminPass": "MySecretPass",
        "accessIPv4": "1.2.3.4",
        "accessIPv6": "80fe::",
        "name" : "new-server-test",
        "imageRef" : "70a599e0-31e7-49b7-b260-868f441e862b",
        "flavorRef" : "6",
        "OS-DCF:diskConfig": "AUTO",
        "metadata" : {
            "My Server Name" : "Apache1"
        },
        "security_groups": [
            {
                "name": "default"
            }
        ],
        "user_data" : "IyEvYmluL2Jhc2gKL2Jpbi9zdQplY2hvICJJIGFtIGluIHlvdSEiCg==",
        "networks": "auto",
        "host": "openstack-node-01",
        "hypervisor_hostname": "openstack-node-01"
    }
}'  http://10.100.201.201/8774/v2.1/${项目id}/servers

删除卷:

curl -X DELETE -H "X-Auth-Token:$token" -H "Content-Type: application/json" http://10.100.201.201:8776/v2/${项目id}/volumes/${卷id}

 

posted @ 2020-06-09 10:54  Mr-呵呵哒  阅读(314)  评论(0编辑  收藏  举报