Framework/Abstracts/ComplianceBase.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using namespace Microsoft.Azure.Management.Storage.Models
Set-StrictMode -Version Latest
class ComplianceBase
{ 
    [SubscriptionContext] $SubscriptionContext;
    hidden [StorageHelper] $azskStorageInstance = $null;
    hidden [string] $ComplianceTableName = [Constants]::ComplianceReportTableName;

    ComplianceBase([SubscriptionContext] $subscriptionContext)
    {
        $this.SubscriptionContext = $subscriptionContext
        $this.GetStorageHelperInstance();
    }
    [StorageHelper] GetStorageHelperInstance()
    {
        if($null -eq $this.azskStorageInstance)
        {
            try {
                $azskStorageAccount = [UserSubscriptionDataHelper]::GetUserSubscriptionStorage();
    
                if(($azskStorageAccount | Measure-Object).Count -eq 1 -and $azskStorageAccount.Kind -ne [Kind]::StorageV2)
                {
                    [UserSubscriptionDataHelper]::UpgradeBlobToV2Storage();
                }
                $azskRGName = [UserSubscriptionDataHelper]::GetUserSubscriptionRGName();     
                if($azskStorageAccount)
                {
                    $this.azskStorageInstance = [StorageHelper]::new($this.SubscriptionContext.subscriptionId, $azskRGName,$azskStorageAccount.Location, $azskStorageAccount.Name, [Kind]::StorageV2);
                    $this.azskStorageInstance.CreateTableIfNotExists([Constants]::ComplianceReportTableName);        
                }
            }
            catch {
                #eat this exception as the storage account would be null in the case of exception
            }
        }
        return $this.azskStorageInstance
    }

    hidden [bool] HaveRequiredPermissions()
    {
        if($null -eq $this.azskStorageInstance -or ($null -ne $this.azskStorageInstance -and $this.azskStorageInstance.HaveWritePermissions -eq 0))
        {
            return $false;
        }
        else {
            return $true;
        }
    }
}