Framework/Abstracts/AzSdkRoot.ps1
Set-StrictMode -Version Latest class AzSdkRoot: EventBase { [AzureContext] $Context; hidden [AzSdkConfig] $RootConfig; AzSdkRoot([string] $subscriptionId) { [Helpers]::AbstractClass($this, [AzSdkRoot]); #Set up subscription $this.Context = [AzureContext]@{ SubscriptionId = $subscriptionId; }; $this.SetAzureContext(); $this.RootConfig = [ConfigurationManager]::GetAzSdkConfigData(); } hidden [void] SetAzureContext() { $isContextNull = $false; $currentContext = $null; try { $currentContext = Get-AzureRmContext -ErrorAction Stop } catch { $isContextNull = $true } if(($null -eq $currentContext) -or $isContextNull) { $this.PublishCustomMessage("No active Azure login session found. Initiating login flow...", [MessageType]::Warning); $currentContext = Add-AzureRmAccount try { $currentContext = Get-AzureRmContext -ErrorAction Stop } catch { throw [System.ArgumentException] ("Subscription Id [" + $this.Context.SubscriptionId + "] is invalid or you may not have permissions.") } } if($currentContext.Subscription -and $currentContext.Subscription.SubscriptionId) { if($currentContext.Subscription.SubscriptionId -ne $this.Context.SubscriptionId) { $currentContext = Set-AzureRmContext -SubscriptionId $this.Context.SubscriptionId -ErrorAction Stop # $currentContext will contain the desired subscription (or $null if id is wrong or no permission) if ($null -eq $currentContext) { throw [System.ArgumentException] ("Invalid Subscription Id: [" + $this.Context.SubscriptionId + "]") } } } else { throw [System.ArgumentException] ("Subscription Id [" + $this.Context.SubscriptionId + "] is invalid or you may not have permissions.") } if ($null -ne $currentContext) { $this.Context.SubscriptionName = $currentContext.Subscription.SubscriptionName; } } [PSObject] LoadJsonFile([string] $fileName) { return [ConfigurationManager]::LoadJsonFile($fileName); } hidden [void] PublishAzSdkRootEvent([string] $eventType, [MessageData[]] $messages) { [AzSdkRootEventArgument] $arguments = [AzSdkRootEventArgument]@{ Context = $this.Context; }; if($messages) { $arguments.Messages += $messages; } $this.PublishEvent($eventType, $arguments); } hidden [void] PublishAzSdkRootEvent([string] $eventType, [string] $message, [MessageType] $messageType) { if (-not [string]::IsNullOrEmpty($message)) { [MessageData] $data = [MessageData]@{ Message = $message; MessageType = $messageType; }; $this.PublishAzSdkRootEvent($eventType, $data); } else { [MessageData[]] $blankMessages = @(); $this.PublishAzSdkRootEvent($eventType, $blankMessages); } } hidden [void] PublishAzSdkRootEvent([string] $eventType, [PSObject] $dataObject) { if ($dataObject) { [MessageData] $data = [MessageData]@{ DataObject = $dataObject; }; $this.PublishAzSdkRootEvent($eventType, $data); } else { [MessageData[]] $blankMessages = @(); $this.PublishAzSdkRootEvent($eventType, $blankMessages); } } [void] PublishCustomMessage([MessageData[]] $messages) { if($messages) { $this.PublishAzSdkRootEvent([AzSdkRootEvent]::CustomMessage, $messages); } } [void] CommandProcessing([MessageData[]] $messages) { if($messages) { $this.PublishAzSdkRootEvent([AzSdkRootEvent]::CommandProcessing, $messages); } } [void] PublishRunIdentifier([System.Management.Automation.InvocationInfo] $invocationContext) { if($invocationContext) { $this.InvocationContext = $invocationContext; } $this.RunIdentifier = $this.GenerateRunIdentifier(); $this.PublishAzSdkRootEvent([AzSdkRootEvent]::GenerateRunIdentifier, [MessageData]::new($this.RunIdentifier, $invocationContext)); } hidden [string[]] ConvertToStringArray([string] $stringArray) { $result = @(); if(-not [string]::IsNullOrWhiteSpace($stringArray)) { $result += $stringArray.Split(',', [StringSplitOptions]::RemoveEmptyEntries) | Where-Object { -not [string]::IsNullOrWhiteSpace($_) } | ForEach-Object { $_.Trim() } | Select-Object -Unique; } return $result; } } |