namespace PS.IPAM.Cmdlets; using System; using System.Collections.Generic; using System.Management.Automation; using PS.IPAM.Helpers; /// /// Retrieves VLAN information from phpIPAM. /// [Cmdlet(VerbsCommon.Get, "Vlan", DefaultParameterSetName = "NoParams")] [OutputType(typeof(Vlan))] public class GetVlanCmdlet : BaseCmdlet { [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByID", HelpMessage = "The VLAN ID.")] [ValidateNotNullOrEmpty] public int? Id { get; set; } [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByNumber", HelpMessage = "The VLAN number.")] [ValidateNotNullOrEmpty] public int? Number { get; set; } [Parameter( Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByL2Domain", HelpMessage = "The L2 domain ID to get VLANs from.")] [ValidateNotNullOrEmpty] public int? L2DomainId { get; set; } [Parameter( Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "BySubnetObject", HelpMessage = "Get the VLAN associated with this subnet.")] [ValidateNotNullOrEmpty] public Subnetwork? SubnetObject { get; set; } [Parameter( Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, ParameterSetName = "ByDomainObject", HelpMessage = "Get VLANs in this L2 domain.")] [ValidateNotNullOrEmpty] public Domain? DomainObject { get; set; } protected override void ProcessRecord() { try { var controller = ApiController.Vlan; var identifiers = new List(); switch (ParameterSetName) { case "ByID": identifiers.Add(Id!.Value.ToString()); break; case "ByNumber": identifiers.Add("search"); identifiers.Add(Number!.Value.ToString()); break; case "ByL2Domain": controller = ApiController.L2Domains; identifiers.Add(L2DomainId!.Value.ToString()); identifiers.Add(ApiSubController.Vlans.ToString().ToLowerInvariant()); break; case "BySubnetObject": if (SubnetObject == null || SubnetObject.VlanId <= 0) { return; } identifiers.Add(SubnetObject.VlanId.ToString()); break; case "ByDomainObject": if (DomainObject != null) { var result = RequestHelper.InvokeRequest( "GET", ApiController.L2Domains, ModelType.Vlan, ApiSubController.Vlans, null, new[] { DomainObject.Id.ToString() } ).GetAwaiter().GetResult(); WriteResult(result); } return; } var vlanResult = RequestHelper.InvokeRequest( "GET", controller, ModelType.Vlan, null, null, identifiers.Count > 0 ? identifiers.ToArray() : null ).GetAwaiter().GetResult(); WriteResult(vlanResult); } catch (Exception ex) { WriteError(new ErrorRecord(ex, "GetVlanError", ErrorCategory.InvalidOperation, null)); } } }