namespace PS.IPAM.Cmdlets; using System; using System.Collections.Generic; using System.Management.Automation; using PS.IPAM.Helpers; /// /// Updates an existing IP address entry in phpIPAM. /// [Cmdlet(VerbsCommon.Set, "Address", DefaultParameterSetName = "ById")] [OutputType(typeof(Address))] public class SetAddressCmdlet : BaseCmdlet { [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ById", HelpMessage = "The address ID to update.")] [ValidateNotNullOrEmpty] public int? Id { get; set; } [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByAddressObject", HelpMessage = "The address object to update.")] [ValidateNotNullOrEmpty] public Address? AddressObject { get; set; } [Parameter(Position = 1, HelpMessage = "Set as gateway address.")] public bool? Gateway { get; set; } [Parameter(Position = 2, HelpMessage = "Description for the address.")] public string? Description { get; set; } [Parameter(Position = 3, HelpMessage = "Hostname for the address.")] public string? Hostname { get; set; } [Parameter(Position = 4, HelpMessage = "MAC address.")] public string? MAC { get; set; } [Parameter(Position = 5, HelpMessage = "Owner of the address.")] public string? Owner { get; set; } [Parameter(Position = 6, HelpMessage = "Tag ID for the address.")] public int? TagId { get; set; } [Parameter(Position = 7, HelpMessage = "Ignore PTR record generation.")] public bool? PTRIgnore { get; set; } [Parameter(Position = 8, HelpMessage = "PTR record ID.")] public int? PTRId { get; set; } [Parameter(Position = 9, HelpMessage = "Note for the address.")] public string? Note { get; set; } [Parameter(Position = 10, HelpMessage = "Exclude from ping scanning.")] public bool? ExcludePing { get; set; } [Parameter(Position = 11, HelpMessage = "Associated device ID.")] public int? DeviceId { get; set; } [Parameter(Position = 12, HelpMessage = "Port information.")] public string? Port { get; set; } [Parameter(HelpMessage = "Custom fields as a hashtable or PSObject.")] public object? CustomFields { get; set; } protected override void ProcessRecord() { try { var addressId = ParameterSetName == "ById" ? Id!.Value : AddressObject!.Id; var identifiers = new[] { addressId.ToString() }; var body = BuildRequestBody(); try { RequestHelper.InvokeRequest( "PATCH", ApiController.Addresses, null, null, body, identifiers ).GetAwaiter().GetResult(); } finally { // Always return the updated address var address = RequestHelper.InvokeRequest( "GET", ApiController.Addresses, ModelType.Address, null, null, identifiers ).GetAwaiter().GetResult(); WriteResult(address); } } catch (Exception ex) { WriteError(new ErrorRecord(ex, "SetAddressError", ErrorCategory.InvalidOperation, null)); } } private Dictionary BuildRequestBody() { var body = new Dictionary(); if (Gateway.HasValue) body["is_gateway"] = Gateway.Value; 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.HasValue) body["PTRignore"] = PTRIgnore.Value; if (PTRId.HasValue) body["PTR"] = PTRId.Value; if (!string.IsNullOrEmpty(Note)) body["note"] = Note; if (ExcludePing.HasValue) body["excludePing"] = ExcludePing.Value; 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; } }