using System;
using System.Management.Automation;
using PSProxmox.Client;
using PSProxmox.Session;
namespace PSProxmox.Cmdlets
{
///
/// Removes a role from Proxmox VE.
/// The Remove-ProxmoxRole cmdlet removes a role from Proxmox VE.
///
/// Remove a role
/// Remove-ProxmoxRole -Connection $connection -RoleID "Developer"
///
///
/// Remove a role using pipeline input
/// Get-ProxmoxRole -Connection $connection -RoleID "Developer" | Remove-ProxmoxRole -Connection $connection
///
///
[Cmdlet(VerbsCommon.Remove, "ProxmoxRole", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
public class RemoveProxmoxRoleCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The ID of the role to remove.
///
[Parameter(Mandatory = true, ValueFromPipelineByPropertyName = true)]
public string RoleID { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Confirm removal
if (!ShouldProcess($"Role {RoleID}", "Remove"))
{
return;
}
// Remove the role
WriteVerbose($"Removing role {RoleID}");
client.Delete($"access/roles/{Uri.EscapeDataString(RoleID)}");
WriteVerbose($"Role {RoleID} removed");
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "RemoveProxmoxRoleError", ErrorCategory.OperationStopped, RoleID));
}
}
}
}