-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Cmdlet to remove catlet disks (#76)
* Add Cmdlet for removing catlet disks * Improve Cmdlet for listing catlet disks
- Loading branch information
1 parent
5f5236f
commit 31ac120
Showing
4 changed files
with
142 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/Eryph.ComputeClient.Commands/Catlets/CatletDiskCmdlet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Eryph.ComputeClient.Models; | ||
|
||
namespace Eryph.ComputeClient.Commands.Catlets; | ||
|
||
public abstract class CatletDiskCmdlet : ComputeCmdLet | ||
{ | ||
protected VirtualDisk GetSingleCatletDisk(string id) | ||
{ | ||
return Factory.CreateVirtualDisksClient().Get(id); | ||
} | ||
|
||
protected void WaitForOperation(Operation operation, bool noWait, bool alwaysWriteCatletDisk, string knownCatletDiskId = default) | ||
{ | ||
if (noWait) | ||
{ | ||
if (knownCatletDiskId == default || !alwaysWriteCatletDisk) | ||
WriteObject(operation); | ||
else | ||
WriteObject(GetSingleCatletDisk(knownCatletDiskId)); | ||
return; | ||
} | ||
|
||
WaitForOperation(operation, (op) => ResourceWriter(op, Write)); | ||
return; | ||
|
||
void Write(ResourceType resourceType, string id) | ||
{ | ||
if (resourceType == ResourceType.VirtualDisk) | ||
WriteObject(GetSingleCatletDisk(id)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/Eryph.ComputeClient.Commands/Catlets/RemoveCatletDiskCmdlet.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Management.Automation; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Eryph.ComputeClient.Models; | ||
using JetBrains.Annotations; | ||
|
||
namespace Eryph.ComputeClient.Commands.Catlets; | ||
|
||
[PublicAPI] | ||
[Cmdlet(VerbsCommon.Remove, "CatletDisk")] | ||
[OutputType(typeof(Operation), typeof(VirtualDisk))] | ||
public class RemoveCatletDiskCmdlet : CatletDiskCmdlet | ||
{ | ||
[Parameter( | ||
Position = 0, | ||
ValueFromPipeline = true, | ||
Mandatory = true, | ||
ValueFromPipelineByPropertyName = true)] | ||
public string[] Id { get; set; } | ||
|
||
/// <summary> | ||
/// This parameter overrides the ShouldContinue call to force | ||
/// the cmdlet to stop its operation. This parameter should always | ||
/// be used with caution. | ||
/// </summary> | ||
[Parameter] | ||
public SwitchParameter Force { get; set; } | ||
|
||
/// <summary> | ||
/// This parameter indicates that the cmdlet should return | ||
/// an object to the pipeline after the processing has been | ||
/// completed. | ||
/// </summary> | ||
[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) | ||
{ | ||
VirtualDisk virtualDisk; | ||
try | ||
{ | ||
virtualDisk = Factory.CreateVirtualDisksClient().Get(id); | ||
} | ||
catch (Exception ex) | ||
{ | ||
WriteError(new ErrorRecord(ex, "CatletDiskNotFound", ErrorCategory.ObjectNotFound, id)); | ||
continue; | ||
} | ||
|
||
if (!Force && !ShouldContinue($"Catlet disk '{virtualDisk.Name}' (Id:{id}) will be deleted!", "Warning!", | ||
ref _yesToAll, ref _noToAll)) | ||
{ | ||
continue; | ||
} | ||
|
||
WaitForOperation(Factory.CreateVirtualDisksClient().Delete(id).Value, NoWait, false, id); | ||
|
||
if (PassThru) | ||
WriteObject(virtualDisk); | ||
} | ||
} | ||
} |