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 virtual machines from a Proxmox VE server.
/// The Get-ProxmoxVM cmdlet retrieves virtual machines from a Proxmox VE server.
///
/// Get all virtual machines
/// $vms = Get-ProxmoxVM -Connection $connection
///
///
/// Get a specific virtual machine by ID
/// $vm = Get-ProxmoxVM -Connection $connection -VMID 100
///
///
/// Get virtual machines on a specific node
/// $vms = Get-ProxmoxVM -Connection $connection -Node "pve1"
///
///
[Cmdlet(VerbsCommon.Get, "ProxmoxVM")]
[OutputType(typeof(ProxmoxVM), typeof(string))]
public class GetProxmoxVMCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The ID of the virtual machine to retrieve.
///
[Parameter(Mandatory = false)]
public int? VMID { get; set; }
///
/// The node to retrieve virtual machines from.
///
[Parameter(Mandatory = false)]
public string Node { 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 (VMID.HasValue)
{
// Get a specific VM
if (string.IsNullOrEmpty(Node))
{
// First, get the list of nodes
string nodesResponse = client.Get("nodes");
var nodesData = JsonUtility.DeserializeResponse(nodesResponse);
// Search for the VM on each node
foreach (var nodeObj in nodesData)
{
string nodeName = nodeObj["node"].ToString();
try
{
response = client.Get($"nodes/{nodeName}/qemu/{VMID.Value}/status/current");
var vm = JsonUtility.DeserializeResponse(response);
vm.Node = nodeName;
vm.VMID = VMID.Value;
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(vm);
}
return;
}
catch
{
// VM not found on this node, continue to the next one
}
}
WriteError(new ErrorRecord(
new Exception($"VM with ID {VMID.Value} not found on any node"),
"VMNotFound",
ErrorCategory.ObjectNotFound,
VMID.Value));
}
else
{
// Get the VM from the specified node
response = client.Get($"nodes/{Node}/qemu/{VMID.Value}/status/current");
var vm = JsonUtility.DeserializeResponse(response);
vm.Node = Node;
vm.VMID = VMID.Value;
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(vm);
}
}
}
else
{
// Get all VMs
if (string.IsNullOrEmpty(Node))
{
// First, get the list of nodes
string nodesResponse = client.Get("nodes");
var nodesData = JsonUtility.DeserializeResponse(nodesResponse);
var allVMs = new List();
// Get VMs from each node
foreach (var nodeObj in nodesData)
{
string nodeName = nodeObj["node"].ToString();
try
{
response = client.Get($"nodes/{nodeName}/qemu");
var vms = JsonUtility.DeserializeResponse(response);
foreach (var vmObj in vms)
{
var vm = vmObj.ToObject();
vm.Node = nodeName;
allVMs.Add(vm);
}
}
catch
{
// Error getting VMs from this node, continue to the next one
WriteWarning($"Failed to get VMs from node {nodeName}");
}
}
if (RawJson.IsPresent)
{
WriteObject(JsonConvert.SerializeObject(allVMs));
}
else
{
WriteObject(allVMs, true);
}
}
else
{
// Get VMs from the specified node
response = client.Get($"nodes/{Node}/qemu");
var vms = JsonUtility.DeserializeResponse(response);
var nodeVMs = new List();
foreach (var vmObj in vms)
{
var vm = vmObj.ToObject();
vm.Node = Node;
nodeVMs.Add(vm);
}
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(nodeVMs, true);
}
}
}
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "GetProxmoxVMError", ErrorCategory.OperationStopped, Connection));
}
}
}
}