lib/Providers.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 |
## TM Providers Function New-TMProvider { param( [Parameter(Mandatory = $false)][String]$TMSession = "Default", [Parameter(Mandatory = $true)][PSObject]$Provider, [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 Provider: "$Provider.name ## Action 1, Confirm the name is unique $instance = $Server.Replace('/tdstm', '') $instance = $instance.Replace('https://', '') $instance = $instance.Replace('http://', '') $uri = "https://" $uri += $instance $uri += "/tdstm/ws/dataingestion/provider/validateUnique" $PostBody = @{ name = $Provider.name } Set-TMHeaderContentType -ContentType JSON -TMSession $TMSession $PostBodyJSON = $PostBody | ConvertTo-Json -Depth 100 try { $response = Invoke-WebRequest -Method Post -Uri $uri -WebSession $TMSessionConfig.TMWebSession -Body $PostBodyJSON @TMCertSettings if ($response.StatusCode -eq 200) { $responseContent = $response.Content | ConvertFrom-Json if ($responseContent.status -eq "success") { $isUnique = $responseContent.data.isUnique if ($isUnique -ne $true) { $ExistingProvider = Get-TMProvider -Name $Provider.name -TMSession $TMSession if ($PassThru) { return $ExistingProvider } else { return } } } } } catch { Write-Host "Unable to determine if Provider is unique." return $_ } # Step 2, Create the provider $uri = "https://" $uri += $instance $uri += '/tdstm/ws/dataingestion/provider/' Set-TMHeaderContentType -ContentType JSON -TMSession $TMSession $ProviderJson = $Provider | ConvertTo-Json -Depth 100 try { $response = Invoke-WebRequest -Method Post -Uri $uri -WebSession $TMSessionConfig.TMWebSession -Body $ProviderJson @TMCertSettings if ($response.StatusCode -eq 200) { $responseContent = $response.Content | ConvertFrom-Json if ($responseContent.status -eq "success") { if ($PassThru) { return $responseContent.data.provider } else { return } } } elseif ($response.StatusCode -eq 204) { return } } catch { Write-Host "Unable to create Provider." return $_ } } Function Get-TMProvider { param( [Parameter(Mandatory = $false)][String]$TMSession = "Default", [Parameter(Mandatory = $false)][String]$Name, [Parameter(Mandatory = $false)][String]$Server = $global:TMSessions[$TMSession].TMServer, [Parameter(Mandatory = $false)]$AllowInsecureSSL = $global:TMSessions[$TMSession].AllowInsecureSSL, [Parameter(Mandatory = $false)][Switch]$ResetIDs ) ## 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/dataingestion/provider/list' 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 collect Providers." } ## Sort the Providers $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 } } |