ps/Modules/Alkami.DevOps.Common/Public/Get-HostnamesByEnvironmentName.ps1

116 lines
3.3 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-HostnamesByEnvironmentName {
<#
.SYNOPSIS
Retrieves the hostnames for a specified environment.
.PARAMETER EnvironmentName
[string] The moniker associated with the environment (e.g. 'Smith', '16.1')
.PARAMETER EnvironmentType
[string] The type associated with the environment (e.g. 'prod', 'dr').
.PARAMETER IncludeOffline
[switch] Flag indicating whether or not to retrieve hostnames of offline instances.
.PARAMETER ProfileName
[string] AWS profile name to use in the query.
.PARAMETER Region
[string] AWS region to use in the query.
.EXAMPLE
Get-HostnamesByEnvironmentName -EnvironmentName '16' -EnvironmentType 'DR' -Region 'us-west-2' -IncludeOffline -Verbose
VERBOSE: [Get-HostnamesByEnvironmentName] : Using environment value 'DR'
[Get-DesignationTagNameByEnvironment] : Checking designation value for environment DR
VERBOSE: [Get-HostnamesByEnvironmentName] : Designation value resolved to 'pod'
APP32101109
APP326418
MIC3210483
MIC327179
WEB3229114
WEB323468
.EXAMPLE
Get-HostnamesByEnvironmentName -EnvironmentName 16 -Verbose
VERBOSE: [Get-HostnamesByEnvironmentName] : Environment type not specified; attempting to determine value.
[Get-Environment] : Environment determined to be prod based on the alk:env tag value
VERBOSE: [Get-HostnamesByEnvironmentName] : Using environment value 'prod'
[Get-DesignationTagNameByEnvironment] : Checking designation value for environment prod
VERBOSE: [Get-HostnamesByEnvironmentName] : Designation value resolved to 'pod'
APP16107240
APP1611088
APP1612031
APP16121149
APP16122255
MIC16106199
MIC16108100
MIC16117162
MIC1612736
MIC1665169
WEB162253
WEB1623247
WEB163753
WEB1647241
WEB165798
WEB166072
#>
[OutputType([string[]])]
[CmdletBinding()]
param(
[Parameter (Mandatory = $true)]
[ValidateNotNullOrEmpty()]
[string]$EnvironmentName,
[Parameter (Mandatory = $false)]
[string]$EnvironmentType = $null,
[Parameter(Mandatory = $false)]
[switch]$IncludeOffline,
[Parameter(Mandatory = $false)]
[string]$ProfileName = $null,
[Parameter(Mandatory = $false)]
[string]$Region = $null
)
$logLead = (Get-LogLeadName)
if ( [string]::IsNullOrEmpty( $EnvironmentType ) ) {
Write-Verbose "$logLead : Environment type not specified; attempting to determine value."
[string] $EnvironmentType = ( Get-Environment )
}
Write-Verbose "$logLead : Using environment value '$EnvironmentType'"
[string] $designation = ( Get-DesignationTagNameByEnvironment $environmentType )
Write-Verbose "$logLead : Designation value resolved to '$designation'"
$designationTag = ( "alk:{0}" -f $designation )
$searchTags = @{
$designationTag = $EnvironmentName
$Global:AlkamiTagKeyEnvironment = $EnvironmentType.ToLowerInvariant()
}
[Amazon.EC2.Model.Instance[]] $Instances = Get-InstancesByTag -tags $searchTags -IncludeOffline:$IncludeOffline -ProfileName $ProfileName -Region $Region
[string[]] $hostNames = @()
foreach ( $instance in $instances ) {
$hostname = Get-InstanceHostname $instance
if ( ! [string]::IsNullOrEmpty( $hostname ) ) {
$hostNames += $hostname
}
}
[Array]::Sort($hostNames)
return $hostNames
}