Framework/Helpers/ConfigOverride.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
112
113
114
115
116
117
118
119
120
121
122
123
Set-StrictMode -Version Latest

class ConfigOverride
{
    hidden [string] $ConfigFileName;
    [PSObject] $ParsedFile;
    hidden [string[]] $ChangedProperties = @();
    
    ConfigOverride([string] $configFileName)
    {
        if([string]::IsNullOrWhiteSpace($configFileName))
        {
            throw [System.ArgumentException] ("The argument 'configFileName' is null or empty")
        }

        $this.ConfigFileName = $configFileName;
        $this.ParsedFile = [ConfigurationHelper]::LoadModuleJsonFile($configFileName);

        if(-not $this.ParsedFile)
        {
            throw [System.ArgumentException] ("The file '$configFileName' is empty")
        }
    }

    ConfigOverride([string] $FolderPath, [string] $fileName)
    {
        if([string]::IsNullOrWhiteSpace($fileName))
        {
            throw [System.ArgumentException] ("The argument 'configFileName' is null or empty")
        }

        $this.ConfigFileName = $fileName;
        #Load file from AzSK App folder
        $rootConfigPath = $FolderPath ;
        $extension = [System.IO.Path]::GetExtension($fileName);

        $filePath = $null
        if(Test-Path -Path $rootConfigPath)
        {
            $filePath = (Get-ChildItem $rootConfigPath -Name -Recurse -Include $fileName) | Select-Object -First 1 
        }

        if ($filePath) {
            if($extension -eq ".json")
            {
                $this.ParsedFile = (Get-Content -Raw -Path ($rootConfigPath + $filePath)) | ConvertFrom-Json
            }
            else
            {
                $this.ParsedFile = (Get-Content -Raw -Path ($rootConfigPath + $filePath)) 
            }
        }
        else {
            throw "Unable to find the specified file '$fileName'"          
        }        
    }

    [bool] UpdatePropertyValue([string] $propertyName, [PSObject] $propertyValue)
    {
        if([string]::IsNullOrWhiteSpace($propertyName))
        {
            throw [System.ArgumentException] ("The argument 'propertyName' is null or empty")
        }

        #if(-not $propertyValue)
        #{
        # throw [System.ArgumentException] ("The argument 'propertyValue' is null or empty")
        #}

        if([Helpers]::CheckMember($this.ParsedFile, $propertyName, $false))
        {
            $this.ParsedFile.$propertyName = $propertyValue;
            $this.ChangedProperties += $propertyName;
            return $true;
        }
        else
        {
            $this.ParsedFile | Add-Member -Type NoteProperty -Name $propertyName -Value $propertyValue
            $this.ChangedProperties += $propertyName;
            return $true;
        }

        return $false;
    }

    [void] WriteToFolder()
    {
        $this.WriteToFolder([Constants]::AzSKAppFolderPath + "\Temp\PolicySetup");
    }

    [void] WriteToFolder([string] $folderName)
    {
        if([string]::IsNullOrWhiteSpace($folderName))
        {
            throw [System.ArgumentException] ("The argument 'folderName' is null or empty")
        }

        if (-not (Test-Path $folderName)) 
        {
            mkdir -Path $folderName -ErrorAction Stop | Out-Null
        }

        if (-not $folderName.EndsWith("\"))
        {
            $folderName += "\";
        }

        [Helpers]::ConvertToJsonCustom(($this.ParsedFile | Select-Object -Property $this.ChangedProperties)) | Out-File -Force -FilePath ($folderName + $this.ConfigFileName) -Encoding utf8
    }

    [void] static ClearConfigInstance()
    {
        [AzSKSettings]::Instance = $null
        [AzSKConfig]::Instance = $null
        [ConfigurationHelper]::ServerConfigMetadata = $null
        [ConfigurationHelper]::OfflineMode = $false
        [ConfigurationHelper]::ConfigVersion = $null
        [ConfigurationHelper]::IsIssueLogged = $false
        [ConfigurationHelper]::LocalPolicyEnabled = $false
        [Helpers]::currentRMContext = $null
    }
}