Skip to content

Commit dee1089

Browse files
committed
Feature: Add PsLaTaskTemplate.ps1
1 parent 4dff364 commit dee1089

9 files changed

+213
-12
lines changed

PsLogicAppExtractor/PsLogicAppExtractor.psd1

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@
2121
Description = 'A set of tools that will assist you with extracting / exporting Azure Logic Apps, and turn them into a fully working ARM template. It contains of several small tasks, which can be configured to meet your needs.'
2222

2323
# Minimum version of the Windows PowerShell engine required by this module
24-
PowerShellVersion = '5.0'
24+
PowerShellVersion = '7.0'
2525

2626
# Modules that must be imported into the global environment prior to importing
2727
# this module
2828
RequiredModules = @(
2929
@{ ModuleName = 'PSFramework'; ModuleVersion = '1.6.214' }
3030
, @{ ModuleName = 'psake'; ModuleVersion = '4.9.0' }
31-
, @{ ModuleName = 'newtonsoft.json'; ModuleVersion = '1.0.2.201' }
3231
)
3332

3433
# Assemblies that must be loaded prior to importing this module
@@ -42,11 +41,12 @@
4241

4342
# Functions to export from this module
4443
FunctionsToExport = @(
45-
'Get-PsLaTask'
44+
'Get-PsLaArmParameter'
45+
, 'Get-PsLaTask'
4646
, 'Get-PsLaTaskByFile'
4747
, 'Get-PsLaTaskByPath'
4848
, 'Get-PsLaTaskOrderByFile'
49-
49+
, 'Get-PsLaTaskTemplate'
5050
, 'Invoke-PsLaExtractor'
5151

5252
, 'New-PsLaRunbookByPath'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<#
2+
.SYNOPSIS
3+
Get parameters from ARM template
4+
5+
.DESCRIPTION
6+
Get parameters from the ARM template
7+
8+
You can include / exclude parameters, so your parameter file only contains the parameters you want to handle at deployment
9+
10+
The default value is promoted as the initial value of the parameter
11+
12+
.PARAMETER Path
13+
Path to the ARM template that you want to work against
14+
15+
.PARAMETER Exclude
16+
Instruct the cmdlet to exclude the given set of parameter names
17+
18+
Supports array / list
19+
20+
.PARAMETER Include
21+
Instruct the cmdlet to include the given set of parameter names
22+
23+
Supports array / list
24+
25+
.PARAMETER AsFile
26+
Instruct the cmdlet to save a valid ARM template parameter file next to the ARM template file
27+
28+
.PARAMETER BlankValues
29+
Instructs the cmdlet to blank the values in the parameter file
30+
31+
.PARAMETER CopyMetadata
32+
Instructs the cmdlet to copy over the metadata property from the original parameter in the ARM template, if present
33+
34+
.EXAMPLE
35+
PS C:\> Get-PsLaArmParameter -Path "C:\temp\work_directory\TestLogicApp.json"
36+
37+
Gets all parameters from the "TestLogicApp.json" ARM template
38+
The output is written to the console
39+
40+
.EXAMPLE
41+
PS C:\> Get-PsLaArmParameter -Path "C:\temp\work_directory\TestLogicApp.json" -Exclude "logicAppLocation","trigger_Frequency"
42+
43+
Gets all parameters from the "TestLogicApp.json" ARM template
44+
Will exclude the parameters "logicAppLocation" & "trigger_Frequency" if present
45+
The output is written to the console
46+
47+
.EXAMPLE
48+
PS C:\> Get-PsLaArmParameter -Path "C:\temp\work_directory\TestLogicApp.json" -Include "trigger_Interval","trigger_Frequency"
49+
50+
Gets all parameters from the "TestLogicApp.json" ARM template
51+
Will only copy over the parameters "trigger_Interval" & "trigger_Frequency" if present
52+
The output is written to the console
53+
54+
.EXAMPLE
55+
PS C:\> Get-PsLaArmParameter -Path "C:\temp\work_directory\TestLogicApp.json" -AsFile
56+
57+
Gets all parameters from the "TestLogicApp.json" ARM template
58+
The output is written the "C:\temp\work_directory\TestLogicApp.parameters.json" file
59+
60+
.EXAMPLE
61+
PS C:\> Get-PsLaArmParameter -Path "C:\temp\work_directory\TestLogicApp.json" -BlankValues
62+
63+
Gets all parameters from the "TestLogicApp.json" ARM template
64+
Blank all values for each parameter
65+
The output is written to the console
66+
67+
.EXAMPLE
68+
PS C:\> Get-PsLaArmParameter -Path "C:\temp\work_directory\TestLogicApp.json" -CopyMetadata
69+
70+
Gets all parameters from the "TestLogicApp.json" ARM template
71+
Copies over the metadata property from the original parameter, if present
72+
The output is written to the console
73+
74+
.NOTES
75+
76+
Author: Mötz Jensen (@Splaxi)
77+
78+
#>
79+
function Get-PsLaArmParameter {
80+
[CmdletBinding()]
81+
param (
82+
[string] $Path,
83+
84+
[string[]] $Exclude,
85+
86+
[string[]] $Include,
87+
88+
[switch] $AsFile,
89+
90+
[switch] $BlankValues,
91+
92+
[switch] $CopyMetadata
93+
)
94+
95+
$armObj = [ArmTemplate]$(Get-TaskWorkObject -Path $Path)
96+
97+
$res = [ordered]@{}
98+
$res.'$schema' = "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#"
99+
$res.contentVersion = "1.0.0.0"
100+
$res.parameters = [ordered]@{}
101+
102+
foreach ($item in $armObj.parameters.PsObject.Properties) {
103+
if ($item.Name -in $Exclude) { continue }
104+
105+
if ($Include.Count -gt 0) {
106+
if (-not ($item.Name -in $Include)) { continue }
107+
}
108+
109+
$valueObj = [ordered]@{}
110+
111+
if ($BlankValues) {
112+
switch ($item.Value.Type) {
113+
"int" {
114+
$valueObj.value = 0
115+
}
116+
"bool" {
117+
$valueObj.value = $false
118+
}
119+
"object" {
120+
$valueObj.value = $null
121+
}
122+
"array" {
123+
$valueObj.value = @()
124+
}
125+
Default {
126+
$valueObj.value = ""
127+
}
128+
}
129+
}
130+
else {
131+
$valueObj.value = $item.Value.DefaultValue
132+
}
133+
134+
if ($CopyMetadata -and $item.Value.metadata) {
135+
$valueObj.metadata = $item.Value.metadata
136+
}
137+
138+
$res.parameters."$($item.Name)" = $valueObj
139+
}
140+
141+
if ($AsFile) {
142+
$pathLocal = $Path.Replace(".json", ".parameters.json")
143+
144+
$encoding = New-Object System.Text.UTF8Encoding($true)
145+
[System.IO.File]::WriteAllLines($pathLocal, $($([PSCustomObject] $res) | ConvertTo-Json -Depth 10), $encoding)
146+
147+
Get-Item -Path $pathLocal | Select-Object -ExpandProperty FullName
148+
}
149+
else {
150+
$([PSCustomObject] $res) | ConvertTo-Json -Depth 10
151+
}
152+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<#
2+
.SYNOPSIS
3+
Short description
4+
5+
.DESCRIPTION
6+
Long description
7+
8+
.PARAMETER Category
9+
Instruct the cmdlet which template type you want to have outputted
10+
11+
.PARAMETER OutputPath
12+
Path to were the Task template file will be persisted
13+
14+
The path has to be a directory
15+
16+
The file will be named: _set-XYA.Template.ps1
17+
18+
.EXAMPLE
19+
PS C:\> Get-PsLaTaskTemplate -Category "Arm"
20+
21+
Outputs the task template of the type Arm to the console
22+
23+
.EXAMPLE
24+
PS C:\> Get-PsLaTaskTemplate -Category "Arm" -OutputPath "C:\temp\work_directory"
25+
26+
Outputs the task template of the type Arm
27+
Persists the file into the "C:\temp\work_directory" directory
28+
29+
.NOTES
30+
31+
Author: Mötz Jensen (@Splaxi)
32+
33+
#>
34+
function Get-PsLaTaskTemplate {
35+
[CmdletBinding()]
36+
param (
37+
[ValidateSet('Arm', 'Converter', 'Raw')]
38+
[string] $Category,
39+
40+
[string] $OutputPath
41+
)
42+
43+
if ($OutputPath) {
44+
Copy-Item -Path "$($script:ModuleRoot)\internal\tasks\_Set-$Category.Template.tmp" -Destination "$OutputPath\_Set-$Category.Template.ps1" -PassThru -Force | Select-Object -ExpandProperty FullName
45+
}
46+
else {
47+
Get-Content -Path "$($script:ModuleRoot)\internal\tasks\_Set-$Category.Template.tmp" -Raw
48+
}
49+
}

PsLogicAppExtractor/functions/New-PsLaRunbookByPath.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function New-PsLaRunbookByPath {
203203

204204
$path = Join-Path -Path $OutputPath -ChildPath "PsLaExtractor.default.psakefile.ps1"
205205

206-
$encoding = New-Object System.Text.UTF8Encoding($false)
206+
$encoding = New-Object System.Text.UTF8Encoding($true)
207207
[System.IO.File]::WriteAllLines($path, $($res.ToArray() -join "`r`n"), $encoding)
208208

209209
Get-Item -Path $path | Select-Object -ExpandProperty FullName

PsLogicAppExtractor/functions/New-PsLaRunbookByTask.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function New-PsLaRunbookByTask {
140140

141141
$path = Join-Path -Path $OutputPath -ChildPath "PsLaExtractor.default.psakefile.ps1"
142142

143-
$encoding = New-Object System.Text.UTF8Encoding($false)
143+
$encoding = New-Object System.Text.UTF8Encoding($true)
144144
[System.IO.File]::WriteAllLines($path, $($res.ToArray() -join "`r`n"), $encoding)
145145

146146
Get-Item -Path $path | Select-Object -ExpandProperty FullName

PsLogicAppExtractor/functions/Out-TaskFile.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function Out-TaskFile {
5353
$Content = $InputObject | ConvertTo-Json -Depth 20
5454
}
5555

56-
$encoding = New-Object System.Text.UTF8Encoding($false)
56+
$encoding = New-Object System.Text.UTF8Encoding($true)
5757
[System.IO.File]::WriteAllLines($Path, $Content, $encoding)
5858

5959
if ($(Get-PSFConfigValue -FullName PsLogicAppExtractor.Execution.TaskInputNext)) {

PsLogicAppExtractor/internal/tasks/All/All.task.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -566,15 +566,15 @@ Task -Name "Set-Arm.Trigger.ApiConnection.Recurrence.AsParameter" @parm -Action
566566
$armObj = Add-ArmParameter -InputObject $armObj -Name "$frequencyPreSuf" `
567567
-Type "string" `
568568
-Value $orgFrequency `
569-
-Description "The frequency used for the trigger to evalutate / run."
569+
-Description "The frequency used for the trigger to evaluate / run."
570570

571571
$armObj.resources[0].properties.definition.triggers.PsObject.Properties.Value.recurrence.frequency = "[parameters('$frequencyPreSuf')]"
572572

573573
$orgInterval = $armObj.resources[0].properties.definition.triggers.PsObject.Properties.Value.recurrence.interval
574574
$armObj = Add-ArmParameter -InputObject $armObj -Name "$intervalPreSuf" `
575575
-Type "int" `
576576
-Value $orgInterval `
577-
-Description "The interval used for the trigger to evalutate / run."
577+
-Description "The interval used for the trigger to evaluate / run."
578578

579579
$armObj.resources[0].properties.definition.triggers.PsObject.Properties.Value.recurrence.interval = "[parameters('$intervalPreSuf')]"
580580
}

PsLogicAppExtractor/internal/tasks/Set-Arm.Trigger.ApiConnection.Recurrence.AsParameter.task.ps1

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ Task -Name "Set-Arm.Trigger.ApiConnection.Recurrence.AsParameter" @parm -Action
2525
$armObj = Add-ArmParameter -InputObject $armObj -Name "$frequencyPreSuf" `
2626
-Type "string" `
2727
-Value $orgFrequency `
28-
-Description "The frequency used for the trigger to evalutate / run."
28+
-Description "The frequency used for the trigger to evaluate / run."
2929

3030
$armObj.resources[0].properties.definition.triggers.PsObject.Properties.Value.recurrence.frequency = "[parameters('$frequencyPreSuf')]"
3131

3232
$orgInterval = $armObj.resources[0].properties.definition.triggers.PsObject.Properties.Value.recurrence.interval
3333
$armObj = Add-ArmParameter -InputObject $armObj -Name "$intervalPreSuf" `
3434
-Type "int" `
3535
-Value $orgInterval `
36-
-Description "The interval used for the trigger to evalutate / run."
36+
-Description "The interval used for the trigger to evaluate / run."
3737

3838
$armObj.resources[0].properties.definition.triggers.PsObject.Properties.Value.recurrence.interval = "[parameters('$intervalPreSuf')]"
3939
}

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The PsLogicAppExtractor project is deeply inspired by [LogicAppTemplateCreator](
1010

1111
PsLogicAppExtractor offers a different approach to extract a LogicApp, where the module should be considered an orchestration engine, that comes with a lot of tools, and makes it possible for the end user to design their own export orchestration runbook.
1212

13-
The core idea is that a single operation that you want to have applied against your LogicApp while exporting it / converting it to an ARM template is a task. Put in other words, you should view the exporting / converting to an ARM template as a series of micro steps, which breaks down the complexity for building the ARM template. The tradeoff is that you have to orchestrate the steps and their execution sequence, and ones you have done that - you're ready to fully automated things.
13+
The core idea is that a single operation that you want to have applied against your LogicApp while exporting it / converting it to an ARM template is a task. Put in other words, you should view the exporting / converting to an ARM template as a series of micro steps, which breaks down the complexity for building the ARM template. The tradeoff is that you have to orchestrate the steps and their execution sequence, and ones you have done that - you're ready to fully automate things.
1414

1515
The key difference of PsLogicAppExtractor is that it **encourages** the end user to have custom tasks, that handles their specific needs and build a runbook that they can share across their team and that way makes the export process as smooth as possible.
1616

0 commit comments

Comments
 (0)