| Works
vCloud DirectorのAPIを使って組織VDCを作成する
仕事でvCloud Directorを使う機会があるのですが、組織VDCの作成を自動化するためのスクリプト作成が結構難航していました。
そもそもVMwareのPowerCLIには"New-OrgVdc"というコマンドレットが用意されているのですが、これでVDCを作成するにはストレージポリシーを"Any"する必要があるようです。ですが、うちの環境ではそれが許されていないため、PowerCLIでは手詰まりになってしまいました。そもそもvCloud関連のPowerCLIはまだまだ足りてない機能が多い気が…。情報も少ないし。
そこで、vCloud DirectorのREST APIを使うしかないと。
やることは「特定のストレージポリシーが適用された組織VDCを作成する」ということ。
環境はvCloud Director 5.5、リソースの割り当てモデルとか制限は主題ではないので従量課金で無制限に決め打ち。
$VcdUser = "administrator"
$VcdPass = "password"
$ApiEp = "https://vcd.example.com/api"
$ApiVer = "5.5"
$ProviderVdcName = "ProviderVDC"
$OrgName = "Organization"
$StoragePolicy = "HighIOPS"
$PrivateNetworkPool = "PrivateNW"
$VdcName = "NewVDC"
# 認証トークンの取得
$Auth = $VcdUser + "@system:" + $VcdPass
$Encoded = [System.Text.Encoding]::UTF8.GetBytes($Auth)
$EncodedPassword = [System.Convert]::ToBase64String($Encoded)
$SendUri = $ApiEp + "/sessions"
$Headers = @{
"Authorization" = "Basic " + $EncodedPassword;
"Accept" = "application/*+xml;version=$ApiVer"
}
$Res = Invoke-WebRequest -Headers $Headers -Uri $SendUri -Method POST
$VcdAuth = $Res.Headers["x-vcloud-authorization"]
$Headers = @{
"x-vcloud-authorization" = $VcdAuth;
"Accept" = "application/*+xml;version=$ApiVer"
}
# プロバイダーVDCのURLを取得
$SendUri = $ApiEp + "/admin"
$Res = Invoke-RestMethod -Headers $Headers -Uri $SendUri -Method GET
foreach ($objPvDC in $Res.vCloud.ProviderVdcReferences.ProviderVdcReference) {
if ($objPvDC.Name -eq $ProviderVdcName) {
$PvDCUri = $objPvDC.href
}
}
# 組織のURLを取得
foreach ($objOrg in $Res.vCloud.OrganizationReferences.OrganizationReference) {
if ($objOrg.Name -eq $OrgName) {
$OrgUri = $objOrg.href
}
}
# 適用するストレージポリシーのURLを取得
$Res = Invoke-RestMethod -Headers $Headers -Uri $PvDCUri -Method GET
foreach ($objSp in $Res.ProviderVdc.StorageProfiles.ProviderVdcStorageprofile) {
if ($objSp.Name -eq $StoragePolicy) {
$SpUri = $objSp.href
}
}
# プライベートネットワークプールのURLを取得
foreach ($objNw in $Res.ProviderVdc.NetworkPoolReferences.NetworkPoolReference) {
if ($objNw.Name -eq $PrivateNetworkPool) {
$NwUri = $objNw.href
}
}
# 作成するVDCのパラメータ
$CreateOrgVdcXml = @"
<?xml version="1.0" encoding="UTF-8"?>
<CreateVdcParams
name="$VdcName"
xmlns="http://www.vmware.com/vcloud/v1.5">
<Description>NewVirtualDatacenter</Description>
<AllocationModel>AllocationVApp</AllocationModel>
<ComputeCapacity>
<Cpu>
<Units>MHz</Units>
<Allocated>0</Allocated>
<Limit>0</Limit>
</Cpu>
<Memory>
<Units>MB</Units>
<Allocated>0</Allocated>
<Limit>0</Limit>
</Memory>
</ComputeCapacity>
<NicQuota>0</NicQuota>
<NetworkQuota>10</NetworkQuota>
<VdcStorageProfile>
<Enabled>true</Enabled>
<Units>MB</Units>
<Limit>0</Limit>
<Default>true</Default>
<ProviderVdcStorageProfile
href="$SpUri" />
</VdcStorageProfile>
<ResourceGuaranteedMemory>1</ResourceGuaranteedMemory>
<ResourceGuaranteedCpu>1</ResourceGuaranteedCpu>
<VCpuInMhz>2000</VCpuInMhz>
<IsThinProvision>true</IsThinProvision>
<NetworkPoolReference
href="$NwUri" />
<ProviderVdcReference
name="$ProviderVdcName"
href="$PvDCUri" />
<UsesFastProvisioning>false</UsesFastProvisioning>
</CreateVdcParams>
"@
# VDCの作成
$SendUri = $OrgUri + "/vdcsparams"
$Headers = @{
"x-vcloud-authorization" = $VcdAuth;
"Accept" = "application/*+xml;version=$ApiVer";
"Content-Type" = "application/vnd.vmware.admin.createVdcParams+xml"
}
$Res = Invoke-RestMethod -Headers $Headers -Uri $SendUri -Method POST -Body $CreateOrgVdcXml
"NewVDC"という名前に組織VDCを作成しています。
APIの使い方はオフィシャルの"vCloud API Programming Guide"の通りですが、PowerShellで実行するため"Invoke-RestMethod"や"Invoke-WebRequest"に手こずりました。API使うならLinux環境から"curl"でもよかった気もするのですが、後学のためですね。BodyがXMLなのも、JSONに慣れるとなんとなくやりにくい。
とりあえずこれをベースにして、他のスクリプト(組織や初期ネットワークの作成)と繋いで手作業が減らせそうです。あと、関数化したり整理しないと。