Skip to content

Commit f6e4ce2

Browse files
Implemented the validation with failure banner.
1 parent 11b7f39 commit f6e4ce2

File tree

1 file changed

+110
-6
lines changed

1 file changed

+110
-6
lines changed

Deployment/resourcedeployment.ps1

Lines changed: 110 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,23 @@ function successBanner(){
4141
Write-Host " |_| |___/ "
4242
}
4343

44+
function failureBanner(){
45+
Write-Host " _____ _ _ "
46+
Write-Host "| __ \ | | | | "
47+
Write-Host "| | | | ___ _ __ | | ___ _ _ _ __ ___ ___ _ __ | |_ "
48+
Write-Host "| | | |/ _ \ '_ \| |/ _ \| | | | '_ ` _ \ / _ \ '_ \| __| "
49+
Write-Host "| |__| | __/ |_) | | (_) | |_| | | | | | | __/ | | | |_ "
50+
Write-Host "|_____/ \___| .__/|_|\___/ \__, |_| |_| |_|\___|_| |_|\__| "
51+
Write-Host " | | __/ | "
52+
Write-Host " ______ _|_| _ |___/ "
53+
Write-Host "| ____| (_) | | | "
54+
Write-Host "| |__ __ _ _| | ___ __| | "
55+
Write-Host "| __/ _` | | |/ _ \/ _` | "
56+
Write-Host "| | | (_| | | | __/ (_| | "
57+
Write-Host "|_| \__,_|_|_|\___|\__,_| "
58+
}
59+
60+
4461
# Function to prompt for parameters with kind messages
4562
function PromptForParameters {
4663
param(
@@ -395,10 +412,61 @@ try {
395412
###############################################################
396413
# Get the storage account key
397414
$storageAccountKey = az storage account keys list --account-name $deploymentResult.StorageAccountName --resource-group $deploymentResult.ResourceGroupName --query "[0].value" -o tsv
415+
# Validate if the storage account key is empty or null
416+
if ([string]::IsNullOrEmpty($storageAccountKey)) {
417+
Write-Host "Storage account key is empty. Exiting and Stopping execution..." -ForegroundColor Red
418+
failureBanner
419+
exit 1 # Exit the script due to Storage account key is empty.
420+
} else {
421+
Write-Host "Storage account key retrieved successfully." -ForegroundColor Green
422+
}
398423
## Construct the connection string manually
399424
$storageAccountConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($deploymentResult.StorageAccountName);AccountKey=$storageAccountKey;EndpointSuffix=core.windows.net"
425+
# Validate if the Storage Account Connection String is empty or null
426+
if ([string]::IsNullOrEmpty($storageAccountConnectionString)) {
427+
Write-Host "Storage Account Connection String is empty. Exiting and Stopping execution..." -ForegroundColor Red
428+
failureBanner
429+
exit 1 # Exit the script due to Storage Account Connection String is empty.
430+
} else {
431+
Write-Host "Storage Account Connection String retrieved successfully." -ForegroundColor Green
432+
}
400433
## Assign the connection string to the deployment result object
401-
$deploymentResult.StorageAccountConnectionString = $storageAccountConnectionString
434+
$deploymentResult.StorageAccountConnectionString = $storageAccountConnectionString
435+
436+
# Check if ResourceGroupName is valid
437+
if ([string]::IsNullOrEmpty($deploymentResult.ResourceGroupName)) {
438+
Write-Host "Error: Resource group name is null or empty." -ForegroundColor Red
439+
failureBanner
440+
exit 1
441+
}
442+
443+
# Check if AzCosmosDBName is valid
444+
if ([string]::IsNullOrEmpty($deploymentResult.AzCosmosDBName)) {
445+
Write-Host "Error: Cosmos DB name is null or empty." -ForegroundColor Red
446+
failureBanner
447+
exit 1
448+
}
449+
450+
# Check if AzCognitiveServiceName is valid
451+
if ([string]::IsNullOrEmpty($deploymentResult.AzCognitiveServiceName)) {
452+
Write-Host "Error: Cognitive Service name is null or empty." -ForegroundColor Red
453+
failureBanner
454+
exit 1
455+
}
456+
457+
# Check if AzSearchServiceName is valid
458+
if ([string]::IsNullOrEmpty($deploymentResult.AzSearchServiceName)) {
459+
Write-Host "Error: Azure Search Service name is null or empty." -ForegroundColor Red
460+
failureBanner
461+
exit 1
462+
}
463+
464+
# Check if AzOpenAiServiceName is valid
465+
if ([string]::IsNullOrEmpty($deploymentResult.AzOpenAiServiceName)) {
466+
Write-Host "Error: OpenAI Service name is null or empty." -ForegroundColor Red
467+
failureBanner
468+
exit 1
469+
}
402470
# Get MongoDB connection string
403471
$deploymentResult.AzCosmosDBConnectionString = az cosmosdb keys list --name $deploymentResult.AzCosmosDBName --resource-group $deploymentResult.ResourceGroupName --type connection-strings --query "connectionStrings[0].connectionString" -o tsv
404472
# Get Azure Cognitive Service API Key
@@ -601,47 +669,83 @@ try {
601669
# 6-1. Get Az Network resource Name with the public IP address
602670
Write-Host "Assign DNS Name to the public IP address" -ForegroundColor Green
603671
$publicIpName=$(az network public-ip list --query "[?ipAddress=='$externalIP'].name" --output tsv)
604-
605672
# 6-2. Generate Unique backend API fqdn Name - esgdocanalysis-3 digit random number with padding 0
606673
$dnsName = "kmgs$($(Get-Random -Minimum 0 -Maximum 9999).ToString("D4"))"
607-
608674
# Validate if the resource group name, public IP name and dns name are provided
609675
if ([string]::IsNullOrEmpty($aksResourceGroupName)) {
610-
Write-Host "Error: Resource Group name is null or empty." -ForegroundColor Red
676+
Write-Host "Error: aks Resource Group name is null or empty." -ForegroundColor Red
677+
failureBanner
611678
exit 1 # Exit the script if the resource group name is missing
612679
}
613680
if ([string]::IsNullOrEmpty($publicIpName)) {
614681
Write-Host "Error: Public IP name is null or empty." -ForegroundColor Red
682+
failureBanner
615683
exit 1 # Exit the script if the public IP name is missing
616684
}
617685
if ([string]::IsNullOrEmpty($dnsName)) {
618686
Write-Host "Error: DNS name is null or empty." -ForegroundColor Red
687+
failureBanner
619688
exit 1 # Exit the script if the dns name is missing
620689
}
621690
# 6-3. Assign DNS Name to the public IP address
622691
az network public-ip update --resource-group $aksResourceGroupName --name $publicIpName --dns-name $dnsName
623692

624693
# 6-4. Get FQDN for the public IP address
625694
$fqdn = az network public-ip show --resource-group $aksResourceGroupName --name $publicIpName --query "dnsSettings.fqdn" --output tsv
626-
627695
# Validate if the FQDN is null or empty
628696
if ([string]::IsNullOrEmpty($fqdn)) {
629697
Write-Host "No FQDN is associated with the public IP address." -ForegroundColor Red
698+
failureBanner
630699
Exit 1
631700
} else {
632701
Write-Host "FQDN for the public IP address is: $fqdn" -ForegroundColor Green
633702
}
634703

635704
# 7. Assign the role for aks system assigned managed identity to App Configuration Data Reader role with the scope of Resourcegroup
636705
Write-Host "Assign the role for aks system assigned managed identity to App Configuration Data Reader role" -ForegroundColor Green
706+
# Ensure that the required fields are not null or empty
707+
if ([string]::IsNullOrEmpty($deploymentResult.ResourceGroupName)) {
708+
Write-Host "Error: Resource group name for AKS deployment is null or empty." -ForegroundColor Red
709+
failureBanner
710+
exit 1
711+
}
712+
if ([string]::IsNullOrEmpty($deploymentResult.AksName)) {
713+
Write-Host "Error: AKS cluster name is null or empty." -ForegroundColor Red
714+
failureBanner
715+
exit 1
716+
}
717+
637718
# Get vmss resource group name
638719
$vmssResourceGroupName = $(az aks show --resource-group $deploymentResult.ResourceGroupName --name $deploymentResult.AksName --query nodeResourceGroup --output tsv)
720+
721+
# Validate if vmss Resource Group Name is null or empty
722+
if ([string]::IsNullOrEmpty($vmssResourceGroupName)) {
723+
Write-Host "Error: Unable to retrieve the VMSS resource group name." -ForegroundColor Red
724+
failureBanner
725+
exit 1
726+
}
727+
639728
# Get vmss name
640729
$vmssName = $(az vmss list --resource-group $vmssResourceGroupName --query "[0].name" --output tsv)
730+
731+
# Validate if vmss Name is null or empty
732+
if ([string]::IsNullOrEmpty($vmssName)) {
733+
Write-Host "Error: Unable to retrieve the VMSS name." -ForegroundColor Red
734+
failureBanner
735+
exit 1
736+
}
737+
641738
# Create System Assigned Managed Identity
642739
$systemAssignedIdentity = $(az vmss identity assign --resource-group $vmssResourceGroupName --name $vmssName --query systemAssignedIdentity --output tsv)
643740

644-
741+
# Validate if System Assigned Identity is null or empty
742+
if ([string]::IsNullOrEmpty($systemAssignedIdentity)) {
743+
Write-Host "Error: Failed to assign system-assigned managed identity to the VMSS." -ForegroundColor Red
744+
failureBanner
745+
exit 1
746+
} else {
747+
Write-Host "Retrived System Assigned Identity is: $systemAssignedIdentity" -ForegroundColor Green
748+
}
645749

646750
# Assign the role for aks system assigned managed identity to App Configuration Data Reader role with the scope of Resourcegroup
647751
az role assignment create --assignee $systemAssignedIdentity --role "App Configuration Data Reader" --scope $deploymentResult.ResourceGroupId

0 commit comments

Comments
 (0)