1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

13. IBM Cloud: VMware as a Service(VMWaaS) - VMware Cloud Director OpenAPIの実行例

Last updated at Posted at 2024-03-14

1. はじめに

今まで色々な作業をVMware Console(VMware Cloud Director)を使って操作してきましたが、作業内容によってはAPIを使った自動化作業を実施したいということもあるでしょう。前回に紹介したAPIはあくまでVMWaaSのAPIであって、VDC networkなどを管理するためのAPIではありません。今回は、VDC内のコンポーネント管理することができる、VMware Cloud Director OpenAPIを呼び出す方法を紹介したいと思います。

2. VMware Cloud Director OpenAPI: API Reference

  1. クエスチョンマークのボタンを押下して、API Explorerを選択。image.png
  2. このページで紹介されているAPI一覧を使えば良いことがわかる。image.png
  3. 例えば、VDCのnetwork一覧を呼び出すためには、orgVdcNetworksに移動し、Try it outを実行する。image.png
  4. pageやpagesizeなどの値はデフォルトのまま、Executeを実行。image.png
  5. 以下の結果が得られる。image.png

以上により、VMware Cloud Director OpenAPIの呼び出し方は分かりました。しかし、その際のAccess Token(Bearer以下の箇所)が不明です。まさかAPIを実行する度に、コンソールアクセスしてAccess Tokenを取得するなんてことはできませんからね。。。

3. Access Tokenの生成方法

VMware Cloud Director OpenAPIのAccess Tokenの取得方法は、Generate an API Access Tokenが参考になります。要は、

  • UI上からAPI Token(=Refresh Token)を生成し、手元に保管しておく。このRefresh Tokenの有効期限はない。
  • このRefresh TokenからAccess Tokenを生成する。

という手順を踏みます。以下、実際に作業をしてみます。

  1. User preferencesを選択image.png
  2. API TokensにてNEWを押下。このAPI TokenがRefresh Tokenです。image.png
  3. API Token名を入力してCREATEimage.png
  4. API Token(Refresh Token)が生成された。この情報は二度と参照することができない。今回は以下のようにibmcloud_vmwaas_apitokenというファイルに控えておく。image.png
ibmcloud_vmwaas_apitoken
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

この情報を使って、Access Tokenを得ることができます。

organization_nameは自身のものに変更してください。
$ vmwaas_apitoken=$(cat ibmcloud_vmwaas_apitoken)
$ organization_name="8ed285a1-b804-4f6c-bad7-783b25605194"

