using System;
using System.Collections.Generic;
using System.Management.Automation;
using PSProxmox.Client;
using PSProxmox.Models;
using PSProxmox.Session;
using PSProxmox.Utilities;
namespace PSProxmox.Cmdlets
{
///
/// Creates a new role in Proxmox VE.
/// The New-ProxmoxRole cmdlet creates a new role in Proxmox VE.
///
/// Create a new role
/// $role = New-ProxmoxRole -Connection $connection -RoleID "Developer" -Privileges "VM.Allocate", "VM.Config.Disk", "VM.Config.CPU", "VM.PowerMgmt"
///
///
[Cmdlet(VerbsCommon.New, "ProxmoxRole")]
[OutputType(typeof(ProxmoxRole))]
public class NewProxmoxRoleCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The role ID.
///
[Parameter(Mandatory = true)]
public string RoleID { get; set; }
///
/// The role's privileges.
///
[Parameter(Mandatory = true)]
public string[] Privileges { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Create the role
var parameters = new Dictionary
{
["roleid"] = RoleID,
["privs"] = string.Join(",", Privileges)
};
// Create the role
client.Post("access/roles", parameters);
// Get the created role
string roleResponse = client.Get($"access/roles/{Uri.EscapeDataString(RoleID)}");
var role = JsonUtility.DeserializeResponse(roleResponse);
WriteObject(role);
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "NewProxmoxRoleError", ErrorCategory.OperationStopped, Connection));
}
}
}
}