@@ -57,7 +57,19 @@ function failureBanner(){
5757 Write-Host " |_| \__,_|_|_|\___|\__,_| "
5858}
5959
60-
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+ }
6173# Function to prompt for parameters with kind messages
6274function PromptForParameters {
6375 param (
@@ -412,61 +424,33 @@ try {
412424 # ##############################################################
413425 # Get the storage account key
414426 $storageAccountKey = az storage account keys list -- account- name $deploymentResult.StorageAccountName -- resource- group $deploymentResult.ResourceGroupName -- query " [0].value" - o tsv
427+
415428 # 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- }
429+ ValidateVariableIsNullOrEmpty - variableValue $storageAccountKey - variableName " Storage account key"
430+
423431 # # Construct the connection string manually
424432 $storageAccountConnectionString = " DefaultEndpointsProtocol=https;AccountName=$ ( $deploymentResult.StorageAccountName ) ;AccountKey=$storageAccountKey ;EndpointSuffix=core.windows.net"
425433 # 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- }
434+ ValidateVariableIsNullOrEmpty - variableValue $storageAccountConnectionString - variableName " Storage Account Connection String"
435+
433436 # # Assign the connection string to the deployment result object
434437 $deploymentResult.StorageAccountConnectionString = $storageAccountConnectionString
435438
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-
439+ # Check if ResourceGroupName is valid
440+ ValidateVariableIsNullOrEmpty - variableValue $deploymentResult.ResourceGroupName - variableName " Resource group name"
441+
443442 # 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-
443+ ValidateVariableIsNullOrEmpty - variableValue $deploymentResult.AzCosmosDBName - variableName " Az Cosmos DB name"
444+
450445 # 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-
446+ ValidateVariableIsNullOrEmpty - variableValue $deploymentResult.AzCognitiveServiceName - variableName " Az Cognitive Service name"
447+
457448 # 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-
449+ ValidateVariableIsNullOrEmpty - variableValue $deploymentResult.AzSearchServiceName - variableName " Az Search Service name"
450+
464451 # 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- }
452+ ValidateVariableIsNullOrEmpty - variableValue $deploymentResult.AzOpenAiServiceName - variableName " Az OpenAI Service name"
453+
470454 # Get MongoDB connection string
471455 $deploymentResult.AzCosmosDBConnectionString = az cosmosdb keys list -- name $deploymentResult.AzCosmosDBName -- resource- group $deploymentResult.ResourceGroupName -- type connection- strings -- query " connectionStrings[0].connectionString" - o tsv
472456 # Get Azure Cognitive Service API Key
@@ -671,81 +655,46 @@ try {
671655 $publicIpName = $ (az network public- ip list -- query " [?ipAddress=='$externalIP '].name" -- output tsv)
672656 # 6-2. Generate Unique backend API fqdn Name - esgdocanalysis-3 digit random number with padding 0
673657 $dnsName = " kmgs$ ( $ (Get-Random - Minimum 0 - Maximum 9999 ).ToString(" D4" )) "
674- # Validate if the resource group name, public IP name and dns name are provided
675- if ([string ]::IsNullOrEmpty($aksResourceGroupName )) {
676- Write-Host " Error: aks Resource Group name is null or empty." - ForegroundColor Red
677- failureBanner
678- exit 1 # Exit the script if the resource group name is missing
679- }
680- if ([string ]::IsNullOrEmpty($publicIpName )) {
681- Write-Host " Error: Public IP name is null or empty." - ForegroundColor Red
682- failureBanner
683- exit 1 # Exit the script if the public IP name is missing
684- }
685- if ([string ]::IsNullOrEmpty($dnsName )) {
686- Write-Host " Error: DNS name is null or empty." - ForegroundColor Red
687- failureBanner
688- exit 1 # Exit the script if the dns name is missing
689- }
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"
663+
664+ ValidateVariableIsNullOrEmpty - variableValue $dnsName - variableName " DNS Name"
665+
690666 # 6-3. Assign DNS Name to the public IP address
691667 az network public- ip update -- resource- group $aksResourceGroupName -- name $publicIpName -- dns- name $dnsName
692668
693669 # 6-4. Get FQDN for the public IP address
694670 $fqdn = az network public- ip show -- resource- group $aksResourceGroupName -- name $publicIpName -- query " dnsSettings.fqdn" -- output tsv
695671 # Validate if the FQDN is null or empty
696- if ([string ]::IsNullOrEmpty($fqdn )) {
697- Write-Host " No FQDN is associated with the public IP address." - ForegroundColor Red
698- failureBanner
699- Exit 1
700- } else {
701- Write-Host " FQDN for the public IP address is: $fqdn " - ForegroundColor Green
702- }
672+ ValidateVariableIsNullOrEmpty - variableValue $fqdn - variableName " FQDN"
703673
704674 # 7. Assign the role for aks system assigned managed identity to App Configuration Data Reader role with the scope of Resourcegroup
705675 Write-Host " Assign the role for aks system assigned managed identity to App Configuration Data Reader role" - ForegroundColor Green
706676 # 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- }
677+ ValidateVariableIsNullOrEmpty - variableValue $deploymentResult.ResourceGroupName - variableName " Resource group name for AKS deployment"
717678
679+ ValidateVariableIsNullOrEmpty - variableValue $deploymentResult.AksName - variableName " AKS cluster name"
680+
718681 # Get vmss resource group name
719682 $vmssResourceGroupName = $ (az aks show -- resource- group $deploymentResult.ResourceGroupName -- name $deploymentResult.AksName -- query nodeResourceGroup -- output tsv)
720683
721684 # 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-
685+ ValidateVariableIsNullOrEmpty - variableValue $vmssResourceGroupName - variableName " VMSS resource group"
686+
728687 # Get vmss name
729688 $vmssName = $ (az vmss list -- resource- group $vmssResourceGroupName -- query " [0].name" -- output tsv)
730689
731690 # 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-
691+ ValidateVariableIsNullOrEmpty - variableValue $vmssName - variableName " VMSS name"
692+
738693 # Create System Assigned Managed Identity
739694 $systemAssignedIdentity = $ (az vmss identity assign -- resource- group $vmssResourceGroupName -- name $vmssName -- query systemAssignedIdentity -- output tsv)
740695
741696 # 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- }
697+ ValidateVariableIsNullOrEmpty - variableValue $systemAssignedIdentity - variableName " System-assigned managed identity"
749698
750699 # Assign the role for aks system assigned managed identity to App Configuration Data Reader role with the scope of Resourcegroup
751700 az role assignment create -- assignee $systemAssignedIdentity -- role " App Configuration Data Reader" -- scope $deploymentResult.ResourceGroupId
0 commit comments