$ curl -sX POST "https://dirw082.us-south.vmware.cloud.ibm.com/oauth/tenant/${organization_name}/token" -H 'Accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -d "grant_type=refresh_token&refresh_token=${vmwaas_apitoken}" | jq
{
  "access_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "token_type": "Bearer",
  "expires_in": 86400,
  "refresh_token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

以上により、アクセストークンを取得できたことを確認できました。

4. 再度API Explorerで参照したAPIを実行してみる。

organization_nameは自身のものに変更してください。
$ vmwaas_apitoken=$(cat ibmcloud_vmwaas_apitoken)
$ organization_name="8ed285a1-b804-4f6c-bad7-783b25605194"

$ vmwaas_accesstoken=$(curl -sX POST "https://dirw082.us-south.vmware.cloud.ibm.com/oauth/tenant/${organization_name}/token" -H 'Accept: application/json' -H 'Content-Type: application/x-www-form-urlencoded' -d "grant_type=refresh_token&refresh_token=${vmwaas_apitoken}"  | jq -r '.access_token')

$ curl -sX GET "https://dirw082.us-south.vmware.cloud.ibm.com/cloudapi/1.0.0/orgVdcNetworks?page=1&pageSize=16" -H "accept: application/json;version=37.1" -H "Authorization: Bearer ${vmwaas_accesstoken}" | jq
{
  "resultTotal": 3,
  "pageCount": 1,
  "page": 1,
  "pageSize": 16,
  "associations": null,
  "values": [
    {
      "id": "urn:vcloud:network:549a62bc-8553-492f-ad8f-6524f9a9c448",
      "name": "nw1-isolated-vdc01",
      "description": "",
      "subnets": {
        "values": [
          {
            "gateway": "192.168.102.1",
            "prefixLength": 24,
            "dnsSuffix": null,
            "dnsServer1": "161.26.0.10",
            "dnsServer2": "161.26.0.11",
            "ipRanges": {
              "values": [
                {
                  "startAddress": "192.168.102.2",
                  "endAddress": "192.168.102.99"
                }
              ]
            },
            "enabled": true,
            "totalIpCount": 98,
            "usedIpCount": 0
          }
        ]
      },
      "backingNetworkId": null,
      "backingNetworkType": "NSXT_FLEXIBLE_SEGMENT",
      "parentNetworkId": null,
      "networkType": "ISOLATED",
      "orgVdc": {
        "name": "mt-vdc01",
        "id": "urn:vcloud:vdc:686d5f0d-1af2-4b5d-9262-1c4b3081799a"
      },
      "ownerRef": {
        "name": "mt-vdc01",
        "id": "urn:vcloud:vdc:686d5f0d-1af2-4b5d-9262-1c4b3081799a"
      },
      "orgVdcIsNsxTBacked": null,
      "orgRef": {
        "name": "8ed285a1-b804-4f6c-bad7-783b25605194",
        "id": "urn:vcloud:org:41860b51-a949-4e2b-a21c-ba9128fe5853"
      },
      "connection": null,
      "isDefaultNetwork": null,
      "shared": false,
      "enableDualSubnetNetwork": false,
      "status": "REALIZED",
      "lastTaskFailureMessage": null,
      "guestVlanTaggingAllowed": false,
      "retainNicResources": false,
      "crossVdcNetworkId": null,
      "crossVdcNetworkLocationId": null,
      "overlayId": null,
      "totalIpCount": 98,
      "usedIpCount": 0,
      "routeAdvertised": false,
      "securityGroups": null,
      "segmentProfileTemplateRef": null
    },
    {
      "id": "urn:vcloud:network:b1ded7f0-bd5b-48ed-be59-9e0415cfb738",
      "name": "nw1-routed-vdc01",
      "description": "",
      "subnets": {
        "values": [
          {
            "gateway": "192.168.100.1",
            "prefixLength": 24,
            "dnsSuffix": "",
            "dnsServer1": "161.26.0.10",
            "dnsServer2": "161.26.0.11",
            "ipRanges": {
              "values": [
                {
                  "startAddress": "192.168.100.2",
                  "endAddress": "192.168.100.99"
                }
              ]
            },
            "enabled": true,
            "totalIpCount": 98,
            "usedIpCount": 1
          }
        ]
      },
      "backingNetworkId": null,
      "backingNetworkType": "NSXT_FLEXIBLE_SEGMENT",
      "parentNetworkId": null,
      "networkType": "NAT_ROUTED",
      "orgVdc": null,
      "ownerRef": {
        "name": "DAL-VDC-GROUP1",
        "id": "urn:vcloud:vdcGroup:895fa195-890e-4e4d-b85f-0cb8097c6cf0"
      },
      "orgVdcIsNsxTBacked": null,
      "orgRef": {
        "name": "8ed285a1-b804-4f6c-bad7-783b25605194",
        "id": "urn:vcloud:org:41860b51-a949-4e2b-a21c-ba9128fe5853"
      },
      "connection": {
        "routerRef": {
          "name": "mt-vdc01-e116ab26-3f47-43f9-8cf6-05",
          "id": "urn:vcloud:gateway:1225668b-e935-407b-8190-645a3f24d628"
        },
        "connectionType": "INTERNAL",
        "connectionTypeValue": "INTERNAL",
        "connected": true
      },
      "isDefaultNetwork": null,
      "shared": true,
      "enableDualSubnetNetwork": false,
      "status": "REALIZED",
      "lastTaskFailureMessage": null,
      "guestVlanTaggingAllowed": false,
      "retainNicResources": false,
      "crossVdcNetworkId": null,
      "crossVdcNetworkLocationId": null,
      "overlayId": null,
      "totalIpCount": 98,
      "usedIpCount": 1,
      "routeAdvertised": false,
      "securityGroups": null,
      "segmentProfileTemplateRef": null
    },
    {
      "id": "urn:vcloud:network:6b2b9879-6236-464e-ae9e-d0bfe2dcf519",
      "name": "nw2-routed-vdc01",
      "description": "",
      "subnets": {
        "values": [
          {
            "gateway": "192.168.101.1",
            "prefixLength": 24,
            "dnsSuffix": "",
            "dnsServer1": "161.26.0.10",
            "dnsServer2": "161.26.0.11",
            "ipRanges": {
              "values": [
                {
                  "startAddress": "192.168.101.2",
                  "endAddress": "192.168.101.99"
                }
              ]
            },
            "enabled": true,
            "totalIpCount": 98,
            "usedIpCount": 1
          }
        ]
      },
      "backingNetworkId": null,
      "backingNetworkType": "NSXT_FLEXIBLE_SEGMENT",
      "parentNetworkId": null,
      "networkType": "NAT_ROUTED",
      "orgVdc": null,
      "ownerRef": {
        "name": "DAL-VDC-GROUP1",
        "id": "urn:vcloud:vdcGroup:895fa195-890e-4e4d-b85f-0cb8097c6cf0"
      },
      "orgVdcIsNsxTBacked": null,
      "orgRef": {
        "name": "8ed285a1-b804-4f6c-bad7-783b25605194",
        "id": "urn:vcloud:org:41860b51-a949-4e2b-a21c-ba9128fe5853"
      },
      "connection": {
        "routerRef": {
          "name": "mt-vdc01-e116ab26-3f47-43f9-8cf6-05",
          "id": "urn:vcloud:gateway:1225668b-e935-407b-8190-645a3f24d628"
        },
        "connectionType": "INTERNAL",
        "connectionTypeValue": "INTERNAL",
        "connected": true
      },
      "isDefaultNetwork": null,
      "shared": true,
      "enableDualSubnetNetwork": false,
      "status": "REALIZED",
      "lastTaskFailureMessage": null,
      "guestVlanTaggingAllowed": false,
      "retainNicResources": false,
      "crossVdcNetworkId": null,
      "crossVdcNetworkLocationId": null,
      "overlayId": null,
      "totalIpCount": 98,
      "usedIpCount": 1,
      "routeAdvertised": false,
      "securityGroups": null,
      "segmentProfileTemplateRef": null
    }
  ]
}
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?