Public/Dependencies.ps1


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

    ## 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/wsAsset/bulkDeleteDependencies"

    try {
        $JsonQuery = @{
            dependencies = $DependencyId
        } | 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
            Write-Verbose $Data.resp

        } else {
            return "Unable to Remove Dependency"
        }
    } catch {
        return $_
    }
}

Function Get-TMDependency {
    param(
        [Parameter(Mandatory = $false)][PSObject]$TMSession = 'Default',
        [Parameter(Mandatory = $false)][String]$DependencyType,
        [Parameter(Mandatory = $false)][int]$AssetId,
        [Parameter(Mandatory = $false)][String]$AssetName,
        [Parameter(Mandatory = $false)][int]$DependentId,
        [Parameter(Mandatory = $false)][String]$DependentName,
        [Parameter(Mandatory = $false)][String]$Status,
        [Parameter(Mandatory = $false)][String]$Comment
    )

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

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

    ## 4.7.1 Query format
    # Reference JSON:

    $Query = @{
        rows = 1000
        page = 1
        sdix = 'assetName'
        sord = 'asc'
    }

    ## IDs are not supported in lookup. They are filtered after the results come back
    # if ( $AssetId ) { Add-Member -InputObject $Query -NotePropertyName 'assetId' -NotePropertyValue ($AssetId -as [System.Int64]) }
    # if ( $DependentId ) { Add-Member -InputObject $Query -NotePropertyName 'dependentId' -NotePropertyValue ($DependentId -as [System.Int64]) }

    ## Add Filtering for the Dependencies we aer looking for
    if ( $AssetName ) { Add-Member -InputObject $Query -NotePropertyName 'assetName' -NotePropertyValue ($AssetName) }
    if ( $DependentName ) { Add-Member -InputObject $Query -NotePropertyName 'dependentName' -NotePropertyValue ($DependentName) }
    if ( $DependencyType ) { Add-Member -InputObject $Query -NotePropertyName 'type' -NotePropertyValue $DependencyType }
    if ( $Status ) { Add-Member -InputObject $Query -NotePropertyName 'status' -NotePropertyValue $Status }
    if ( $Comment ) { Add-Member -InputObject $Query -NotePropertyName 'comment' -NotePropertyValue $Comment }

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

    try {
        $JsonQuery = $Query | 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
            Switch ($Data.Status) {
                "error" {
                    Write-Host $Data.errors
                    break
                }
                "success" {
                    $Deps = $Data.data.dependencies

                    if ( $AssetId ) { $Deps = $Deps | Where-Object { $_.assetId -eq $AssetId } }
                    if ( $DependentId ) { $Deps = $Deps | Where-Object { $_.dependentId -eq $DependentId } }

                    return $Deps
                    break
                }
                default {
                    break
                }
            }
        } else {
            return "Unable to get Dependency."
        }
    } catch {
        return $_
    }
}