Public/Dependencies.ps1


function Remove-TMDependency {
    <#
    .SYNOPSIS
        Removes Dependencies from TransitionManager.
 
    .DESCRIPTION
        Deletes one or more Dependency records in TM by Dependency identifier.
 
    .PARAMETER TMSession
        A TMSession object or session name to use for the request. Defaults to
        `'Default'`.
 
    .PARAMETER DependencyId
        One or more Dependency identifiers to remove.
 
    .EXAMPLE
        Remove-TMDependency -DependencyId 101, 102
 
        Removes the specified Dependency records from TransitionManager.
 
    .NOTES
        Use `Get-TMDependency` first when you need to locate Dependency ids.
    #>

    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 {
    <#
    .SYNOPSIS
        Gets Dependencies from TransitionManager.
 
    .DESCRIPTION
        Retrieves Dependency records from TM and optionally filters them by
        Dependency type, asset, dependent asset, or status.
 
    .PARAMETER TMSession
        A TMSession object or session name to use for the request. Defaults to
        `'Default'`.
 
    .PARAMETER DependencyType
        The Dependency type to filter on.
 
    .PARAMETER AssetId
        The source asset identifier to filter on.
 
    .PARAMETER AssetName
        The source asset name to filter on.
 
    .PARAMETER DependentId
        The dependent asset identifier to filter on.
 
    .PARAMETER DependentName
        The dependent asset name to filter on.
 
    .PARAMETER Status
        The Dependency status to filter on.
 
    .EXAMPLE
        Get-TMDependency -AssetName 'APP01' -DependentName 'DB01'
 
        Retrieves Dependencies between the specified asset and dependent records.
 
    .NOTES
        You can combine multiple filters to narrow the returned Dependency set.
    #>

    [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
    }

}