2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

vCenterのREST API使用方法

Last updated at Posted at 2020-02-13

個人的備忘録
vSphere Client バージョン 6.7.0.20000

apiexplorer

  • VMwareのapiexplorerにアクセスすることで、REST APIのリファレンスを確認することができる。
    • URL: https://<vCenter IP or FQDN>/apiexplorer/
  • 例えばVM情報をGETするAPIは以下のように確認することができる。
    image.png
  • TRY IT OUT!をクリックすることで、実際にvCenterに対してAPIの実行を試すことができる。
    image.png

※上記はheader内のsession-idがnullになっているため、認証エラーとなる。

curlによるAPI実行

  • vCenterとIPリーチャブルなマシンにログインする。
  • 事前にsession-idを取得する。
$ curl --insecure -X POST -u <your-id>:<your-password> \
 https://<vCenter IP or FQDN>/rest/com/vmware/cis/session
{"value":"d8da17d7cba6677d67a271873666eecf"}
  • session-idをheaderに追加して、任意のRESTリクエストを実行する。
$ curl -X GET --header "Accept: application/json" --header \
 "vmware-api-session-id: d8da17d7cba6677d67a271873666eecf" \
 "https://<vCenter IP or FQDN>/rest/vcenter/vm" -k
  • ちなみにsession-idを環境変数にして実行すると便利。
    • もちろんjqでも可。
$ export session_id=`curl --insecure -X POST -u 'user:pass' https://<vCenter IP or FQDN>/rest/com/vmware/cis/session | python3 -c "import sys, json; print(json.load(sys.stdin)['value'])"`
$ curl -k -X GET --header "Accept: application/json" --header "vmware-api-session-id: $session_id" "https://<vCenter IP or FQDN>/rest/vcenter/vm" | python3 -m json.tool 

参考:vCenter REST-API一覧

  • apiexplorerに記載されているAPIの一覧
rest-api
cluster
datacenter
datastore
datastore/default_policy
deployment
deployment/import_history
deployment/install
deployment/install/initial_config/remote_psc/thumbprint
deployment/install/psc/replicated
deployment/install/psc/standalone
deployment/install/remote_psc
deployment/question
deployment/upgrade
folder
guest/customization_specs
host
inventory/datastore
inventory/network
iso/image
network
ovf/capability
ovf/export_flag
ovf/import_flag
ovf/library_item
resource_pool
services/service
storage/policies
storage/policies/compliance
storage/policies/compliance/VM
storage/policies/VM
system_config/deployment_type
system_config/psc_registration
vcha/capabilities
vcha/cluster
vcha/cluster/active
vcha/cluster/deployment_type
vcha/cluster/mode
vcha/cluster/passive
vcha/cluster/witness
vcha/operations
vcha/vc_credentials
VM
vm_template/library_items
vm/guest/identity
vm/guest/local_filesystem
vm/guest/power
vm/hardware
vm/hardware/adapter/sata
vm/hardware/adapter/scsi
vm/hardware/boot
vm/hardware/boot/device
vm/hardware/cdrom
vm/hardware/cpu
vm/hardware/disk
vm/hardware/ethernet
vm/hardware/floppy
vm/hardware/memory
vm/hardware/parallel
vm/hardware/serial
vm/power
vm/storage/policy
vm/storage/policy/compliance
2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?