Framework/Managers/FeatureFlightingManager.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
Set-StrictMode -Version Latest
#
# FeatureFlightingManager.ps1
#
class FeatureFlightingManager
{
    hidden static [FeatureFlight] $FeatureFlight = $null;

    hidden static [bool] GetFeatureStatus([string] $FeatureName, [string] $SubscriptionId)
    {
        $featureStatus = $true;
        if($null -eq [FeatureFlightingManager]::FeatureFlight)
        {
            [FeatureFlightingManager]::FeatureFlight = [FeatureFlightingManager]::FetchFeatureFlightConfigData();
        }
        $feature = [FeatureFlightingManager]::FeatureFlight.Features | Where-Object { $_.Name -eq $FeatureName};
        if(($feature | Measure-Object).Count -eq 1)
        {
            if($feature.IsEnabled -eq $true)
            {
                #Print preview note if the preview flag is enabled for this feature
                if($feature.UnderPreview -eq $true)
                {
                    [EventBase]::PublishGenericCustomMessage("Note: $FeatureName Feature is currently under Preview.", [MessageType]::Info);
                }
                $scanSource = [AzSKSettings]::GetInstance().GetScanSource();
                $compatibleScanSource = ($feature.Sources | Where-Object { $_ -eq $scanSource -or $_ -eq "*" } | Measure-Object).Count -gt 0;                
                #Check if the feature scan source is compatible
                if(-not $compatibleScanSource)
                {
                    $featureStatus = $false;    
                }
                #Check if the sub is marked under disabled list for this feature
                elseif(($feature.DisabledForSubs | Measure-Object).Count -gt 0 -and ($feature.DisabledForSubs | Where-Object { $_ -eq $SubscriptionId } | Measure-Object).Count -eq 1)
                {
                    $featureStatus = $false;    
                }
                #Check if the sub is marked under enabled list or * for this feature
                elseif(($feature.EnabledForSubs | Measure-Object).Count -gt 0 -and ($feature.EnabledForSubs | Where-Object { $_ -eq $SubscriptionId -or $_ -eq "*"} | Measure-Object).Count -eq 0)
                {
                    $featureStatus = $false;
                }
            }
            else {
                $featureStatus = $false;
            }
        }
        return $featureStatus;        
    }
    
    hidden static [FeatureFlight] FetchFeatureFlightConfigData()
    {
        [FeatureFlight] $flightingData = [FeatureFlight]::new();
        $flightingData = [FeatureFlight] [ConfigurationManager]::LoadServerConfigFile("FeatureFlighting.json");
        return $flightingData;
    }        
}