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 SDN zones from Proxmox VE.
/// The Get-ProxmoxSDNZone cmdlet retrieves SDN zones from Proxmox VE.
///
/// Get all SDN zones
/// $zones = Get-ProxmoxSDNZone -Connection $connection
///
///
/// Get a specific SDN zone by name
/// $zone = Get-ProxmoxSDNZone -Connection $connection -Zone "zone1"
///
///
[Cmdlet(VerbsCommon.Get, "ProxmoxSDNZone")]
[OutputType(typeof(ProxmoxSDNZone), typeof(string))]
public class GetProxmoxSDNZoneCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The name of the zone to retrieve.
///
[Parameter(Mandatory = false)]
public string Zone { 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(Zone))
{
// Get all zones
response = client.Get("sdn/zones");
var zonesData = JsonUtility.DeserializeResponse(response);
var zones = new List();
foreach (var zoneObj in zonesData)
{
var zone = zoneObj.ToObject();
zones.Add(zone);
}
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(zones, true);
}
}
else
{
// Get a specific zone
response = client.Get($"sdn/zones/{Zone}");
var zoneData = JsonUtility.DeserializeResponse(response);
var zone = zoneData.ToObject();
if (RawJson.IsPresent)
{
WriteObject(response);
}
else
{
WriteObject(zone);
}
}
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "GetProxmoxSDNZoneError", ErrorCategory.OperationStopped, Connection));
}
}
}
}