using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using PSProxmox.Client;
using PSProxmox.Session;
namespace PSProxmox.Cmdlets
{
///
/// Removes a node from a Proxmox VE cluster.
/// The Leave-ProxmoxCluster cmdlet removes a node from a Proxmox VE cluster.
///
/// Remove a node from a cluster
/// Leave-ProxmoxCluster -Connection $connection -Force
///
///
[Cmdlet("Leave", "ProxmoxCluster", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
public class LeaveProxmoxClusterCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// Whether to force leaving the cluster.
///
[Parameter(Mandatory = false)]
public SwitchParameter Force { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Confirm leaving the cluster
if (!ShouldProcess($"Node {Connection.Server}", "Leave cluster"))
{
return;
}
// Leave the cluster
var parameters = new Dictionary();
if (Force.IsPresent)
{
parameters["force"] = "1";
}
WriteVerbose($"Removing node {Connection.Server} from cluster");
string queryString = "";
if (parameters.Count > 0)
{
queryString = "?" + string.Join("&", parameters.Select(p => $"{p.Key}={p.Value}"));
}
client.Delete($"cluster/leave{queryString}");
WriteVerbose($"Node {Connection.Server} removed from cluster");
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "LeaveProxmoxClusterError", ErrorCategory.OperationStopped, Connection));
}
}
}
}