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 network interface in Proxmox VE.
/// The New-ProxmoxNetwork cmdlet creates a new network interface in Proxmox VE.
///
/// Create a new bridge interface
/// $network = New-ProxmoxNetwork -Connection $connection -Node "pve1" -Interface "vmbr1" -Type "bridge" -BridgePorts "eth1" -Method "static" -Address "192.168.2.1" -Netmask "255.255.255.0" -Autostart
///
///
[Cmdlet(VerbsCommon.New, "ProxmoxNetwork")]
[OutputType(typeof(ProxmoxNetwork))]
public class NewProxmoxNetworkCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// The node to create the network interface on.
///
[Parameter(Mandatory = true)]
public string Node { get; set; }
///
/// The name of the interface.
///
[Parameter(Mandatory = true)]
public string Interface { get; set; }
///
/// The type of the interface.
///
[Parameter(Mandatory = true)]
[ValidateSet("bridge", "bond", "eth", "vlan")]
public string Type { get; set; }
///
/// The method of the interface.
///
[Parameter(Mandatory = false)]
[ValidateSet("static", "dhcp", "manual")]
public string Method { get; set; } = "static";
///
/// The IP address of the interface.
///
[Parameter(Mandatory = false)]
public string Address { get; set; }
///
/// The netmask of the interface.
///
[Parameter(Mandatory = false)]
public string Netmask { get; set; }
///
/// The gateway of the interface.
///
[Parameter(Mandatory = false)]
public string Gateway { get; set; }
///
/// The bridge ports of the interface.
///
[Parameter(Mandatory = false)]
public string BridgePorts { get; set; }
///
/// The bridge STP of the interface.
///
[Parameter(Mandatory = false)]
public SwitchParameter BridgeSTP { get; set; }
///
/// The bridge FD of the interface.
///
[Parameter(Mandatory = false)]
public int? BridgeFD { get; set; }
///
/// The bond slaves of the interface.
///
[Parameter(Mandatory = false)]
public string BondSlaves { get; set; }
///
/// The bond mode of the interface.
///
[Parameter(Mandatory = false)]
[ValidateSet("balance-rr", "active-backup", "balance-xor", "broadcast", "802.3ad", "balance-tlb", "balance-alb")]
public string BondMode { get; set; }
///
/// The VLAN ID of the interface.
///
[Parameter(Mandatory = false)]
public int? VlanID { get; set; }
///
/// The VLAN raw device of the interface.
///
[Parameter(Mandatory = false)]
public string VlanRawDevice { get; set; }
///
/// Whether the interface should autostart.
///
[Parameter(Mandatory = false)]
public SwitchParameter Autostart { get; set; }
///
/// The comments of the interface.
///
[Parameter(Mandatory = false)]
public string Comments { get; set; }
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Create the network interface
var parameters = new Dictionary
{
["iface"] = Interface,
["type"] = Type
};
if (!string.IsNullOrEmpty(Method))
{
parameters["method"] = Method;
}
if (!string.IsNullOrEmpty(Address))
{
parameters["address"] = Address;
}
if (!string.IsNullOrEmpty(Netmask))
{
parameters["netmask"] = Netmask;
}
if (!string.IsNullOrEmpty(Gateway))
{
parameters["gateway"] = Gateway;
}
if (!string.IsNullOrEmpty(BridgePorts))
{
parameters["bridge_ports"] = BridgePorts;
}
if (BridgeSTP.IsPresent)
{
parameters["bridge_stp"] = "on";
}
if (BridgeFD.HasValue)
{
parameters["bridge_fd"] = BridgeFD.Value.ToString();
}
if (!string.IsNullOrEmpty(BondSlaves))
{
parameters["bond_slaves"] = BondSlaves;
}
if (!string.IsNullOrEmpty(BondMode))
{
parameters["bond_mode"] = BondMode;
}
if (VlanID.HasValue)
{
parameters["vlan-id"] = VlanID.Value.ToString();
}
if (!string.IsNullOrEmpty(VlanRawDevice))
{
parameters["vlan-raw-device"] = VlanRawDevice;
}
parameters["autostart"] = Autostart.IsPresent ? "1" : "0";
if (!string.IsNullOrEmpty(Comments))
{
parameters["comments"] = Comments;
}
// Create the network interface
client.Post($"nodes/{Node}/network", parameters);
// Apply the network configuration
client.Put($"nodes/{Node}/network", new Dictionary());
// Get the created network interface
string networkResponse = client.Get($"nodes/{Node}/network/{Interface}");
var network = JsonUtility.DeserializeResponse(networkResponse);
network.Node = Node;
WriteObject(network);
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "NewProxmoxNetworkError", ErrorCategory.OperationStopped, Connection));
}
}
}
}