Framework/Listeners/CA/WriteCAStatus.ps1

Set-StrictMode -Version Latest 
#Listner to write CA scan status on completion of resource scan
class WriteCAStatus: ListenerBase
{
    hidden static [WriteCAStatus] $Instance = $null;
    static [WriteCAStatus] GetInstance()
    {
        if ( $null -eq  [WriteCAStatus]::Instance)
        {
            [WriteCAStatus]::Instance = [WriteCAStatus]::new();
        }    
        return [WriteCAStatus]::Instance
    }


    [void] RegisterEvents()
    {
        $this.UnregisterEvents();       

        $this.RegisterEvent([AzSdkRootEvent]::GenerateRunIdentifier, {
            $currentInstance = [WriteCAStatus]::GetInstance();
            try 
            {
                $currentInstance.SetRunIdentifier([AzSdkRootEventArgument] ($Event.SourceArgs | Select-Object -First 1));                         
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });

        $this.RegisterEvent([SVTEvent]::EvaluationCompleted, {
            $currentInstance = [WriteCAStatus]::GetInstance();
            try 
            {
                $props = $Event.SourceArgs[0];
                if($props)
                {
                    if($props.IsResource())
                    {
                        #Update resource scan completion in CA storage account
                        $currentInstance = [WriteCAStatus]::GetInstance();
                        $scanSource = [AzSdkSettings]::GetInstance().GetScanSource();
                        [ControlBaselineManager] $controlBaselineMngr = [ControlBaselineManager]::GetInstance();
                        $baselineControlsDetails = $controlBaselineMngr.GetBaselineControlDetails()
                        #If Scan source is in suported sources or UsePartialCommits switch is available
                        if ($currentInstance.InvocationContext.BoundParameters["UsePartialCommits"] -or ($baselineControlsDetails.SupportedSources -contains $scanSource))
                        {
                            $controlBaselineMngr.UpdateResourceStatus( $props.ResourceContext.ResourceId,"COMP");
                        }
                        #$currentInstance.AddOutputLog(([Constants]::CompletedAnalysis -f $props.FeatureName, $props.ResourceContext.ResourceGroupName, $props.ResourceContext.ResourceName));
                    }
                    else
                    {
                        
                    }
                }            
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });

         $this.RegisterEvent([SVTEvent]::CommandCompleted, {
            $currentInstance = [ControlBaselineManager]::GetInstance();
            try 
            {
                $currentInstance = [WriteCAStatus]::GetInstance();
                $scanSource = [AzSdkSettings]::GetInstance().GetScanSource();
                [ControlBaselineManager] $controlBaselineMngr = [ControlBaselineManager]::GetInstance();
                $baselineControlsDetails = $controlBaselineMngr.GetBaselineControlDetails()
                #If Scan source is in suported sources or UsePartialCommits switch is available
                if ($currentInstance.InvocationContext.BoundParameters["UsePartialCommits"] -or ($baselineControlsDetails.SupportedSources -contains $scanSource))
                {
                    $controlBaselineMngr.RemoveControlBaseline();   
                }
            }
            catch 
            {
                $currentInstance.PublishException($_);
            }
        });
    }


}