티스토리 뷰

Cloud/Private Cloud

cURL with OpenStack

jacobbaek Jacob_baek 2021. 4. 9. 22:21

OpenStack API 요청을 cURL로 확인해야할 일이 있어 간단히 script으로 정리해보았다.

step by step

cURL과 같은 cli 기반의 요청을 위해서는 먼저 token 정보가 필요하다.
먼저 token을 가져오는 아래와 같은 요청을 먼저 수행하자.

우선 수행을 위해서는 당연하게도 openrc 파일내에 선언된 환경변수가 필요하다.

[root@deploy ~]# source admin-openrc.sh
[root@deploy ~]# curl -v -s -X POST $OS_AUTH_URL/auth/tokens?nocatalog  \
 -H "Content-Type: application/json" \
 -d '{ "auth": { "identity": { "methods": ["password"],"password": {"user": {"domain": {"name": "'"$OS_USER_DOMAIN_NAME"'"},"name": "'"$OS_USERNAME"'", "password": "'"$OS_PASSWORD"'"} } }, "scope": { "project": { "domain": { "name": "'"$OS_PROJECT_DOMAIN_NAME"'" }, "name":  "'"$OS_PROJECT_NAME"'" } } }}' \
 | python -m json.tool

실제 명령을 실행하게 되면 아래와 같은 X-Subject-Token field를 포함한 결과를 얻게 된다.

