using System; using System.Management.Automation; using PSProxmox.Models; namespace PSProxmox.Cmdlets { /// /// Creates a new virtual machine configuration builder for Proxmox VE. /// The New-ProxmoxVMBuilder cmdlet creates a new virtual machine configuration builder that can be used with New-ProxmoxVM. /// /// Create a basic VM builder /// $builder = New-ProxmoxVMBuilder -Name "web-server" /// /// /// Create a VM builder with initial configuration /// $builder = New-ProxmoxVMBuilder -Name "db-server" -Memory 4096 -Cores 2 -Node "pve1" /// /// /// Create a VM builder and add configuration /// $builder = New-ProxmoxVMBuilder -Name "app-server" /// $builder.WithMemory(8192).WithCores(4).WithDisk(100, "local-lvm") /// $builder.WithNetwork("virtio", "vmbr0").WithIPConfig("192.168.1.10/24", "192.168.1.1") /// /// [Cmdlet(VerbsCommon.New, "ProxmoxVMBuilder")] [OutputType(typeof(ProxmoxVMBuilder))] public class NewProxmoxVMBuilderCmdlet : PSCmdlet { /// /// The name of the VM. /// [Parameter(Mandatory = true, Position = 0)] public string Name { get; set; } /// /// The VM ID. If not specified, the next available ID will be used. /// [Parameter(Mandatory = false)] public int? VMID { get; set; } /// /// The node to create the VM on. /// [Parameter(Mandatory = false)] public string Node { get; set; } /// /// The description of the VM. /// [Parameter(Mandatory = false)] public string Description { get; set; } /// /// The tags for the VM. /// [Parameter(Mandatory = false)] public string[] Tags { get; set; } /// /// The amount of memory in MB. /// [Parameter(Mandatory = false)] public int Memory { get; set; } = 512; /// /// The number of CPU cores. /// [Parameter(Mandatory = false)] public int Cores { get; set; } = 1; /// /// The CPU type. /// [Parameter(Mandatory = false)] public string CPUType { get; set; } = "host"; /// /// The operating system type. /// [Parameter(Mandatory = false)] public string OSType { get; set; } = "l26"; // Linux 2.6+ /// /// Whether to start the VM after creation. /// [Parameter(Mandatory = false)] public SwitchParameter Start { get; set; } /// /// The IP pool to use for assigning an IP address. /// [Parameter(Mandatory = false)] public string IPPool { get; set; } /// /// Processes the cmdlet. /// protected override void ProcessRecord() { try { var builder = new ProxmoxVMBuilder(Name) { Memory = Memory, Cores = Cores, CPUType = CPUType, OSType = OSType, Start = Start.IsPresent, IPPool = IPPool }; if (VMID.HasValue) { builder.WithVMID(VMID.Value); } if (!string.IsNullOrEmpty(Node)) { builder.WithNode(Node); } if (!string.IsNullOrEmpty(Description)) { builder.WithDescription(Description); } if (Tags != null && Tags.Length > 0) { builder.WithTags(Tags); } WriteObject(builder); } catch (Exception ex) { WriteError(new ErrorRecord(ex, "NewProxmoxVMBuilderError", ErrorCategory.InvalidOperation, Name)); } } } }