Framework/Helpers/UserSubscriptionDataHelper.ps1

Set-StrictMode -Version Latest
class UserSubscriptionDataHelper: AzSKRoot
{
    hidden static [string] $ResourceGroupName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName
    hidden static [string] $AutomationAccountName = [Constants]::AutomationAccountName
    hidden static [string] $StorageResourceType = "Microsoft.Storage/storageAccounts";

    <#hidden [string] $AttestationContainerName
    hidden [string] $ScanProgressSnapshotsContainerName
    hidden [string] $CAMultiSubScanConfigContainerName
    hidden [string] $CAScanOutputLogsContainerName#>


    UserSubscriptionDataHelper([string] $subscriptionId):
        Base($subscriptionId)
    {
    }
    #region: Get operations
    static [PSObject] GetUserSubscriptionRGName()
    {
        return [UserSubscriptionDataHelper]::ResourceGroupName
    }
    static [PSObject] GetUserSubscriptionRG()
    {
        $ResourceGroup = Get-AzureRmResourceGroup -Name $([UserSubscriptionDataHelper]::ResourceGroupName) -ErrorAction Stop
        return $ResourceGroup
    }
    static [PSObject] GetUserSubscriptionStorage()
    {
        $StorageAccountPreName = [Constants]::StorageAccountPreName
        $storageAccount = Find-AzureRmResource -ResourceGroupNameEquals $([UserSubscriptionDataHelper]::ResourceGroupName) `
        -ResourceNameContains $StorageAccountPreName `
        -ResourceType $([UserSubscriptionDataHelper]::StorageResourceType) `
        -ErrorAction Stop
        $storageAccount = $storageAccount | Where-Object{$_.ResourceName -match '^azsk\d{14}$'}

        if(($storageAccount|Measure-Object).Count -gt 1)
        {
            throw [SuppressedException]::new("Multiple storage accounts found in resource group: [$([UserSubscriptionDataHelper]::ResourceGroupName)]. This is not expected. Please contact support team.");
        }
        return $storageAccount
    }
    [PSObject] GetUserSubscriptionStorageContainer([string] $StorageContainerType)
    {
        return "<containerobject>"
    }
    [string] GetUserSubscriptionStorageContainerName([string] $StorageContainerType)
    {
        return "<containername>"
    }    
    [PSObject] GetUserSubscriptionStorageContainerData([string] $StorageContainerType)
    {
        return "<containerdata>"
    }    
    static [PSObject] GetStorageContainer($ContainerName)
    {
        $storage = [UserSubscriptionDataHelper]::GetUserSubscriptionStorage()
        $container = $null
        if($storage)
        {
            $keys = Get-AzureRmStorageAccountKey -ResourceGroupName $([UserSubscriptionDataHelper]::ResourceGroupName) -Name $storage.Name
            $currentContext = New-AzureStorageContext -StorageAccountName $storage.Name -StorageAccountKey $keys[0].Value -Protocol Https
            $container = Get-AzureStorageContainer -Name $ContainerName -Context $currentContext -ErrorAction SilentlyContinue | Out-Null
        }
        return $container
    }
    static [string] GetCAName()
    {
        return [UserSubscriptionDataHelper]::AutomationAccountName
    }

    static [bool] IsMigrationRequired()
    {
        #TODO: Add more checks
        $isMigrationRequired = $true;
        try
        {
            $storage = [UserSubscriptionDataHelper]::GetOldStorage()
            $container = $null
            if($storage)
            {
                $temp = ($env:temp + "\AzSKTemp\");
                if(-not (Test-Path -Path $temp))
                {
                    mkdir -Path $temp -Force
                }
                $filePath = "$temp\MigrationOutputFromServer.json"
                Remove-Item -Path $filePath -Force -ErrorAction Ignore
                $keys = Get-AzureRmStorageAccountKey -ResourceGroupName $storage.ResourceGroupName -Name $storage.Name
                $currentContext = New-AzureStorageContext -StorageAccountName $storage.Name -StorageAccountKey $keys[0].Value -Protocol Https
                Get-AzureStorageBlobContent -Blob "MigrationOutput.json" -Container "migration" -Context $currentContext -Force -Destination $filepath -ErrorAction SilentlyContinue
                if((Test-Path $filePath))
                {
                    $MigrationOutput = Get-Content -Path $filePath | ConvertFrom-Json
                    if($MigrationOutput -and [Helpers]::CheckMember($MigrationOutput,"ErrorOccurred") -and $MigrationOutput.ErrorOccurred)
                    {
                            #TODO need to check the condition whether one failure should we consider as migration incomplete
                            $isMigrationRequired = $true;
                    }
                    else
                    {
                        #TODO need to check the condition whether one success should we consider as migration complete
                        $isMigrationRequired = $false;
                    }
                    
                }
                else
                {
                    #if the migration blob is not found then we need to migrate
                    $isMigrationRequired = $true;
                }
            }
            else
            {
                #if no storage found, there is no need to migrate as there are no old resources present on the subscription
                $isMigrationRequired = $false;                
            }
        }
        catch
        {
            $isMigrationRequired = $false
            throw;
            
        }
        return $isMigrationRequired
    }

    static [string] IsMigrationCompleted([string] $subscriptionId)
    {
        $MigrationCompleted = "NOTSET"
        $resources = Find-AzureRmResource -TagName $([Constants]::MigrationTagName) | Measure-Object
        $OldRGResource =  Get-AzureRmResourceGroup -Name $([OldConstants]::AzSDKRGName) -ErrorAction SilentlyContinue    
        if(($resources.Count -gt 0) -or (($OldRGResource | Measure-Object).Count -eq  0))
        {
            $MigrationCompleted = "COMP"
        }
        else
        {
            $MigrationCompleted = "INIT"
        }
        return $MigrationCompleted;
    }

    static [PSObject] GetOldStorage()
    {
        $RGName = [OldConstants]::AzSDKRGName
        $StorageAccountPreName = [OldConstants]::StorageAccountPreName
        $existingStorage = Find-AzureRmResource -ResourceGroupNameEquals $RGName `
        -ResourceNameContains $StorageAccountPreName `
        -ResourceType $([UserSubscriptionDataHelper]::StorageResourceType) `
        -ErrorAction Stop
        if(($existingStorage|Measure-Object).Count -gt 1)
        {
            throw [SuppressedException]::new("Multiple storage accounts found in resource group: [$RGName]. This is not expected. Please contact support team.");
        }
        return $existingStorage
    }

