15
27

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 5 years have passed since last update.

VMware vCenter REST APIのまとめメモ

Posted at

VMware vCenter 6.5からREST APIの操作が可能になったため少し触った部分をメモしておきます。

1. vCenter REST API

1-1. API Explorer

vCenter 6.5以上であれば apiexplorer へアクセスしてAPIの仕様を確認することが可能です。
URLは以下の通りです。

URL
https://vCenter IP or FQDN/apiexplorer/

アクセスすると以下のような画面が表示されます。

スクリーンショット 2018-05-20 15.17.27.png

1-2. API仕様の確認

確認したい項目をクリックします。
例えば VM をクリックしてみます。

スクリーンショット 2018-05-20 15.20.38.png

すると、APIの仕様が確認できます。

スクリーンショット 2018-05-20 15.22.17.png

リクエストの方法は TRY IT OUT をクリックすると確認できます。

スクリーンショット 2018-05-20 15.24.00.png

API Explorerでは curlでの例リクエスト先URL レスポンス などが確認できます。

スクリーンショット 2018-05-20 15.24.07.png

2. REST APIの利用

2-1. シーケンス

REST APIの利用流れは以下のようなイメージになっています。

スクリーンショット 2018-05-20 16.07.39.png

2-2. APIのベースURL

APIのベースURLは以下の通りです。

API URL
https://vCenter IP or FQDN/rest

2-3. サンプルソース

vcenterpassword は環境に合わせてください。

# !/usr/bin/env python3
import json
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

vcenter = "vCenter IP or FQDN"
username = "administrator@vsphere.local"
password = "secret"

if __name__ == "__main__":
    # セッション作成リクエスト
    url = "https://" + vcenter + "/rest/com/vmware/cis/session"
    headers = {"Accept":"application/json"}
    r = requests.post(url,
                      headers=headers,
                      auth=(username, password),
                      verify=False)

    # 取得した資格情報をヘッダに反映
    token = json.loads(r.text)["value"]
    headers.update({"vmware-api-session-id":token})

    # VM情報を取得するリクエスト
    url = "https://" + vcenter + "/rest/vcenter/vm/"
    r = requests.get(url,
                     headers=headers,
                     verify=False)

    # 結果を表示
    print("VM情報")
    print(json.dumps(json.loads(r.text), indent=2))
    print()

    # 資格削除リクエスト
    url = "https://" + vcenter + "/rest/com/vmware/cis/session"
    r = requests.delete(url,
                        headers=headers,
                        verify=False)

    print("資格削除結果")
    print(r.status_code)

2-4. サンプルソース実行

実行するとVM情報や資格情報削除結果が出力されます。

$ ./example.py
VM情報
{
  "value": [
    {
      "power_state": "POWERED_ON",
      "cpu_count": 1,
      "memory_size_MiB": 2048,
      "name": "devel",
      "vm": "vm-101"
    },
(snip)

資格削除結果
200

3. 最後に

vSphere APIは基本SOAPで操作する方式でしたが、6.5からREST APIが提供され始めました。
ただし、提供された始めたばかりのためできる事はSOAPよりも少ないです。
ただ、複雑なSOAPよりRESTで操作できた方が簡単だと思うので今後の展開に期待です :-)

4. 参考

15
27
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
15
27

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?