using System;
using System.Collections.Generic;
using System.Management.Automation;
using PSProxmox.Client;
using PSProxmox.Models;
using PSProxmox.Session;
using PSProxmox.Utilities;
namespace PSProxmox.Cmdlets
{
///
/// Creates a new SDN zone in Proxmox VE.
/// The New-ProxmoxSDNZone cmdlet creates a new SDN zone in Proxmox VE.
///
/// Create a new SDN zone
/// $zone = New-ProxmoxSDNZone -Connection $connection -Zone "zone1" -Type "vlan" -Bridge "vmbr0"
///
///
[Cmdlet(VerbsCommon.New, "ProxmoxSDNZone")]
[OutputType(typeof(ProxmoxSDNZone))]
public class NewProxmoxSDNZoneCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The name of the zone.
///
[Parameter(Mandatory = true)]
public string Zone { get; set; }
///
/// The type of the zone.
///
[Parameter(Mandatory = true)]
[ValidateSet("vlan", "vxlan", "qinq", "simple")]
public string Type { get; set; }
///
/// The bridge of the zone.
///
[Parameter(Mandatory = false)]
public string Bridge { get; set; }
///
/// The DNS servers of the zone.
///
[Parameter(Mandatory = false)]
public string DNS { get; set; }
///
/// The DNS search domains of the zone.
///
[Parameter(Mandatory = false)]
public string DNSZone { get; set; }
///
/// The DHCP server of the zone.
///
[Parameter(Mandatory = false)]
public string DHCP { get; set; }
///
/// The reverse DNS of the zone.
///
[Parameter(Mandatory = false)]
public string ReverseDNS { get; set; }
///
/// The IPv6 of the zone.
///
[Parameter(Mandatory = false)]
public string IPv6 { get; set; }
///
/// The MTU of the zone.
///
[Parameter(Mandatory = false)]
public int? MTU { get; set; }
///
/// Whether the zone is VLAN aware.
///
[Parameter(Mandatory = false)]
public SwitchParameter VLANAware { get; set; }
///
/// The controller of the zone.
///
[Parameter(Mandatory = false)]
public string Controller { get; set; }
///
/// The gateway of the zone.
///
[Parameter(Mandatory = false)]
public string Gateway { get; set; }
///
/// The MAC prefix of the zone.
///
[Parameter(Mandatory = false)]
public string MACPrefix { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Create the zone
var parameters = new Dictionary
{
["zone"] = Zone,
["type"] = Type
};
if (!string.IsNullOrEmpty(Bridge))
{
parameters["bridge"] = Bridge;
}
if (!string.IsNullOrEmpty(DNS))
{
parameters["dns"] = DNS;
}
if (!string.IsNullOrEmpty(DNSZone))
{
parameters["dnszone"] = DNSZone;
}
if (!string.IsNullOrEmpty(DHCP))
{
parameters["dhcp"] = DHCP;
}
if (!string.IsNullOrEmpty(ReverseDNS))
{
parameters["reversedns"] = ReverseDNS;
}
if (!string.IsNullOrEmpty(IPv6))
{
parameters["ipv6"] = IPv6;
}
if (MTU.HasValue)
{
parameters["mtu"] = MTU.Value.ToString();
}
parameters["vlanaware"] = VLANAware.IsPresent ? "1" : "0";
if (!string.IsNullOrEmpty(Controller))
{
parameters["controller"] = Controller;
}
if (!string.IsNullOrEmpty(Gateway))
{
parameters["gateway"] = Gateway;
}
if (!string.IsNullOrEmpty(MACPrefix))
{
parameters["mac_prefix"] = MACPrefix;
}
// Create the zone
client.Post("sdn/zones", parameters);
// Get the created zone
string zoneResponse = client.Get($"sdn/zones/{Zone}");
var zone = JsonUtility.DeserializeResponse(zoneResponse);
WriteObject(zone);
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "NewProxmoxSDNZoneError", ErrorCategory.OperationStopped, Connection));
}
}
}
}