Skip to content

Commit cc921da

Browse files
committed
Added examples for python and powershell
1 parent c5cf2fc commit cc921da

File tree

3 files changed

+465
-2
lines changed

3 files changed

+465
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1-
# RestAPIExamples
2-
Scale Computing's System REST API Examples
1+
# Scale Computing System REST API Examples
2+
3+
This repository contains several example scripts for running API queries against a scale system.
4+
5+
These scripts are only examples and demonstrate common use cases with the API.
6+
Refer to the API docs on a scale system for a detailed guide on available calls.
7+
8+
Refer to this repository's tags for examples tied to a specific Scale release.

vm-lifecycle.ps1

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
#!/usr/bin/env pwsh
2+
3+
<#
4+
.SYNOPSIS
5+
Demonstrate basic VM examples and waiting for task completion using the Scale Computing REST API
6+
7+
.PARAMETER Server
8+
Cluster/System to test the API against
9+
10+
.PARAMETER Credential
11+
User credentials used to authenticate with the server
12+
13+
.PARAMETER SkipCertificateCheck
14+
Ignore Invalid/self-signed certificate errors
15+
16+
.EXAMPLE
17+
./vm-lifecycle.ps1 -Server server-name -Credential (Get-Credential)
18+
#>
19+
20+
[CmdletBinding()]
21+
22+
Param(
23+
[Parameter(Mandatory = $true,Position = 0)]
24+
[ValidateNotNullOrEmpty()]
25+
[string] $Server,
26+
[PSCredential] $Credential = (Get-Credential -Message "Enter Scale HC3 Credentials"),
27+
[switch] $SkipCertificateCheck
28+
)
29+
30+
$ErrorActionPreference = 'Stop';
31+
32+
$url = "https://$Server/rest/v1"
33+
34+
35+
$restOpts = @{
36+
Credential = $Credential
37+
ContentType = 'application/json'
38+
}
39+
40+
if ($PSVersionTable.PSEdition -eq 'Core') {
41+
$restOpts.SkipCertificateCheck = $SkipCertificateCheck
42+
}
43+
elseif ($SkipCertificateCheck) {
44+
try
45+
{
46+
add-type -ErrorAction stop @"
47+
using System.Net;
48+
using System.Security.Cryptography.X509Certificates;
49+
public class TrustAllCertsPolicy : ICertificatePolicy {
50+
public bool CheckValidationResult(
51+
ServicePoint srvPoint, X509Certificate certificate,
52+
WebRequest request, int certificateProblem) {
53+
return true;
54+
}
55+
}
56+
"@
57+
} catch { write-error "Failed to create TrustAllCertsPolicy: $_" }
58+
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
59+
}
60+
61+
62+
# Many actions are asynchronous, often we need a way to wait for a returned taskTag to complete before taking further action
63+
function Wait-ScaleTask {
64+
Param(
65+
[Parameter(Mandatory = $true,Position = 1, ValueFromPipeline = $true)]
66+
[ValidateNotNullOrEmpty()]
67+
[string] $TaskTag
68+
)
69+
70+
$retryDelay = [TimeSpan]::FromSeconds(1)
71+
$timeout = [TimeSpan]::FromSeconds(300)
72+
73+
$timer = [Diagnostics.Stopwatch]::new()
74+
$timer.Start()
75+
76+
while ($timer.Elapsed -lt $timeout)
77+
{
78+
Start-Sleep -Seconds $retryDelay.TotalSeconds
79+
$taskStatus = Invoke-RestMethod @restOpts "$url/TaskTag/$TaskTag" -Method GET
80+
81+
if ($taskStatus.state -eq 'ERROR') {
82+
throw "Task '$TaskTag' failed!"
83+
}
84+
elseif ($taskStatus.state -eq 'COMPLETE') {
85+
Write-Verbose "Task '$TaskTag' completed!"
86+
return
87+
}
88+
}
89+
throw [TimeoutException] "Task '$TaskTag' failed to complete in $($timeout.Seconds) seconds"
90+
}
91+
92+
93+
94+
Write-Host "Create VM"
95+
$json = @{
96+
name = "VMLifeycleTest"
97+
description = "An example created for testing the rest API"
98+
mem = 4GB
99+
numVCPU = 4;
100+
blockDevs = @(
101+
@{
102+
capacity = 1GB
103+
type = 'VIRTIO_DISK'
104+
cacheMode = 'WRITETHROUGH'
105+
}
106+
)
107+
netDevs = @(
108+
@{
109+
type = 'VIRTIO'
110+
}
111+
)
112+
} | ConvertTo-Json
113+
$result = Invoke-RestMethod @restOpts "$url/VirDomain/" -Method POST -Body $json
114+
$vmUUID = $($result.createdUUID)
115+
Wait-ScaleTask -TaskTag $($result.taskTag)
116+
117+
Write-Host "Create a block device for the VM"
118+
$json = @{
119+
virDomainUUID = $vmUUID
120+
capacity = 10GB
121+
type = 'VIRTIO_DISK'
122+
cacheMode = 'WRITETHROUGH'
123+
} | ConvertTo-Json
124+
$result = Invoke-RestMethod @restOpts "$url/VirDomainBlockDevice" -Method POST -Body $json
125+
Wait-ScaleTask -TaskTag $($result.taskTag)
126+
127+
128+
129+
Write-Host "Start VM"
130+
$json = ConvertTo-Json @(@{
131+
actionType = 'START'
132+
virDomainUUID = $vmUUID
133+
})
134+
$result = Invoke-RestMethod @restOpts "$url/VirDomain/action" -Method POST -Body $json
135+
Wait-ScaleTask -TaskTag $($result.taskTag)
136+
137+
138+
139+
# Get VM Info to determine which node the VM is currently running on
140+
$nodes = Invoke-RestMethod @restOpts "$url/Node" -Method GET
141+
if ($nodes.Count -gt 1) {
142+
Write-Host "Live migrate VM to another node"
143+
144+
$vm = Invoke-RestMethod @restOpts "$url/VirDomain/$vmUUID" -Method GET
145+
# Get Node UUID where VM is not running currently
146+
$migrateTargetNode = ($nodes | ? { $_ -ne $vm.nodeUUID })[0]
147+
$json = ConvertTo-Json @(@{
148+
actionType = 'LIVEMIGRATE'
149+
nodeUUID = $migrateTargetNode.uuid
150+
virDomainUUID = $vmUUID
151+
})
152+
$result = Invoke-RestMethod @restOpts "$url/VirDomain/action" -Method POST -Body $json
153+
Wait-ScaleTask -TaskTag $($result.taskTag)
154+
}
155+
156+
157+
158+
Write-Host "Stop VM"
159+
$json = ConvertTo-Json @(@{
160+
actionType = 'STOP'
161+
virDomainUUID = $vmUUID
162+
})
163+
$result = Invoke-RestMethod @restOpts "$url/VirDomain/action" -Method POST -Body $json
164+
Wait-ScaleTask -TaskTag $($result.taskTag)
165+
166+
167+
168+
Write-Host "Update VM properties"
169+
$json = @{
170+
name = "VMLifecycleUpdated";
171+
description = "An updated example using the rest API";
172+
mem = 8GB
173+
numVCPU = 2
174+
} | ConvertTo-Json
175+
176+
$result = Invoke-RestMethod @restOpts "$url/VirDomain/$vmUUID" -Method PATCH -Body $json
177+
Wait-ScaleTask -TaskTag $($result.taskTag)
178+
179+
180+
181+
Write-Host "Clone VM"
182+
$json = @{
183+
template = @{
184+
name = "VMLifecycle-Clone";
185+
description = "An example VM cloned using the rest API"
186+
}
187+
} | ConvertTo-Json
188+
189+
$result = Invoke-RestMethod @restOpts "$url/VirDomain/$vmUUID/clone" -Method POST -Body $json
190+
$cloneUUID = $($result.createdUUID)
191+
Wait-ScaleTask -TaskTag $($result.taskTag)
192+
193+
194+
195+
Write-Host "Create VM snapshot"
196+
$json = @{
197+
domainUUID = $vmUUID;
198+
label = "This is a test snapshot created via rest API"
199+
} | ConvertTo-Json
200+
$result = Invoke-RestMethod @restOpts "$url/VirDomainSnapshot" -Method POST -Body $json
201+
Wait-ScaleTask -TaskTag $($result.taskTag)
202+
$snapUUID = $($result.createdUUID)
203+
204+
205+
206+
Write-Host "Delete VM snapshot"
207+
$result = Invoke-RestMethod @restOpts "$url/VirDomainSnapshot/$snapUUID" -Method DELETE
208+
Wait-ScaleTask -TaskTag $($result.taskTag)
209+
210+
211+
212+
Write-Host "Delete original VM"
213+
$result = Invoke-RestMethod @restOpts "$url/VirDomain/$vmUUID" -Method DELETE
214+
Wait-ScaleTask -TaskTag $($result.taskTag)
215+
216+
Write-Host "Delete cloned VM"
217+
$result = Invoke-RestMethod @restOpts "$url/VirDomain/$cloneUUID" -Method DELETE
218+
Wait-ScaleTask -TaskTag $($result.taskTag)

0 commit comments

Comments
 (0)