Skip to content
This repository was archived by the owner on Feb 19, 2025. It is now read-only.

Commit 73c76be

Browse files
author
Konstantina Chremmou
committed
Merge pull request #6 from kc284/master
CP-4823: Exposed http calls in the Powershell SDK; also added an example...
2 parents 5c0118e + 55d8700 commit 73c76be

File tree

9 files changed

+357
-14
lines changed

9 files changed

+357
-14
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
1.1.0 (23 Jul 2013)
2+
-------------------
3+
4+
* Exposed XenAPI HTTP calls in the PowerShell SDK.
5+
* Removed PowerShell Snap-In installer. Ship as PowerShell Module instead.
6+
* Added PowerShell example script HttpTest.ps1. Updated AutomatedTestCore.ps1.
7+
* Added C# overload for Host.apply_edition.
8+
9+
1.0.0 (17 Jun 2013)
10+
-------------------
11+
12+
* First public release.

ChangeLog.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.1.0

mk/powershell.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ then
5151
remote_cmd_passwd2 "cd ${TMPDIR} && ${CMD_EXEC} sign.bat XenServerPowerShell.dll 'Citrix XenServer PowerShell Module'"
5252
remote_cmd_passwd2 "cd ${TMPDIR} && ${CMD_EXEC} sign-ps.bat Initialize-Environment.ps1"
5353
remote_cmd_passwd2 "cd ${TMPDIR} && ${CMD_EXEC} sign-ps.bat AutomatedTestCore.ps1"
54+
remote_cmd_passwd2 "cd ${TMPDIR} && ${CMD_EXEC} sign-ps.bat HttpTest.ps1"
5455
remote_cmd_passwd2 "cd ${TMPDIR} && ${CMD_EXEC} sign-ps.bat XenServer.format.ps1xml"
5556
remote_cmd_passwd2 "cd ${TMPDIR} && ${CMD_EXEC} sign-ps.bat XenServer.types.ps1xml"
5657
fi
5758

58-
EXTRA_FILES="AutomatedTestCore.ps1 Initialize-Environment.ps1 XenServer.format.ps1xml XenServer.types.ps1xml"
59+
EXTRA_FILES="AutomatedTestCore.ps1 HttpTest.ps1 Initialize-Environment.ps1 XenServer.format.ps1xml XenServer.types.ps1xml"

