Ansibleのモジュールテストで使われているvCenterシミュレーターを使ってみたメモ

AnsibleプロジェクトにvCenterのシミュレーターコンテナがあったので、簡単にですが使ってみたメモです。

# vcenter-test-container

https://github.com/ansible/vcenter-test-container

実際は以下のGoで作られたシミュレータをコンテナ内で実行しています。

https://github.com/vmware/govmomi/tree/master/vcsim

# シミュレーターインストール

インストールして起動してみます。

```shell-session
[root@docker02 ~]# docker pull quay.io/ansible/vcenter-test-container
[root@docker02 ~]# docker run -d -it --name vcsim quay.io/ansible/vcenter-test-container:latest
[root@docker02 ~]# docker inspect -f '{{ .NetworkSettings.IPAddress }}' vcsim
172.17.0.2
```

# 色々やってみる

## シミュレーター起動

シミュレーターを起動します。

```shell-session
[root@docker02 ~]# curl http://172.17.0.2:5000/spawn
{
  "GOVC_URL": "https://user:pass@172.17.0.2:443",
  "cmd": "/go/bin/vcsim -httptest.serve 172.17.0.2:443 -app=0 -cluster=0 -dc=1 -ds=1 -folder=1 -host=3 -pg=1 -pod=1 -pool=1 -vm=2 > vcsim.log 2>&1",
  "host": "172.17.0.2",
  "password": "pass",
  "pid": 14,
  "port": 443,
  "username": "user"
}
```

## サポートメソッドを確認

シミュレーターがサポートしているメソッドを確認してみます。

```
[root@docker02 ~]# curl -sk https://user:pass@172.17.0.2:443/about
{
  "Methods": [
    "AcquireCloneTicket",
    "AcquireGenericServiceTicket",
    "AddAuthorizationRole",
    "AddCustomFieldDef",
    "AddDVPortgroup_Task",
    "AddLicense",
    "AddPortGroup",
    "AddStandaloneHost_Task",
    "AddVirtualSwitch",
    "AllocateIpv4Address",
    "AllocateIpv6Address",
    "CancelWaitForUpdates",
    "CloneSession",
    "CloneVM_Task",
    "CopyDatastoreFile_Task",
    "CopyVirtualDisk_Task",
    "CreateClusterEx",
    "CreateCollectorForEvents",
    "CreateContainerView",
    "CreateDVS_Task",
    "CreateDatacenter",
    "CreateFilter",
    "CreateFolder",
    "CreateIpPool",
    "CreateLocalDatastore",
    "CreateNasDatastore",
    "CreatePropertyCollector",
    "CreateResourcePool",
    "CreateSnapshot_Task",
    "CreateStoragePod",
    "CreateVApp",
    "CreateVM_Task",
    "CreateVirtualDisk_Task",
    "CurrentTime",
    "DeleteDatastoreFile_Task",
    "DeleteVirtualDisk_Task",
    "DestroyIpPool",
    "DestroyPropertyCollector",
    "Destroy_Task",
    "DisableRuleset",
    "EnableRuleset",
    "EnterMaintenanceMode_Task",
    "ExitMaintenanceMode_Task",
    "FetchDVPorts",
    "FindByDatastorePath",
    "FindByInventoryPath",
    "FindByUuid",
    "FindChild",
    "Login",
    "LoginByToken",
    "LoginExtensionByCertificate",
    "Logout",
    "MakeDirectory",
    "MarkAsTemplate",
    "MoveDatastoreFile_Task",
    "MoveIntoFolder_Task",
    "MoveVirtualDisk_Task",
    "PostEvent",
    "PowerOffVM_Task",
    "PowerOnMultiVM_Task",
    "PowerOnVM_Task",
    "QueryAssignedLicenses",
    "QueryEvents",
    "QueryIPAllocations",
    "QueryIpPools",
    "QueryOptions",
    "QueryVirtualDiskUuid",
    "ReconfigVM_Task",
    "ReconfigureDVPortgroup_Task",
    "ReconfigureDvs_Task",
    "RefreshDatastore",
    "RegisterVM_Task",
    "ReleaseIpAllocation",
    "RelocateVM_Task",
    "RemoveAllSnapshots_Task",
    "RemoveAuthorizationRole",
    "RemoveCustomFieldDef",
    "RemoveEntityPermission",
    "RemoveLicense",
    "RemovePortGroup",
    "RemoveVirtualSwitch",
    "RenameCustomFieldDef",
    "Rename_Task",
    "RetrieveAllPermissions",
    "RetrieveEntityPermissions",
    "RetrieveProperties",
    "RetrievePropertiesEx",
    "RetrieveRolePermissions",
    "RetrieveServiceContent",
    "RetrieveUserGroups",
    "RevertToCurrentSnapshot_Task",
    "SearchDatastoreSubFolders_Task",
    "SearchDatastore_Task",
    "SetEntityPermissions",
    "SetField",
    "SetVirtualDiskUuid",
    "ShutdownGuest",
    "TerminateSession",
    "UnregisterVM",
    "UpdateAuthorizationRole",
    "UpdateConfig",
    "UpdateIpPool",
    "UpdateNetworkConfig",
    "UpdateOptions",
    "WaitForUpdates",
    "WaitForUpdatesEx"
  ],
  "Types": [
    "AuthorizationManager",
    "ComputeResource",
    "CustomFieldsManager",
    "Datacenter",
    "Datastore",
    "DistributedVirtualPortgroup",
    "DistributedVirtualSwitch",
    "EventManager",
    "FileManager",
    "Folder",
    "HostDatastoreBrowser",
    "HostDatastoreSystem",
    "HostFirewallSystem",
    "HostNetworkSystem",
    "HostSystem",
    "IpPoolManager",
    "LicenseAssignmentManager",
    "LicenseManager",
    "Network",
    "OptionManager",
    "PerformanceManager",
    "PropertyCollector",
    "ResourcePool",
    "SearchIndex",
    "ServiceInstance",
    "SessionManager",
    "StoragePod",
    "Task",
    "TaskManager",
    "UserDirectory",
    "ViewManager",
    "VirtualDiskManager",
    "VirtualMachine"
  ]
}
```

## VM情報を取得

以下のコードでVM名を取得してみます。

```python:example.py
#!/usr/bin/env python3
from pyVim.connect import SmartConnect, Disconnect
from pyVmomi import vim, vmodl
import ssl
import atexit

host = "172.17.0.2"
username = "user"
password = "pass"

if __name__ == "__main__":
    # SSL証明書対策
    context = None
    if hasattr(ssl, '_create_unverified_context'):
        context = ssl._create_unverified_context()

    # 接続
    si = SmartConnect(host=host,
                      user=username,
                      pwd=password,
                      sslContext=context)

    # 処理完了時にvCenterから切断
    atexit.register(Disconnect, si)

    # ServiceContentを取得
    content = si.content

    mob_list = content.viewManager.CreateContainerView(content.rootFolder,
                                                       [vim.VirtualMachine],
                                                       True)

    for obj in mob_list.view:
        print(obj.name)
```

実行します。

```
[root@docker02 ~]# ./example.py
DC0_H0_VM0
DC0_H0_VM1
```

問題なく動く事が確認できました :)