[root@deploy ~]# curl -v -s -X POST $OS_AUTH_URL/auth/tokens?nocatalog  \
>  -H "Content-Type: application/json" \
>  -d '{ "auth": { "identity": { "methods": ["password"],"password": {"user": {"domain": {"name": "'"$OS_USER_DOMAIN_NAME"'"},"name": "'"$OS_USERNAME"'", "password": "'"$OS_PASSWORD"'"} } }, "scope": { "project": { "domain": { "name": "'"$OS_PROJECT_DOMAIN_NAME"'" }, "name":  "'"$OS_PROJECT_NAME"'" } } }}' \
> | python -m json.tool
* About to connect() to openstack.jacobbaek.com port 5000 (#0)
*   Trying 192.168.1.100...
* Connected toopenstack.jacobbaek.com (192.168.1.100) port 5000 (#0)
> POST /v3/auth/tokens?nocatalog HTTP/1.1
> User-Agent: curl/7.29.0
> Host: openstack.jacobbaek.com:5000
> Accept: */*
> Content-Type: application/json
> Content-Length: 241
>
} [data not shown]
* upload completely sent off: 241 out of 241 bytes
< HTTP/1.1 201 Created
< Date: Wed, 23 Dec 2020 07:39:54 GMT
< Server: Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5
< X-Subject-Token: gBBBBAAf4vRK0j3WcTqjf7U3Tlrl8aogrqh_i4B1p4ynHHMc3iIGe-e_QsUduiv_e3XIf9jVt_4biEDRCoSs--yo858R2egFwzUOM8KeCVPcC8PEHE9i73xFVyJA9NfUminy8Ed60s5SWD0kanygIQxxEFAeDY3Vs7oZ4jTy5imyAcFWgIsIXmE
< Vary: X-Auth-Token
< x-openstack-request-id: req-8223f69c-d96c-408f-bd6b-5247bae7115c
< Content-Length: 650
< Content-Type: application/json

이제 앞서 이야기한 X-Subject-Token을 복사하여 환경변수로 설정해보자.

[root@deploy ~]# OS_TOKEN="gBBBBAAf4vRK0j3WcTqjf7U3Tlrl8aogrqh_i4B1p4ynHHMc3iIGe-e_QsUduiv_e3XIf9jVt_4biEDRCoSs--yo858R2egFwzUOM8KeCVPcC8PEHE9i73xFVyJA9NfUminy8Ed60s5SWD0kanygIQxxEFAeDY3Vs7oZ4jTy5imyAcFWgIsIXmE"

NOTE
물론 환경변수 지정안하고 바로 cURL 명령상에 넣어도 무방하다.

이후 아래와 같이 cURL을 통해 header에 앞서 지정한 Token정보를 포함시켜 OpenStack API 호출을 해볼수 있다.

[root@deploy ~]# curl -s -H "X-Auth-Token: $OS_TOKEN" "openstack.jacobbaek.com:8776/v2/32ebd527c20944e59718849360b772c2/volumes/2aa5d100-4404-4dd5-8f5f-d18af800a4an"
{"volume": {"migration_status": null, "attachments": [], "links": [{"href": "http://openstack.jacobbaek.com:8776/v2/32ebd527c20944e59718849360b772c2/volumes/2aa5d100-4404-4dd5-8f5f-d18af800a4an", "rel": "self"}, {"href": "http://openstack.jacobbaek.com:8776/32ebd527c20944e59718849360b772c2/volumes/2aa5d100-4404-4dd5-8f5f-d18af800a4an", "rel": "bookmark"}], "availability_zone": "nova", "os-vol-host-attr:host": "openstack.jacobbaek.com@ceph#RBD", "encrypted": false, "updated_at": "2021-04-06T09:35:51.000000", "replication_status": null, "snapshot_id": null, "id": "2aa5d100-4404-4dd5-8f5f-d18af800a4an", "size": 30, "user_id": "ec7e1fb83ba24036bc971d0ba05146b1", "os-vol-tenant-attr:tenant_id": "32ebd527c20944e59718849360b772c2", "os-vol-mig-status-attr:migstat": null, "metadata": {}, "status": "available", "volume_image_metadata": {"checksum": "9f5a31870d75669527e3d0460b017353", "min_ram": "0", "disk_format": "raw", "image_name": "ubuntu-20.04.LTS", "image_id": "33727ed6-9cf0-46f3-96b3-d9760365e993", "container_format": "bare", "min_disk": "0", "size": "2361393152"}, "description": "", "multiattach": false, "source_volid": null, "consistencygroup_id": null, "os-vol-mig-status-attr:name_id": null, "name": "", "bootable": "true", "created_at": "2021-04-06T09:35:46.000000", "volume_type": null}}

참고로 아래와 같은 api browsing site를 통해 api example을 확인할수 있다.

NOTE
참고로 호출할 API의 version을 정확하게 확인하고 수행하는것이 좋다.
버전마다 호출URL의 구성이 상이할수 있다.

script

아무래도 여러번의 과정이 거쳐져서 각 명령을 지속적으로 입력하는 불편함이 있어 이를 조금이나마 간편화 시키는 방법을 정리해본다.

[root@deploy ~]# OS_TOKEN=`curl -isX POST $OS_AUTH_URL/auth/tokens?nocatalog -H "Content-Type: application/json"  -d '{ "auth": { "identity": { "methods": ["password"],"password": {"user": {"domain": {"name": "'"$OS_USER_DOMAIN_NAME"'"},"name": "'"$OS_USERNAME"'", "password": "'"$OS_PASSWORD"'"} } }, "scope": { "project": { "domain": { "name": "'"$OS_PROJECT_DOMAIN_NAME"'" }, "name":  "'"$OS_PROJECT_NAME"'" } } }}' | grep X-Subject-Token | awk '{print $2}'`
alias oscurl='curl -s -H "X-Auth-Token: $OS_TOKEN"'

이후 alias로 등록한 oscurl 명령을 사용하여 손쉽게 url 호출을 수행할 수 있다.

[root@deploy ~]# oscurl http://openstack.jacobbaek.com:8776/v2/32ebd527c20944e59718849360b772c2/volumes/2aa5d100-4404-4dd5-8f5f-d18af800a4an
{"volume": {"migration_status": null, "attachments": [], "links": [{"href": "http://openstack.jacobbaek.com:8776/v2/32ebd527c20944e59718849360b772c2/volumes/2aa5d100-4404-4dd5-8f5f-d18af800a4an", "rel": "self"}, {"href": "http://openstack.jacobbaek.com:8776/32ebd527c20944e59718849360b772c2/volumes/2aa5d100-4404-4dd5-8f5f-d18af800a4an", "rel": "bookmark"}], "availability_zone": "nova", "os-vol-host-attr:host": "openstack.jacobbaek.com@ceph#RBD", "encrypted": false, "updated_at": "2021-04-06T09:35:51.000000", "replication_status": null, "snapshot_id": null, "id": "2aa5d100-4404-4dd5-8f5f-d18af800a4an", "size": 30, "user_id": "ec7e1fb83ba24036bc971d0ba05146b1", "os-vol-tenant-attr:tenant_id": "32ebd527c20944e59718849360b772c2", "os-vol-mig-status-attr:migstat": null, "metadata": {}, "status": "available", "volume_image_metadata": {"checksum": "9f5a31870d75669527e3d0460b017353", "min_ram": "0", "disk_format": "raw", "image_name": "ubuntu-20.04.LTS", "image_id": "33727ed6-9cf0-46f3-96b3-d9760365e993", "container_format": "bare", "min_disk": "0", "size": "2361393152"}, "description": "", "multiattach": false, "source_volid": null, "consistencygroup_id": null, "os-vol-mig-status-attr:name_id": null, "name": "", "bootable": "true", "created_at": "2021-04-06T09:35:46.000000", "volume_type": null}}

참고사이트

'Cloud > Private Cloud' 카테고리의 다른 글

cURL with OpenStack  (0) 2021.04.09
openstack instance build stuck  (0) 2021.02.24
kolla-ansible with elasticsearch  (0) 2021.02.23
Packer on OpenStack  (0) 2021.02.19
gophercloud sdk  (0) 2021.02.08
prometheus alertmanager workflow  (0) 2020.12.28
댓글
댓글쓰기 폼