powershell/XenServerPSModule.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ FormatsToProcess = @('XenServer.format.ps1xml')
6060
FileList = @('about_XenServer.help.txt',
6161
'AutomatedTestCore.ps1',
6262
'CookComputing.XmlRpcV2.dll',
63+
'HttpTest.ps1',
6364
'Initialize-Environment.ps1',
6465
'LICENSE.CookComputing.XmlRpcV2',
6566
'LICENSE.txt',

powershell/common_functions.ml

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ and cut_msg_name message_name fn_type =
210210
end
211211
else if (fn_type = "Remove") then
212212
begin
213-
if (name_len > 10)&& (String.sub message_name 0 10) = "RemoveFrom" then
213+
if (name_len > 10) && (String.sub message_name 0 10) = "RemoveFrom" then
214214
String.sub message_name 10 (name_len - 10)
215215
else if (name_len > 6) && (String.sub message_name 0 6) = "Remove" then
216216
String.sub message_name 6 (name_len - 6)
@@ -227,3 +227,41 @@ and has_uuid x =
227227

228228
and has_name x =
229229
DU.obj_has_get_by_name_label x
230+
231+
and get_http_action_verb name meth =
232+
let parts = String.split '_' name in
233+
if (List.exists (fun x -> x = "import") parts) then "Import"
234+
else if (List.exists (fun x -> x = "export") parts) then "Export"
235+
else if (List.exists (fun x -> x = "get") parts) then "Receive"
236+
else if (List.exists (fun x -> x = "put") parts) then "Send"
237+
else
238+
match meth with
239+
| Get -> "Receive"
240+
| Put -> "Send"
241+
| _ -> assert false
242+
243+
and get_common_verb_category verb =
244+
match verb with
245+
| "Import"
246+
| "Export" -> "VerbsData"
247+
| "Receive"
248+
| "Send" -> "VerbsCommunications"
249+
| _ -> assert false
250+
251+
and get_http_action_stem name =
252+
let parts = String.split '_' name in
253+
let filtered = List.filter trim_http_action_stem parts in
254+
let trimmed = String.concat "_" filtered in
255+
match trimmed with
256+
| "" -> pascal_case_ "vm"
257+
| _ -> pascal_case_ trimmed
258+
259+
and trim_http_action_stem x =
260+
match x with
261+
| "get"
262+
| "put"
263+
| "import"
264+
| "export"
265+
| "download"
266+
| "upload" -> false
267+
| _ -> true

powershell/gen_powershell_binding.ml

Lines changed: 135 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,133 @@ let generated x =
9191
let rec main() =
9292
List.iter (fun x -> if generated x then gen_binding x) classes;
9393
gen_xenref_converters classes;
94+
List.iter gen_http_action
95+
(List.filter (fun (_, (_, _, sdk, _, _, _)) -> sdk) http_actions)
96+
97+
(****************)
98+
(* Http actions *)
99+
(****************)
100+
101+
and gen_http_action action =
102+
match action with (name, (meth, uri, _, args, _, _)) ->
103+
let commonVerb = get_http_action_verb name meth in
104+
let verbCategory = get_common_verb_category commonVerb in
105+
let stem = get_http_action_stem name in
106+
let content = sprintf "%s
107+
108+
using System;
109+
using System.Collections;
110+
using System.Collections.Generic;
111+
using System.Management.Automation;
112+
using System.Net;
113+
using System.Text;
114+
115+
using XenAPI;
116+
117+
namespace Citrix.XenServer.Commands
118+
{
119+
[Cmdlet(%s.%s, \"Xen%s\"%s)]
120+
[OutputType(typeof(void))]
121+
public class %sXen%sCommand : XenServerHttpCmdlet
122+
{
123+
#region Cmdlet Parameters
124+
%s%s
125+
#endregion
126+
127+
#region Cmdlet Methods
128+
129+
protected override void ProcessRecord()
130+
{
131+
GetSession();
132+
%s
133+
RunApiCall(() => %s);
134+
}
135+
136+
#endregion
137+
}
138+
}\n" Licence.bsd_two_clause
139+
verbCategory commonVerb stem (gen_should_process_http_decl meth)
140+
commonVerb stem
141+
(gen_progress_tracker meth)
142+
(gen_arg_params args)
143+
(gen_should_process_http meth uri)
144+
(gen_http_action_call action)
145+
in
146+
write_file (sprintf "%s-Xen%s.cs" commonVerb stem) content
147+
148+
and gen_should_process_http_decl meth =
149+
match meth with
150+
| Put -> ", SupportsShouldProcess = true"
151+
| Get -> ", SupportsShouldProcess = false"
152+
| _ -> assert false
153+
154+
and gen_should_process_http meth uri =
155+
match meth with
156+
| Put -> sprintf "
157+
if (!ShouldProcess(\"%s\"))
158+
return;\n" uri
159+
| _ -> ""
160+
161+
and gen_progress_tracker meth =
162+
match meth with
163+
| Get -> "
164+
[Parameter]
165+
public HTTP.DataCopiedDelegate DataCopiedDelegate { get; set; }\n"
166+
| Put -> "
167+
[Parameter]
168+
public HTTP.UpdateProgressDelegate ProgressDelegate { get; set; }\n"
169+
| _ -> assert false
170+
171+
and gen_arg_params args =
172+
match args with
173+
| [] -> ""
174+
| hd::tl -> sprintf "%s%s" (gen_arg_param hd) (gen_arg_params tl)
175+
176+
and gen_arg_param = function
177+
| String_query_arg x -> sprintf "
178+
[Parameter%s]
179+
public string %s { get; set; }\n"
180+
(if (String.lowercase x) = "uuid" then
181+
"(ValueFromPipelineByPropertyName = true)"
182+
else "")
183+
(pascal_case_ x)
184+
| Int64_query_arg x -> sprintf "
185+
[Parameter]
186+
public long %s { get; set; }\n" (pascal_case_ x)
187+
| Bool_query_arg x ->
188+
let y = if x = "host" then "is_host" else x in
189+
sprintf "
190+
[Parameter]
191+
public bool %s { get; set; }\n" (pascal_case_ y)
192+
| Varargs_query_arg -> sprintf "
193+
///<summary>
194+
/// Alternate names & values
195+
///</summary>
196+
[Parameter]
197+
public string[] Args { get; set; }\n"
198+
199+
and gen_http_action_call (name, (meth, _, _, args, _, _)) =
200+
let progressTracker = match meth with
201+
| Get -> "DataCopiedDelegate"
202+
| Put -> "ProgressDelegate"
203+
| _ -> assert false in
204+
sprintf "XenAPI.HTTP_actions.%s(%s,
205+
CancellingDelegate, TimeoutMs, XenHost, Proxy, Path, TaskRef,
206+
session.opaque_ref%s)"
207+
name progressTracker (gen_call_arg_params args)
208+
209+
and gen_call_arg_params args =
210+
match args with
211+
| [] -> ""
212+
| hd::tl -> sprintf "%s%s" (gen_call_arg_param hd) (gen_call_arg_params tl)
213+
214+
and gen_call_arg_param = function
215+
| String_query_arg x -> sprintf ", %s" (pascal_case_ x)
216+
| Int64_query_arg x -> sprintf ", %s" (pascal_case_ x)
217+
| Bool_query_arg x ->
218+
let y = if x = "host" then "is_host" else x in
219+
sprintf ", %s" (pascal_case_ y)
220+
| Varargs_query_arg -> sprintf ", Args"
94221

95222

96223
(***********************************)
@@ -223,7 +350,7 @@ using XenAPI;
223350
224351
namespace Citrix.XenServer.Commands
225352
{
226-
[Cmdlet(VerbsCommon.Get, \"Xen%s\", DefaultParameterSetName = \"Ref\")]
353+
[Cmdlet(VerbsCommon.Get, \"Xen%s\", DefaultParameterSetName = \"Ref\", SupportsShouldProcess = false)]
227354
[OutputType(typeof(%s[]))]
228355
public class GetXen%sCommand : XenServerCmdlet
229356
{\n"
@@ -341,7 +468,7 @@ using XenAPI;
341468
342469
namespace Citrix.XenServer.Commands
343470
{
344-
[Cmdlet(VerbsCommon.New, \"Xen%s\", SupportsShouldProcess=true)]
471+
[Cmdlet(VerbsCommon.New, \"Xen%s\", SupportsShouldProcess = true)]
345472
[OutputType(typeof(%s))]%s
346473
[OutputType(typeof(void))]
347474
public class NewXen%sCommand : XenServerCmdlet
@@ -562,7 +689,7 @@ using XenAPI;
562689
563690
namespace Citrix.XenServer.Commands
564691
{
565-
[Cmdlet(VerbsCommon.Remove, \"Xen%s\", SupportsShouldProcess=true)]
692+
[Cmdlet(VerbsCommon.Remove, \"Xen%s\", SupportsShouldProcess = true)]
566693
[OutputType(typeof(%s))]%s
567694
[OutputType(typeof(void))]
568695
public class RemoveXen%s : XenServerCmdlet
@@ -639,7 +766,7 @@ using XenAPI;
639766
640767
namespace Citrix.XenServer.Commands
641768
{
642-
[Cmdlet(VerbsCommon.Remove, \"Xen%sProperty\", SupportsShouldProcess=true)]
769+
[Cmdlet(VerbsCommon.Remove, \"Xen%sProperty\", SupportsShouldProcess = true)]
643770
[OutputType(typeof(%s))]%s
644771
public class RemoveXen%sProperty : XenServerCmdlet
645772
{
@@ -709,7 +836,7 @@ using XenAPI;
709836
710837
namespace Citrix.XenServer.Commands
711838
{
712-
[Cmdlet(VerbsCommon.Set, \"Xen%s\", SupportsShouldProcess=true)]
839+
[Cmdlet(VerbsCommon.Set, \"Xen%s\", SupportsShouldProcess = true)]
713840
[OutputType(typeof(%s))]%s
714841
[OutputType(typeof(void))]
715842
public class SetXen%s : XenServerCmdlet
@@ -780,7 +907,7 @@ using XenAPI;
780907
781908
namespace Citrix.XenServer.Commands
782909
{
783-
[Cmdlet(VerbsCommon.Add, \"Xen%s\", SupportsShouldProcess=true)]
910+
[Cmdlet(VerbsCommon.Add, \"Xen%s\", SupportsShouldProcess = true)]
784911
[OutputType(typeof(%s))]%s
785912
[OutputType(typeof(void))]
786913
public class AddXen%s : XenServerCmdlet
@@ -850,7 +977,7 @@ using XenAPI;
850977
851978
namespace Citrix.XenServer.Commands
852979
{
853-
[Cmdlet(VerbsLifecycle.Invoke, \"Xen%s\", SupportsShouldProcess=true)]
980+
[Cmdlet(VerbsLifecycle.Invoke, \"Xen%s\", SupportsShouldProcess = true)]
854981
public class InvokeXen%s : XenServerCmdlet
855982
{
856983
#region Cmdlet Parameters
@@ -924,7 +1051,7 @@ using XenAPI;
9241051
9251052
namespace Citrix.XenServer.Commands
9261053
{
927-
[Cmdlet(VerbsCommon.Get, \"Xen%sProperty\", SupportsShouldProcess=false)]
1054+
[Cmdlet(VerbsCommon.Get, \"Xen%sProperty\", SupportsShouldProcess = false)]
9281055
public class GetXen%sProperty : XenServerCmdlet
9291056
{
9301057
#region Cmdlet Parameters

powershell/samples/HttpTest.ps1

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
#
2+
# Copyright (c) Citrix Systems, Inc.
3+
# All rights reserved.
4+
#
5+
# Redistribution and use in source and binary forms, with or without
6+
# modification, are permitted provided that the following conditions
7+
# are met:
8+
#
9+
# 1) Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
#
12+
# 2) Redistributions in binary form must reproduce the above
13+
# copyright notice, this list of conditions and the following
14+
# disclaimer in the documentation and/or other materials
15+
# provided with the distribution.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20+
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21+
# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22+
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26+
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28+
# OF THE POSSIBILITY OF SUCH DAMAGE.
29+
#
30+
31+
32+
Param([Parameter(Mandatory=$true)][String]$svr,
33+
[Parameter(Mandatory=$true)][String]$usr,
34+
[Parameter(Mandatory=$true)][String]$pwd,
35+
[Parameter(Mandatory=$true)][String]$patchPath)
36+
37+
### Connect to a server
38+
39+
Connect-XenServer -Server $svr -UserName $usr -Password $pwd
40+
41+
42+
### Create a VM
43+
44+
$template = @(Get-XenVM -Name 'Windows XP*' | where {$_.is_a_template})[0]
45+
46+
Invoke-XenVM -VM $template -XenAction Clone -NewName "testVM" -Async `
47+
-PassThru | Wait-XenTask -ShowProgress
48+
49+
$vm = Get-XenVM -Name "testVM"
50+
$sr = Get-XenSR -Ref (Get-XenPool).default_SR
51+
$other_config = $vm.other_config
52+
$other_config["disks"] = $other_config["disks"].Replace('sr=""', 'sr="{0}"' -f $sr.uuid)
53+
54+
New-XenVBD -VM $vm -VDI $null -Userdevice 3 -Bootable $false -Mode RO `
55+
-Type CD -Unpluggable $true -Empty $true -OtherConfig @{} `
56+
-QosAlgorithmType "" -QosAlgorithmParams @{}
57+
58+
Set-XenVM -VM $vm -OtherConfig $other_config
59+
Invoke-XenVM -VM $vm -XenAction Provision -Async -PassThru | Wait-XenTask -ShowProgress
60+
61+
62+
# Export the VM using the DataCopiedDelegate parameter to track bytes received
63+
64+
$path = $env:TEMP + "\vm.xva"
65+
66+
$trackDataReceived = [XenAPI.HTTP+DataCopiedDelegate]{
67+
param($bytes);
68+
Write-Host "Bytes received: $bytes" }
69+
70+
Export-XenVm -XenHost $svr -Uuid $vm.uuid -Path $path -DataCopiedDelegate $trackDataReceived
71+
72+
$vm | Remove-XenVM
73+
74+
75+
### Import the previously exported VM using the ProgressDelegate parameter to track send progress
76+
77+
$trackProgress = [XenAPI.HTTP+UpdateProgressDelegate]{
78+
param($percent);
79+
Write-Progress -Activity "Importing Vm..." -PercentComplete $percent }
80+
81+
Import-XenVm -XenHost $svr -Path $path -ProgressDelegate $trackProgress
82+
83+
84+
### Upload a patch
85+
86+
$trackProgress = [XenAPI.HTTP+UpdateProgressDelegate]{
87+
param($percent);
88+
Write-Progress -Activity "Uploading patch..." -PercentComplete $percent }
89+
90+
Send-XenPoolPatch -XenHost $svr -Path $patchPath
91+
92+
93+
### Get host RRDs
94+
95+
$path = $env:TEMP + "\rrd.xml"
96+
Receive-XenHostRrd -XenHost $svr -Path $path -DataCopiedDelegate $trackDataReceived
97+
98+
99+
### Disconnect before finishing
100+
101+
Get-XenSession | Disconnect-XenServer

0 commit comments

Comments
 (0)