ps/Modules/Alkami.DevOps.Installation/Public/Submit-DeploymentToNewRelic.ps1
2023-05-30 22:51:22 -07:00

65 lines
2.3 KiB
PowerShell

function Submit-DeploymentToNewRelic {
<#
.SYNOPSIS
Posts a Deployment to a New Relic application via the API
.PARAMETER ApiKey
New Relic Api Key
.PARAMETER ApplicationId
New Relic supplied Application Id
.PARAMETER AppVersion
Version of the application being updated.
.PARAMETER DeployUser
Name of person doing the deploy
.PARAMETER EnvironmentKey
Name of the Environment being deployed to
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true)]
[string] $ApiKey,
[Parameter(Mandatory = $true)]
[string] $ApplicationId,
[Parameter(Mandatory = $true)]
[string] $AppVersion,
[Parameter(Mandatory = $true)]
[string] $DeployUser,
[Parameter(Mandatory = $true)]
[string] $EnvironmentKey
)
$postParams = @{
"deployment[revision]" = $appVersion;
"deployment[description]" = "Deployment of $($appVersion) to $($environmentKey)";
"deployment[changelog]" = "Update to $($appVersion)";
"deployment[user]" = $deployUser
}
$postUri = ("https://api.newrelic.com/v2/applications/{0}/deployments.xml" -f $applicationId)
Write-Verbose ("$logLead : Posting Deployment Version {0} to URI {1}" -f $appVersion, $postUri)
try {
$postResponse = Invoke-WebRequest -UseBasicParsing -Uri $postUri -Method Post -Headers @{"X-Api-Key" = $apiKey } -Body $postParams
if ($postResponse.StatusCode -notmatch "20\d") {
Write-Host "$logLead : Posting to Newrelic returned a status code other than 20x for Uri PostUrl=$postUri StatusCode=$($postResponse.StatusCode) StatusDescription=$($postResponse.StatusDescription)"
return $false
}
} catch [System.Net.WebException] {
$errorMessage = $_.Exception.Message
$contentResponse = (New-Object System.IO.StreamReader($_.Exception.Response.GetResponseStream())).ReadToEnd()
Write-Warning "$logLead : Could not complete deployment to NewRelic. Error message: [$errorMessage]. Content of response was [$contentResponse]"
return $false
} catch {
$errorMessage = $_.Exception.Message
Write-Warning "$logLead : Could not complete deployment to NewRelic. Error message: [$errorMessage]"
return $false
}
return $true
}