lib/AssetViews.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316


## TM AssetViewConfigurations
Function New-TMAssetViewConfiguration {
    param(
        [Parameter(Mandatory = $false)][String]$TMSession = 'Default',
        [Parameter(Mandatory = $true, Position = 0)][PSObject]$AssetViewConfiguration,
        [Parameter(Mandatory = $false)][String]$Server = $global:TMSessions[$TMSession].TMServer,
        [Parameter(Mandatory = $false)]$AllowInsecureSSL = $global:TMSessions[$TMSession].AllowInsecureSSL,
        [Parameter(Mandatory = $false)][Switch]$PassThru,
        [Parameter(Mandatory = $false)][Switch]$Update
    )

    ## 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
    if ($TMSessionConfig.AllowInsecureSSL) {
        $TMCertSettings = @{SkipCertificateCheck = $true }
    } else {
        $TMCertSettings = @{SkipCertificateCheck = $false }
    }


    # Write-Host "Creating AssetViewConfiguration: "$AssetViewConfiguration.name

    $instance = $Server.Replace('/tdstm', '')
    $instance = $instance.Replace('https://', '')
    $instance = $instance.Replace('http://', '')


    ## Action 1, Confirm the name is unique
    $AssetViewCheck = Get-TMAssetViewConfiguration -Name $AssetViewConfiguration.name

    if ($AssetViewCheck -and -not $Update) {
        if ($PassThru) {
            return $AssetViewCheck
        }
        return
    }

    ## Get the Current asset View and make updates to it
    if ($AssetViewCheck -and $Update) {

        $AssetViewConfiguration.id = $AssetViewCheck.id
        $AssetViewConfiguration.isExternal = $AssetViewCheck.isExternal
        $AssetViewConfiguration.isFavorite = $AssetViewCheck.isFavorite
        $AssetViewConfiguration.isGlobal = $AssetViewCheck.isGlobal
        $AssetViewConfiguration.isOverride = $AssetViewCheck.isOverride
        $AssetViewConfiguration.isOwner = $AssetViewCheck.isOwner
        $AssetViewConfiguration.isShared = $AssetViewCheck.isShared
        $AssetViewConfiguration.isSystem = $AssetViewCheck.isSystem
        Add-Member -InputObject $AssetViewConfiguration -NotePropertyName isExternal -NotePropertyValue ($AssetViewCheck.isExternal ?? $false) -Force
        # $AssetViewConfiguration.createdBy = $AssetViewCheck.createdBy
        # $AssetViewConfiguration.createdBy = $AssetViewCheck.createdBy
        # $AssetViewConfiguration.createdOn = $AssetViewCheck.createdOn
        # $AssetViewConfiguration.hasOverride = $AssetViewCheck.hasOverride
        # $AssetViewConfiguration.linkedCatalog = $AssetViewCheck.linkedCatalog
        # $AssetViewConfiguration.name = $AssetViewCheck.name
        # $AssetViewConfiguration.overridesView = $AssetViewCheck.overridesView
        # $AssetViewConfiguration.path = $AssetViewCheck.path
        # $AssetViewConfiguration.schema = $AssetViewCheck.schema
        # $AssetViewConfiguration.type = $AssetViewCheck.type
    }

    # Step 2, Create the AssetViewConfiguration
    $uri = 'https://'
    $uri += $instance
    $uri += '/tdstm/ws/assetExplorer/view'
    Set-TMHeaderContentType -ContentType JSON -TMSession $TMSession

    ## If the Asset view is not in the format to create a new one, transform it
    if ($Update -and $AssetViewCheck) {
        $uri += "/$($AssetViewCheck.id)"

        ## Create the Put Body
        $PutBody = $AssetViewConfiguration | ConvertTo-Json -Depth 100

        ## Post a New Asset View Configuration
        try {
            $response = Invoke-WebRequest -Method Put -Uri $uri -WebSession $TMSessionConfig.TMWebSession -Body $PutBody @TMCertSettings
            if ($response.StatusCode -eq 200) {
                $responseContent = $response.Content | ConvertFrom-Json
                if ($responseContent.status -eq 'success') {
                    if ($PassThru) { return $responseContent.data.AssetViewConfiguration } else { return }
                }
            }
        } catch {
            Write-Host 'Unable to create AssetViewConfiguration.'
            return $_
        }

    } else {
        ## This is a New Asset View
        ## Ensure the body is formatted properly
        if ($AssetViewConfiguration.PSObject.Properties.name -notcontains 'saveOptions') {

            $PostBody = [PSCustomObject]@{
                saveOptions  = [PSCustomObject]@{
                    canoverride   = $false
                    canShare      = $true
                    save          = $false
                    saveAsOptions = @('MY_VIEW')
                }
                isFavorite   = $AssetViewConfiguration.isFavorite
                isOwner      = $AssetViewConfiguration.isOwner
                isShared     = $AssetViewConfiguration.isShared
                isSystem     = $AssetViewConfiguration.isSystem
                schema       = $AssetViewConfiguration.schema
                queryString  = [PSCustomObject]@{}
                saveAsOption = 'MY_VIEW'
                name         = $AssetViewConfiguration.name
            } | ConvertTo-Json -Depth 100
        } else {
            $PostBody = $AssetViewConfiguration | ConvertTo-Json -Depth 100
        }

        ## Post a New Asset View Configuration
        try {
            $response = Invoke-WebRequest -Method Post -Uri $uri -WebSession $TMSessionConfig.TMWebSession -Body $PostBody @TMCertSettings
            if ($response.StatusCode -eq 200) {
                $responseContent = $response.Content | ConvertFrom-Json
                if ($responseContent.status -eq 'success') {
                    if ($PassThru) { return $responseContent.data.AssetViewConfiguration } else { return }
                }
            }
        } catch {
            Write-Host 'Unable to create AssetViewConfiguration.'
            return $_
        }
    }
}
Function Get-TMAssetView {
    param(
        [Parameter(Mandatory = $false)][String]$Name,
        [Parameter(Mandatory = $false)][String]$Id,
        [Parameter(Mandatory = $false)][PSObject]$Filter,
        [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)][Int]$Limit = 0,
        [Parameter(Mandatory = $false)][Int]$Offset = 0

    )
    # 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
    if ($TMSessionConfig.AllowInsecureSSL) {
        $TMCertSettings = @{SkipCertificateCheck = $true }
    } else {
        $TMCertSettings = @{SkipCertificateCheck = $false }
    }

    ## Make sure we have a view to use
    if (-not $Id -and $Name) {
        $AssetViewId = (Get-TMAssetViewConfiguration -Name $Name).id
    } else {
        $AssetViewId = $Id
    }


    ## Get the Field Schema for the AssetView
    $instance = $Server.Replace('/tdstm', '')
    $instance = $instance.Replace('https://', '')
    $instance = $instance.Replace('http://', '')

    $uri = 'https://'
    $uri += $instance
    $uri += '/tdstm/ws/assetExplorer/view/'
    $uri += $AssetViewId

    ## Request the data
    $response = Invoke-WebRequest -Method Get -Uri $uri -WebSession $TMSessionConfig.TMWebSession @TMCertSettings

    ## Ensure Success
    if ($response.StatusCode -eq 200) {
        $responseContent = $response.Content | ConvertFrom-Json
        if ($responseContent.status -eq 'success') {

            ## Assign the results to a Variable
            $AssetView = $responseContent.data.dataView

        }
    }

    ## Return an error if there is no AssetView
    if (-not $AssetView) {
        throw 'Unable to Get Asset View, check name and try again'
    }

    ## Construct a query of the view endpoint using the schema from the Provided Asset View
    $uri = 'https://'
    $uri += $instance
    $uri += '/tdstm/ws/assetExplorer/query/'
    $uri += $AssetView.id

    ## Create a Query Post
    $Body = @{
        filters      = @{
            columns = $AssetView.schema.columns
            domains = $AssetView.schema.domains
        }
        limit        = $Limit
        offset       = $Offset
        sortDomain   = $AssetView.schema.sort.domain
        sortOrder    = $AssetView.schema.sort.order
        sortProperty = $AssetView.schema.sort.property
    }

    ## Update the Asset Class Filtering
    if ($Filter) {
        $Body.filters = $Filter
    }

    $PostBody = $Body | ConvertTo-Json -Depth 10 -Compress

    ## Request the data
    $response = Invoke-WebRequest -Method Post -Uri $uri -Body $PostBody -WebSession $TMSessionConfig.TMWebSession @TMCertSettings

    ## Ensure Success
    if ($response.StatusCode -eq 200) {
        $responseContent = $response.Content | ConvertFrom-Json
        if ($responseContent.status -eq 'success') {

            ## Assign the results to a Variable
            $Assets = $responseContent.data.assets

        }
    } elseif ($response.StatusCode -eq 204) {
        return
    }

    ## Return located assets
    if ($Assets) { return $Assets }

}
Function Get-TMAssetViewConfiguration {
    param(
        [Parameter(Mandatory = $false)][String]$Name,
        [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)][Switch]$ResetIDs,
        [Parameter(Mandatory = $false)][Switch]$ExcludeSystem,
        [Parameter(Mandatory = $false)][Switch]$PassThru

    )
    ## 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
    if ($TMSessionConfig.AllowInsecureSSL) {
        $TMCertSettings = @{SkipCertificateCheck = $true }
    } else {
        $TMCertSettings = @{SkipCertificateCheck = $false }
    }

    $instance = $Server.Replace('/tdstm', '')
    $instance = $instance.Replace('https://', '')
    $instance = $instance.Replace('http://', '')

    $uri = 'https://'
    $uri += $instance
    $uri += '/tdstm/ws/assetExplorer/views'
    try {
        $response = Invoke-WebRequest -Method Get -Uri $uri -WebSession $TMSessionConfig.TMWebSession @TMCertSettings
    } catch {
        return $_
    }

    if ($response.StatusCode -eq 200) {
        $Result = ($response.Content | ConvertFrom-Json).data
    } else {
        return 'Unable to get Asset Views.'
    }

    ## Remove System Views
    if ($ExcludeSystem) {
        $Result = $Result | Where-Object { $_.isSystem -eq $False }
    }

    ## Sort the Asset Views by Name
    $Result = $Result | Sort-Object -Property 'Name'

    if ($ResetIDs) {
        for ($i = 0; $i -lt $Result.Count; $i++) {
            $Result[$i].id = $null
        }
    }

    if ($Name) {
        return ($Result | Where-Object { $_.name -eq $Name })
    } else {
        return $Result
    }
}