lib/AssetViews.ps1
## 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 ) ## 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) { if ($PassThru) { return $AssetViewCheck } return } # 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 ($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 } 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]$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." } 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 } } |