Public/New-DuneCollection.ps1

<#
.SYNOPSIS
Create a new Dune collection.
 
.DESCRIPTION
Creates a new collection in Dune. Supports `ShouldProcess` semantics. Provide `Name` and optional display name, description, variables and tags.
 
.PARAMETER Name
The unique name for the collection. Mandatory.
 
.PARAMETER DisplayName
A human-friendly display name for the collection.
 
.PARAMETER Description
A description for the collection.
 
.PARAMETER Variables
An array of hashtables defining collection variables.
 
.PARAMETER IsLostAndFound
If set, marks the collection as the lost-and-found collection.
 
.PARAMETER Tags
An array of hashtables used as tags for the collection.
 
.EXAMPLE
PS> New-DuneCollection -Name "web-tier" -DisplayName "Web Tier" -Description "Front-end servers"
Creates a collection named `web-tier`.
 
.EXAMPLE
PS> @{Name='env';Value='prod'} | New-DuneCollection -Name "web-tier" -Variables @(@{Name='env';Value='prod'})
Creates a collection while setting variables.
#>

function New-DuneCollection {
    [CmdletBinding(
        SupportsShouldProcess,
        ConfirmImpact = 'None'
    )]
    param (
        [Parameter(Mandatory)]
        [string]$Name,

        [Parameter()]
        [string]$DisplayName,

        [Parameter()]
        [string]$Description,

        [Parameter()]
        [hashtable[]]$Variables,

        [Parameter()]
        [switch]$IsLostAndFound,

        [Parameter()]
        [hashtable[]]$Tags
    )

    begin {}

    process {
        Write-Debug "$($MyInvocation.MyCommand)|process"
        $Body = @{    
            DisplayName     = $DisplayName
            Name            = $Name
            Description     = $Description
            Tags            = $Tags
        }
        if ($IsLostAndFound) { $Body.IsLostAndFound = $true }
        if ($PSBoundParameters.ContainsKey('Variables')) {
            $Variables | ForEach-Object {
                $_.Value = ConvertTo-Json -InputObject $Variables
            }
            $Body.Variables = $Variables
        }
        if ($PSCmdlet.ShouldProcess($($Body | ConvertTo-Json -Depth 16 -Compress))) {
            $Return = Invoke-DuneApiRequest -Uri "Collections" -Method POST -Body $Body
            $ReturnObject = if ($Return.Content) { $Return.Content | ConvertFrom-Json | ConvertTo-DuneClassObject -Class DuneCollection }
            return $ReturnObject
        }
    }

    end {}
}