function Set-NewRelicDeployment { <# .SYNOPSIS Posts a Deployment to a New Relic application via the API for either: All non-microservices that New Relic knows about in a given Environment OR All specified microservices that New Relic knows about in a given Environment. .PARAMETER EnvironmentKey Name of the environment to update. .PARAMETER AppVersion Version of the service that is being deployed. Only valid for legacy services. Providing this and a set of microservices will throw. .PARAMETER DeployUser The user performing the deployment. Optional. .PARAMETER Microservices A hash table of microservices and their versions. .EXAMPLE Set-NewRelicDeployment -EnvironmentKey "AWS CICD CI1" -AppVersion 1.0 -DeployUser "testUser" $microservices = @{"Alkami.Microservice.Sample" = "1.0"} Set-NewRelicDeployment -EnvironmentKey "AWS CICD CI1" -DeployUser "testUser" -micoserviceNames $microservices #> [CmdletBinding()] Param( [Parameter(Mandatory = $true)] [Alias("Environment")] [string]$EnvironmentKey, [Parameter(Mandatory = $false, ParameterSetName = "StandardServices")] [Alias("Version")] [string]$AppVersion, [Parameter(Mandatory = $false)] [Alias("DeploymentEngineer")] [string]$DeployUser = "Alkami", [Parameter(Mandatory = $false, ParameterSetName = "Microservices")] [Alias("SpecifiedMicroservices")] [hashtable]$Microservices ) $logLead = (Get-LogLeadName) Write-Host ("$logLead : Pulling list of Applications from NewRelic") $apiKey = (Get-NewRelicAccountDetails $EnvironmentKey).APIKey $postFailed = @() try { $applications = (Get-NewRelicObjects -apiKey $apiKey -initialUrl "https://api.newrelic.com/v2/applications.json" -ObjectKey "applications" -FilterKey "name" -FilterValue $EnvironmentKey).applications } catch { # If an error gets thrown below me on the stack, we should just abort with an error message Write-Error "$logLead : Error thrown while getting NewRelic objects, can not process deployments. Exiting function with no action performed.`r`n$_" return } if (![string]::IsNullOrWhiteSpace($EnvironmentKey) -and (Test-IsCollectionNullOrEmpty $applications)) { Write-Warning "$logLead : No results at all returned from NewRelic for [$EnvironmentKey]. Cannot continue, returning early." return } $filteredApplications = $applications.Where({$_.name -match "^$($EnvironmentKey)[^.]"}) if (Test-IsCollectionNullOrEmpty $filteredApplications) { Write-Warning "$logLead : No results returned from NewRelic that match the filter for [$EnvironmentKey]. Cannot continue, returning early." return } Write-Host "$logLead : Posting Deployment to NewRelic Applications for Environment [$EnvironmentKey]" # If provided with microservice names, loop through and send them to new-relic, if they already exist. if ($Microservices) { foreach ($microservice in $Microservices.GetEnumerator()) { $foundMicroserviceViaFilter = $false $AppVersion = $microservice.value $microserviceName = $microservice.key $matchedApplications = $filteredApplications.Where({$_.name -match $microserviceName}) foreach($matchedApplication in $matchedApplications) { $foundMicroserviceViaFilter = $true Write-Verbose "$logLead : Looking to update $($matchedApplication.Name)" if (Submit-DeploymentToNewRelic -ApiKey $apiKey -ApplicationId $matchedApplication.id -AppVersion $AppVersion -DeployUser $DeployUser -EnvironmentKey $EnvironmentKey) { Write-Verbose "$logLead : Updated $($matchedApplication.Name)" } else { Write-Verbose "$logLead : Failed to update $($matchedApplication.Name)" $postFailed += $matchedApplication.Name } } if (!$foundMicroserviceViaFilter -and $microserviceName -like "*Alkami.M*") { Write-Warning "$microserviceName was specified to be updated in New Relic, but New Relic does not know about it for [$EnvironmentKey]. There is likely no entry for [$EnvironmentKey $microserviceName]" } } } else { if([string]::IsNullOrWhiteSpace($AppVersion)) { throw "$logLead : Somehow we got to a New Relic API call with no AppVersion value. That shouldn't happen. Please investigate." } else { Write-Host "$logLead : Looking for non-service accounts to be updated in NR" $matchedApplications = $filteredApplications.Where({$_.name -notlike "*Alkami.M*" }) if (Test-IsCollectionNullOrEmpty $matchedApplications) { Write-Warning "$logLead : No applications found for [$EnvironmentKey] in NewRelic" } else { foreach($matchedApplication in $matchedApplications) { Write-Verbose "$logLead : Looking to update $($matchedApplication.Name)" if (Submit-DeploymentToNewRelic -ApiKey $apiKey -ApplicationId $matchedApplication.id -AppVersion $AppVersion -DeployUser $DeployUser -EnvironmentKey $EnvironmentKey) { Write-Verbose "$logLead : Updated $($matchedApplication.Name)" } else { Write-Verbose "$logLead : Failed to update $($matchedApplication.Name)" $postFailed += $matchedApplication.Name } } } } } if (!(Test-IsCollectionNullOrEmpty $postFailed)) { foreach($failed in $postFailed) { Write-Warning "$logLead : Failed to update deployment for [$failed]" } throw "$logLead : Not all Post Requests were successful. Review the log output for details" } }