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 VNet in Proxmox VE.
/// The New-ProxmoxSDNVnet cmdlet creates a new SDN VNet in Proxmox VE.
///
/// Create a new SDN VNet
/// $vnet = New-ProxmoxSDNVnet -Connection $connection -VNet "vnet1" -Zone "zone1" -IPv4 "192.168.1.0/24" -Gateway "192.168.1.1"
///
///
[Cmdlet(VerbsCommon.New, "ProxmoxSDNVnet")]
[OutputType(typeof(ProxmoxSDNVnet))]
public class NewProxmoxSDNVnetCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The name of the VNet.
///
[Parameter(Mandatory = true)]
public string VNet { get; set; }
///
/// The zone of the VNet.
///
[Parameter(Mandatory = true)]
public string Zone { get; set; }
///
/// The alias of the VNet.
///
[Parameter(Mandatory = false)]
public string Alias { get; set; }
///
/// The VLAN ID of the VNet.
///
[Parameter(Mandatory = false)]
public int? VlanID { get; set; }
///
/// The tag of the VNet.
///
[Parameter(Mandatory = false)]
public int? Tag { get; set; }
///
/// The IPv4 subnet of the VNet.
///
[Parameter(Mandatory = false)]
public string IPv4 { get; set; }
///
/// The IPv6 subnet of the VNet.
///
[Parameter(Mandatory = false)]
public string IPv6 { get; set; }
///
/// The MAC address of the VNet.
///
[Parameter(Mandatory = false)]
public string MAC { get; set; }
///
/// The gateway of the VNet.
///
[Parameter(Mandatory = false)]
public string Gateway { get; set; }
///
/// The IPv6 gateway of the VNet.
///
[Parameter(Mandatory = false)]
public string Gateway6 { get; set; }
///
/// The MTU of the VNet.
///
[Parameter(Mandatory = false)]
public int? MTU { get; set; }
///
/// Whether DHCP is enabled for the VNet.
///
[Parameter(Mandatory = false)]
public SwitchParameter DHCP { get; set; }
///
/// The DNS servers of the VNet.
///
[Parameter(Mandatory = false)]
public string DNS { get; set; }
///
/// The DNS search domain of the VNet.
///
[Parameter(Mandatory = false)]
public string DNSSearchDomain { get; set; }
///
/// The reverse DNS of the VNet.
///
[Parameter(Mandatory = false)]
public string ReverseDNS { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Create the VNet
var parameters = new Dictionary
{
["vnet"] = VNet,
["zone"] = Zone
};
if (!string.IsNullOrEmpty(Alias))
{
parameters["alias"] = Alias;
}
if (VlanID.HasValue)
{
parameters["vlanid"] = VlanID.Value.ToString();
}
if (Tag.HasValue)
{
parameters["tag"] = Tag.Value.ToString();
}
if (!string.IsNullOrEmpty(IPv4))
{
parameters["ipv4"] = IPv4;
}
if (!string.IsNullOrEmpty(IPv6))
{
parameters["ipv6"] = IPv6;
}
if (!string.IsNullOrEmpty(MAC))
{
parameters["mac"] = MAC;
}
if (!string.IsNullOrEmpty(Gateway))
{
parameters["gateway"] = Gateway;
}
if (!string.IsNullOrEmpty(Gateway6))
{
parameters["gateway6"] = Gateway6;
}
if (MTU.HasValue)
{
parameters["mtu"] = MTU.Value.ToString();
}
parameters["dhcp"] = DHCP.IsPresent ? "1" : "0";
if (!string.IsNullOrEmpty(DNS))
{
parameters["dns"] = DNS;
}
if (!string.IsNullOrEmpty(DNSSearchDomain))
{
parameters["dnssearchdomain"] = DNSSearchDomain;
}
if (!string.IsNullOrEmpty(ReverseDNS))
{
parameters["reversedns"] = ReverseDNS;
}
// Create the VNet
client.Post("sdn/vnets", parameters);
// Get the created VNet
string vnetResponse = client.Get($"sdn/vnets/{VNet}");
var vnet = JsonUtility.DeserializeResponse(vnetResponse);
WriteObject(vnet);
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "NewProxmoxSDNVnetError", ErrorCategory.OperationStopped, Connection));
}
}
}
}