Skip to content
Merged
8 changes: 7 additions & 1 deletion build/Eryph.ComputeClient.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ CmdletsToExport = @(
"New-Catlet",
"Remove-Catlet",
"Update-Catlet",
"Deploy-Catlet",
"Start-Catlet",
"Stop-Catlet",
"Get-CatletIp",
Expand All @@ -95,7 +96,12 @@ CmdletsToExport = @(
"Add-EryphProjectMemberRole",
"Get-EryphProjectMemberRole",
"Remove-EryphProjectMemberRole",
"Test-Catlet"
"Test-Catlet",
"Get-CatletSpecification",
"New-CatletSpecification",
"Update-CatletSpecification",
"Remove-CatletSpecification",
"Get-CatletSpecificationVersion"
)

# Variables to export from this module
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Eryph.ComputeClient.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;

namespace Eryph.ComputeClient.Commands.CatletSpecifications;

[PublicAPI]
public abstract class CatletSpecificationCmdlet : ComputeCmdLet
{
protected CatletSpecification GetSingleCatletSpecification(string id)
{
return Factory.CreateCatletSpecificationsClient().Get(id);
}

protected void WaitForOperation(
Operation operation,
bool noWait,
bool alwaysWriteSpecification,
string knownSpecificationId = null)
{
if (noWait)
{
if (knownSpecificationId == null || !alwaysWriteSpecification)
WriteObject(operation);
else
WriteObject(GetSingleCatletSpecification(knownSpecificationId));
return;
}

var completedOperation = WaitForOperation(operation);
WriteResources(completedOperation, ResourceType.CatletSpecification);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using Eryph.ComputeClient.Models;
using Eryph.ConfigModel.Json;
using Eryph.ConfigModel.Yaml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Text;
using System.Threading.Tasks;
using JetBrains.Annotations;

namespace Eryph.ComputeClient.Commands.CatletSpecifications;

[PublicAPI]
[Cmdlet(VerbsCommon.Get, "CatletSpecification", DefaultParameterSetName = "get")]
[OutputType(typeof(CatletSpecification), ParameterSetName = ["get"])]
[OutputType(typeof(CatletSpecification), ParameterSetName = ["list"])]
public class GetCatletSpecification : CatletSpecificationCmdlet
{
[Parameter(
ParameterSetName = "get",
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
public string[] Id { get; set; }

[Parameter(
ParameterSetName = "list",
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

protected override void ProcessRecord()
{
if (Id is not null)
{
foreach (var id in Id)
{
WriteObject(GetSingleCatletSpecification(id));
}

return;
}

var projectId = GetProjectId(ProjectName);
var specifications = Factory.CreateCatletSpecificationsClient().List(projectId: projectId);
foreach (var specification in specifications)
{
if (Stopping) break;

WriteObject(specification, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Management.Automation;
using Eryph.ComputeClient.Models;
using JetBrains.Annotations;

namespace Eryph.ComputeClient.Commands.CatletSpecifications;

[PublicAPI]
[Cmdlet(VerbsCommon.Get, "CatletSpecificationVersion", DefaultParameterSetName = "get")]
[OutputType(typeof(CatletSpecificationVersion), ParameterSetName = ["get"])]
[OutputType(typeof(CatletSpecificationVersionInfo), ParameterSetName = ["list"])]
public class GetCatletSpecificationVersion : CatletSpecificationCmdlet
{
[Parameter(
ParameterSetName = "get",
Position = 0,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
public string[] Id { get; set; }

[Parameter(
ParameterSetName = "list",
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true)]
[Parameter(ParameterSetName = "get", Mandatory = true)]
[ValidateNotNullOrEmpty]
public string SpecificationId { get; set; }

protected override void ProcessRecord()
{
if (Id is not null)
{
var specificationsClient = Factory.CreateCatletSpecificationsClient();
foreach (var id in Id)
{
WriteObject(specificationsClient.GetVersion(SpecificationId, id).Value);
}

return;
}

var specificationVersions = Factory.CreateCatletSpecificationsClient()
.ListVersions(specificationId: SpecificationId);
foreach (var specification in specificationVersions)
{
if (Stopping) break;

WriteObject(specification, true);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Management.Automation;
using System.Text;
using Eryph.ComputeClient.Models;
using JetBrains.Annotations;

namespace Eryph.ComputeClient.Commands.CatletSpecifications;

[PublicAPI]
[Cmdlet(VerbsCommon.New, "CatletSpecification")]
[OutputType(typeof(Operation), typeof(CatletSpecification))]
public class NewCatletSpecification : CatletSpecificationCmdlet
{
[Parameter(
ParameterSetName = "InputObject",
Position = 0,
ValueFromPipeline = true,
Mandatory = true)]
[AllowEmptyString]
public string[] InputObject { get; set; }

[Parameter(ParameterSetName = "Config", Mandatory = true)]
[ValidateNotNullOrEmpty]
public string Config { get; set; }

[Parameter]
[ValidateNotNullOrEmpty]
public string Comment { get; set; }

[Parameter]
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

[Parameter]
public SwitchParameter Json { get; set; }

[Parameter]
public string[] Architectures { get; set; }

[Parameter]
public SwitchParameter NoWait { get; set; }

private readonly StringBuilder _input = new StringBuilder();

protected override void ProcessRecord()
{
if (InputObject != null)
{
foreach (var input in InputObject)
{
_input.AppendLine($"{input}");
}
}

if (Config != null)
_input.Append(Config);
}

protected override void EndProcessing()
{
var input = _input.ToString();
if (string.IsNullOrEmpty(input))
return;

var projectName = string.IsNullOrWhiteSpace(ProjectName) ? "default" : ProjectName;
var projectId = GetProjectId(projectName);

var config = new CatletSpecificationConfig(
Json.ToBool() ? "application/json" : "application/yaml",
input);

WaitForOperation(Factory.CreateCatletSpecificationsClient().Create(
new NewCatletSpecificationRequest(Guid.Parse(projectId), config)
{
Comment = Comment,
CorrelationId = Guid.NewGuid(),
Architectures = Architectures,
}),
NoWait,
true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Management.Automation;
using Eryph.ComputeClient.Models;
using JetBrains.Annotations;

namespace Eryph.ComputeClient.Commands.CatletSpecifications;

[PublicAPI]
[Cmdlet(VerbsCommon.Remove, "CatletSpecification")]
[OutputType(typeof(Operation), typeof(CatletSpecification))]
public class RemoveCatletSpecification : CatletSpecificationCmdlet
{
[Parameter(
Position = 0,
ValueFromPipeline = true,
Mandatory = true,
ValueFromPipelineByPropertyName = true)]
public string[] Id { get; set; }

[Parameter]
public SwitchParameter RemoveCatlet { get; set; }

[Parameter]
public SwitchParameter Force { get; set; }

[Parameter]
public SwitchParameter PassThru { get; set; }

[Parameter]
public SwitchParameter NoWait { get; set; }

private bool _yesToAll;
private bool _noToAll;

protected override void ProcessRecord()
{
foreach (var id in Id)
{
CatletSpecification specification;
try
{
specification = Factory.CreateCatletSpecificationsClient().Get(id);
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "CatletSpecificationNotFound", ErrorCategory.ObjectNotFound, id));
continue;
}

if (!Force && !ShouldContinue($"Catlet specification '{specification.Name}' (Id:{id}) will be deleted!", "Warning!",
ref _yesToAll, ref _noToAll))
{
continue;
}

WaitForOperation(Factory.CreateCatletSpecificationsClient().Delete(
id,
new DeleteCatletSpecificationRequestBody
{
DeleteCatlet = RemoveCatlet,
}),
NoWait,
false,
id);

if (PassThru)
WriteObject(specification);
}
}
}
Loading
Loading