public/Test-AdfCode.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<#
.SYNOPSIS
Validates files of ADF in a given location, returning warnings or errors.
 
.DESCRIPTION
Validates files of ADF in a given location. The following validation will be perform:
- Reads all files and validates its json format
- Checks whether all dependant objects exist
- Checks whether file name equals object name
 
.PARAMETER RootFolder
Source folder where all ADF objects are kept. The folder should contain subfolders like pipeline, linkedservice, etc.
 
.EXAMPLE
Test-AdfCode -RootFolder "$RootFolder"
 
#>

function Test-AdfCode {
    [CmdletBinding()]
    param (
        [parameter(Mandatory = $true)] 
        [String] $RootFolder
    )

    $ErrorCount = 0
    $WarningCount = 0
    $adfName = Split-Path -Path "$RootFolder" -Leaf

    Write-Host "=== Loading files from location: $RootFolder ..."
    $adf = Import-AdfFromFolder -FactoryName "$adfName" -RootFolder "$RootFolder" -ErrorAction "SilentlyContinue"
    $ObjectsCount = $adf.AllObjects().Count

    Write-Host "=== Validating files ..."

    if ($ObjectsCount -eq 0) {
        $WarningCount += 1
        Write-Warning "No Azure Data Factory files have been found in a given location."
    }

    $adf.AllObjects() | ForEach-Object {
        $FullName = $_.FullName($true)
        Write-Host "Checking: $FullName..."
        $HasBody = $null -ne $_.Body
        if (-not $HasBody) {
            $ErrorCount += 1
            Write-Error -Message "Object $FullName was not loaded properly."
        }
        if ($HasBody) {

            if ($_.name -ne $_.Body.name) {
                $ErrorCount += 1
                Write-Error -Message "Object $FullName has mismatch file name."
            }

            $_.DependsOn | ForEach-Object {
                Write-Verbose -Message " - Checking dependency: [$_]"
                $ref_arr = $adf.GetObjectsByFullName("$_")
                if ($ref_arr.Count -eq 0) {
                    $ErrorCount += 1
                    Write-Error -Message "Couldn't find referenced object $_."
                }
            }
        }

    }

    $msg = "Test code completed ($ObjectsCount objects)."
    if ($ErrorCount -gt 0) { $msg = "Test code failed." }
    $line1 = $adf.Name.PadRight(63) + " # of Errors: $ErrorCount".PadLeft(28)
    $line2 = $msg.PadRight(63)      + "# of Warnings: $WarningCount".PadLeft(28)
    Write-Host "============================================================================================="
    Write-Host " $line1"
    Write-Host " $line2"
    Write-Host "============================================================================================="

}