144 lines
5.3 KiB
C#
144 lines
5.3 KiB
C#
namespace PS.IPAM.Cmdlets;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Management.Automation;
|
|
using PS.IPAM.Helpers;
|
|
|
|
/// <summary>
|
|
/// Creates a new subnet in phpIPAM.
|
|
/// </summary>
|
|
[Cmdlet(VerbsCommon.New, "Subnet")]
|
|
[OutputType(typeof(Subnetwork))]
|
|
public class NewSubnetCmdlet : BaseCmdlet
|
|
{
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 0,
|
|
HelpMessage = "The subnet in CIDR notation (e.g., 192.168.1.0/24).")]
|
|
[ValidatePattern(@"^\d+\.\d+\.\d+\.\d+/\d{1,2}$")]
|
|
[ValidateNotNullOrEmpty]
|
|
public string CIDR { get; set; } = string.Empty;
|
|
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 1,
|
|
HelpMessage = "The section ID to create the subnet in.")]
|
|
[ValidateNotNullOrEmpty]
|
|
public int SectionId { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 2, HelpMessage = "Description for the subnet.")]
|
|
public string? Description { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 3, HelpMessage = "VLAN ID to associate.")]
|
|
public int? VlanId { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 4, HelpMessage = "VRF ID to associate.")]
|
|
public int? VrfId { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 5, HelpMessage = "Master subnet ID for hierarchy.")]
|
|
public int? MasterSubnetId { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 6, HelpMessage = "Nameserver ID.")]
|
|
public int? NameserverId { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 7, HelpMessage = "Show subnet name.")]
|
|
public SwitchParameter ShowName { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 8, HelpMessage = "Enable recursive DNS.")]
|
|
public SwitchParameter DNSRecursive { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 9, HelpMessage = "Enable DNS records.")]
|
|
public SwitchParameter DNSRecords { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 10, HelpMessage = "Allow IP requests.")]
|
|
public SwitchParameter AllowRequests { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 11, HelpMessage = "Scan agent ID.")]
|
|
public int? ScanAgentId { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 12, HelpMessage = "Enable subnet discovery.")]
|
|
public SwitchParameter DiscoverSubnet { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 13, HelpMessage = "Mark subnet as full.")]
|
|
public SwitchParameter IsFull { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 14, HelpMessage = "Tag ID for the subnet.")]
|
|
public int? TagId { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 15, HelpMessage = "Usage threshold percentage (1-100).")]
|
|
[ValidateRange(1, 100)]
|
|
public int? Threshold { get; set; }
|
|
|
|
[Parameter(Mandatory = false, Position = 16, 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 body = BuildRequestBody();
|
|
|
|
var result = RequestHelper.InvokeRequest(
|
|
"POST", ApiController.Subnets, null, null, body
|
|
).GetAwaiter().GetResult();
|
|
|
|
if (result != null)
|
|
{
|
|
// Get the created subnet
|
|
var subnet = RequestHelper.InvokeRequest(
|
|
"GET", ApiController.Subnets, ModelType.Subnetwork, null, null,
|
|
new[] { "cidr", CIDR }
|
|
).GetAwaiter().GetResult();
|
|
|
|
WriteResult(subnet);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteError(new ErrorRecord(ex, "NewSubnetError", ErrorCategory.InvalidOperation, null));
|
|
}
|
|
}
|
|
|
|
private Dictionary<string, object> BuildRequestBody()
|
|
{
|
|
var parts = CIDR.Split('/');
|
|
var body = new Dictionary<string, object>
|
|
{
|
|
["subnet"] = parts[0],
|
|
["mask"] = parts[1],
|
|
["sectionId"] = SectionId
|
|
};
|
|
|
|
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;
|
|
}
|
|
}
|