Public/Bundles.ps1

function Get-TMBundle {
    param(
        [Parameter(Mandatory = $false)][PSObject]$TMSession = 'Default',
        [Parameter(Mandatory = $false)][String]$Name,
        [Parameter(Mandatory = $false)][Switch]$ResetIDs,
        [Parameter(Mandatory = $false)][Switch]$Label

    )

    $TMSession = Get-TMSession $TMSession

    $RestSplat = @{
        Uri                  = "https://$($TMSession.TMServer)/tdstm/api/bundle?project=$($TMSession.UserContext.Project.Id)"
        Method               = 'GET'
        WebSession           = $TMSession.TMRestSession
        SkipHttpErrorCheck   = $true
        StatusCodeVariable   = 'StatusCode'
        SkipCertificateCheck = $TMSession.AllowInsecureSSL
    }

    try {
        $Response = Invoke-RestMethod @RestSplat

        if ($StatusCode -ne 200) {
            throw $Response
        }
    } catch {
        throw "Error getting bundles: $($_.Exception.Message)"
    }

    $Results = $Response | Sort-Object -Property 'name'

    if ($ResetIDs) {
        for ($i = 0; $i -lt $Results.Count; $i++) {

            $Results[$i].assetQuantity = $null
            $Results[$i].id = $null
        }
    }

    if ($Name) {
        $Results = $Results | Where-Object { $_.name -eq $Name }
    }

    return $Results
}


function New-TMBundle {
    [CmdletBinding()]      # Always add CmdletBinding to expose the common Cmdlet variables
    param(
        [Parameter(Mandatory = $false)][psobject]$TMSession = 'Default',
        [Parameter(Mandatory = $true)][psobject]$Bundle,
        [Parameter(Mandatory = $false)][switch]$PassThru
    )

    $TMSession = Get-TMSession $TMSession

    $ExistingBundle = Get-TMBundle -Name $Bundle.name -TMSession $TMSession

    if ($ExistingBundle) {
        Write-Warning 'Bundle already exists'
        if ($PassThru) {
            return $ExistingBundle
        } else {
            return
        }
    }

    $RestSplat = @{
        Uri                  = "https://$($TMSession.TMServer)/tdstm/api/bundle?project=$($TMSession.UserContext.Project.Id)"
        Method               = 'POST'
        WebSession           = $TMSession.TMRestSession
        SkipHttpErrorCheck   = $true
        StatusCodeVariable   = 'StatusCode'
        SkipCertificateCheck = $TMSession.AllowInsecureSSL
        Body                 = (@{
                name           = $Bundle.name
                description    = $Bundle.description
                useForPlanning = $Bundle.useForPlanning
                project        = $TMSession.userContext.project.id
            } | ConvertTo-Json -Compress)
    }

    try {
        $Response = Invoke-RestMethod @RestSplat
        if ($StatusCode -ne 200) {
            throw "Unable to Create Bundle: $Response"
        }
        return $Response
    } catch {
        throw "Error creating bundle '$($Bundle.name)': $($_.Exception.Message)"
    }
}