Files
ps.ipam/classlib/Cmdlets/SetSubnetCmdlet.cs

124 lines
4.4 KiB
C#

namespace PS.IPAM.Cmdlets;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using PS.IPAM.Helpers;
/// <summary>
/// Updates an existing subnet in phpIPAM.
/// </summary>
[Cmdlet(VerbsCommon.Set, "Subnet")]
[OutputType(typeof(Subnetwork))]
public class SetSubnetCmdlet : BaseCmdlet
{
[Parameter(
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
Position = 0,
HelpMessage = "The subnet ID to update.")]
[ValidateNotNullOrEmpty]
public int Id { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Description for the subnet.")]
public string? Description { get; set; }
[Parameter(Mandatory = false, HelpMessage = "VLAN ID to associate.")]
public int? VlanId { get; set; }
[Parameter(Mandatory = false, HelpMessage = "VRF ID to associate.")]
public int? VrfId { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Master subnet ID for hierarchy.")]
public int? MasterSubnetId { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Nameserver ID.")]
public int? NameserverId { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Show subnet name.")]
public SwitchParameter ShowName { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Enable recursive DNS.")]
public SwitchParameter DNSRecursive { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Enable DNS records.")]
public SwitchParameter DNSRecords { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Allow IP requests.")]
public SwitchParameter AllowRequests { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Scan agent ID.")]
public int? ScanAgentId { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Enable subnet discovery.")]
public SwitchParameter DiscoverSubnet { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Mark subnet as full.")]
public SwitchParameter IsFull { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Tag ID for the subnet.")]
public int? TagId { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Usage threshold percentage (1-100).")]
[ValidateRange(1, 100)]
public int? Threshold { get; set; }
[Parameter(Mandatory = false, HelpMessage = "Location ID.")]
public int? LocationId { 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[] { Id.ToString() };
var body = BuildRequestBody();
RequestHelper.InvokeRequest(
"PATCH", ApiController.Subnets, null, null, body, identifiers
).GetAwaiter().GetResult();
var subnet = RequestHelper.InvokeRequest(
"GET", ApiController.Subnets, ModelType.Subnetwork, null, null, identifiers
).GetAwaiter().GetResult();
WriteResult(subnet);
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "SetSubnetError", ErrorCategory.InvalidOperation, null));
}
}
private Dictionary<string, object> BuildRequestBody()
{
var body = new Dictionary<string, object>();
if (!string.IsNullOrEmpty(Description)) body["description"] = Description;
if (VlanId.HasValue) body["vlanId"] = VlanId.Value;
if (VrfId.HasValue) body["vrfId"] = VrfId.Value;
if (MasterSubnetId.HasValue) body["masterSubnetId"] = MasterSubnetId.Value;
if (NameserverId.HasValue) body["nameserverId"] = NameserverId.Value;
if (ShowName.IsPresent) body["showName"] = "1";
if (DNSRecursive.IsPresent) body["DNSrecursive"] = "1";
if (DNSRecords.IsPresent) body["DNSrecords"] = "1";
if (AllowRequests.IsPresent) body["allowRequests"] = "1";
if (ScanAgentId.HasValue) body["scanAgent"] = ScanAgentId.Value;
if (DiscoverSubnet.IsPresent) body["discoverSubnet"] = "1";
if (IsFull.IsPresent) body["isFull"] = "1";
if (TagId.HasValue) body["state"] = TagId.Value;
if (Threshold.HasValue) body["threshold"] = Threshold.Value;
if (LocationId.HasValue) body["location"] = LocationId.Value;
foreach (var kvp in ConvertCustomFields(CustomFields))
{
body[kvp.Key] = kvp.Value;
}
return body;
}
}