using System;
using System.Management.Automation;
using PSProxmox.IPAM;
using PSProxmox.Models;
namespace PSProxmox.Cmdlets
{
///
/// Clears an IP address pool.
/// The Clear-ProxmoxIPPool cmdlet clears all used IP addresses from an IP pool and returns them to the available pool.
///
/// Clear a specific IP pool
/// Clear-ProxmoxIPPool -Name "Production"
///
///
/// Clear all IP pools
/// Clear-ProxmoxIPPool -All
///
///
[Cmdlet(VerbsCommon.Clear, "ProxmoxIPPool")]
[OutputType(typeof(ProxmoxIPPool))]
public class ClearProxmoxIPPoolCmdlet : PSCmdlet
{
private static readonly IPAMManager _ipamManager = new IPAMManager();
///
/// The name of the IP pool to clear.
///
[Parameter(Mandatory = true, Position = 0, ParameterSetName = "ByName")]
public string Name { get; set; }
///
/// Whether to clear all IP pools.
///
[Parameter(Mandatory = true, ParameterSetName = "All")]
public SwitchParameter All { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
if (ParameterSetName == "ByName")
{
// Clear a specific pool
var pool = _ipamManager.GetPool(Name);
pool.Clear();
WriteObject(ProxmoxIPPool.FromIPPool(pool));
}
else
{
// Clear all pools
foreach (var pool in _ipamManager.GetPools())
{
pool.Clear();
}
}
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "ClearProxmoxIPPoolError", ErrorCategory.InvalidOperation, Name));
}
}
}
}