Skip to content

Instantly share code, notes, and snippets.

@Wesley-Lomax
Created July 31, 2017 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Wesley-Lomax/c42bebe3c1c8f68896b7cad3ca981d31 to your computer and use it in GitHub Desktop.
Save Wesley-Lomax/c42bebe3c1c8f68896b7cad3ca981d31 to your computer and use it in GitHub Desktop.
Octopus Depoy - Check for HTTP Response Code
{
"Id": "ActionTemplates-3",
"Name": "HTTP - Test URL",
"Description": "Makes a GET request to a HTTP(S) end point and verifies that a particular status code is returned within a specified period of time",
"ActionType": "Octopus.Script",
"Version": 1,
"Properties": {
"Octopus.Action.Script.ScriptBody": "$uri = $OctopusParameters['Uri']\n$expectedCode = [int] $OctopusParameters['ExpectedCode']\n$timeoutSeconds = [int] $OctopusParameters['TimeoutSeconds']\n$Username = $OctopusParameters['AuthUsername']\n$Password = $OctopusParameters['AuthPassword']\n$UseWindowsAuth = $OctopusParameters['UseWindowsAuth']\n\nWrite-Host \"Starting verification request to $uri\"\nWrite-Host \"Expecting response code $expectedCode.\"\n\n$timer = [System.Diagnostics.Stopwatch]::StartNew()\n$success = $false\ndo\n{\n try\n {\n if ($Username -and $Password -and $UseWindowsAuth)\n\t\t\t{\n\t\t\t Write-Host \"Making request to $uri using windows authentication for user $Username\"\n\t\t\t $request = [system.Net.WebRequest]::Create($uri)\n\t\t\t $Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)\n $request.Credentials = $Credential \n \n try\n {\n $response = $request.GetResponse()\n }\n catch [System.Net.WebException]\n {\n Write-Host \"Request failed :-( System.Net.WebException\"\n Write-Host $_.Exception\n $response = $_.Exception.Response\n }\n \n\t\t\t}\n\t\telseif ($Username -and $Password)\n\t\t\t{\n\t\t\t Write-Host \"Making request to $uri using basic authentication for user $Username\"\n\t\t\t\t$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)\n\t\t\t\t$response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -Credential $Credential\n\t\t\t}\n\t\telse\n\t\t\t{\n\t\t\t Write-Host \"Making request to $uri using anonymous authentication\"\n\t\t\t\t$response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing\n\t\t\t}\n \n $code = $response.StatusCode\n Write-Host \"Recieved response code: $code\"\n \n if($response.StatusCode -eq $expectedCode)\n {\n $success = $true\n }\n }\n catch\n {\n # Anything other than a 200 will throw an exception so\n # we check the exception message which may contain the \n # actual status code to verify\n \n Write-Host \"Request failed :-(\"\n Write-Host $_.Exception\n\n if($_.Exception -like \"*($expectedCode)*\")\n {\n $success = $true\n }\n }\n\n if(!$success)\n {\n Write-Host \"Trying again in 5 seconds...\"\n Start-Sleep -s 5\n }\n}\nwhile(!$success -and $timer.Elapsed -le (New-TimeSpan -Seconds $timeoutSeconds))\n\n$timer.Stop()\n\n# Verify result\n\nif(!$success)\n{\n throw \"Verification failed - giving up.\"\n}\n\nWrite-Host \"Sucesss! Found status code $expectedCode\"\n",
"Octopus.Action.Script.Syntax": "PowerShell",
"Octopus.Action.Script.ScriptSource": "Inline"
},
"Parameters": [
{
"Name": "Uri",
"Label": "URI",
"HelpText": "The full Uri of the endpoint",
"DefaultValue": null,
"DisplaySettings": {}
},
{
"Name": "ExpectedCode",
"Label": "Expected code",
"HelpText": "The expected HTTP status code",
"DefaultValue": "200",
"DisplaySettings": {}
},
{
"Name": "TimeoutSeconds",
"Label": "Timeout (Seconds)",
"HelpText": "The number of seconds before the step fails and times out",
"DefaultValue": "30",
"DisplaySettings": {}
},
{
"Name": "AuthUsername",
"Label": "Username",
"HelpText": "Username for authentication. Leave blank to use Anonymous.",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "SingleLineText"
}
},
{
"Name": "AuthPassword",
"Label": "Password",
"HelpText": "Password for authentication. Leave blank for Anonymous.",
"DefaultValue": null,
"DisplaySettings": {
"Octopus.ControlType": "Sensitive"
}
},
{
"Name": "UseWindowsAuth",
"Label": "Use Windows Authentication?",
"HelpText": "Should the request be made passing windows authentication (kerberos) credentials",
"DefaultValue": "False",
"DisplaySettings": {
"Octopus.ControlType": "Checkbox"
}
}
],
"$Meta": {
"ExportedAt": "2017-07-31T13:06:18.661Z",
"OctopusVersion": "3.3.4",
"Type": "ActionTemplate"
}
}
$uri = $OctopusParameters['Uri']
$expectedCode = [int] $OctopusParameters['ExpectedCode']
$timeoutSeconds = [int] $OctopusParameters['TimeoutSeconds']
$Username = $OctopusParameters['AuthUsername']
$Password = $OctopusParameters['AuthPassword']
$UseWindowsAuth = $OctopusParameters['UseWindowsAuth']
Write-Host "Starting verification request to $uri"
Write-Host "Expecting response code $expectedCode."
$timer = [System.Diagnostics.Stopwatch]::StartNew()
$success = $false
do
{
try
{
if ($Username -and $Password -and $UseWindowsAuth)
{
Write-Host "Making request to $uri using windows authentication for user $Username"
$request = [system.Net.WebRequest]::Create($uri)
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)
$request.Credentials = $Credential
try
{
$response = $request.GetResponse()
}
catch [System.Net.WebException]
{
Write-Host "Request failed :-( System.Net.WebException"
Write-Host $_.Exception
$response = $_.Exception.Response
}
}
elseif ($Username -and $Password)
{
Write-Host "Making request to $uri using basic authentication for user $Username"
$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $Username, $(ConvertTo-SecureString -String $Password -AsPlainText -Force)
$response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing -Credential $Credential
}
else
{
Write-Host "Making request to $uri using anonymous authentication"
$response = Invoke-WebRequest -Uri $uri -Method Get -UseBasicParsing
}
$code = $response.StatusCode
Write-Host "Recieved response code: $code"
if($response.StatusCode -eq $expectedCode)
{
$success = $true
}
}
catch
{
# Anything other than a 200 will throw an exception so
# we check the exception message which may contain the
# actual status code to verify
Write-Host "Request failed :-("
Write-Host $_.Exception
if($_.Exception -like "*($expectedCode)*")
{
$success = $true
}
}
if(!$success)
{
Write-Host "Trying again in 5 seconds..."
Start-Sleep -s 5
}
}
while(!$success -and $timer.Elapsed -le (New-TimeSpan -Seconds $timeoutSeconds))
$timer.Stop()
# Verify result
if(!$success)
{
throw "Verification failed - giving up."
}
Write-Host "Sucesss! Found status code $expectedCode"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment