forked from fifthecho/CloudStack-PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CloudStackClient.psm1
291 lines (276 loc) · 12.8 KB
/
CloudStackClient.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<#
.SYNOPSIS
A CloudStack/CloudPlatform API client.
.DESCRIPTION
A feature-rich Apache CloudStack/Citrix CloudPlatform API client for issuing commands to the Cloud Management system.
.PARAMETER command
The command parameter is MANDATORY and specifies which command you are wanting to run against the API.
.PARAMETER options
Optional command options that can be passed in to commands.
#>
# Writen by Jeff Moody ([email protected])
# Based off code written by Takashi Kanai ([email protected])
#
# 2011/9/16 v1.0 created
# 2013/5/13 v1.1 created to work with CloudPlatform 3.0.6 and migrated to entirely new codebase for maintainability and readability.
# 2013/5/17 v2.0 created to modularize everything.
# 2013/6/20 v2.1 created to add Powershell 2 support
# 2013/9/03 v2.5 created to add better error handling
[VOID][System.Reflection.Assembly]::Load("System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
$WebClient = New-Object net.WebClient
function New-CloudStack{
Param(
[Parameter(Mandatory = $true)]
[String] $apiEndpoint
,
[Parameter(Mandatory = $true)]
[String] $apiPublicKey
,
[Parameter(Mandatory = $true)]
[String] $apiSecretKey
)
$cloudStack = @()
$cloudStack += $apiEndpoint
$cloudStack += $apiPublicKey
$cloudStack += $apiSecretKey
return $cloudStack
}
Export-ModuleMember -Function New-CloudStack
function calculateSignature{
Param(
[Parameter(Mandatory=$true)]
[String[]]
$SECRET_KEY
,
[Parameter(Mandatory=$true)]
[String]
$HASH_STRING
)
Write-Debug("Hash String: $HASH_STRING")
Write-Debug("Signature: $SECRET_KEY")
$HMAC_SHA1 = New-Object System.Security.Cryptography.HMACSHA1
$HMAC_SHA1.key = [Text.Encoding]::ASCII.GetBytes($SECRET_KEY)
$Digest = $HMAC_SHA1.ComputeHash([Text.Encoding]::ASCII.GetBytes($HASH_STRING))
$Base64Digest = [System.Convert]::ToBase64String($Digest)
$signature = [System.Web.HttpUtility]::UrlEncode($Base64Digest)
Write-Debug("Digest: $Base64Digest")
Write-Debug("Signature: $signature")
return $signature
}
function Get-CloudStack{
Param(
[Parameter(Mandatory=$true)]
[String[]]
$cloudStack
,
[Parameter(Mandatory=$true)]
[String]
$command
,
[String[]]
$options
)
$ADDRESS = $cloudStack[0]
$API_KEY = $cloudStack[1]
$SECRET_KEY = $cloudStack[2]
$URL=$ADDRESS+"?apikey="+($API_KEY)+"&"+"command="+$command
$optionString="apikey="+($API_KEY)+"&"+"command="+$command
$options += "response=xml"
$options = $options | Sort-Object
foreach($o in $options){
$o = $o -replace " ", "%20"
$optionString += "&"+$o
$URL += "&"+$o
}
Write-Debug("Pre-signed URL: $URL")
Write-Debug("Option String: $optionString")
$signature = calculateSignature -SECRET_KEY $SECRET_KEY -HASH_STRING $optionString.ToLower()
$URL += "&signature="+$signature
Write-Debug("URL: $URL")
$Response = ""
try {
if ($psversiontable.psversion.Major -ge 3) {
$Response = Invoke-RestMethod -Uri $URL -Method Get -ErrorAction Stop -ErrorVariable ErrorOut
Write-Debug $Response
}
else {
$httpWebRequest = [System.Net.WebRequest]::Create($URL);
$httpWebRequest.Method = "GET";
$httpWebRequest.Headers.Add("Accept-Language: en-US");
try {
$httpWebResponse = $httpWebRequest.GetResponse();
}
catch [System.Net.WebException] {
$ErrorOut = $_.Exception.Response
}
$responseStream = $httpWebResponse.GetResponseStream();
$streamReader = New-Object System.IO.StreamReader($responseStream);
$temp = $streamReader.ReadToEnd();
$Response = [xml]$temp
$responseStream.Close();
$streamReader.Close();
$httpWebResponse.Close();
Write-Debug $Response
}
}
catch{
Write-Error "ERROR!"
$errorType = $ErrorOut.GetType()
if ($errorType.Name -eq "ArrayList") {
Write-Error $ErrorOut[0]
}
else {
Write-Error $ErrorOut
}
Write-Error "If this is unclear, please go to $URL in a browser for the API response."
}
Write-Debug "Response: $Response"
return $Response
}
Export-ModuleMember -Function Get-CloudStack
function Import-CloudStackConfig{
# Read configuration values for API Endpoint and keys
$ChkFile = "$env:userprofile\cloud-settings.txt"
$FileExists = (Test-Path $ChkFile -PathType Leaf)
If (!($FileExists))
{
Write-Error "Config file does not exist. Writing a basic config that you now need to customize."
Write-Output "[general]`n" | Out-File $ChkFile
Write-Output "Address=http://(your URL):8080/client/api`n" | Out-File -Append $ChkFile
Write-Output "ApiKey=(Your API Key)`n" | Out-File -Append $ChkFile
Write-Output "SecretKey=(Your Secret Key)" | Out-File -Append $ChkFile
Return 1
}
ElseIf ($FileExists)
{
Get-Content "$env:userprofile\cloud-settings.txt" | foreach-object -begin {$h=@{}} -process { $k = [regex]::split($_,'='); if(($k[0].CompareTo("") -ne 0) -and ($k[0].StartsWith("[") -ne $True)) { $h.Add($k[0], $k[1]) } }
$ADDRESS=$h.Get_Item("Address")
$API_KEY=$h.Get_Item("ApiKey")
$SECRET_KEY=$h.Get_Item("SecretKey")
Write-Debug "Address: $ADDRESS"
Write-Debug "API Key: $API_KEY"
Write-Debug "Secret Key: $SECRET_KEY"
$config = @()
$config += $ADDRESS
$config += $API_KEY
$config += $SECRET_KEY
if (($ADDRESS -ne "http://(your URL:8080/client/api?") -and ($API_KEY -ne "(Your API Key)") -and ($SECRET_KEY -ne "(Your Secret Key)")) {
return $config
}
else {
Write-Error "Please configure the $env:userprofile\cloud-settings.txt file"
return 1
}
}
}
Export-ModuleMember -Function Import-CloudstackConfig
function Get-CloudStackUserData{
Param(
[Parameter(Mandatory=$true)]
[String]
$userdata
)
Write-Debug "User Data: $userdata"
$userdatab64 = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($userdata))
Write-Debug "Base64 Encoded User Data: $userdatab64"
$encodeduserdata = [System.Web.HttpUtility]::UrlEncode($userdatab64)
return $encodeduserdata
}
Export-ModuleMember -Function Get-CloudStackUserData
# SIG # Begin signature block
# MIIRpQYJKoZIhvcNAQcCoIIRljCCEZICAQExCzAJBgUrDgMCGgUAMGkGCisGAQQB
# gjcCAQSgWzBZMDQGCisGAQQBgjcCAR4wJgIDAQAABBAfzDtgWUsITrck0sYpfvNR
# AgEAAgEAAgEAAgEAAgEAMCEwCQYFKw4DAhoFAAQUEeT6811QixaRHhyJD4NmUsVz
# MVWggg3aMIIGcDCCBFigAwIBAgIBJDANBgkqhkiG9w0BAQUFADB9MQswCQYDVQQG
# EwJJTDEWMBQGA1UEChMNU3RhcnRDb20gTHRkLjErMCkGA1UECxMiU2VjdXJlIERp
# Z2l0YWwgQ2VydGlmaWNhdGUgU2lnbmluZzEpMCcGA1UEAxMgU3RhcnRDb20gQ2Vy
# dGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMDcxMDI0MjIwMTQ2WhcNMTcxMDI0MjIw
# MTQ2WjCBjDELMAkGA1UEBhMCSUwxFjAUBgNVBAoTDVN0YXJ0Q29tIEx0ZC4xKzAp
# BgNVBAsTIlNlY3VyZSBEaWdpdGFsIENlcnRpZmljYXRlIFNp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