Class/UpdateContainersNetworkDefinition.Class.psm1

## Namespaces.
using namespace System.Text.Json;
using namespace System.Text.Json.Serialization;

## Custom Modules.
using module '..\Enum\UpdateContainersEngine.Enum.psm1';

## Definition Replacement Class.
class UpdateContainersNetworkDefinition
{
    <#
    .NAME
        UpdateContainersNetworkDefinition

    .SYNOPSIS
        Class defining a network configuration for container engines.

    .DESCRIPTION
        This class represents the configuration details for a network that can be
        created and managed by container engines like Docker and Podman.

    .PROPERTIES
        Bridge
            The name of the bridge interface to use for the network.

        Command
            The container engine command to use (Docker or Podman).

        Driver
            The network driver to use (e.g., bridge, macvlan).

        Interface
            The name of the network interface to bind to (if applicable).

        IPv4Prefix
            The IPv4 subnet prefix for the network (e.g., 192.168.1).

        IPv6Prefix
            The IPv6 subnet prefix for the network (e.g., fd00:abcd:1234:1).

        Name
            The name of the network.
    #>


    ## Bridge Property.
    [AllowNull()]
    [JsonPropertyName('bridge')]
    [String] $Bridge = $null;

    ## Command Property.
    [AllowNull()]
    [JsonPropertyName('command')]
    [Nullable[UpdateContainersEngineEnum]] $Command = $null;

    ## Driver Property.
    [AllowNull()]
    [JsonPropertyName('driver')]
    [String] $Driver = "bridge";

    ## Interface Name Property.
    [AllowNull()]
    [JsonPropertyName('interface')]
    [String] $Interface = $null;

    ## IPv4Prefix Property.
    [JsonPropertyName('prefix')]
    [JsonRequired()]
    [String] $IPv4Prefix;

    ## IPv6 Property.
    [AllowNull()]
    [JsonPropertyName('prefix6')]
    [String] $IPv6Prefix;

    ## Name Property.
    [JsonPropertyName('name')]
    [JsonRequired()]
    [String] $Name;

    ## ToCommandLine Method.
    [String] ToCommandLine()
    {
        <#
        .NAME
            ToCommandLine

        .SYNOPSIS
            Generates the command line string to create the network.

        .DESCRIPTION
            This method constructs the appropriate command line string to create
            a network using the specified container engine, based on the properties
            defined in the NetworkDefinition class.

        .OUTPUTS
            String
                The command line string to create the network.
        #>


        ## Define our list of non-namable interface drivers.
        [String[]] $unnamedDrivers = @("macvlan", "ipvlan");

        ## Define our command line string.
        [String] $commandLine = "network create ```n`t--driver '$($this.Driver.ToLower().Trim())'";

        ## Check for an IPv4 prefix.
        if ($null -ne $this.IPv4Prefix -and "" -ne $this.IPv4Prefix.Trim())
        {
            ## Add the IPv6 gateway and subnet to the command line string.
            $commandLine = "${commandLine} ```n`t--gateway '$($this.IPv4Prefix.Trim()).1' ```n`t--ipv4 ```n`t--subnet '$($this.IPv4Prefix.Trim()).0/24'";
        }

        ## Check for an IPv6 prefix
        if ($null -ne $this.IPv6Prefix -and "" -ne $this.IPv6Prefix.Trim())
        {
            ## Append the IPv6 to the command line string.
            $commandLine = "${commandLine} ```n`t--gateway '$($this.IPv6Prefix.Trim())::1' ```n`t--ipv6 ```n`t--subnet '$($this.IPv6Prefix.Trim())::/64'";
        }

        ## Check for a bridge name.
        if ($null -ne $this.Bridge -and "" -ne $this.Bridge.Trim() -and ($unnamedDrivers -Contains $this.Driver.ToLower().Trim()))
        {
            ## Append the bridge name to the command line string.
            $commandLine = "${commandLine} ```n`t--opt 'parent=$($this.Bridge.Trim() )'";
        }

        ## Check for an interface name.
        if ($null -ne $this.Interface -and "" -ne $this.Interface.Trim() -and -not ($unnamedDrivers -Contains $this.Driver.ToLower().Trim()))
        {
            ## Append the interface name to the command line string.
            $commandLine = "${commandLine} ```n`t--opt 'com.docker.network.bridge.name=$($this.Interface.Trim() )'";
        }

        ## We're done, return the command line string.
        return "${commandLine} ```n`t'$($this.Name.Trim())'".Trim();
    }
};