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

Commit 7f234a9

Browse files
author
Mihaela Stoica
authored
Merge pull request #113 from kc284/master
Some refactoring in the C# SDK generation code and the samples.
2 parents acf9a82 + 821c7ad commit 7f234a9

File tree

17 files changed

+410
-834
lines changed

17 files changed

+410
-834
lines changed

csharp/README.dist

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ B. To build the source code:
7171
3. You should now be ready to build the source code.
7272

7373
C. To run the examples:
74-
1. Copy CookComputing.XmlRpcV2.dll from the bin folder into the samples folder
75-
at the same level as the XenSdkSample.sln file.
74+
1. Copy XenServer.dll and CookComputing.XmlRpcV2.dll from the bin folder into
75+
the samples folder at the same level as the XenSdkSample.csproj file.
7676
2. Open XenSdkSample.sln inside Visual Studio (2013 or greater).
7777
3. You should now be ready to compile the solution and run the examples.
78-
Each of the solution projects is a console application expecting the parameters
78+
The solution project is a console application expecting the parameters
7979
<host> <username> <password> to be passed to its Main method.

csharp/gen_csharp_binding.ml

Lines changed: 13 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ let rec main() =
133133
List.iter (fun x -> if generated x then gen_class_file x) classes;
134134
TypeSet.iter gen_enum !enums;
135135
gen_maps();
136-
gen_i18n_errors();
137136
gen_http_actions();
138137
gen_relations();
139138
let sorted_members = List.sort String.compare !api_members in
@@ -326,8 +325,6 @@ using System;
326325
using System.Collections;
327326
using System.Collections.Generic;
328327
329-
using CookComputing.XmlRpc;
330-
331328
332329
namespace XenAPI
333330
{
@@ -714,7 +711,7 @@ and exposed_param p =
714711
and exposed_call_params message classname params =
715712
let exposedParams = List.map exposed_call_param params in
716713
let name = String.lowercase classname in
717-
let refParam = sprintf "(_%s != null) ? _%s : \"\"" name name in
714+
let refParam = sprintf "_%s ?? \"\"" name in
718715
let exposedParams = if is_method_static message then exposedParams else refParam::exposedParams in
719716
String.concat ", " ("session.uuid"::exposedParams)
720717

@@ -1273,8 +1270,8 @@ and convert_to_proxy thing ty = (*function*)
12731270
| Int -> sprintf "%s.ToString()" thing
12741271
| Bool
12751272
| Float -> thing
1276-
| Ref _ -> sprintf "(%s != null) ? %s : \"\"" thing thing
1277-
| String -> sprintf "(%s != null) ? %s : \"\"" thing thing
1273+
| Ref _ -> sprintf "%s ?? \"\"" thing
1274+
| String -> sprintf "%s ?? \"\"" thing
12781275
| Enum (name,_) -> sprintf "%s_helper.ToString(%s)" name thing
12791276
| Set (Ref name) -> sprintf "(%s != null) ? Helper.RefListToStringArray(%s) : new string[] {}" thing thing
12801277
| Set(String) -> thing
@@ -1320,159 +1317,24 @@ and is_static_readonly field =
13201317
| DynamicRO -> "false"
13211318
| _ -> "false"
13221319

1323-
and i18n_header out_chan =
1324-
let print format = fprintf out_chan format in
1325-
print
1326-
"<?xml version=\"1.0\" encoding=\"utf-8\"?>
1327-
<root>
1328-
<!--
1329-
Microsoft ResX Schema
1330-
1331-
Version 2.0
1332-
1333-
The primary goals of this format is to allow a simple XML format
1334-
that is mostly human readable. The generation and parsing of the
1335-
various data types are done through the TypeConverter classes
1336-
associated with the data types.
1337-
1338-
Example:
1339-
1340-
... ado.net/XML headers & schema ...
1341-
<resheader name=\"resmimetype\">text/microsoft-resx</resheader>
1342-
<resheader name=\"version\">2.0</resheader>
1343-
<resheader name=\"reader\">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
1344-
<resheader name=\"writer\">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
1345-
<data name=\"Name1\"><value>this is my long string</value><comment>this is a comment</comment></data>
1346-
<data name=\"Color1\" type=\"System.Drawing.Color, System.Drawing\">Blue</data>
1347-
<data name=\"Bitmap1\" mimetype=\"application/x-microsoft.net.object.binary.base64\">
1348-
<value>[base64 mime encoded serialized .NET Framework object]</value>
1349-
</data>
1350-
<data name=\"Icon1\" type=\"System.Drawing.Icon, System.Drawing\" mimetype=\"application/x-microsoft.net.object.bytearray.base64\">
1351-
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
1352-
<comment>This is a comment</comment>
1353-
</data>
1354-
1355-
There are any number of \"resheader\" rows that contain simple
1356-
name/value pairs.
1357-
1358-
Each data row contains a name, and value. The row also contains a
1359-
type or mimetype. Type corresponds to a .NET class that support
1360-
text/value conversion through the TypeConverter architecture.
1361-
Classes that don't support this are serialized and stored with the
1362-
mimetype set.
1363-
1364-
The mimetype is used for serialized objects, and tells the
1365-
ResXResourceReader how to depersist the object. This is currently not
1366-
extensible. For a given mimetype the value must be set accordingly:
1367-
1368-
Note - application/x-microsoft.net.object.binary.base64 is the format
1369-
that the ResXResourceWriter will generate, however the reader can
1370-
read any of the formats listed below.
1371-
1372-
mimetype: application/x-microsoft.net.object.binary.base64
1373-
value : The object must be serialized with
1374-
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
1375-
: and then encoded with base64 encoding.
1376-
1377-
mimetype: application/x-microsoft.net.object.soap.base64
1378-
value : The object must be serialized with
1379-
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
1380-
: and then encoded with base64 encoding.
1381-
1382-
mimetype: application/x-microsoft.net.object.bytearray.base64
1383-
value : The object must be serialized into a byte array
1384-
: using a System.ComponentModel.TypeConverter
1385-
: and then encoded with base64 encoding.
1386-
-->
1387-
<xsd:schema id=\"root\" xmlns=\"\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">
1388-
<xsd:import namespace=\"http://www.w3.org/XML/1998/namespace\" />
1389-
<xsd:element name=\"root\" msdata:IsDataSet=\"true\">
1390-
<xsd:complexType>
1391-
<xsd:choice maxOccurs=\"unbounded\">
1392-
<xsd:element name=\"metadata\">
1393-
<xsd:complexType>
1394-
<xsd:sequence>
1395-
<xsd:element name=\"value\" type=\"xsd:string\" minOccurs=\"0\" />
1396-
</xsd:sequence>
1397-
<xsd:attribute name=\"name\" use=\"required\" type=\"xsd:string\" />
1398-
<xsd:attribute name=\"type\" type=\"xsd:string\" />
1399-
<xsd:attribute name=\"mimetype\" type=\"xsd:string\" />
1400-
<xsd:attribute ref=\"xml:space\" />
1401-
</xsd:complexType>
1402-
</xsd:element>
1403-
<xsd:element name=\"assembly\">
1404-
<xsd:complexType>
1405-
<xsd:attribute name=\"alias\" type=\"xsd:string\" />
1406-
<xsd:attribute name=\"name\" type=\"xsd:string\" />
1407-
</xsd:complexType>
1408-
</xsd:element>
1409-
<xsd:element name=\"data\">
1410-
<xsd:complexType>
1411-
<xsd:sequence>
1412-
<xsd:element name=\"value\" type=\"xsd:string\" minOccurs=\"0\" msdata:Ordinal=\"1\" />
1413-
<xsd:element name=\"comment\" type=\"xsd:string\" minOccurs=\"0\" msdata:Ordinal=\"2\" />
1414-
</xsd:sequence>
1415-
<xsd:attribute name=\"name\" type=\"xsd:string\" use=\"required\" msdata:Ordinal=\"1\" />
1416-
<xsd:attribute name=\"type\" type=\"xsd:string\" msdata:Ordinal=\"3\" />
1417-
<xsd:attribute name=\"mimetype\" type=\"xsd:string\" msdata:Ordinal=\"4\" />
1418-
<xsd:attribute ref=\"xml:space\" />
1419-
</xsd:complexType>
1420-
</xsd:element>
1421-
<xsd:element name=\"resheader\">
1422-
<xsd:complexType>
1423-
<xsd:sequence>
1424-
<xsd:element name=\"value\" type=\"xsd:string\" minOccurs=\"0\" msdata:Ordinal=\"1\" />
1425-
</xsd:sequence>
1426-
<xsd:attribute name=\"name\" type=\"xsd:string\" use=\"required\" />
1427-
</xsd:complexType>
1428-
</xsd:element>
1429-
</xsd:choice>
1430-
</xsd:complexType>
1431-
</xsd:element>
1432-
</xsd:schema>
1433-
<resheader name=\"resmimetype\">
1434-
<value>text/microsoft-resx</value>
1435-
</resheader>
1436-
<resheader name=\"version\">
1437-
<value>2.0</value>
1438-
</resheader>
1439-
<resheader name=\"reader\">
1440-
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
1441-
</resheader>
1442-
<resheader name=\"writer\">
1443-
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
1444-
</resheader>
1445-
"
1446-
and i18n_footer out_chan =
1447-
let print format = fprintf out_chan format in
1448-
print
1449-
"</root>\n"
14501320
and gen_i18n_errors () =
14511321
Friendly_error_names.parse_sr_xml sr_xml;
14521322
Friendly_error_names.parse_resx resx_file;
1453-
let out_chan = open_out (Filename.concat destdir "FriendlyErrorNames.resx")
1323+
let errors = Friendly_error_names.friendly_names_all Datamodel.errors in
1324+
let json = `O [
1325+
"i18n_errors", `A (List.map (fun (x, y) ->
1326+
`O [
1327+
"i18n_error_key", `String x;
1328+
"i18n_error_description", `String y;
1329+
];) errors);
1330+
]
14541331
in
1455-
finally (fun () ->
1456-
i18n_header out_chan;
1457-
List.iter (gen_i18n_error_field out_chan)
1458-
(Friendly_error_names.friendly_names_all Datamodel.errors);
1459-
i18n_footer out_chan)
1460-
(fun () -> close_out out_chan)
1461-
1462-
1463-
and gen_i18n_error_field out_chan (error, desc) =
1464-
let print format = fprintf out_chan format in
1465-
(* Note that we can't use Xml.to_string for the whole block, because
1466-
we need the output to be whitespace-identical to what Visual Studio
1467-
would produce. We need to use it for the inner <value> though, to
1468-
get the escaping right. *)
1469-
print " <data name=\"%s\" xml:space=\"preserve\">\n %s\n </data>\n"
1470-
error
1471-
(Xml.to_string (Xml.Element("value", [], [(Xml.PCData desc)])))
1332+
render_file ("FriendlyErrorNames.mustache", "FriendlyErrorNames.resx") json templdir destdir
14721333

14731334
let populate_releases ()=
14741335
render_file ("ApiVersion.mustache", "ApiVersion.cs") json_releases templdir destdir
14751336

14761337
let _ =
14771338
main();
1339+
gen_i18n_errors();
14781340
populate_releases()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Linq;
3+
using XenAPI;
4+
5+
6+
namespace GetVmRecords
7+
{
8+
static class GetVariousRecords
9+
{
10+
public static void Run(Session session)
11+
{
12+
PrintTitle("Printing various records");
13+
PrintHostRecords(session);
14+
PrintStorageRepositories(session);
15+
PrintVmRecords(session);
16+
PrintPhysicalNetworkInterfaces(session);
17+
}
18+
19+
private static void PrintHostRecords(Session session)
20+
{
21+
PrintTitle("Hosts");
22+
var hostRecords = Host.get_all_records(session);
23+
24+
foreach (var hostRec in hostRecords)
25+
{
26+
var host = hostRec.Value;
27+
System.Console.WriteLine("Name: {0}", host.name_label);
28+
System.Console.WriteLine("Hostname: {0}", host.hostname);
29+
System.Console.WriteLine("Description: {0}", host.name_description);
30+
System.Console.WriteLine();
31+
}
32+
}
33+
34+
private static void PrintStorageRepositories(Session session)
35+
{
36+
PrintTitle("Storage Repositories");
37+
var srRecords = SR.get_all_records(session);
38+
39+
foreach (var srRec in srRecords)
40+
{
41+
var sr = srRec.Value;
42+
System.Console.WriteLine("Name: {0}", sr.name_label);
43+
System.Console.WriteLine("Description: {0}", sr.name_description);
44+
System.Console.WriteLine("Usage: {0:0.0}GB / {1:0.0}GB", sr.physical_utilisation / 1e9, sr.physical_size / 1e9);
45+
System.Console.WriteLine();
46+
}
47+
}
48+
49+
private static void PrintVmRecords(Session session)
50+
{
51+
PrintTitle("Virtual Machines");
52+
53+
var vmRecords = VM.get_all_records(session);
54+
foreach (var vmRec in vmRecords)
55+
{
56+
var vm = vmRec.Value;
57+
System.Console.WriteLine(vm.is_a_template ? "VM name: {0}" : "Template name {0}", vm.name_label);
58+
System.Console.WriteLine("Power state: {0}", vm.power_state);
59+
string ops = string.Join(",", vm.allowed_operations.Select(op => op.ToString()));
60+
System.Console.WriteLine("Allowed operations: {0}", ops);
61+
System.Console.WriteLine("vCPUs: {0}", vm.VCPUs_at_startup);
62+
System.Console.WriteLine();
63+
}
64+
}
65+
66+
private static void PrintPhysicalNetworkInterfaces(Session session)
67+
{
68+
PrintTitle("Physical network interfaces");
69+
var pifRecords = PIF.get_all_records(session);
70+
71+
foreach (var pifRec in pifRecords)
72+
{
73+
var pif = pifRec.Value;
74+
75+
Host host = Host.get_record(session, pif.host);
76+
System.Console.WriteLine("Host: {0}", host.name_label);
77+
System.Console.WriteLine("IP: {0}", pif.IP);
78+
System.Console.WriteLine("MAC address: {0}", pif.MAC);
79+
System.Console.WriteLine();
80+
}
81+
}
82+
83+
private static void PrintTitle(string title)
84+
{
85+
System.Console.WriteLine();
86+
System.Console.WriteLine("*** {0} ***", title);
87+
System.Console.WriteLine();
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)