HorizonやCLIではOpenStackを色々触りますが、REST APIを直接使ってみる機会がなかったので使ってみた。
ちなみにOpenStackのIPは192.168.101.50です。
1. まずはcurlをインストール
# yum install curl
2. keystoneからトークンをいただく
$ curl -s -X POST http://192.168.101.50:5000/v2.0/tokens \ -H "Content-Type: application/json" \ -d '{"auth": {"tenantName": "'"<TENANT NAME>"'", "passwordCredentials": {"username": "'"<USER NAME>"'", "password": "'"<PASSWORD>"'"}}}' \ {"access": {"token": {"issued_at": "2015-06-12T13:21:20.717612", "expires": "2015-06-12T14:21:20Z", "id": "8ba8bbfaa7b5435892d0809157f0711e", "tenant": {"description": null, "enabled": true, "id": "2388b8af7ad64b79883a1107f4d91815", "name": "demo"}, "audit_ids": ["iiNELLRfRd-CegvHFuhHlg"]}, "serviceCatalog": [{"endpoints": [{"adminURL": "http://192.168.0.8:8774/v2/2388b8af7ad64b79883a1107f4d91815", "region": "RegionOne", "internalURL": "http://192.168.0.8:8774/v2/2388b8af7ad64b79883a1107f4d91815", "id": "10ab473ddf614466b1e637b0a4912401", "publicURL": "http://192.168.0.8:8774/v2/2388b8af7ad64b79883a1107f4d91815"}], "endpoints_links": [], "type": "compute", "name": "nova"}, {"endpoints": [{"adminURL": "http://192.168.0.8:9696/", "region": "RegionOne", "internalURL": "http://192.168.0.8:9696/", "id": "71910a1ad56b47f18806c7e8a52d0f7c", "publicURL": "http://192.168.0.8:9696/"}], "endpoints_links": [], "type": "network", "name": "neutron"}, {"endpoints": [{"adminURL": "http://192.168.0.8:9292", "region": "RegionOne", "internalURL": "http://192.168.0.8:9292", "id": "9ca72780c43b4c448feb1ec7cc23f052", "publicURL": "http://192.168.0.8:9292"}], "endpoints_links": [], "type": "image", "name": "glance"}, {"endpoints": [{"adminURL": "http://192.168.0.8:6385", "region": "RegionOne", "internalURL": "http://192.168.0.8:6385", "id": "120802297c02425fa71e974157fc1324", "publicURL": "http://192.168.0.8:6385"}], "endpoints_links": [], "type": "baremetal", "name": "ironic"}, {"endpoints": [{"adminURL": "http://192.168.0.8:8773/", "region": "RegionOne", "internalURL": "http://192.168.0.8:8773/", "id": "08108e610d264fc596fbc9e81970c97a", "publicURL": "http://192.168.0.8:8773/"}], "endpoints_links": [], "type": "ec2", "name": "ec2"}, {"endpoints": [{"adminURL": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815", "region": "RegionOne", "internalURL": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815", "id": "0140ba60ba364ccba73e76d903370aa0", "publicURL": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815"}], "endpoints_links": [], "type": "computev21", "name": "novav21"}, {"endpoints": [{"adminURL": "http://192.168.0.8:35357/v2.0", "region": "RegionOne", "internalURL": "http://192.168.0.8:5000/v2.0", "id": "559258fe2f794f41af0abb07cf035b13", "publicURL": "http://192.168.0.8:5000/v2.0"}], "endpoints_links": [], "type": "identity", "name": "keystone"}], "user": {"username": "admin", "roles_links": [], "id": "5bb643c0948f42189c2018a714575c1a", "roles": [{"name": "admin"}], "name": "admin"}, "metadata": {"is_admin": 0, "roles": ["c9e49d3583944d32acebf764a72db86a"]}}}
jsonが帰ってくるのですが、見づらいです。そんな時は、python -m json.toolに食わせてあげると整形して表示してくれます。
$ curl -s -X POST http://192.168.101.50:5000/v2.0/tokens \ -H "Content-Type: application/json" \ -d '{"auth": {"tenantName": "'"<TENANT NAME>"'", "passwordCredentials": {"username": "'"<USER NAME>"'", "password": "'"<PASSWORD>"'"}}}' \ | python -m json.tool { "access": { "metadata": { "is_admin": 0, "roles": [ "c9e49d3583944d32acebf764a72db86a" ] }, "serviceCatalog": [ { "endpoints": [ { "adminURL": "http://192.168.0.8:8774/v2/2388b8af7ad64b79883a1107f4d91815", "id": "10ab473ddf614466b1e637b0a4912401", "internalURL": "http://192.168.0.8:8774/v2/2388b8af7ad64b79883a1107f4d91815", "publicURL": "http://192.168.0.8:8774/v2/2388b8af7ad64b79883a1107f4d91815", "region": "RegionOne" } ], "endpoints_links": [], "name": "nova", "type": "compute" }, { "endpoints": [ { "adminURL": "http://192.168.0.8:9696/", "id": "71910a1ad56b47f18806c7e8a52d0f7c", "internalURL": "http://192.168.0.8:9696/", "publicURL": "http://192.168.0.8:9696/", "region": "RegionOne" } ], "endpoints_links": [], "name": "neutron", "type": "network" }, { "endpoints": [ { "adminURL": "http://192.168.0.8:9292", "id": "9ca72780c43b4c448feb1ec7cc23f052", "internalURL": "http://192.168.0.8:9292", "publicURL": "http://192.168.0.8:9292", "region": "RegionOne" } ], "endpoints_links": [], "name": "glance", "type": "image" }, { "endpoints": [ { "adminURL": "http://192.168.0.8:6385", "id": "120802297c02425fa71e974157fc1324", "internalURL": "http://192.168.0.8:6385", "publicURL": "http://192.168.0.8:6385", "region": "RegionOne" } ], "endpoints_links": [], "name": "ironic", "type": "baremetal" }, { "endpoints": [ { "adminURL": "http://192.168.0.8:8773/", "id": "08108e610d264fc596fbc9e81970c97a", "internalURL": "http://192.168.0.8:8773/", "publicURL": "http://192.168.0.8:8773/", "region": "RegionOne" } ], "endpoints_links": [], "name": "ec2", "type": "ec2" }, { "endpoints": [ { "adminURL": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815", "id": "0140ba60ba364ccba73e76d903370aa0", "internalURL": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815", "publicURL": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815", "region": "RegionOne" } ], "endpoints_links": [], "name": "novav21", "type": "computev21" }, { "endpoints": [ { "adminURL": "http://192.168.0.8:35357/v2.0", "id": "559258fe2f794f41af0abb07cf035b13", "internalURL": "http://192.168.0.8:5000/v2.0", "publicURL": "http://192.168.0.8:5000/v2.0", "region": "RegionOne" } ], "endpoints_links": [], "name": "keystone", "type": "identity" } ], "token": { "audit_ids": [ "pfJ9S9m7SfWVpSAJyHRlBA" ], "expires": "2015-06-12T14:23:49Z", "id": "4ccd80bb4e1442a39715e366b248a7de", "issued_at": "2015-06-12T13:23:49.927660", "tenant": { "description": null, "enabled": true, "id": "2388b8af7ad64b79883a1107f4d91815", "name": "demo" } }, "user": { "id": "5bb643c0948f42189c2018a714575c1a", "name": "admin", "roles": [ { "name": "admin" } ], "roles_links": [], "username": "admin" } } }
これでREST API使う準備ができたので、こちらを参考に使ってみたいREST APIを使ってみる。とりあえず、flavorのリストを見てみよう。flavorはnovaのv2.1のAPIでAPIのreferenceによると/v2.1/flavorsをGETすればよいみたい。
novaのv2.1APIのURLを先ほどのJSONの中から探すと、"http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815"
tokenのIDを探すと、"4ccd80bb4e1442a39715e366b248a7de"
これらを使ってAPIに問い合わせます。
$ curl -s -H \ "X-Auth-Token:4ccd80bb4e1442a39715e366b248a7de" \ http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors \ | python -m json.tool { "flavors": [ { "id": "1", "links": [ { "href": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors/1", "rel": "self" }, { "href": "http://192.168.0.8:8774/2388b8af7ad64b79883a1107f4d91815/flavors/1", "rel": "bookmark" } ], "name": "m1.tiny" }, { "id": "2", "links": [ { "href": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors/2", "rel": "self" }, { "href": "http://192.168.0.8:8774/2388b8af7ad64b79883a1107f4d91815/flavors/2", "rel": "bookmark" } ], "name": "m1.small" }, { "id": "3", "links": [ { "href": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors/3", "rel": "self" }, { "href": "http://192.168.0.8:8774/2388b8af7ad64b79883a1107f4d91815/flavors/3", "rel": "bookmark" } ], "name": "m1.medium" }, { "id": "4", "links": [ { "href": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors/4", "rel": "self" }, { "href": "http://192.168.0.8:8774/2388b8af7ad64b79883a1107f4d91815/flavors/4", "rel": "bookmark" } ], "name": "m1.large" }, { "id": "42", "links": [ { "href": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors/42", "rel": "self" }, { "href": "http://192.168.0.8:8774/2388b8af7ad64b79883a1107f4d91815/flavors/42", "rel": "bookmark" } ], "name": "m1.nano" }, { "id": "5", "links": [ { "href": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors/5", "rel": "self" }, { "href": "http://192.168.0.8:8774/2388b8af7ad64b79883a1107f4d91815/flavors/5", "rel": "bookmark" } ], "name": "m1.xlarge" }, { "id": "551", "links": [ { "href": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors/551", "rel": "self" }, { "href": "http://192.168.0.8:8774/2388b8af7ad64b79883a1107f4d91815/flavors/551", "rel": "bookmark" } ], "name": "baremetal" }, { "id": "84", "links": [ { "href": "http://192.168.0.8:8774/v2.1/2388b8af7ad64b79883a1107f4d91815/flavors/84", "rel": "self" }, { "href": "http://192.168.0.8:8774/2388b8af7ad64b79883a1107f4d91815/flavors/84", "rel": "bookmark" } ], "name": "m1.micro" } ] }
できました。