Framework/Models/AzSdkSettings.ps1

#
# AzSdkSettings.ps1
#
Set-StrictMode -Version Latest
class AzSdkSettings {
    [string] $OMSWorkspaceId;
    [string] $OMSSharedKey;
    [string] $AltOMSWorkspaceId;
    [string] $AltOMSSharedKey;
    [string] $OMSType;
    [string] $OMSSource;

    [string] $EventHubNamespace;
    [string] $EventHubName;
    [string] $EventHubSendKeyName;
    [string] $EventHubSendKey;
    [string] $EventHubType;
    [string] $EventHubSource;

    [string] $WebhookUrl;
    [string] $WebhookAuthZHeaderName;
    [string] $WebhookAuthZHeaderValue;
    [string] $WebhookType;
    [string] $WebhookSource;
    [string] $AutoUpdateCommand;
    [AutoUpdate] $AutoUpdateSwitch = [AutoUpdate]::NotSet;

    [string] $OutputFolderPath;


    [bool] $EnableAADAuthForOnlinePolicyStore;
    [bool] $UseOnlinePolicyStore;
    [string] $OnlinePolicyStoreUrl;
    [string] $UsageTelemetryLevel;
    [string] $LocalControlTelemetryKey;
    [bool] $LocalEnableControlTelemetry;
    [bool] $PrivacyNoticeAccepted = $false;
    hidden static [AzSdkSettings] $Instance = $null;
    hidden static [string] $FileName = "AzSdkSettings.json";


    static [AzSdkSettings] GetInstance() {
        if (-not [AzSdkSettings]::Instance)
        {
            [AzSdkSettings]::LoadAzSdkSettings($false);
        }

        return [AzSdkSettings]::Instance
    }

    static [AzSdkSettings] GetLocalInstance() {
        return [AzSdkSettings]::LoadAzSdkSettings($true);
    }

    hidden static [AzSdkSettings] LoadAzSdkSettings([bool] $loadUserCopy) {
        #Filename will be static.
        #For AzSDK Settings, never use online policy store. Its assumed that file will be available offline
        $localAppDataSettings = [ConfigurationHelper]::LoadOfflineConfigFile([AzSdkSettings]::FileName)
        [AzSdkSettings] $parsedSettings = $null;
        [AzSdkSettings] $localModuleSettings = $null;
        [AzSdkSettings] $serverSettings = $null;
        $migratedPropNames = @();
        #Validate settings content is not null
        if ($localAppDataSettings) {
            try
            {
                #Step1: Try parsing the object from local app data settings. If parse is successful then there is no change to settings schema.
                $parsedSettings = [AzSdkSettings] $localAppDataSettings
            }
            catch
            {
                #Step2: Any error occurred while converting local json file indicates change in schema
                #Load latest AzSDKSettings from modules folder
                $parsedSettings = [ConfigurationHelper]::LoadModuleJsonFile([AzSdkSettings]::FileName)
                $parsedSettings | Get-Member -MemberType Properties |
                    ForEach-Object {
                        $propertyName = $_.Name;
                        if([Helpers]::CheckMember($localAppDataSettings, $propertyName))
                        {
                            $parsedSettings.$propertyName = $localAppDataSettings.$propertyName;
                            $migratedPropNames += $propertyName;
                        }
                    };
                if($migratedPropNames.Count -ne 0)
                {
                    [AzSdkSettings]::Update($parsedSettings);
                    [EventBase]::PublishGenericCustomMessage("Local AzSDK settings file was not compatible with the latest version. `r`nMigrated the existing values for properties: [$([string]::Join(", ", $migratedPropNames))] ", [MessageType]::Warning);
                }
            }

            #Step 3: Get the latest server settings and merge with that

            if(-not $loadUserCopy)
            {
                [bool] $_useOnlinePolicyStore = $parsedSettings.UseOnlinePolicyStore;
                [string] $_onlineStoreUri = $parsedSettings.OnlinePolicyStoreUrl;
                [bool] $_enableAADAuthForOnlinePolicyStore = $parsedSettings.EnableAADAuthForOnlinePolicyStore;
                $serverSettings = [ConfigurationHelper]::LoadServerConfigFile([AzSdkSettings]::FileName, $_useOnlinePolicyStore, $_onlineStoreUri, $_enableAADAuthForOnlinePolicyStore);

                $mergedServerPropNames = @();
                $serverSettings | Get-Member -MemberType Properties |
                    ForEach-Object {
                        $propertyName = $_.Name;
                        if([string]::IsNullOrWhiteSpace($parsedSettings.$propertyName) -and -not [string]::IsNullOrWhiteSpace($serverSettings.$propertyName))
                        {
                            $parsedSettings.$propertyName = $serverSettings.$propertyName;
                            $mergedServerPropNames += $propertyName;
                        }
                    };
                if($mergedServerPropNames.Count -ne 0)
                {
                    #TODO need to check for the right message here
                    #[EventBase]::PublishGenericCustomMessage("Merged the existing values for properties from server settings file: [$([string]::Join(", ", $mergedServerPropNames))] ", [MessageType]::Warning);
                }
                [AzSdkSettings]::Instance = $parsedSettings;
            }
            #Sever merged settings should not be persisted, as it should always take latest from the server
            return $parsedSettings;
        }
        else
        {
            return $null;
        }
    }

    [void] Update()
    {
        if (-not (Test-Path $([Constants]::AzSdkAppFolderPath)))
        {
            mkdir -Path $([Constants]::AzSdkAppFolderPath) -ErrorAction Stop | Out-Null
        }

        #persisting back to file
        [AzSdkSettings]::Instance | ConvertTo-Json | Out-File -Force -FilePath ([Constants]::AzSdkAppFolderPath + "\" + [AzSdkSettings]::FileName)
    }

    static [void] Update([AzSdkSettings] $localSettings)
    {
        if (-not (Test-Path $([Constants]::AzSdkAppFolderPath)))
        {
            mkdir -Path $([Constants]::AzSdkAppFolderPath) -ErrorAction Stop | Out-Null
        }

        #persisting back to file
        $localSettings | ConvertTo-Json | Out-File -Force -FilePath ([Constants]::AzSdkAppFolderPath + "\" + [AzSdkSettings]::FileName)
    }

    #TODO: Replace OMSSource with framework level source
    hidden [string] GetScanSource()
    {
        return $this.OMSSource
    }
}