Class/UpdateContainersContainerHealthCheckDefinition.Class.psm1

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

## Container Health Check Definition Class.
class UpdateContainersContainerHealthCheckDefinition
{
    <#
    .NAME
        UpdateContainersContainerHealthCheckDefinition

    .SYNOPSIS
        Class defining a health check configuration for container engines.

    .DESCRIPTION
        This class represents the configuration details for a health check that can be
        applied to containers managed by container engines like Docker and Podman.

    .PROPERTIES
        Command
            The command to run to check the health of the container.

        Interval
            The time interval between health checks (e.g., "30s" for 30 seconds).

        Retries
            The number of consecutive failures required before marking the container as unhealthy.

        StartPeriod
            The initial delay before starting health checks after the container starts (e.g., "3s" for 3 seconds).

        Timeout
            The maximum time to wait for the health check command to complete (e.g., "30s" for 30 seconds).
    #>


    ## Command Property.
    [JsonPropertyName('command')]
    [JsonRequired()]
    [String] $Command;

    ## Interval Property.
    [AllowNull()]
    [JsonPropertyName('interval')]
    [String] $Interval = "30s";

    ## Retries Property.
    [AllowNull()]
    [JsonPropertyName('retries')]
    [Range(1, 10)]
    [Int32] $Retries = 3;

    ## Start Period Property.
    [AllowNull()]
    [JsonPropertyName('start')]
    [String] $StartPeriod = "3s";

    ## Timeout Property.
    [AllowNull()]
    [JsonPropertyName('timeout')]
    [String] $Timeout = "30s";

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

        .SYNOPSIS
            Generates the command line string for the health check configuration.

        .DESCRIPTION
            This method constructs the appropriate command line string to define
            a health check for a container using the properties defined in the
            ContainerHealthCheckDefinition class.

        .OUTPUTS
            String
                The command line string for the health check configuration.
        #>


        ## Define our command line string.
        [String] $commandLine = "--health-cmd='$($this.Command.Trim())'";

        ## Check for a provided interval.
        if ($null -ne $this.Interval -and "" -ne $this.Interval.Trim())
        {
            ## Append the interval to the command line string.
            $commandLine = "${commandLine} ```n`t--health-interval='$($this.Interval.Trim())'";
        }

        ## Check for a provided retries.
        if ($null -ne $this.Retries)
        {
            ## Append the retries to the command line string.
            $commandLine = "${commandLine} ```n`t--health-retries=$($this.Retries)";
        }

        ## Check for a provided start period.
        if ($null -ne $this.StartPeriod -and "" -ne $this.StartPeriod.Trim())
        {
            ## Append the start period to the command line string.
            $commandLine = "${commandLine} ```n`t--health-start-period='$($this.StartPeriod.Trim())'";
        }

        ## Check for a provided timeout.
        if ($null -ne $this.Timeout -and "" -ne $this.Timeout.Trim())
        {
            ## Append the timeout to the command line string.
            $commandLine = "${commandLine} ```n`t--health-timeout='$($this.Timeout.Trim())'";
        }

        ## We're done, return the command line string.
        return $commandLine.Trim();
    }
};