namespace PS.IPAM.Cmdlets; using System; using System.Collections.Generic; using System.Management.Automation; using PS.IPAM.Helpers; /// /// Creates a new address using the first available IP in a subnet. /// [Cmdlet(VerbsCommon.New, "FirstFreeIP")] [OutputType(typeof(Address))] public class NewFirstFreeIPCmdlet : BaseCmdlet { [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "The subnet ID to create the address in.")] [ValidateNotNullOrEmpty] public string SubnetId { get; set; } = string.Empty; [Parameter(Mandatory = false, Position = 1, HelpMessage = "Mark this address as a gateway.")] public SwitchParameter Gateway { get; set; } [Parameter(Mandatory = false, Position = 2, HelpMessage = "Description for the address.")] public string? Description { get; set; } [Parameter(Mandatory = false, Position = 3, HelpMessage = "Hostname for the address.")] public string? Hostname { get; set; } [Parameter(Mandatory = false, Position = 4, HelpMessage = "MAC address.")] public string? MAC { get; set; } [Parameter(Mandatory = false, Position = 5, HelpMessage = "Owner of the address.")] public string? Owner { get; set; } [Parameter(Mandatory = false, Position = 6, HelpMessage = "Tag ID for the address.")] public string? TagId { get; set; } [Parameter(Mandatory = false, Position = 7, HelpMessage = "Ignore PTR record generation.")] public SwitchParameter PTRIgnore { get; set; } [Parameter(Mandatory = false, Position = 8, HelpMessage = "PTR record ID.")] public string? PTRId { get; set; } [Parameter(Mandatory = false, Position = 9, HelpMessage = "Note for the address.")] public string? Note { get; set; } [Parameter(Mandatory = false, Position = 10, HelpMessage = "Exclude from ping scanning.")] public SwitchParameter ExcludePing { get; set; } [Parameter(Mandatory = false, Position = 11, HelpMessage = "Associated device ID.")] public string? DeviceId { get; set; } [Parameter(Mandatory = false, Position = 12, HelpMessage = "Port information.")] public string? Port { get; set; } [Parameter(Mandatory = false, HelpMessage = "Custom fields as a hashtable or PSObject.")] public object? CustomFields { get; set; } protected override void ProcessRecord() { try { var identifiers = new[] { "first_free" }; var body = BuildRequestBody(); var result = RequestHelper.InvokeRequest( "POST", ApiController.Addresses, null, null, body, identifiers ).GetAwaiter().GetResult(); if (result != null) { var ip = result.ToString(); var address = RequestHelper.InvokeRequest( "GET", ApiController.Addresses, ModelType.Address, null, null, new[] { "search", ip! } ).GetAwaiter().GetResult(); WriteResult(address); } } catch (Exception ex) { WriteError(new ErrorRecord(ex, "NewFirstFreeIPError", ErrorCategory.InvalidOperation, null)); } } private Dictionary BuildRequestBody() { var body = new Dictionary { ["subnetId"] = SubnetId }; if (Gateway.IsPresent) body["is_gateway"] = "1"; if (!string.IsNullOrEmpty(Description)) body["description"] = Description; if (!string.IsNullOrEmpty(Hostname)) body["hostname"] = Hostname; if (!string.IsNullOrEmpty(MAC)) body["mac"] = MAC; if (!string.IsNullOrEmpty(Owner)) body["owner"] = Owner; if (!string.IsNullOrEmpty(TagId)) body["tag"] = TagId; if (PTRIgnore.IsPresent) body["PTRignore"] = "1"; if (!string.IsNullOrEmpty(PTRId)) body["PTR"] = PTRId; if (!string.IsNullOrEmpty(Note)) body["note"] = Note; if (ExcludePing.IsPresent) body["excludePing"] = "1"; if (!string.IsNullOrEmpty(DeviceId)) body["deviceId"] = DeviceId; if (!string.IsNullOrEmpty(Port)) body["port"] = Port; foreach (var kvp in ConvertCustomFields(CustomFields)) { body[kvp.Key] = kvp.Value; } return body; } }