PrivateFunctions/Get-ApiChecksToUpload.ps1

<#
 
  Prepare DQ Monitor Api check objects to upload.
 
#>

Function Get-ApiChecksToUpload
{
  [OutputType([Array])]
  [CmdletBinding()]
  Param(
    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [String] $CheckJsonRaw,

    [Parameter(Mandatory=$True)]
    [ValidateNotNullOrEmpty()]
    [String] $CheckStatementRootPath
  )

  Write-Verbose "Voorbereiden requests naar de server..."
  $CheckJsonObject = $CheckJsonRaw | ConvertFrom-Json
  $ChecksToUpload = @()

  If ($CheckJsonObject.checks.Count -eq 0)
  {
    Return $ChecksToUpload;
  }

  # First get information about available entities.
  [Array]$Entities = Get-DqEntities

  ForEach($CustomCheck in $CheckJsonObject.checks)
  {
    $StatementFile = $CustomCheck.statementFile
    If (-not([System.IO.Path]::IsPathRooted($StatementFile)))
    {
      $StatementFile = Join-Path -Path $CheckStatementRootPath -ChildPath $StatementFile
    }
    
    If (-not(Test-Path -Path $StatementFile -PathType Leaf))
    { 
      Write-Warning "T-SQL statement bestand '$($StatementFile)' kon niet worden gevonden."
      Write-Warning "Controle $($CustomCheck.name)' zal worden overgeslagen."
      Continue;
    }

    $Statement = [String](Get-Content -Path $StatementFile -Raw -ErrorAction Stop)
   
    $EntityId = -1;
    If ([Int]::TryParse($CustomCheck.entity, [ref]$EntityId))
    {
      $EntityId = [Int]$CustomCheck.entity
    }
    Else
    {
      Write-Verbose "Opzoeken id voor entiteit '$($CustomCheck.entity)'..."
      
      $Entity = $Entities | Where-Object { $_.Name -eq $CustomCheck.entity } 
      If (-not($Entity))
      {
        Write-Warning "Opgegeven entiteit '$($CustomCheck.entity)' kon niet gevonden worden op de server. Neem contact op met een CNS consultant om deze entiteit op te voeren."
        Write-Warning "Controle '$($CustomCheck.name)' zal worden overgeslagen."
        Continue;
      }
      
      $EntityId = $Entity.Id
    }
   
    Try 
    {
      $ChecksToUpload += [PSCustomObject]@{
        check = @{
          name = $CustomCheck.name;
          entityId = $EntityId;
          tags = $(If ($CustomCheck.PSObject.Properties.name -match "tags") { $CustomCheck.tags } Else { @() });
      
          documentation = [PSCustomObject]@{
            caption = $CustomCheck.caption;
            description = $CustomCheck.description;
            technicalDescription = $CustomCheck.technicalDescription;
          };

          resultCaptionIsSensitive = $(If ($CustomCheck.PSObject.Properties.name -match "isSensitive") { $CustomCheck.isSensitive } Else { $false });
      
          source = $CustomCheck.source;
          statement = $Statement;
        }
      }
    }
    Catch
    {
      Write-Warning "Er is een fout opgetreden bij het voorbereiden van controle '$($CustomCheck.name)'. Controleer de syntax van het bestand."
      Write-Warning "Deze controle wordt genegeerd."
      Continue; 
    }
  }

  Return $ChecksToUpload;
}