Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit 6ec42eb

Browse files
author
Vlastimil Holer
committed
gh-33: More error handling in reportReady(), formatting
1 parent 5138584 commit 6ec42eb

1 file changed

Lines changed: 35 additions & 14 deletions

File tree

context.ps1

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -652,39 +652,60 @@ function extendPartitions()
652652

653653
function reportReady()
654654
{
655-
$reportReady = $context["REPORT_READY"]
655+
$reportReady = $context['REPORT_READY']
656+
$oneGateEndpoint = $context['ONEGATE_ENDPOINT']
657+
$vmId = $context['VMID']
656658

657-
if ($reportReady) {
658-
Write-Output "Report Ready to onegate"
659+
if ($reportReady -and $reportReady.ToUpper() -eq 'YES') {
660+
Write-Output 'Report Ready to OneGate'
661+
662+
if (!$oneGateEndpoint) {
663+
Write-Output ' ... Failed: ONEGATE_ENDPOINT not set'
664+
return
665+
}
666+
667+
if (!$vmId) {
668+
Write-Output ' ... Failed: VMID not set'
669+
return
670+
}
659671

660672
try {
661-
$body = "READY = YES"
662-
$token= Get-Content "${contextLetter}token.txt"
663-
$target= $context.ONEGATE_ENDPOINT+"/vm"
673+
$tokenPath = $contextLetter + 'token.txt'
674+
if (Test-Path $tokenPath) {
675+
$token = Get-Content $tokenPath
676+
} else {
677+
Write-Output " ... Failed: Token file not found"
678+
return
679+
}
680+
681+
$body = 'READY = YES'
682+
$target= $oneGateEndpoint + '/vm'
683+
664684
[System.Net.HttpWebRequest] $webRequest = [System.Net.WebRequest]::Create($target)
665685
$webRequest.Timeout = 10000
666-
$webRequest.Method = "PUT"
667-
$webRequest.Headers.Add("X-ONEGATE-TOKEN", $token)
668-
$webRequest.Headers.Add("X-ONEGATE-VMID", $context.VMID)
686+
$webRequest.Method = 'PUT'
687+
$webRequest.Headers.Add('X-ONEGATE-TOKEN', $token)
688+
$webRequest.Headers.Add('X-ONEGATE-VMID', $vmId)
669689
$buffer = [System.Text.Encoding]::UTF8.GetBytes($body)
670690
$webRequest.ContentLength = $buffer.Length
691+
671692
$requestStream = $webRequest.GetRequestStream()
672693
$requestStream.Write($buffer, 0, $buffer.Length)
673694
$requestStream.Flush()
674695
$requestStream.Close()
675-
$response = $webRequest.getResponse()
676696

677-
if ($response.StatusCode -eq "OK") {
678-
Write-Output " ... Success"
697+
$response = $webRequest.getResponse()
698+
if ($response.StatusCode -eq 'OK') {
699+
Write-Output ' ... Success'
679700
} else {
680-
Write-Output " ... Failed"
701+
Write-Output ' ... Failed'
681702
Write-Output $response.StatusCode
682703
}
683704
}
684705
catch {
685706
$errorMessage = $_.Exception.Message
686707

687-
Write-Output " ... Failed"
708+
Write-Output ' ... Failed'
688709
Write-Output $errorMessage
689710
}
690711
}

0 commit comments

Comments
 (0)