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 storage from a Proxmox VE server. /// The Get-ProxmoxStorage cmdlet retrieves storage from a Proxmox VE server. /// /// Get all storage /// $storage = Get-ProxmoxStorage -Connection $connection /// /// /// Get a specific storage by name /// $storage = Get-ProxmoxStorage -Connection $connection -Name "local" /// /// /// Get storage on a specific node /// $storage = Get-ProxmoxStorage -Connection $connection -Node "pve1" /// /// [Cmdlet(VerbsCommon.Get, "ProxmoxStorage")] [OutputType(typeof(ProxmoxStorage), typeof(string))] public class GetProxmoxStorageCmdlet : PSCmdlet { /// /// The connection to the Proxmox VE server. /// [Parameter(Mandatory = true, Position = 0)] public ProxmoxConnection Connection { get; set; } /// /// The name of the storage to retrieve. /// [Parameter(Mandatory = false)] public string Name { get; set; } /// /// The node to retrieve storage 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 (string.IsNullOrEmpty(Node)) { // Get storage from all nodes if (string.IsNullOrEmpty(Name)) { // Get all storage response = client.Get("storage"); var storageData = JsonUtility.DeserializeResponse(response); var allStorage = new List(); foreach (var storageObj in storageData) { var storage = storageObj.ToObject(); allStorage.Add(storage); } if (RawJson.IsPresent) { WriteObject(response); } else { WriteObject(allStorage, true); } } else { // Get a specific storage response = client.Get($"storage/{Name}"); var storage = JsonUtility.DeserializeResponse(response); if (RawJson.IsPresent) { WriteObject(response); } else { WriteObject(storage); } } } else { // Get storage from a specific node if (string.IsNullOrEmpty(Name)) { // Get all storage on the node response = client.Get($"nodes/{Node}/storage"); var storageData = JsonUtility.DeserializeResponse(response); var nodeStorage = new List(); foreach (var storageObj in storageData) { var storage = storageObj.ToObject(); storage.Node = Node; nodeStorage.Add(storage); } if (RawJson.IsPresent) { WriteObject(response); } else { WriteObject(nodeStorage, true); } } else { // Get a specific storage on the node response = client.Get($"nodes/{Node}/storage/{Name}"); var storage = JsonUtility.DeserializeResponse(response); storage.Node = Node; if (RawJson.IsPresent) { WriteObject(response); } else { WriteObject(storage); } } } } catch (Exception ex) { WriteError(new ErrorRecord(ex, "GetProxmoxStorageError", ErrorCategory.OperationStopped, Connection)); } } } }