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 network interfaces from Proxmox VE.
/// The Get-ProxmoxNetwork cmdlet retrieves network interfaces from Proxmox VE.
///
/// Get all network interfaces
/// $networks = Get-ProxmoxNetwork -Connection $connection -Node "pve1"
///
///
/// Get a specific network interface by name
/// $network = Get-ProxmoxNetwork -Connection $connection -Node "pve1" -Interface "vmbr0"
///
///
[Cmdlet(VerbsCommon.Get, "ProxmoxNetwork")]
[OutputType(typeof(ProxmoxNetwork), typeof(string))]
public class GetProxmoxNetworkCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The node to retrieve network interfaces from.
///
[Parameter(Mandatory = true)]
public string Node { get; set; }
///
/// The name of the interface to retrieve.
///
[Parameter(Mandatory = false)]
public string Interface { 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(Interface))
{
// Get all network interfaces
response = client.Get($"nodes/{Node}/network");
var networksData = JsonUtility.DeserializeResponse(response);
var networks = new List();
foreach (var networkObj in networksData)
{
var network = networkObj.ToObject();
network.Node = Node;
networks.Add(network);
}
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(networks, true);
}
}
else
{
// Get a specific network interface
response = client.Get($"nodes/{Node}/network/{Interface}");
var networkData = JsonUtility.DeserializeResponse(response);
var network = networkData.ToObject();
network.Node = Node;
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(network);
}
}
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "GetProxmoxNetworkError", ErrorCategory.OperationStopped, Connection));
}
}
}
}