ps/Modules/Alkami.PowerShell.ServiceFabric/Public/Get-AlkamiServiceFabricApplications.ps1

105 lines
4.3 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-AlkamiServiceFabricApplications {
<#
.SYNOPSIS
Returns a specific microservice package from the cluster, if it exists.
.PARAMETER Name
The name of the package to look up.
.PARAMETER Version
The optional version of the package to look for.
.PARAMETER ComputerName
The host name of any FAB server in the Service Fabric cluster.
.PARAMETER EnvironmentName
The optional environment name of the environment you are targeting.
Specifying the environment name is a performance improvement. Otherwise, it will be looked up from the remote machine.
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[string]$Name = $null,
[Parameter(Mandatory = $false)]
[string]$Version = $null,
[Parameter(Mandatory = $false)]
[Alias("server")]
[string]$ComputerName = "localhost",
[Parameter(Mandatory = $false)]
[string]$EnvironmentName = $null
)
$loglead = (Get-LogLeadName);
# Connect to the cluster.
Write-Verbose "$loglead Connecting to cluster on server $ComputerName";
Connect-AlkamiServiceFabricCluster -hostname $ComputerName | Out-Null;
# Get the environment name so we can target packages from the specific environment, if it wasn't specified.
if([string]::IsNullOrWhiteSpace($EnvironmentName)) {
$EnvironmentName = (Get-AppSetting -appSettingKey "Environment.Name" -ComputerName $ComputerName);
}
$EnvironmentName = (Format-AlkamiEnvironmentName -name $environmentName);
# Produce a list of packages deployed to the cluster.
Write-Verbose "$loglead Retrieving package data from Service Fabric.";
$packages = @();
$sfPackages = (Get-ServiceFabricApplication);
foreach($package in $sfPackages) {
# Parse the environmentName-packageName style application type name.
$packageEnvironmentName = $null;
$packageName = $null;
$typeNameSplit = $package.ApplicationTypeName.Split("-");
if($typeNameSplit.count -gt 1) {
# Microservice Package
$packageEnvironmentName = $typeNameSplit[0];
$packageName = $typeNameSplit[1];
} else {
# Reliable Service
$packageEnvironmentName = $null ;
$packageName = $package.ApplicationTypeName;
}
$packageMap = @{
Name = $packageName
Version = $package.ApplicationTypeVersion
Environment = $packageEnvironmentName
ServiceFabricApplicationName = $package.ApplicationName
ServiceFabricApplicationTypeName = $package.ApplicationTypeName
Feed = $null
}
$packages += (New-Object -TypeName PSObject -Property $packageMap);
}
# Filter down the packages to the appropriate environment. We don't want to accidentally target a wrong environment.
$podlessPackages = [array]($packages | Where-Object { $null -eq $_.Environment });
$packages = [array]($packages | Where-Object { $_.Environment -eq $EnvironmentName });
# If a package does not have an environment, it is a podless TDE service.
# Replace the names of these packages with their actual proget package ID names by reading the name from the application manifest.
if(!(Test-IsCollectionNullOrEmpty $podlessPackages)) {
foreach($package in $podlessPackages) {
$manifestData = (Get-ServiceFabricApplicationType -ApplicationTypeName $package.ServiceFabricApplicationTypeName -ApplicationTypeVersion $package.Version);
$appParams = $manifestData.DefaultParameters;
$nugetPackageName = $appParams["NugetPackageName"].Value;
$package.Name = $nugetPackageName;
}
# Add the podless packages into the normal list of packages.
$packages = $packages + $podlessPackages;
}
# Filter down the package names, if it was specified.
if(!([string]::IsNullOrWhiteSpace($Name))) {
$packages = $packages | Where-Object { $_.Name -eq $Name };
}
# Filter down for the specific version, if it was specified.
if(!([string]::IsNullOrWhiteSpace($Version))) {
$packages = $packages | Where-Object { $_.Version -eq $Version };
}
# Return the list of packages.
if(Test-IsCollectionNullOrEmpty $packages) {
return $null;
} else {
return $packages;
}
}