Framework/Core/AzureMonitoring/OMSMonitoring.ps1

Set-StrictMode -Version Latest 

class OMSMonitoring: AzSdkRoot
{
    [string] $OMSSubCCViewTemplateFilepath;
    [string] $OMSAppCCViewTemplateFilepath;

    OMSMonitoring([string] $subscriptionId): 
        Base([string] $subscriptionId) 
    { 
        #$this.PublishRunIdentifier();
        $OMSSubCCViewTemplateFile = $this.LoadServerConfigFile("AzSDK.AM.OMSSetup.json"); 
        $OMSAppCCViewTemplateFile = $this.LoadServerConfigFile("AzSDK.AM.OMSSetup.App.json"); 
        $OMSLogPath = [Constants]::AzSdkTempFolderPath + "\OMS";
        if(-not (Test-Path -Path $OMSLogPath))
        {
            mkdir -Path $OMSLogPath -Force | Out-Null
        }
        $this.OMSSubCCViewTemplateFilepath = $OMSLogPath+"\AzSDK.AM.OMSSetup.json";
        $this.OMSAppCCViewTemplateFilepath = $OMSLogPath+"\AzSDK.AM.OMSSetup.App.json";

        $OMSSubCCViewTemplateFile | ConvertTo-Json -Depth 99 | Out-File $this.OMSSubCCViewTemplateFilepath
        $OMSAppCCViewTemplateFile | ConvertTo-Json -Depth 99 | Out-File $this.OMSAppCCViewTemplateFilepath
    }

    [void] CreateOMSPolicies([string]$OMSResourceGroupName, [string]$OMSSubCCParamFilePath, [string]$OMSAppCCParamFilePath, [bool]$ValidateOnly)    
    {        
           
        $OptionalParameters = New-Object -TypeName Hashtable
        $ErrorMessages = @()
        if ($ValidateOnly) {
            $ErrorMessages =@()
            if(-not [string]::IsNullOrWhiteSpace($OMSSubCCParamFilePath))
            {
                Test-AzureRmResourceGroupDeployment -ResourceGroupName $OMSResourceGroupName `
                                                                                        -TemplateFile $this.OMSSubCCViewTemplateFilepath `
                                                                                        -TemplateParameterFile $OMSSubCCParamFilePath -Verbose
                #$SubErrorMessages = $SubErrorMessages | ForEach-Object { $_.Exception.Message.TrimEnd("`r`n") }
                #$ErrorMessages += $SubErrorMessages
            }
            if(-not [string]::IsNullOrWhiteSpace($OMSAppCCParamFilePath))
            {
                Test-AzureRmResourceGroupDeployment -ResourceGroupName $OMSResourceGroupName `
                                                                                            -TemplateFile $this.OMSAppCCViewTemplateFilepath `
                                                                                            -TemplateParameterFile $OMSAppCCParamFilePath -Verbose
                #$SubErrorMessages = $SubErrorMessages | ForEach-Object { $_.Exception.Message.TrimEnd("`r`n") }
                #$ErrorMessages += $SubErrorMessages
            }
        }
        else {

            $ErrorMessages =@()
            if(-not [string]::IsNullOrWhiteSpace($OMSSubCCParamFilePath))
            {
                $SubErrorMessages = @()
                New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $this.OMSSubCCViewTemplateFilepath).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
                                            -ResourceGroupName $OMSResourceGroupName `
                                            -TemplateFile $this.OMSSubCCViewTemplateFilepath `
                                             -TemplateParameterFile $OMSSubCCParamFilePath `
                                                                                  @OptionalParameters `
                                            -Verbose -Force -ErrorVariable SubErrorMessages
                $SubErrorMessages = $SubErrorMessages | ForEach-Object { $_.Exception.Message.TrimEnd("`r`n") }
                $ErrorMessages += $SubErrorMessages
            }
            if(-not [string]::IsNullOrWhiteSpace($OMSAppCCParamFilePath))
            {
                $SubErrorMessages = @()
                New-AzureRmResourceGroupDeployment -Name ((Get-ChildItem $this.OMSAppCCViewTemplateFilepath).BaseName + '-' + ((Get-Date).ToUniversalTime()).ToString('MMdd-HHmm')) `
                    -ResourceGroupName $OMSResourceGroupName `
                    -TemplateFile $this.OMSAppCCViewTemplateFilepath `
                        -TemplateParameterFile $OMSAppCCParamFilePath `
                                                            @OptionalParameters `
                    -Verbose -Force -ErrorVariable SubErrorMessages
                $SubErrorMessages = $SubErrorMessages | ForEach-Object { $_.Exception.Message.TrimEnd("`r`n") }
                $ErrorMessages += $SubErrorMessages                        
            }
        }
        if ($ErrorMessages)
        {
            "", ("{0} returned the following errors:" -f ("Template deployment", "Validation")[[bool]$ValidateOnly]), @($ErrorMessages) | ForEach-Object { $this.PublishCustomMessage([MessageData]::new($_));}
        }        
    }
}