private/Update-PropertiesFromFile.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
function Update-PropertiesFromFile {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)] [Adf] $adf,
        [Parameter(Mandatory)] [string] $stage
        )

    Write-Debug "BEGIN: Update-PropertiesFromFile(adf=$adf, stage=$stage)"

    $option = $adf.PublishOptions
    $srcFolder = $adf.Location
    if ([string]::IsNullOrEmpty($srcFolder)) {
        Write-Error "ADFT0011: adf.Location property has not been provided."
    }
    
    $ext = "CSV"
    if ($stage.EndsWith(".csv")) { 
        $configFileName = $stage 
    } elseif ($stage.EndsWith(".json")) {
        $configFileName = $stage 
        $ext = "JSON"
    } 
    else {
        $configFileName = Join-Path $srcFolder "deployment\config-$stage.csv"
    }

    Write-Verbose "Replacing values for ADF properties from $ext config file"
    Write-Host "Config file: $configFileName"

    if ($ext -eq "CSV") {
        $config = Read-CsvConfigFile -Path $configFileName
    } else {
        $config = Read-JsonConfigFile -Path $configFileName -adf $adf
    }
    # $config | Out-Host

    $report = @{ Updated = 0; Added = 0; Removed = 0}
    $config | ForEach-Object {
        Write-Debug "Item: $_"
        $path = $_.path
        $value = $_.value
        $name = $_.name
        $type = $_.type

        # Omit commented lines
        if ($type.StartsWith('#')) { 
            Write-Debug "Skipping this line..."
            return      # return is like continue for foreach and go to next item in collection
        }

        $action = "update"
        if ($path.StartsWith('+')) { 
            $action = 'add';
            $path = $path.Substring(1)
        }
        if ($path.StartsWith('-')) { 
            $action = 'remove';
            $path = $path.Substring(1)
        }
        if ($path.StartsWith("`$.properties.")) { 
            $path = $path.Substring(13) 
        }

        $objArr = Get-AdfObjectByPattern -adf $adf -name $name -type $type
        if ($null -eq $objArr) {
            if ($option.FailsWhenConfigItemNotFound -eq $false) {
                Write-Warning "Could not find object: $type.$name, skipping..."
            } else {
                Write-Error -Message "ADFT0007: Could not find object: $type.$name"
            }
        } else {
            Write-Verbose "- Performing: $action for object(path): $type.$name(properties.$path)"
            $objArr | ForEach-Object {
                $null = Update-PropertiesForObject -o $_ -action $action -path $path -value $value -name $name -type $type -report $report
            }
        }
    }
    Write-Host "*** Properties modification report ***"
    $report | Out-Host 

    Write-Debug "END: Update-PropertiesFromFile"

}


function Update-PropertiesForObject {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]          [AdfObject] $o,
        [Parameter(Mandatory)]          [string] $action,
        [Parameter(Mandatory)]          [string] $path,
        [Parameter(Mandatory = $false)] [string] $value,
        [Parameter(Mandatory)]          [string] $name,
        [Parameter(Mandatory)]          [string] $type,
        [Parameter(Mandatory)]          $report
    )

    Write-Debug "BEGIN: Update-PropertiesForObject"

    # if ($null -eq $o -and $action -ne "add") {
    # Write-Error "ADFT0008: Could not find object: $type.$name"
    # }
    $json = $o.Body
    if ($null -eq $json) {
        Write-Error "ADFT0009: Body of the object is empty!"
    }
    
    $objName = $o.Name
    if ($objName -ne $name) { 
        Write-Verbose " - Matched object: $type.$objName(properties.$path)"
    }

    $validPath = $true

    try {
        if ($action -ne "add") {
            Invoke-Expression "`$isExist = (`$null -ne `$json.properties.$path)"
        }
    }
    catch {
        $validPath = $false

        if ($option.FailsWhenPathNotFound -eq $false) {
            Write-Warning "Wrong path defined in config for object(path): $type.$name(properties.$path), skipping..."
        } else {
            $exc = ([System.Data.DataException]::new())
            Write-Error -Message "ADFT0010: Wrong path defined in config for object(path): $type.$name(properties.$path)" -Exception $exc
        }
    }

    if ($validPath) {
        switch -Exact ($action)
        {
            'update'
            {
                Update-ObjectProperty -obj $json -path "properties.$path" -value "$value"
                $report['Updated'] += 1
            }
            'add'
            {
                Add-ObjectProperty -obj $json -path "properties.$path" -value "$value"
                $report['Added'] += 1
            }
            'remove'
            {
                Remove-ObjectProperty -obj $json -path "properties.$path"
                $report['Removed'] += 1
            }
        }
    }

    # Save new file for deployment purposes and change pointer in object instance
    $f = (Save-AdfObjectAsFile -obj $o)
    $o.FileName = $f

    Write-Debug "END: Update-PropertiesForObject"

}