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, [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.isFavorite = $AssetViewCheck.isFavorite ?? $False $AssetViewConfiguration.isGlobal = $AssetViewCheck.isGlobal ?? $true $AssetViewConfiguration.isOverride = $AssetViewCheck.isOverride ?? $False $AssetViewConfiguration.isOwner = $AssetViewCheck.isOwner ?? $true $AssetViewConfiguration.isShared = $AssetViewCheck.isShared ?? $true $AssetViewConfiguration.isSystem = $AssetViewCheck.isSystem ?? $false 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 } } |