namespace PS.IPAM.Cmdlets; using System; using System.Collections.Generic; using System.Management.Automation; using PS.IPAM.Helpers; /// /// Assigns a tag to an address in phpIPAM. /// [Cmdlet("Assign", "Tag", SupportsShouldProcess = true)] [OutputType(typeof(Address))] public class AssignTagCmdlet : BaseCmdlet { [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "The address to assign the tag to.")] [ValidateNotNullOrEmpty] public Address? AddressObject { get; set; } [Parameter( Mandatory = true, Position = 1, HelpMessage = "The tag to assign.")] [ValidateNotNullOrEmpty] public Tag? Tag { get; set; } protected override void ProcessRecord() { try { var identifiers = new[] { AddressObject!.Id.ToString() }; var body = new Dictionary { ["tag"] = Tag!.Id }; if (ShouldProcess($"Address {AddressObject.Ip}", $"Assign tag '{Tag.Type}'")) { RequestHelper.InvokeRequest( "PATCH", ApiController.Addresses, null, null, body, identifiers ).GetAwaiter().GetResult(); // 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, "AssignTagError", ErrorCategory.InvalidOperation, null)); } } }