Framework/Managers/ControlStateManager.ps1
Set-StrictMode -Version Latest class ControlStateManager { hidden static [PSObject] $AzSDKStorageAccount = $null; hidden static [PSObject] $AzSDKStorageContainer = $null; hidden static [PSObject] $ControlStateIndexer = $null; hidden static [PSObject] GetControlStateIndexer() { if($null -eq [ControlStateManager]::ControlStateIndexer) { [ControlStateManager]::ControlStateIndexer = -1; $azSDKConfigData = [ConfigurationManager]::GetAzSdkConfigData() #$azSDKConfigData.$AzSDKRGName #Look of is there is a AzSDK RG and AzSDK Storage account if($null -eq [ControlStateManager]::AzSDKStorageAccount) { $StorageAccount = Get-AzureRmStorageAccount -ResourceGroupName $azSDKConfigData.AzSDKRGName | Where-Object {$_.StorageAccountName -like 'azsdk*'} -ErrorAction SilentlyContinue #if no storage account found then it assumes that there is no control state feature is not used and if there are more than one storage account found it assumes the same if($null -eq $StorageAccount -or ($StorageAccount | Measure-Object).Count -gt 1) { [ControlStateManager]::AzSDKStorageAccount = -1 return [ControlStateManager]::ControlStateIndexer; } [ControlStateManager]::AzSDKStorageAccount = $StorageAccount; } else { $StorageAccount = [ControlStateManager]::AzSDKStorageAccount; } if($null -eq [ControlStateManager]::AzSDKStorageContainer) { $ContainerName = "azsdk-controls-state" $containerObject = Get-AzureStorageContainer -Context $StorageAccount.Context -Name $ContainerName -ErrorAction SilentlyContinue if($null -eq $containerObject) { [ControlStateManager]::AzSDKStorageContainer = -1; return [ControlStateManager]::ControlStateIndexer; } [ControlStateManager]::AzSDKStorageContainer = $containerObject; } else { $containerObject = [ControlStateManager]::AzSDKStorageContainer; } $IndexerBlobName ="Resource.index" $ContainerName = "azsdk-controls-state" $indexerBlob = Get-AzureStorageBlob -Container $ContainerName -Blob $IndexerBlobName -Context $StorageAccount.Context -ErrorAction SilentlyContinue if($null -eq $indexerBlob) { return [ControlStateManager]::ControlStateIndexer; } $AzSDKTemp = [Constants]::AzSdkAppFolderPath + "\Temp\ServerControlState"; if(-not (Test-Path -Path $AzSDKTemp)) { mkdir -Path $AzSDKTemp -Force } Get-AzureStorageBlobContent -CloudBlob $indexerBlob.ICloudBlob -Context $StorageAccount.Context -Destination $AzSDKTemp -Force $indexerObject = Get-ChildItem -Path "$AzSDKTemp\$IndexerBlobName" -Force | Get-Content | ConvertFrom-Json [ControlStateManager]::ControlStateIndexer = $indexerObject; } return [ControlStateManager]::ControlStateIndexer; } hidden static [PSObject] GetControlState([string] $id) { [ControlState[]] $controlStates = @(); if($null -ne [ControlStateManager]::ControlStateIndexer -and [ControlStateManager]::ControlStateIndexer -ne -1) { $indexes = @(); if(([ControlStateManager]::ControlStateIndexer | Measure-Object).Count -eq 1) { $indexes += [ControlStateManager]::ControlStateIndexer $selectedIndex = $indexes | Where-Object { $_.Id -eq $id} if(($selectedIndex | Measure-Object).Count -gt 0) { $controlStateBlobName = $selectedIndex.MappingID $azSDKConfigData = [ConfigurationManager]::GetAzSdkConfigData() #$azSDKConfigData.$AzSDKRGName #Look of is there is a AzSDK RG and AzSDK Storage account $StorageAccount = [ControlStateManager]::AzSDKStorageAccount; $containerObject = [ControlStateManager]::AzSDKStorageContainer $ContainerName = [ControlStateManager]::AzSDKStorageContainer.Name $controlStateBlob = Get-AzureStorageBlob -Container $ContainerName -Blob $controlStateBlobName -Context $StorageAccount.Context -ErrorAction SilentlyContinue if($null -eq $controlStateBlob) { return $controlStates; } $AzSDKTemp = [Constants]::AzSdkAppFolderPath + "\Temp\ServerControlState"; if(-not (Test-Path -Path $AzSDKTemp)) { mkdir -Path $AzSDKTemp -Force } Get-AzureStorageBlobContent -CloudBlob $controlStateBlob.ICloudBlob -Context $StorageAccount.Context -Destination $AzSDKTemp -Force $ControlStatesJson = Get-ChildItem -Path "$AzSDKTemp\$controlStateBlobName" -Force | Get-Content | ConvertFrom-Json if($null -ne $ControlStatesJson) { $ControlStatesJson | ForEach-Object { Set-Variable -Name controlJson -Value $_ -Scope Local [ControlState] $controlState = [ControlState]::new() $controlState.ControlId = $controlJson.ControlId if(($controlJson.State | Measure-Object).Count -gt 0) { $controlJson.State | ForEach-Object { $controlState.State += [MessageData]::new($_.Message, $_.DataObject, $_.MessageType, $_.MaintainState, $_.DataObjectType) } } $controlStates += $controlState; } } } } } return $controlStates; } hidden static [string] ComputeHash([String] $data) { $HashValue = [System.Text.StringBuilder]::new() [System.Security.Cryptography.HashAlgorithm]::Create("SHA256").ComputeHash([System.Text.Encoding]::UTF8.GetBytes($data))| ForEach-Object { [void]$HashValue.Append($_.ToString("x")) } return $HashValue.ToString() } } |