Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/Eryph.ComputeClient.Commands/Catlets/CatletCmdLet.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,53 @@
using Eryph.ComputeClient.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using Eryph.ComputeClient.Models;
using JetBrains.Annotations;

namespace Eryph.ComputeClient.Commands.Catlets
{
[PublicAPI]
public abstract class CatletCmdLet : ComputeCmdLet
{
/// <summary>Resource kind used in catlet not-found / ambiguity messages.</summary>
protected const string CatletResourceKind = "catlet";

/// <summary>
/// Hint shown when a catlet name is ambiguous, naming the parameter that
/// disambiguates it (a catlet name is unique only per project + environment).
/// </summary>
protected const string EnvironmentAmbiguityHint = "-Environment";

/// <summary>
/// The <see cref="EnvironmentAmbiguityHint"/>, but only when <paramref name="environment"/>
/// was not already supplied. Once -Environment has been applied it did not resolve the
/// ambiguity, so suggesting it again would be misleading; return null in that case.
/// </summary>
protected static string EnvironmentHintIfUnset(string environment) =>
string.IsNullOrWhiteSpace(environment) ? EnvironmentAmbiguityHint : null;

protected Catlet GetSingleCatlet(string id)
{
return Factory.CreateCatletsClient().Get(id);
}

/// <summary>
/// Lists the catlets of a project, optionally narrowed to a single environment.
/// A catlet's name is unique only per project + environment, so a lookup by name
/// can match several catlets across environments; passing an environment restricts
/// the listing to that environment (case-insensitive exact match). An empty/null
/// environment applies no filter and lists every environment. As the server has no
/// environment filter, this is applied client-side, mirroring Get-VNetwork and
/// Get-CatletDisk. An explicit id (a GUID) bypasses this listing entirely.
/// </summary>
protected IEnumerable<Catlet> ListCatlets(string projectId, string environment)
{
IEnumerable<Catlet> catlets = Factory.CreateCatletsClient().List(projectId: projectId);
if (!string.IsNullOrWhiteSpace(environment))
catlets = catlets.Where(c => string.Equals(c.Environment, environment, StringComparison.OrdinalIgnoreCase));
return catlets;
}

protected void WaitForOperation(
Operation operation,
bool noWait,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public abstract class CatletGuestServiceGetCmdlet : CatletCmdLet
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

[Parameter(ParameterSetName = "list")]
[ValidateNotNullOrEmpty]
public string Environment { get; set; }

protected override void BeginProcessing()
{
base.BeginProcessing();
Expand All @@ -46,7 +50,7 @@ protected override void ProcessRecord()
foreach (var id in Id)
{
if (Stopping) break;
if (TryGetById(id, GetSingleCatlet, "catlet", out var catlet))
if (TryGetById(id, GetSingleCatlet, CatletResourceKind, out var catlet))
EmitCatlet(catlet);
}

Expand All @@ -59,9 +63,9 @@ protected override void ProcessRecord()
WriteByNameOrId(
Name,
GetSingleCatlet,
() => Factory.CreateCatletsClient().List(projectId: projectId),
() => ListCatlets(projectId, Environment),
catlet => catlet.Name,
"catlet",
CatletResourceKind,
EmitCatlet);
}

Expand Down
12 changes: 8 additions & 4 deletions src/Eryph.ComputeClient.Commands/Catlets/GetCatletCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class GetCatletCommand : CatletCmdLet
[ValidateNotNullOrEmpty]
public string Name { get; set; }

[Parameter(ParameterSetName = "list")]
[ValidateNotNullOrEmpty]
public string Environment { get; set; }

protected override void ProcessRecord()
{

Expand All @@ -57,12 +61,12 @@ protected override void ProcessRecord()

if (Config.IsPresent)
{
if (TryGetById(id, i => Factory.CreateCatletsClient().GetConfig(i), "catlet", out var config))
if (TryGetById(id, i => Factory.CreateCatletsClient().GetConfig(i), CatletResourceKind, out var config))
WriteConfig(config);
}
else
{
if (TryGetById(id, GetSingleCatlet, "catlet", out var catlet))
if (TryGetById(id, GetSingleCatlet, CatletResourceKind, out var catlet))
WriteObject(catlet);
}
}
Expand Down Expand Up @@ -90,9 +94,9 @@ protected override void ProcessRecord()
WriteByNameOrId(
Name,
GetSingleCatlet,
() => Factory.CreateCatletsClient().List(projectId: projectId),
() => ListCatlets(projectId, Environment),
catlet => catlet.Name,
"catlet");
CatletResourceKind);
}

private void WriteConfig(CatletConfiguration config)
Expand Down
10 changes: 7 additions & 3 deletions src/Eryph.ComputeClient.Commands/Catlets/GetCatletIpCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public class GetCatletIpCommand : CatletCmdLet
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

[Parameter(ParameterSetName = "list")]
[ValidateNotNullOrEmpty]
public string Environment { get; set; }

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

Expand All @@ -42,7 +46,7 @@ protected override void ProcessRecord()
foreach (var id in Id)
{
if (Stopping) break;
if (TryGetById(id, GetSingleCatlet, "catlet", out var catlet))
if (TryGetById(id, GetSingleCatlet, CatletResourceKind, out var catlet))
WriteIp(catlet);
}

Expand All @@ -55,9 +59,9 @@ protected override void ProcessRecord()
WriteByNameOrId(
Name,
GetSingleCatlet,
() => Factory.CreateCatletsClient().List(projectId: projectId),
() => ListCatlets(projectId, Environment),
catlet => catlet.Name,
"catlet",
CatletResourceKind,
WriteIp);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ public class GetCatletProvisioningLogCommand : CatletCmdLet
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

[Parameter(ParameterSetName = "list")]
[ValidateNotNullOrEmpty]
public string Environment { get; set; }

/// <summary>
/// Emit the rendered, human-readable text log instead of the structured events.
/// </summary>
Expand All @@ -57,7 +61,7 @@ protected override void ProcessRecord()
foreach (var id in Id)
{
if (Stopping) break;
if (TryGetById(id, GetSingleCatlet, "catlet", out var catlet))
if (TryGetById(id, GetSingleCatlet, CatletResourceKind, out var catlet))
EmitLog(catlet);
}

Expand All @@ -70,9 +74,9 @@ protected override void ProcessRecord()
WriteByNameOrId(
Name,
GetSingleCatlet,
() => Factory.CreateCatletsClient().List(projectId: projectId),
() => ListCatlets(projectId, Environment),
catlet => catlet.Name,
"catlet",
CatletResourceKind,
EmitLog);
}

