using System; using System.Collections.Generic; using System.Management.Automation; using PSProxmox.Client; using PSProxmox.Models; using PSProxmox.Session; using PSProxmox.Utilities; namespace PSProxmox.Cmdlets { /// /// Creates a new storage in Proxmox VE. /// The New-ProxmoxStorage cmdlet creates a new storage in Proxmox VE. /// /// Create a new directory storage /// $storage = New-ProxmoxStorage -Connection $connection -Name "backup" -Type "dir" -Path "/mnt/backup" -Content "backup,iso" /// /// [Cmdlet(VerbsCommon.New, "ProxmoxStorage")] [OutputType(typeof(ProxmoxStorage))] public class NewProxmoxStorageCmdlet : PSCmdlet { /// /// The connection to the Proxmox VE server. /// [Parameter(Mandatory = true, Position = 0)] public ProxmoxConnection Connection { get; set; } /// /// The name of the storage. /// [Parameter(Mandatory = true)] public string Name { get; set; } /// /// The type of the storage. /// [Parameter(Mandatory = true)] [ValidateSet("dir", "nfs", "cifs", "lvm", "lvmthin", "zfs", "zfspool", "iscsi", "glusterfs", "cephfs", "rbd")] public string Type { get; set; } /// /// The path of the storage. /// [Parameter(Mandatory = false)] public string Path { get; set; } /// /// The content types allowed on the storage. /// [Parameter(Mandatory = false)] public string Content { get; set; } /// /// The node to create the storage on. /// [Parameter(Mandatory = false)] public string Node { get; set; } /// /// Whether the storage is shared. /// [Parameter(Mandatory = false)] public SwitchParameter Shared { get; set; } /// /// Whether the storage is enabled. /// [Parameter(Mandatory = false)] public SwitchParameter Enabled { get; set; } = true; /// /// Additional parameters for the storage. /// [Parameter(Mandatory = false)] public Dictionary AdditionalParameters { get; set; } /// /// Processes the cmdlet. /// protected override void ProcessRecord() { try { var client = new ProxmoxApiClient(Connection, this); // Create the storage var parameters = new Dictionary { ["storage"] = Name, ["type"] = Type }; if (!string.IsNullOrEmpty(Path)) { parameters["path"] = Path; } if (!string.IsNullOrEmpty(Content)) { parameters["content"] = Content; } if (!string.IsNullOrEmpty(Node)) { parameters["nodes"] = Node; } parameters["shared"] = Shared.IsPresent ? "1" : "0"; parameters["disable"] = Enabled.IsPresent ? "0" : "1"; // Add additional parameters if (AdditionalParameters != null) { foreach (var key in AdditionalParameters.Keys) { parameters[key.ToString()] = AdditionalParameters[key].ToString(); } } // Create the storage client.Post("storage", parameters); // Get the created storage string storageResponse = client.Get($"storage/{Name}"); var storage = JsonUtility.DeserializeResponse(storageResponse); WriteObject(storage); } catch (Exception ex) { WriteError(new ErrorRecord(ex, "NewProxmoxStorageError", ErrorCategory.OperationStopped, Connection)); } } } }