lib/Dependencies.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

Function Remove-TMDependency {
    param(
        [Parameter(Mandatory = $false)][String]$TMSession = "Default",    
        [Parameter(Mandatory = $false)][String]$Server = $global:TMSessions[$TMSession].TMServer,
        [Parameter(Mandatory = $false)]$AllowInsecureSSL = $global:TMSessions[$TMSession].AllowInsecureSSL,
        [Parameter(Mandatory = $true, Position = 0)][int[]]$DependencyId
    )

    ## Get Session Configuration
    $TMSessionConfig = $global:TMSessions[$TMSession]
    if (-not $TMSessionConfig) {
        Write-Host 'TMSession: [' -NoNewline
        Write-Host $TMSession -ForegroundColor Cyan
        Write-Host '] was not Found. Please use the New-TMSession command.'
        Throw "TM Session Not Found. Use New-TMSession command before using features."
    }

    #Honor SSL Settings
    $TMCertSettings = @{SkipCertificateCheck = $TMSessionConfig.AllowInsecureSSL }
    
    # Format the uri
    $instance = $Server.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 $TMSessionConfig.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)][String]$TMSession = "Default",    
        [Parameter(Mandatory = $false)][String]$Server = $global:TMSessions[$TMSession].TMServer,
        [Parameter(Mandatory = $false)]$AllowInsecureSSL = $global:TMSessions[$TMSession].AllowInsecureSSL,
        [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
    $TMSessionConfig = $global:TMSessions[$TMSession]
    if (-not $TMSessionConfig) {
        Write-Host 'TMSession: [' -NoNewline
        Write-Host $TMSession -ForegroundColor Cyan
        Write-Host '] was not Found. Please use the New-TMSession command.'
        Throw "TM Session Not Found. Use New-TMSession command before using features."
    }

    #Honor SSL Settings
    $TMCertSettings = @{SkipCertificateCheck = $TMSessionConfig.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 = $Server.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 $TMSessionConfig.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 $_
    }
}