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

165 lines
4.7 KiB
C#

namespace PS.IPAM.Cmdlets;
using System;
using System.Collections.Generic;
using System.Management.Automation;
using PS.IPAM;
using PS.IPAM.Helpers;
[Cmdlet(VerbsCommon.Set, "Subnet")]
[OutputType(typeof(Subnetwork))]
public class SetSubnetCmdlet : PSCmdlet
{
[Parameter(
Mandatory = true,
ValueFromPipeline = true,
ValueFromPipelineByPropertyName = true,
Position = 0)]
[ValidateNotNullOrEmpty]
public int Id { get; set; }
[Parameter(
Mandatory = false)]
public string? Description { get; set; }
[Parameter(
Mandatory = false)]
public int? VlanId { get; set; }
[Parameter(
Mandatory = false)]
public int? VrfId { get; set; }
[Parameter(
Mandatory = false)]
public int? MasterSubnetId { get; set; }
[Parameter(
Mandatory = false)]
public int? NameserverId { get; set; }
[Parameter(
Mandatory = false)]
public SwitchParameter ShowName { get; set; }
[Parameter(
Mandatory = false)]
public SwitchParameter DNSRecursive { get; set; }
[Parameter(
Mandatory = false)]
public SwitchParameter DNSRecords { get; set; }
[Parameter(
Mandatory = false)]
public SwitchParameter AllowRequests { get; set; }
[Parameter(
Mandatory = false)]
public int? ScanAgentId { get; set; }
[Parameter(
Mandatory = false)]
public SwitchParameter DiscoverSubnet { get; set; }
[Parameter(
Mandatory = false)]
public SwitchParameter IsFull { get; set; }
[Parameter(
Mandatory = false)]
public int? TagId { get; set; }
[Parameter(
Mandatory = false)]
[ValidateRange(1, 100)]
public int? Threshold { get; set; }
[Parameter(
Mandatory = false)]
public int? LocationId { get; set; }
[Parameter(
Mandatory = false)]
public object? CustomFields { get; set; }
protected override void ProcessRecord()
{
try
{
var identifiers = new List<string> { Id.ToString() };
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;
if (CustomFields != null)
{
var customDict = ConvertCustomFields(CustomFields);
foreach (var kvp in customDict)
{
body[kvp.Key] = kvp.Value;
}
}
RequestHelper.InvokeRequest("PATCH", controllers.subnets, null, null, body, identifiers.ToArray())
.GetAwaiter().GetResult();
var subnet = RequestHelper.InvokeRequest("GET", controllers.subnets, types.Subnetwork, null, null, identifiers.ToArray())
.GetAwaiter().GetResult();
if (subnet != null)
{
WriteObject(subnet);
}
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "SetSubnetError", ErrorCategory.InvalidOperation, null));
}
}
private Dictionary<string, object> ConvertCustomFields(object customFields)
{
var dict = new Dictionary<string, object>();
if (customFields is PSObject psobj)
{
foreach (var prop in psobj.Properties)
{
dict[prop.Name] = prop.Value ?? new object();
}
}
else if (customFields is Dictionary<string, object> dictObj)
{
return dictObj;
}
return dict;
}
}