    static [PSObject] GetOldRG()
    {
        $RGName = [OldConstants]::AzSDKRGName
        $ResourceGroup = Get-AzureRmResourceGroup -Name $RGName -ErrorAction SilentlyContinue
        return $ResourceGroup
    }

    <# TBR
     
    static [PSObject] GetLatestRG()
    {
        $RGName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName
        $ResourceGroup = Get-AzureRmResourceGroup -Name $RGName -ErrorAction Stop
        return $ResourceGroup
    }
 
     
    static [PSObject] GetLatestStorage()
    {
        $RGName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName
        $StorageAccountPreName = [Constants]::StorageAccountPreName
        $existingStorage = Find-AzureRmResource -ResourceGroupNameEquals $RGName `
        -ResourceNameContains $StorageAccountPreName `
        -ResourceType $([UserSubscriptionDataHelper]::StorageResourceType) `
        -ErrorAction Stop
 
        if(($existingStorage|Measure-Object).Count -gt 1)
        {
            throw [SuppressedException]::new("Multiple storage accounts found in resource group: [$RGName]. This is not expected. Please contact support team.");
        }
        return $existingStorage
    }
     
    <#[void] CreateStorageContainerIfNotExists([string] $StorageContainerType)
    {
        switch($StorageContainerType)
        {
            "AttestationDataContainer"
            {
                $this.StorageAccountInstance.CreateStorageContainerIfNotExists($this.AttestationContainerName);
            }
            "CAMultiSubScanConfigContainer"
            {
                $this.StorageAccountInstance.CreateStorageContainerIfNotExists($this.CAMultiSubScanConfigContainerName);
            }
            "ScanProgressSnapshotsContainer"
            {
                $this.StorageAccountInstance.CreateStorageContainerIfNotExists($this.ScanProgressSnapshotsContainerName);
            }
            "CAScanOutputLogsContainer"
            {
                $this.StorageAccountInstance.CreateStorageContainerIfNotExists($this.CAScanOutputLogsContainerName);
            }
        }
    } #>


    #endregion
}