Skip to content

Commit ea42bdd

Browse files
Merge pull request #21 from microsoft/psl-dkm-us-11110
Implemented the validation for required fields
2 parents b93b65f + bb89f62 commit ea42bdd

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

Deployment/resourcedeployment.ps1

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ 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+
# Common function to check if a variable is null or empty
61+
function ValidateVariableIsNullOrEmpty {
62+
param (
63+
[string]$variableValue,
64+
[string]$variableName
65+
)
66+
67+
if ([string]::IsNullOrEmpty($variableValue)) {
68+
Write-Host "Error: $variableName is null or empty." -ForegroundColor Red
69+
failureBanner
70+
exit 1
71+
}
72+
}
4473
# Function to prompt for parameters with kind messages
4574
function PromptForParameters {
4675
param(
@@ -395,10 +424,33 @@ try {
395424
###############################################################
396425
# Get the storage account key
397426
$storageAccountKey = az storage account keys list --account-name $deploymentResult.StorageAccountName --resource-group $deploymentResult.ResourceGroupName --query "[0].value" -o tsv
427+
428+
# Validate if the storage account key is empty or null
429+
ValidateVariableIsNullOrEmpty -variableValue $storageAccountKey -variableName "Storage account key"
430+
398431
## Construct the connection string manually
399432
$storageAccountConnectionString = "DefaultEndpointsProtocol=https;AccountName=$($deploymentResult.StorageAccountName);AccountKey=$storageAccountKey;EndpointSuffix=core.windows.net"
433+
# Validate if the Storage Account Connection String is empty or null
434+
ValidateVariableIsNullOrEmpty -variableValue $storageAccountConnectionString -variableName "Storage Account Connection String"
435+
400436
## Assign the connection string to the deployment result object
401-
$deploymentResult.StorageAccountConnectionString = $storageAccountConnectionString
437+
$deploymentResult.StorageAccountConnectionString = $storageAccountConnectionString
438+
439+
# Check if ResourceGroupName is valid
440+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.ResourceGroupName -variableName "Resource group name"
441+
442+
# Check if AzCosmosDBName is valid
443+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzCosmosDBName -variableName "Az Cosmos DB name"
444+
445+
# Check if AzCognitiveServiceName is valid
446+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzCognitiveServiceName -variableName "Az Cognitive Service name"
447+
448+
# Check if AzSearchServiceName is valid
449+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzSearchServiceName -variableName "Az Search Service name"
450+
451+
# Check if AzOpenAiServiceName is valid
452+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AzOpenAiServiceName -variableName "Az OpenAI Service name"
453+
402454
# Get MongoDB connection string
403455
$deploymentResult.AzCosmosDBConnectionString = az cosmosdb keys list --name $deploymentResult.AzCosmosDBName --resource-group $deploymentResult.ResourceGroupName --type connection-strings --query "connectionStrings[0].connectionString" -o tsv
404456
# Get Azure Cognitive Service API Key
@@ -601,26 +653,49 @@ try {
601653
# 6-1. Get Az Network resource Name with the public IP address
602654
Write-Host "Assign DNS Name to the public IP address" -ForegroundColor Green
603655
$publicIpName=$(az network public-ip list --query "[?ipAddress=='$externalIP'].name" --output tsv)
604-
605656
# 6-2. Generate Unique backend API fqdn Name - esgdocanalysis-3 digit random number with padding 0
606657
$dnsName = "kmgs$($(Get-Random -Minimum 0 -Maximum 9999).ToString("D4"))"
658+
659+
# Validate if the AKS Resource Group Name, Public IP name and DNS Name are provided
660+
ValidateVariableIsNullOrEmpty -variableValue $aksResourceGroupName -variableName "AKS Resource Group name"
661+
662+
ValidateVariableIsNullOrEmpty -variableValue $publicIpName -variableName "Public IP name"
607663

664+
ValidateVariableIsNullOrEmpty -variableValue $dnsName -variableName "DNS Name"
665+
608666
# 6-3. Assign DNS Name to the public IP address
609667
az network public-ip update --resource-group $aksResourceGroupName --name $publicIpName --dns-name $dnsName
610-
# 6-4. Get FQDN for the public IP address
611-
$fqdn = az network public-ip show --resource-group $aksResourceGroupName --name $publicIpName --query "dnsSettings.fqdn" --output tsv
612-
Write-Host "FQDN for the public IP address is: $fqdn" -ForegroundColor Green
613668

669+
# 6-4. Get FQDN for the public IP address
670+
#$fqdn = az network public-ip show --resource-group $aksResourceGroupName --name $publicIpName --query "dnsSettings.fqdn" --output tsv
671+
$fqdn = $null
672+
# Validate if the FQDN is null or empty
673+
ValidateVariableIsNullOrEmpty -variableValue $fqdn -variableName "FQDN"
674+
614675
# 7. Assign the role for aks system assigned managed identity to App Configuration Data Reader role with the scope of Resourcegroup
615676
Write-Host "Assign the role for aks system assigned managed identity to App Configuration Data Reader role" -ForegroundColor Green
677+
# Ensure that the required fields are not null or empty
678+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.ResourceGroupName -variableName "Resource group name"
679+
680+
ValidateVariableIsNullOrEmpty -variableValue $deploymentResult.AksName -variableName "AKS cluster name"
681+
616682
# Get vmss resource group name
617683
$vmssResourceGroupName = $(az aks show --resource-group $deploymentResult.ResourceGroupName --name $deploymentResult.AksName --query nodeResourceGroup --output tsv)
684+
685+
# Validate if vmss Resource Group Name is null or empty
686+
ValidateVariableIsNullOrEmpty -variableValue $vmssResourceGroupName -variableName "VMSS resource group"
687+
618688
# Get vmss name
619689
$vmssName = $(az vmss list --resource-group $vmssResourceGroupName --query "[0].name" --output tsv)
690+
691+
# Validate if vmss Name is null or empty
692+
ValidateVariableIsNullOrEmpty -variableValue $vmssName -variableName "VMSS name"
693+
620694
# Create System Assigned Managed Identity
621695
$systemAssignedIdentity = $(az vmss identity assign --resource-group $vmssResourceGroupName --name $vmssName --query systemAssignedIdentity --output tsv)
622696

623-
697+
# Validate if System Assigned Identity is null or empty
698+
ValidateVariableIsNullOrEmpty -variableValue $systemAssignedIdentity -variableName "System-assigned managed identity"
624699

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

0 commit comments

Comments
 (0)