Expand All @@ -92,7 +96,7 @@ private void EmitLog(Catlet catlet)
// Prefix a header so the text blocks stay attributable to their catlet
// when several catlets are targeted in one invocation.
var header = $"# Catlet {catlet.Name} ({catlet.Id})";
WriteObject(header + Environment.NewLine + result.RenderedLog);
WriteObject(header + System.Environment.NewLine + result.RenderedLog);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public class GetCatletProvisioningStatusCommand : CatletCmdLet
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

[Parameter(ParameterSetName = "list")]
[ValidateNotNullOrEmpty]
public string Environment { get; set; }

protected override void BeginProcessing()
{
base.BeginProcessing();
Expand All @@ -48,7 +52,7 @@ protected override void ProcessRecord()
foreach (var id in Id)
{
if (Stopping) break;
if (TryGetById(id, GetSingleCatlet, "catlet", out var catlet))
if (TryGetById(id, GetSingleCatlet, CatletResourceKind, out var catlet))
WriteStatus(catlet);
}

Expand All @@ -61,9 +65,9 @@ protected override void ProcessRecord()
WriteByNameOrId(
Name,
GetSingleCatlet,
() => Factory.CreateCatletsClient().List(projectId: projectId),
() => ListCatlets(projectId, Environment),
catlet => catlet.Name,
"catlet",
CatletResourceKind,
WriteStatus);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public SwitchParameter NoWait
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

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

private bool _force;
private bool _nowait;
private bool _passThru;
Expand All @@ -68,7 +72,7 @@ protected override void ProcessRecord()
foreach (var nameOrId in Id)
{
foreach (var catlet in ResolveActionTargets(nameOrId, ProjectName, GetSingleCatlet,
projectId => Factory.CreateCatletsClient().List(projectId: projectId), c => c.Name, "catlet"))
projectId => ListCatlets(projectId, Environment), c => c.Name, CatletResourceKind, EnvironmentHintIfUnset(Environment)))
{
if (Stopping) break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class RemoveCatletGuestServiceAccessKeyCommand : CatletCmdLet
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

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

[Parameter]
public SwitchParameter Force
{
Expand Down Expand Up @@ -64,7 +68,7 @@ protected override void ProcessRecord()
foreach (var nameOrId in Id)
{
foreach (var catlet in ResolveActionTargets(nameOrId, ProjectName, GetSingleCatlet,
projectId => Factory.CreateCatletsClient().List(projectId: projectId), c => c.Name, "catlet"))
projectId => ListCatlets(projectId, Environment), c => c.Name, CatletResourceKind, EnvironmentHintIfUnset(Environment)))
{
if (Stopping) break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class SetCatletGuestServiceConfigCommand : CatletCmdLet
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

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

/// <summary>
/// Shell command for interactive SSH sessions. An empty string clears the
/// override; omitting the parameter leaves the current value unchanged.
Expand Down Expand Up @@ -84,7 +88,7 @@ protected override void ProcessRecord()
foreach (var nameOrId in Id)
{
foreach (var catlet in ResolveActionTargets(nameOrId, ProjectName, GetSingleCatlet,
projectId => Factory.CreateCatletsClient().List(projectId: projectId), c => c.Name, "catlet"))
projectId => ListCatlets(projectId, Environment), c => c.Name, CatletResourceKind, EnvironmentHintIfUnset(Environment)))
{
if (Stopping) break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ public SwitchParameter NoWait
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

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

private bool _force;
private bool _nowait;
private bool _yesToAll, _noToAll;
Expand All @@ -54,7 +58,7 @@ protected override void ProcessRecord()
foreach (var nameOrId in Id)
{
foreach (var catlet in ResolveActionTargets(nameOrId, ProjectName, GetSingleCatlet,
projectId => Factory.CreateCatletsClient().List(projectId: projectId), c => c.Name, "catlet"))
projectId => ListCatlets(projectId, Environment), c => c.Name, CatletResourceKind, EnvironmentHintIfUnset(Environment)))
{
if (Stopping) break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ public class StopCatletCommand : CatletCmdLet
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

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

private bool _yesToAll;
private bool _noToAll;
private bool _yesToKillAll;
Expand All @@ -55,7 +59,7 @@ protected override void ProcessRecord()
foreach (var nameOrId in Id)
{
foreach (var catlet in ResolveActionTargets(nameOrId, ProjectName, GetSingleCatlet,
projectId => Factory.CreateCatletsClient().List(projectId: projectId), c => c.Name, "catlet"))
projectId => ListCatlets(projectId, Environment), c => c.Name, CatletResourceKind, EnvironmentHintIfUnset(Environment)))
{
if (Stopping) break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ public class UpdateCatletCommand : CatletConfigCmdlet
[ValidateNotNullOrEmpty]
public string ProjectName { get; set; }

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

protected override void ProcessRecord()
{
var client = Factory.CreateCatletsClient();

foreach (var nameOrId in Id)
{
foreach (var catlet in ResolveActionTargets(nameOrId, ProjectName, GetSingleCatlet,
projectId => client.List(projectId: projectId), c => c.Name, "catlet"))
projectId => ListCatlets(projectId, Environment), c => c.Name, CatletResourceKind, EnvironmentHintIfUnset(Environment)))
Comment on lines 44 to +45
{
if (Stopping) break;

Expand Down
Loading