Framework/Helpers/UserSubscriptionDataHelper.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using namespace Microsoft.Azure.Management.Storage.Models
Set-StrictMode -Version Latest
class UserSubscriptionDataHelper: AzSKRoot
{
    hidden static [string] $ResourceGroupName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName
    hidden static [string] $ResourceGroupLocation = [ConfigurationManager]::GetAzSKConfigData().AzSKLocation
    hidden static [string] $AutomationAccountName = [Constants]::AutomationAccountName
    hidden static [string] $StorageResourceType = "Microsoft.Storage/storageAccounts";

    UserSubscriptionDataHelper([string] $subscriptionId):
        Base($subscriptionId)
    {
    }
    #region: Get operations
    static [PSObject] GetUserSubscriptionRGName()
    {
        return [UserSubscriptionDataHelper]::ResourceGroupName
    }
    static [PSObject] GetUserSubscriptionRGLocation()
    {
        return [UserSubscriptionDataHelper]::ResourceGroupLocation
    }

    static [PSObject] GetUserSubscriptionRG()
    {
        $ResourceGroup = Get-AzResourceGroup -Name $([UserSubscriptionDataHelper]::ResourceGroupName) -ErrorAction Stop
        return $ResourceGroup
    }
    static [PSObject] GetUserSubscriptionStorage()
    {
        $StorageAccountPreName = [Constants]::StorageAccountPreName
        $storageAccount = Get-AzResource -ResourceGroupName $([UserSubscriptionDataHelper]::ResourceGroupName) `
        -Name "*$StorageAccountPreName*" `
        -ResourceType $([UserSubscriptionDataHelper]::StorageResourceType) `
        -ErrorAction Stop
        $storageAccount = $storageAccount | Where-Object{$_.Name -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-AzStorageAccountKey -ResourceGroupName $([UserSubscriptionDataHelper]::ResourceGroupName) -Name $storage.Name
            $currentContext = New-AzStorageContext -StorageAccountName $storage.Name -StorageAccountKey $keys[0].Value -Protocol Https
            $container = Get-AzStorageContainer -Name $ContainerName -Context $currentContext -ErrorAction SilentlyContinue | Out-Null
        }
        return $container
    }
    static [string] GetCAName()
    {
        return [UserSubscriptionDataHelper]::AutomationAccountName
    }
    
    static [PSObject] UpgradeBlobToV2Storage() 
    {
        #TODO: Check contributor permisison on azskrg
        $RGName = [UserSubscriptionDataHelper]::ResourceGroupName
        $StorageName = [UserSubscriptionDataHelper]::GetUserSubscriptionStorage().Name
        try 
        {
            Set-AzStorageAccount -ResourceGroupName $RGName -Name $StorageName -UpgradeToStorageV2 -ErrorAction Stop 
        }
        catch
        {
            [EventBase]::PublishCustomMessage("Failed to upgrade storage [$StorageName].");
            [EventBase]::PublishException($_)
        }
        #Storage compliance
        $retryAccount = 0
        $storageAccount = $null
        do 
        {
            $storageAccount = Get-AzStorageAccount -ResourceGroupName $RGName -Name $StorageName -ErrorAction SilentlyContinue
            Start-Sleep -seconds 2
            $retryAccount++
        } while (!$storageAccount -and $retryAccount -ne 6)
        if($storageAccount)
        {
            $storageContext = $storageAccount.Context 
            Set-AzStorageServiceLoggingProperty -ServiceType Blob -LoggingOperations 'All' -Context $storageContext -RetentionDays '365' -PassThru
            Set-AzStorageServiceLoggingProperty -ServiceType Queue -LoggingOperations 'All' -Context $storageContext -RetentionDays '365' -PassThru
            Set-AzStorageServiceLoggingProperty -ServiceType Table -LoggingOperations 'All' -Context $storageContext -RetentionDays '365' -PassThru
            return $storageAccount
        }
        else
        {
            return $Null
        }
    }
    #endregion
}