using System;
using System.Collections.Generic;
using System.Management.Automation;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using PSProxmox.Client;
using PSProxmox.Models;
using PSProxmox.Session;
using PSProxmox.Utilities;
namespace PSProxmox.Cmdlets
{
///
/// Gets nodes from a Proxmox VE cluster.
/// The Get-ProxmoxNode cmdlet retrieves nodes from a Proxmox VE cluster.
///
/// Get all nodes
/// $nodes = Get-ProxmoxNode -Connection $connection
///
///
/// Get a specific node by name
/// $node = Get-ProxmoxNode -Connection $connection -Name "pve1"
///
///
[Cmdlet(VerbsCommon.Get, "ProxmoxNode")]
[OutputType(typeof(ProxmoxNode), typeof(string))]
public class GetProxmoxNodeCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The name of the node to retrieve.
///
[Parameter(Mandatory = false)]
public string Name { get; set; }
///
/// Whether to return the raw JSON response.
///
[Parameter(Mandatory = false)]
public SwitchParameter RawJson { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
string response;
if (string.IsNullOrEmpty(Name))
{
// Get all nodes
response = client.Get("nodes");
var nodesData = JsonUtility.DeserializeResponse(response);
var nodes = new List();
foreach (var nodeObj in nodesData)
{
var node = nodeObj.ToObject();
nodes.Add(node);
}
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(nodes, true);
}
}
else
{
// Get a specific node
response = client.Get($"nodes/{Name}/status");
var nodeData = JsonUtility.DeserializeResponse(response);
var node = nodeData.ToObject();
node.Name = Name;
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(node);
}
}
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "GetProxmoxNodeError", ErrorCategory.OperationStopped, Connection));
}
}
}
}