using System;
using System.Collections.Generic;
using System.Management.Automation;
using System.Security;
using PSProxmox.Client;
using PSProxmox.Session;
namespace PSProxmox.Cmdlets
{
///
/// Joins a node to a Proxmox VE cluster.
/// The Join-ProxmoxCluster cmdlet joins a node to a Proxmox VE cluster.
///
/// Join a node to a cluster
/// Join-ProxmoxCluster -Connection $connection -ClusterName "cluster1" -HostName "pve2" -Password $securePassword
///
///
[Cmdlet(VerbsCommon.Join, "ProxmoxCluster", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.High)]
public class JoinProxmoxClusterCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The name of the cluster to join.
///
[Parameter(Mandatory = true)]
public string ClusterName { get; set; }
///
/// The hostname or IP address of an existing cluster member.
///
[Parameter(Mandatory = true)]
public string HostName { get; set; }
///
/// The password for the root@pam user on the existing cluster member.
///
[Parameter(Mandatory = true)]
public SecureString Password { get; set; }
///
/// Whether to force joining 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 joining the cluster
if (!ShouldProcess($"Node {Connection.Server}", $"Join cluster {ClusterName}"))
{
return;
}
// Convert SecureString to plain text (only for sending to API)
string plainPassword = new System.Net.NetworkCredential(string.Empty, Password).Password;
// Join the cluster
var parameters = new Dictionary
{
["hostname"] = HostName,
["password"] = plainPassword
};
if (Force.IsPresent)
{
parameters["force"] = "1";
}
WriteVerbose($"Joining cluster {ClusterName} via host {HostName}");
client.Post("cluster/join", parameters);
WriteVerbose($"Node {Connection.Server} joined cluster {ClusterName}");
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "JoinProxmoxClusterError", ErrorCategory.OperationStopped, Connection));
}
}
}
}