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