Modules/StackDefinition.psm1

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202

Import-Module (Join-Path (Split-Path $PSScriptRoot -Parent) "Helpers\GeneralHelper.psm1") -Force

function Get-StackDefinition
{
    [OutputType([String])]
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$True, 
            Position=0
        )]
        [ValidateScript({Test-Path $_})]
        [String]$Path
    )

    return ConvertFrom-YamlFile $Path
}

function Set-StackDefinition
{
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$True, 
            Position=0
        )]
        [ValidateScript({Test-ObjectValidYaml $_})]
        [Alias('sd', 'definition', 'def')]
        [HashTable]$StackDefinition,

        [Parameter(
            Mandatory=$True, 
            Position=1
        )]
        [String]$Path
    )

    $Parent = Split-Path $Path -Parent

    if (!([String]::Empty -eq $Parent) -And !(Test-Path $Parent)) {
        New-Item $Parent -ItemType Directory
    }

    (ConvertTo-Yaml $StackDefinition) | Set-Content $Path -Force
}

function Invoke-StackDefinitionInterpolation
{
    [CmdletBinding(DefaultParameterSetName='InPlace')]
    Param(
        [Parameter(
            Mandatory=$True, 
            Position=0
        )]
        [ValidateScript({Test-ObjectValidYaml $_})]
        [Alias('sd', 'definition', 'def')]
        [Hashtable]$StackDefinition,

        [Parameter(
            Mandatory=$True, 
            Position=1
        )]
        [ValidateScript({ Test-Path $_ })]
        [ValidateScript({ try { ConvertFrom-YamlFile $_; return $True } catch { return $False } })]
        [String]$Path,

        [Parameter(
            Mandatory=$False, 
            Position=2,
            ParameterSetName='NotInPlace'
        )]
        [Switch]$NotInPlace,

        [Parameter(
            Mandatory=$True, 
            Position=3,
            ParameterSetName='NotInPlace'
        )]
        [String]$Destination,

        [Switch]$Passthru
    )

    Write-Verbose "Starting Interpolation..."
    if ($NotInPlace) {
        $Parent = Split-Path $Destination -Parent
    }
    else {
        $Parent = Split-Path $Path -Parent
    }
    $NewDefinition = Invoke-Interpolation $StackDefinition (ConvertFrom-YamlFile $Path)
    $NewDefinition.Version = "'$($NewDefinition.Version)'"
    Write-Verbose "Finished Interpolation."

    if (!([String]::Empty -eq $Parent) -And !(Test-Path $Parent)) {
        Write-Verbose "Creating Directory: $Parent"
        New-Item $Parent -ItemType Directory
    }

    if ($Passthru) {
        Write-Verbose "Outputting Passthru..."
        return $NewDefinition
    }

    Write-Verbose "Setting $Path to interpolated YAML."
    if ($NotInPlace) {
        (ConvertTo-Yaml $NewDefinition) | Set-Content $Destination -Force
    }
    else {
        (ConvertTo-Yaml $NewDefinition) | Set-Content $Path -Force
    }
}

function Invoke-Interpolation
{
    [OutputType([Hashtable])]
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$True, 
            Position=0
        )]
        [Alias('sd', 'definition', 'def', 'sdef')]
        [Object] $StackDefinition,

        [Parameter(
            Mandatory=$True, 
            Position=1
        )]
        [Alias('nd', 'newdef', 'ndef')]
        [Object]$NewDefinition
    )

    Write-Verbose "Merging properties of source definition and new definition"
    $Extended = Add-PropertyRecurse $StackDefinition $NewDefinition
    $Clone    = $Extended.Clone()

    $Extended `
    | Get-Member -Type NoteProperty, Property `
    | ForEach-Object {
        Write-Verbose "Removing non-stack object $($_.Name) from new definition"
        $Clone.PSObject.Properties.Remove($_.Name)
    }
    $Extended.GetEnumerator() `
    | Where-Object { $_.Key -Like 'x-*' } `
    | ForEach-Object { $Clone.Remove($_.Key) }

    return $Clone
}

function Add-PropertyRecurse
{
    [OutputType([Hashtable])]
    [CmdletBinding()]
    Param(
        [Parameter(
            Mandatory=$False, 
            Position=0
        )]
        [Alias('sd', 'definition', 'def', 'sdef')]
        [Object] $StackDefinition,

        [Parameter(
            Mandatory=$False, 
            Position=1
        )]
        [Alias('nd', 'newdef', 'ndef')]
        [Object]$NewDefinition
    )

    if ($StackDefinition -Is [Hashtable]) {
        $Clone = $StackDefinition.Clone()
        foreach ($Property in $Clone.GetEnumerator()) {
            Write-Verbose "Encountered $($Property.Key)"
            if ($Null -eq $NewDefinition.Item($Property.Key)) {
                $Type = $StackDefinition.Item($Property.Key).GetType()
                if ($Type.Name -eq "Object[]") {
                    Write-Verbose "$($Property.Key) is a mapping sequence -> declaring it as Object[]"
                    $NewDefinition.Item($Property.Key) = New-Object @(@{})
                }
                elseif ($Type.Name -ne "String") {
                    Write-Verbose "$($Property.Key) is not a primitive type -> declaring it as $($Type.Name)"
                    $NewDefinition.Item($Property.Key) = New-Object $Type
                }

                Write-Verbose "Setting the value of $($Property.Key) to $($StackDefinition.Item($Property.Key))"
                $NewDefinition.Item($Property.Key) = $StackDefinition.Item($Property.Key)
            }
            else {
                Write-Verbose "Recursing Add-PropertyRecurse on $($Property.Key)"
               $NewDefinition.Item($Property.Key) = Add-PropertyRecurse $StackDefinition.Item($Property.Key) $NewDefinition.Item($Property.Key)
            }
        }
    }
    else {
        Write-Verbose "Setting the value of new definition to $($StackDefinition)"
        $NewDefinition = $StackDefinition
    }

    return $NewDefinition
}