Skip to content

Instantly share code, notes, and snippets.

@jhorsman
Created July 5, 2018 11:25
Show Gist options
  • Select an option

  • Save jhorsman/22f3d78f487b5bdc2aeddbf628cad0fb to your computer and use it in GitHub Desktop.

Select an option

Save jhorsman/22f3d78f487b5bdc2aeddbf628cad0fb to your computer and use it in GitHub Desktop.
get-publications.ps1
#Requires -Version 5.0
[CmdletBinding()]
Param(
[parameter(Mandatory=$false, HelpMessage="Endpoint to the discovery service. By default it is on port 8082 and it always ends with /discovery.svc")]
[string]$DiscoveryEndpointUrl = 'http://localhost:8082/discovery.svc',
[parameter(Mandatory=$false, HelpMessage="Use micro service URLs relative to the DiscoveryEndpointUrl URL.")]
[switch]$Local,
[parameter(Mandatory=$false)]
[string]$ClientId = 'cduser',
[parameter(Mandatory=$false)]
[string]$ClientSecret = 'CDUserP@ssw0rd',
[parameter(Mandatory=$false, HelpMessage="The serviceTimeoutSec is quite high to give the microservices time to start up in case we are doing the first request to the service.")]
[int]$ServiceTimeoutSec = 10
)
$ErrorActionPreference = "Stop"
function validateDiscoveryUrl([string] $discoveryUrl) {
#todo try to fix the URL if we do not get a full url
#all of the following should lead to http://server:8082/discovery.svc
#server:8082/discovery.svc
#http://server:8082
#http://server:8082/
#http://server
if(-not $discoveryUrl.StartsWith("http")) {
if(-not $discoveryUrl.Contains(".svc")) {
$urlSuggestion = ("http://{0}:8082/discovery.svc" -f $discoveryUrl)
Write-Error ("$discoveryUrl is not a valid URL. Did you mean $urlSuggestion ?")
}
Write-Error ("$discoveryUrl is not a valid URL")
}
}
validateDiscoveryUrl $DiscoveryEndpointUrl
$authorizationHeader = @{}
# This function contains a long ugly piece of code which was copied from another scipt
# todo make this nice and clean
function getContentServiceUrls($DiscoveryEndpointUrl) {
$serviceUrls = @{}
$testtest = @{}
$BaseUrl = ""
if($Local) {
$BaseUrl = ([System.Uri]$DiscoveryEndpointUrl).Scheme + '://' + ([System.Uri]$DiscoveryEndpointUrl).Host
$DiscoveryEndpointUrl = $BaseUrl + ":" + ([System.Uri]$DiscoveryEndpointUrl).Port + ([System.Uri]$DiscoveryEndpointUrl).LocalPath
}
$tokenServiceEndpoint = ([System.Uri]$DiscoveryEndpointUrl).Scheme + '://' + ([System.Uri]$DiscoveryEndpointUrl).Authority + '/token.svc'
$authorizationRequired = $false # we are first trying without authorization
$authorizationHeader = @{}
try {
$discoveryResponse = Invoke-RestMethod -UseBasicParsing -Uri $DiscoveryEndpointUrl -TimeoutSec $ServiceTimeoutSec
} catch {
if($_.Exception.Status -eq "ConnectFailure") {
Write-Error "Unable to connect to $DiscoveryEndpointUrl"
} elseif($_.Exception.Response.StatusCode.Value__ -eq 401) {
$authorizationRequired = $true
try {
$tokenParams = @{
client_id = $ClientId
client_secret = $ClientSecret
grant_type = 'client_credentials'
resources = '/'
}
$tokenResponse = Invoke-RestMethod -UseBasicParsing -Uri $tokenServiceEndpoint -Method Post -Body $tokenParams -TimeoutSec $ServiceTimeoutSec
Write-Host -ForegroundColor Green "token service OK $tokenServiceEndpoint"
} catch {
# check if we are actually talking to the token service
try {
$tokenResponse = Invoke-RestMethod -UseBasicParsing -Uri $tokenServiceEndpoint -TimeoutSec $ServiceTimeoutSec
} catch {
if ($_.Exception.Response.StatusCode.Value__ -eq 403 ) {
#checked
Write-Error "Expected token service at $tokenServiceEndpoint. Are you sure that $DiscoveryEndpointUrl is the SDL Web discovery service URL?"
}
# this is an unexpected error situation
throw $_
}
if($tokenResponse.Content.Contains("SDL Web OAuth 2.0 Token Servlet.")) {
#checked
Write-Error "Authorization error. Check ClientId and ClientSecret"
}
Write-Error "Expected token service at $tokenServiceEndpoint. Are you sure that $DiscoveryEndpointUrl is the SDL Web discovery service URL?"
}
$authorizationHeader = @{Authorization = ($tokenResponse.token_type + ' ' + $tokenResponse.access_token)}
} else {
# this is an unexpected error situation
throw $_
}
try {
$discoveryResponse = Invoke-RestMethod -UseBasicParsing -Uri $DiscoveryEndpointUrl -Headers $authorizationHeader -TimeoutSec $ServiceTimeoutSec
} catch {
if($_.Exception.Response.StatusCode.Value__ -eq 404) {
Write-Error "Expected discovery service at $DiscoveryEndpointUrl. Are you sure that this is the SDL Web discovery service URL?"
}
# this is an unexpected error situation
throw $_
}
}
Write-Host -ForegroundColor Green "discovery service OK $DiscoveryEndpointUrl"
$environmentUrl = $DiscoveryEndpointUrl + '/Environment'
$environment = Invoke-RestMethod -UseBasicParsing -Uri $environmentUrl -Headers $authorizationHeader -TimeoutSec $ServiceTimeoutSec
$capabilities= $environment.entry.link.Where({$_.type -eq "application/atom+xml;type=entry"})
$serviceErrorCounter = 0
$capability = $capabilities.Where{$_.title -eq "ContentServiceCapability"}[0]
$capabilityTitle = $capability.title
$capabilityUrl = $DiscoveryEndpointUrl + '/' + $capability.href
if($BaseUrl) {
$capabilityUrl = $BaseUrl + ":" + ([System.Uri]$capabilityUrl).Port + ([System.Uri]$capabilityUrl).LocalPath
}
$capabilityData = $null
try {
$capabilityData = Invoke-RestMethod -UseBasicParsing -Uri $capabilityUrl -Headers $authorizationHeader -TimeoutSec $ServiceTimeoutSec
$capabilityEndpoint = $capabilityData.entry.content.properties.URI
if($capabilityEndpoint -and $BaseUrl) {
$capabilityEndpoint = $BaseUrl + ":" + ([System.Uri]$capabilityEndpoint).Port + ([System.Uri]$capabilityEndpoint).LocalPath
}
} catch {
if($_.Exception.Response.StatusCode.Value__ -eq 404) {
} else {
# unexpected error
throw $_
}
}
if($capabilityData) {
if(-not $capabilityEndpoint) {
} else {
$serviceUrls["DiscoveryServiceCapability"] = $DiscoveryEndpointUrl
try {
$serviceUrls[$capabilityTitle] = $capabilityEndpoint
# Invoke-RestMethod -UseBasicParsing -Uri $capabilityEndpoint -Headers $authorizationHeader -TimeoutSec $ServiceTimeoutSec | Out-Null
Write-Host -ForegroundColor Green "$capabilityTitle OK $capabilityEndpoint"
if($capabilityTitle.Equals("ContentServiceCapability")) {
# test content serivce endpoints
$contentServiceBaseUrl = ([System.Uri]$capabilityEndpoint).Scheme + '://' + ([System.Uri]$capabilityEndpoint).Authority
$v2ContentUrl = $contentServiceBaseUrl + "/client/v2/content.svc"
$serviceUrls[($capabilityTitle + "-v2")] = $v2ContentUrl
# todo put a try catch block around this request
$capabilityEndpoint = $v2ContentUrl # set $capabilityEndpoint for error reporting in the catch block
Invoke-RestMethod -UseBasicParsing -Uri $v2ContentUrl -Headers $authorizationHeader -TimeoutSec $ServiceTimeoutSec | Out-Null
Write-Host -ForegroundColor Green "$capabilityTitle v2 OK $v2ContentUrl"
$v4ContentUrl = $contentServiceBaseUrl + "/client/v4/content.svc"
$serviceUrls[($capabilityTitle + "-v4")] = $v4ContentUrl
# todo put a try catch block around this request
$capabilityEndpoint = $v4ContentUrl # set $capabilityEndpoint for error reporting in the catch block
Invoke-RestMethod -UseBasicParsing -Uri $v4ContentUrl -Headers $authorizationHeader -TimeoutSec $ServiceTimeoutSec | Out-Null
Write-Host -ForegroundColor Green "$capabilityTitle v4 OK $v4ContentUrl"
}
} catch {
if($_.Exception.Message.Contains("The operation has timed out.")) {
$serviceErrorCounter++
Write-Host -ForegroundColor Red "$capabilityTitle FAIL (timeout) $capabilityEndpoint"
} elseif(($capabilityTitle.Equals("DeployerCapability") -or $capabilityTitle.Equals("SmartTargetManagementCapability")) -and $_.Exception.Response.StatusCode.Value__ -eq 403) {
# this is an expected situation, a GET request to the deployer endpoint will return a 403 Forbidden
Write-Host -ForegroundColor Green "$capabilityTitle OK $capabilityEndpoint"
} elseif ($_.Exception.Response.StatusCode.value__ -eq 403) {
$serviceErrorCounter++
Write-Host -ForegroundColor Red "$capabilityTitle FAIL (403 Forbidden) $capabilityEndpoint"
} elseif ($_.Exception.Response.StatusCode.value__ -eq 404) {
$serviceErrorCounter++
Write-Host -ForegroundColor Red "$capabilityTitle FAIL (404 Not Found) $capabilityEndpoint"
} elseif ($_.Exception.Response.StatusCode.value__ -eq 503) {
$serviceErrorCounter++
Write-Host -ForegroundColor Red "$capabilityTitle FAIL (503 Server Unavailable) $capabilityEndpoint"
} else {
# unexpexted error
$serviceErrorCounter++
Write-Host -ForegroundColor Red ("$capabilityTitle FAIL (unexpected error '" + $_.Exception.Message + "') $capabilityEndpoint")
}
}
}
}
$script:authorizationHeader = $authorizationHeader
return $serviceUrls
}
$services = getContentServiceUrls $DiscoveryEndpointUrl
$requestUrl = $services["ContentServiceCapability-v2"] + "/Publications"
$publications = Invoke-RestMethod -UseBasicParsing -Uri $requestUrl -Headers $authorizationHeader -TimeoutSec $ServiceTimeoutSec
foreach ($pub in $publications) {
New-Object PSObject -Property @{
"id" = $pub.content.properties.id.'#text'
"title" = $pub.title
"updated" = $pub.updated
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment