Public/Dependencies.ps1


function Remove-TMDependency {
    param(
        [Parameter(Mandatory = $false)][PSObject]$TMSession = 'Default',
        [Parameter(Mandatory = $true, Position = 0)][int[]]$DependencyId
    )

    $responseOK = '(\d+) records? w(as|ere) deleted'
    $responseNone = 'No Dependency records were deleted'

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

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

    $uri = "https://{0}/tdstm/ws/dependency/bulkDelete" -f $TMSession.TMServer
    $statement = "find Dependency by 'id' inList {0} fetch 'id'" -f ($DependencyId -join ',')
    $deletedMessage = '{0} out of {1} dependencies were deleted'


    try {
        $JsonQuery = @{
            dependencies = $DependencyId
            statement = $statement
        } | ConvertTo-Json
        Set-TMHeaderContentType 'JSON' -TMSession $TMSession
        $response = Invoke-WebRequest -Method Post -Uri $uri -WebSession $TMSession.TMWebSession @TMCertSettings -Body $JsonQuery

        if ($response.StatusCode -in @(200, 204)) {
            $Data = $response.Content | ConvertFrom-Json
            if ($Data.resp -match $responseOK) {
                if ($Matches[1] -ne $DependencyId.Count) {
                    $message = $deletedMessage -f $Matches[1], $DependencyId.Count
                    Write-Warning -Verbose $message
                }
                else {
                    Write-Verbose $Data.resp
                }

            } elseif ($Data.resp -match $responseNone) {
                throw "Error removing dependency: {0}" -f $Data.resp
            }
            else {
                throw "Unknown response: $responseContent"
            }
        } else {
            throw "Unable to Remove Dependency: $_"
        }
    } catch {
        return $_
    }
}

function Get-TMDependency {
    [CmdletBinding()]
    param(
        [Parameter()][psobject]$TMSession = 'Default',
        [Parameter()][string]$DependencyType,
        [Parameter()][int]$AssetId,
        [Parameter()][string]$AssetName,
        [Parameter()][int]$DependentId,
        [Parameter()][string]$DependentName,
        [Parameter()][string]$Status
    )

    begin {
        $TMSession = Get-TMSession $TMSession

        $paramToField = @{
            DependencyType = 'type'
            AssetId        = 'asset.Id'
            AssetName      = 'asset.Name'
            DependentId    = 'dependent.Id'
            DependentName  = 'dependent.Name'
            Status         = 'status'
        }
        
        $paramsString = $(foreach ($param in $paramToField.Keys.Where( {$PSItem -in $PSBoundParameters.Keys} )) {
                "'{0}' eq '{1}'" -f $paramToField[$param], (Get-Variable -Name $param -ValueOnly)
        }) -join ' and '

        if ( -not $paramsString.Length ) {
            $paramsString = "'id' ne '0'"
        }

        $query = 'find Dependency by {0} fetch "*"' -f $paramsString

        Write-Verbose $query

    }

    process {
        Invoke-TMQLStatement -Statement $query
    }

}