namespace PS.IPAM.Cmdlets; using System; using System.Collections.Generic; using System.Management.Automation; using PS.IPAM.Helpers; /// /// Creates a new IP address entry in phpIPAM. /// [Cmdlet(VerbsCommon.New, "Address", DefaultParameterSetName = "BySubnetId")] [OutputType(typeof(Address))] public class NewAddressCmdlet : BaseCmdlet { [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "BySubnetId", HelpMessage = "The subnet ID to create the address in.")] [ValidateNotNullOrEmpty] public int? SubnetId { get; set; } [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "BySubnetObject", HelpMessage = "The subnet object to create the address in.")] [ValidateNotNullOrEmpty] public Subnetwork? SubnetObject { get; set; } [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 1, HelpMessage = "The IP address to create.")] [ValidateNotNullOrEmpty] public string IP { get; set; } = string.Empty; [Parameter(Mandatory = false, Position = 2, HelpMessage = "Mark this address as a gateway.")] public SwitchParameter Gateway { get; set; } [Parameter(Mandatory = false, Position = 3, HelpMessage = "Description for the address.")] public string? Description { get; set; } [Parameter(Mandatory = false, Position = 4, HelpMessage = "Hostname for the address.")] public string? Hostname { get; set; } [Parameter(Mandatory = false, Position = 5, HelpMessage = "MAC address.")] public string? MAC { get; set; } [Parameter(Mandatory = false, Position = 6, HelpMessage = "Owner of the address.")] public string? Owner { get; set; } [Parameter(Mandatory = false, Position = 7, HelpMessage = "Tag ID for the address.")] public int? TagId { get; set; } [Parameter(Mandatory = false, Position = 8, HelpMessage = "Ignore PTR record generation.")] public SwitchParameter PTRIgnore { get; set; } [Parameter(Mandatory = false, Position = 9, HelpMessage = "PTR record ID.")] public int? PTRId { get; set; } [Parameter(Mandatory = false, Position = 10, HelpMessage = "Note for the address.")] public string? Note { get; set; } [Parameter(Mandatory = false, Position = 11, HelpMessage = "Exclude from ping scanning.")] public SwitchParameter ExcludePing { get; set; } [Parameter(Mandatory = false, Position = 12, HelpMessage = "Associated device ID.")] public int? DeviceId { get; set; } [Parameter(Mandatory = false, Position = 13, HelpMessage = "Port information.")] public string? Port { get; set; } [Parameter(Mandatory = false, Position = 14, HelpMessage = "Custom fields as a hashtable or PSObject.")] public object? CustomFields { get; set; } protected override void ProcessRecord() { try { var actualSubnetId = ParameterSetName == "BySubnetObject" ? SubnetObject!.Id : SubnetId!.Value; var body = BuildRequestBody(actualSubnetId); var result = RequestHelper.InvokeRequest( "POST", ApiController.Addresses, null, null, body ).GetAwaiter().GetResult(); if (result != null) { // Fetch the created address to return it 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, "NewAddressError", ErrorCategory.InvalidOperation, null)); } } private Dictionary BuildRequestBody(int subnetId) { var body = new Dictionary { ["subnetId"] = subnetId, ["ip"] = IP }; 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 (TagId.HasValue) body["tag"] = TagId.Value; if (PTRIgnore.IsPresent) body["PTRignore"] = "1"; if (PTRId.HasValue) body["PTR"] = PTRId.Value; if (!string.IsNullOrEmpty(Note)) body["note"] = Note; if (ExcludePing.IsPresent) body["excludePing"] = "1"; if (DeviceId.HasValue) body["deviceId"] = DeviceId.Value; if (!string.IsNullOrEmpty(Port)) body["port"] = Port; foreach (var kvp in ConvertCustomFields(CustomFields)) { body[kvp.Key] = kvp.Value; } return body; } }