187 lines
5.2 KiB
C#
187 lines
5.2 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, "Address", DefaultParameterSetName = "ById")]
|
|
[OutputType(typeof(Address))]
|
|
public class SetAddressCmdlet : PSCmdlet
|
|
{
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 0,
|
|
ParameterSetName = "ById")]
|
|
[ValidateNotNullOrEmpty]
|
|
public int? Id { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = true,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
Position = 0,
|
|
ParameterSetName = "ByAddressObject")]
|
|
[ValidateNotNullOrEmpty]
|
|
public Address? AddressObject { get; set; }
|
|
|
|
[Parameter(
|
|
Position = 1,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
ParameterSetName = "ById")]
|
|
public bool? Gateway { get; set; }
|
|
|
|
[Parameter(
|
|
Position = 2,
|
|
ValueFromPipeline = true,
|
|
ValueFromPipelineByPropertyName = true,
|
|
ParameterSetName = "ById")]
|
|
public string? Description { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 3)]
|
|
public string? Hostname { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 4)]
|
|
public string? MAC { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 5)]
|
|
public string? Owner { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 6)]
|
|
public int? TagId { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 7)]
|
|
public bool? PTRIgnore { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 8)]
|
|
public int? PTRId { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 9)]
|
|
public string? Note { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 10)]
|
|
public bool? ExcludePing { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 11)]
|
|
public int? DeviceId { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false,
|
|
Position = 12)]
|
|
public string? Port { get; set; }
|
|
|
|
[Parameter(
|
|
Mandatory = false)]
|
|
public object? CustomFields { get; set; }
|
|
|
|
protected override void ProcessRecord()
|
|
{
|
|
try
|
|
{
|
|
int addressId;
|
|
if (ParameterSetName == "ById")
|
|
{
|
|
addressId = Id!.Value;
|
|
}
|
|
else
|
|
{
|
|
addressId = AddressObject!.Id;
|
|
}
|
|
|
|
var identifiers = new List<string> { addressId.ToString() };
|
|
var body = new Dictionary<string, object>();
|
|
|
|
if (Gateway.HasValue)
|
|
body["is_gateway"] = Gateway.Value;
|
|
if (!string.IsNullOrEmpty(Description))
|
|
body["description"] = Description;
|
|
if (!string.IsNullOrEmpty(Hostname))
|
|
body["hostname"] = Hostname;
|
|
if (!string.IsNullOrEmpty(MAC))
|
|
body["mac"] = MAC;
|
|
if (!string.IsNullOrEmpty(Owner))
|
|
body["owner"] = Owner;
|
|
if (TagId.HasValue)
|
|
body["tag"] = TagId.Value;
|
|
if (PTRIgnore.HasValue)
|
|
body["PTRignore"] = PTRIgnore.Value;
|
|
if (PTRId.HasValue)
|
|
body["PTR"] = PTRId.Value;
|
|
if (!string.IsNullOrEmpty(Note))
|
|
body["note"] = Note;
|
|
if (ExcludePing.HasValue)
|
|
body["excludePing"] = ExcludePing.Value;
|
|
if (DeviceId.HasValue)
|
|
body["deviceId"] = DeviceId.Value;
|
|
if (!string.IsNullOrEmpty(Port))
|
|
body["port"] = Port;
|
|
|
|
if (CustomFields != null)
|
|
{
|
|
var customDict = ConvertCustomFields(CustomFields);
|
|
foreach (var kvp in customDict)
|
|
{
|
|
body[kvp.Key] = kvp.Value;
|
|
}
|
|
}
|
|
|
|
try
|
|
{
|
|
RequestHelper.InvokeRequest("PATCH", controllers.addresses, null, null, body, identifiers.ToArray())
|
|
.GetAwaiter().GetResult();
|
|
}
|
|
finally
|
|
{
|
|
var address = RequestHelper.InvokeRequest("GET", controllers.addresses, types.Address, null, null, identifiers.ToArray())
|
|
.GetAwaiter().GetResult();
|
|
if (address != null)
|
|
{
|
|
WriteObject(address);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
WriteError(new ErrorRecord(ex, "SetAddressError", 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;
|
|
}
|
|
}
|