Skip to content

Commit

Permalink
Moved PS destructors to a template.
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantina Chremmou <Konstantina.Chremmou@cloud.com>

# Conflicts:
#	ocaml/sdk-gen/powershell/gen_powershell_binding.ml
  • Loading branch information
kc284 committed Apr 10, 2024
1 parent c7dd5fb commit 24cc4a7
Show file tree
Hide file tree
Showing 2 changed files with 205 additions and 77 deletions.
101 changes: 24 additions & 77 deletions ocaml/sdk-gen/powershell/gen_powershell_binding.ml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ let rec main () =
let cmdlets =
classes |> List.filter generated |> List.map gen_cmdlets |> List.concat
in
List.iter (fun x -> write_file x.filename x.content) cmdlets
List.iter (fun x -> write_file x.filename x.content) cmdlets ;

classes |> List.filter generated |> List.iter gen_destructor

(****************)
(* Http actions *)
Expand Down Expand Up @@ -178,11 +180,6 @@ and gen_cmdlets obj =
; content=
gen_constructor obj classname (List.filter is_constructor messages)
}
; {
filename= sprintf "Remove-Xen%s.cs" stem
; content=
gen_destructor obj classname (List.filter is_destructor messages)
}
; {
filename= sprintf "Remove-Xen%sProperty.cs" stem
; content= gen_remover obj classname (List.filter is_remover messages)
Expand Down Expand Up @@ -598,84 +595,34 @@ and convert_from_hashtable fname ty =
(************************************)
(* Print function for Remove-XenFoo *)
(************************************)
and gen_destructor obj classname messages =
match messages with

and gen_destructor obj =
let {name= classname; messages; _} = obj in
let destructors = List.filter is_destructor messages in
match destructors with
| [] ->
""
()
| [x] ->
let cut_message_name x = cut_msg_name (pascal_case x.msg_name) "Remove" in
let asyncMessages =
List.map cut_message_name (List.filter (fun x -> x.msg_async) messages)
let json =
`O
[
("type", `String (exposed_class_name classname))
; ("class_name", `String (ocaml_class_to_csharp_class classname))
; ("property", `String (ocaml_class_to_csharp_property classname))
; ("type_local", `String (ocaml_class_to_csharp_local_var classname))
; ("async", `Bool x.msg_async)
; ("has_uuid", `Bool (has_uuid obj))
; ("has_name", `Bool (has_name obj))
]
in
sprintf
"%s\n\n\
using System;\n\
using System.Collections;\n\
using System.Collections.Generic;\n\
using System.Management.Automation;\n\
using XenAPI;\n\n\
namespace Citrix.XenServer.Commands\n\
{\n\
\ [Cmdlet(VerbsCommon.Remove, \"Xen%s\", SupportsShouldProcess = \
true)]\n\
\ [OutputType(typeof(%s))]%s\n\
\ [OutputType(typeof(void))]\n\
\ public class RemoveXen%s : XenServerCmdlet\n\
\ {\n\
\ #region Cmdlet Parameters\n\n\
\ [Parameter]\n\
\ public SwitchParameter PassThru { get; set; }\n\
%s%s\n\
\ #endregion\n\n\
\ #region Cmdlet Methods\n\n\
\ protected override void ProcessRecord()\n\
\ {\n\
\ GetSession();\n\n\
\ string %s = Parse%s();\n\n\
\ %s\n\n\
\ UpdateSessions();\n\
\ }\n\n\
\ #endregion\n\n\
\ #region Private Methods\n\
%s%s\n\
\ #endregion\n\
\ }\n\
}\n"
Licence.bsd_two_clause
(ocaml_class_to_csharp_class classname)
(qualified_class_name classname)
( if List.length asyncMessages > 0 then
"\n [OutputType(typeof(XenAPI.Task))]"
else
""
)
(ocaml_class_to_csharp_class classname)
(print_xenobject_params obj classname true true true)
( if List.length asyncMessages > 0 then
sprintf
"\n\
\ protected override bool GenerateAsyncParam\n\
\ {\n\
\ get { return true; }\n\
\ }\n"
else
""
)
(ocaml_class_to_csharp_local_var classname)
(ocaml_class_to_csharp_property classname)
(print_cmdlet_methods_remover classname x)
(print_parse_xenobject_private_method obj classname true)
(print_process_record_private_methods classname messages "Remove"
"asyncpassthru"
render_file
( "Remove-XenObject.mustache"
, sprintf "Remove-Xen%s.cs" (ocaml_class_to_csharp_class classname)
)
json templdir destdir
| _ ->
assert false

and print_cmdlet_methods_remover classname message =
let localVar = ocaml_class_to_csharp_local_var classname in
let cut_message_name x = cut_msg_name (pascal_case x.msg_name) "Remove" in
sprintf "ProcessRecord%s(%s);" (cut_message_name message) localVar

(*****************************************)
(* Print function for Remove-XenFoo -Bar *)
(*****************************************)
Expand Down
181 changes: 181 additions & 0 deletions ocaml/sdk-gen/powershell/templates/Remove-XenObject.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/*
* Copyright (c) Cloud Software Group, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1) Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2) Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/


using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using XenAPI;

namespace Citrix.XenServer.Commands
{
[Cmdlet(VerbsCommon.Remove, "Xen{{class_name}}", SupportsShouldProcess = true)]
[OutputType(typeof({{type}}))]
{{#async}}
[OutputType(typeof(Task))]
{{/async}}
[OutputType(typeof(void))]
public class RemoveXen{{class_name}} : XenServerCmdlet
{
#region Cmdlet Parameters
[Parameter]
public SwitchParameter PassThru { get; set; }

[Parameter(ParameterSetName = "XenObject", Mandatory = true, ValueFromPipeline = true, Position = 0)]
public {{type}} {{property}} { get; set; }

[Parameter(ParameterSetName = "Ref", Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
[Alias("opaque_ref")]
public XenRef<{{type}}> Ref { get; set; }

{{#has_uuid}}
[Parameter(ParameterSetName = "Uuid", Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
public Guid Uuid { get; set; }

{{/has_uuid}}
{{#has_name}}
[Parameter(ParameterSetName = "Name", Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
[Alias("name_label")]
public string Name { get; set; }

{{/has_name}}
{{#async}}
protected override bool GenerateAsyncParam => true;

{{/async}}
#endregion

#region Cmdlet Methods

protected override void ProcessRecord()
{
GetSession();
string {{type_local}} = Parse{{class_name}}();

ProcessRecordDestroy({{type_local}});

UpdateSessions();
}

#endregion

#region Private Methods

private string Parse{{class_name}}()
{
string {{type_local}} = null;

if ({{property}} != null)
{
{{type_local}} = (new XenRef<{{type}}>({{property}})).opaque_ref;
}
{{#has_uuid}}
else if (Uuid != Guid.Empty)
{
var xenRef = {{type}}.get_by_uuid(session, Uuid.ToString());
if (xenRef != null)
{{type_local}} = xenRef.opaque_ref;
}
{{/has_uuid}}
{{#has_name}}
else if (Name != null)
{
var xenRefs = {{type}}.get_by_name_label(session, Name);
if (xenRefs.Count == 1)
{{type_local}} = xenRefs[0].opaque_ref;
else if (xenRefs.Count > 1)
ThrowTerminatingError(new ErrorRecord(
new ArgumentException(string.Format("More than one {{type}} with name label {0} exist", Name)),
string.Empty,
ErrorCategory.InvalidArgument,
Name));
}
{{/has_name}}
else if (Ref != null)
{
{{type_local}} = Ref.opaque_ref;
}
else
{
ThrowTerminatingError(new ErrorRecord(
new ArgumentException("At least one of the parameters '{{property}}', 'Ref', 'Uuid' must be set"),
string.Empty,
ErrorCategory.InvalidArgument,
{{property}}));
}

return {{type_local}};
}

private void ProcessRecordDestroy(string {{type_local}})
{
if (!ShouldProcess({{type_local}}, "{{type}}.destroy"))
return;

{{#async}}
RunApiCall(()=>
{
var contxt = _context as XenServerCmdletDynamicParameters;
if (contxt != null && contxt.Async)
{
taskRef = {{type}}.async_destroy(session, {{type_local}});

if (PassThru)
{
Task taskObj = null;
if (taskRef != "OpaqueRef:NULL")
{
taskObj = Task.get_record(session, taskRef.opaque_ref);
taskObj.opaque_ref = taskRef.opaque_ref;
}

WriteObject(taskObj, true);
}
}
else
{
{{type}}.destroy(session, {{type_local}});
}
});
{{/async}}
{{^async}}
RunApiCall(()=>
{
{{type}}.destroy(session, {{type_local}});
});
{{/async}}
}

#endregion
}
}

0 comments on commit 24cc4a7

Please sign in to comment.