Public/Bundles.ps1

## TM Bundles
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

    )
    ## Get Session Configuration
    $TMSession = Get-TMSession $TMSession

    #Honor SSL Settings
    $TMCertSettings = @{SkipCertificateCheck = $TMSession.AllowInsecureSSL }

    # Format the uri
    $Instance = $TMSession.TMServer.Replace('/tdstm', '').Replace('https://', '').Replace('http://', '')
    $uri = "https://$instance/tdstm/ws/moveBundle/list"

    try {
        $response = Invoke-WebRequest -Method Get -Uri $uri -WebSession $TMSession.TMWebSession @TMCertSettings
    } catch {
        throw $_
    }

    if ($response.StatusCode -in @(200, 204)) {
        $Results = ($response.Content | ConvertFrom-Json).data
    } else {
        throw 'Unable to collect Bundles.'
    }

    ## Sort the Bundles
    $Results = $Results | Sort-Object -Property 'name'

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

            ## Remove the asset qty value
            $Results[$i].assetQuantity = $null
            $Results[$i].id = $null
        }
    }

    ## Return the requested Bundle
    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
    )

    ## Get Session Configuration
    $TMSession = Get-TMSession $TMSession

    #Honor SSL Settings
    $TMCertSettings = @{SkipCertificateCheck = $TMSession.AllowInsecureSSL }

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

    if ($ExistingBundle) {
        # Write-Host "Bundle Exists: "$Bundle.name
        if ($PassThru) {
            return $ExistingBundle
        } else { return }
    }
    # else {
    # Write-Host "Creating Bundle:"$Bundle.name
    # }

    $Instance = $TMSession.TMServer.Replace('/tdstm', '')
    $instance = $instance.Replace('https://', '')
    $instance = $instance.Replace('http://', '')
    $uri = 'https://'
    $uri += $instance
    $uri += '/tdstm/ws/moveBundle'

    Set-TMHeaderContentType -ContentType 'JSON' -TMSession $TMSession

    ## Reset Bundle ID // Regardless of the Version of the import file
    if ($Bundle.PSOBject.Properties.Name -eq 'id') { $Bundle.id = '' }
    if ($Bundle.PSOBject.Properties.Name -eq 'bundleId') { $Bundle.bundleId = '' }

    $PostJson = $Bundle | ConvertTo-Json

    try {
        $response = Invoke-WebRequest -Method Post -Uri $uri -WebSession $TMSession.TMWebSession -Body $PostJson @TMCertSettings
        if ($response.StatusCode -eq 200 ) {
            if ($PassThru) {
                return ($response.Content | ConvertFrom-Json).data
            } else {
                return
            }
        } elseif ($response.StatusCode -eq 204) {
            return
        } else {
            throw 'Unable to Create Bundle'
        }
    } catch {
        throw $_
    }
}