Skip to content

Commit

Permalink
Update ARM TTK to version 20210913 (#52)
Browse files Browse the repository at this point in the history
* Update ARM TTK to version 20210913

Co-authored-by: Sam Meel <[email protected]>
  • Loading branch information
sammeel and Sam Meel authored Oct 5, 2021
1 parent ae6ed19 commit 88fd790
Show file tree
Hide file tree
Showing 25 changed files with 11,484 additions and 7,304 deletions.
85 changes: 78 additions & 7 deletions task/arm-ttk/Expand-AzTemplate.ps1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function Expand-AzTemplate
function Expand-AzTemplate
{
<#
.Synopsis
Expand Down Expand Up @@ -113,7 +113,7 @@
# Now let's try to resolve the template path.
$resolvedTemplatePath =
# If the template path doesn't appear to be a path to a json file,
if ($TemplatePath -notlike '*.json') {
if ($TemplatePath -notmatch '\.json(c)?$') {
# see if it looks like a file
if ( test-path -path $templatePath -PathType leaf) {
$TemplatePath = $TemplatePath | Split-Path # if it does, reassign template path to it's directory.
Expand All @@ -122,7 +122,7 @@
$preferredJsonFile = $TemplatePath |
Get-ChildItem -Filter *.json |
# for a file named azuredeploy.json, prereq.azuredeploy.json or mainTemplate.json
#Where-Object { 'azuredeploy.json', 'mainTemplate.json', 'prereq.azuredeploy.json' -contains $_.Name } |
# Where-Object { 'azuredeploy.json', 'mainTemplate.json', 'prereq.azuredeploy.json' -contains $_.Name } |
Select-Object -First 1 -ExpandProperty Fullname
# If no file was found, write an error and return.
if (-not $preferredJsonFile) {
Expand All @@ -145,7 +145,9 @@
'FolderName', 'HasCreateUIDefinition', 'IsMainTemplate','FolderFiles',
'MainTemplatePath', 'MainTemplateObject', 'MainTemplateText',
'MainTemplateResources','MainTemplateVariables','MainTemplateParameters', 'MainTemplateOutputs', 'TemplateMetadata',
'isParametersFile', 'ParameterFileName', 'ParameterObject', 'ParameterText'
'isParametersFile', 'ParameterFileName', 'ParameterObject', 'ParameterText',
'InnerTemplates', 'ParentTemplateText', 'ParentTemplateObject',
'ExpandedTemplateText', 'ExpandedTemplateObject'

foreach ($_ in $WellKnownVariables) {
$ExecutionContext.SessionState.PSVariable.Set($_, $null)
Expand All @@ -165,6 +167,10 @@
$TemplateText = [IO.File]::ReadAllText($resolvedTemplatePath)
#*$TemplateObject (the template text, converted from JSON)
$TemplateObject = Import-Json -FilePath $TemplateFullPath
#*$ParentTemplateText (the parent or original template (will be the same if no nested deployments is found))
$ParentTemplateText = [IO.File]::ReadAllText($resolvedTemplatePath)
#*$ParentTemplateObject (the parent or original template (will be the same if no nested deployments is found))
$ParentTemplateObject = Import-Json -FilePath $TemplateFullPath

if($TemplateObject.metadata -ne $null){
$TemplateMetadata = $($TemplateObject.metadata)
Expand All @@ -174,7 +180,7 @@

$isParametersFile = $resolvedTemplatePath -like '*.parameters.json'

if ($resolvedTemplatePath -like '*.json' -and
if ($resolvedTemplatePath -match '\.json(c)?$' -and
$TemplateObject.'$schema' -like '*CreateUIDefinition*') {
$createUiDefinitionFullPath = "$resolvedTemplatePath"
$createUIDefinitionText = [IO.File]::ReadAllText($createUiDefinitionFullPath)
Expand Down Expand Up @@ -219,10 +225,10 @@
if ($fileInfo.DirectoryName -eq '__macosx') {
return # (excluding files as side-effects of MAC zips)
}

# All FolderFile objects will have the following properties:


if ($fileInfo.Extension -eq '.json') {
if ($fileInfo.Extension -in '.json', '.jsonc') {
$fileObject = [Ordered]@{
Name = $fileInfo.Name #*Name (the name of the file)
Extension = $fileInfo.Extension #*Extension (the file extension)
Expand Down Expand Up @@ -283,6 +289,71 @@
$FolderFiles = @(@($createUIDefFile) + @($otherFolderFiles) -ne $null)
}


$innerTemplates = @(if ($templateText -and $TemplateText.Contains('"template"')) {
Find-JsonContent -InputObject $templateObject -Key template |
Where-Object { $_.expressionEvaluationOptions.scope -eq 'inner' -or $_.jsonPath -like '*.policyRule.*' } |
Sort-Object JSONPath -Descending
})

if ($innerTemplates) {
$anyProblems = $false
$originalTemplateText = "$TemplateText"
foreach ($it in $innerTemplates) {
$foundInnerTemplate = $it | Resolve-JSONContent -JsonText $TemplateText
if (-not $foundInnerTemplate) { $anyProblems = $true; break }
$TemplateText = $TemplateText.Remove($foundInnerTemplate.Index, $foundInnerTemplate.Length)
$TemplateText = $TemplateText.Insert($foundInnerTemplate.Index, '"template": {}')
}

if (-not $anyProblems) {
$TemplateObject = $TemplateText | ConvertFrom-Json
} else {
Write-Error "Could not extract inner templates for '$TemplatePath'." -ErrorId InnerTemplate.Extraction.Error
}
}


if ($TemplateText) {
$variableReferences = $TemplateText | ?<ARM_Variable>
$expandedTemplateText = $TemplateText | ?<ARM_Variable> -ReplaceEvaluator {
param($match)

$templateVariableValue = $templateObject.variables.$($match.Groups['VariableName'])
if ($match.Groups["Property"].Success) {

$v = $templateVariableValue
foreach ($prop in $match.Groups["Property"] -split '\.' -ne '') {
if ($prop -match '\[(?<Index>\d+)]$') {
$v.($prop.Replace("$($matches.0)", ''))[[int]$matches.Index]
} else {
$v = $v.$prop
}
}
return "'$("$v".Replace("'","\'"))'"
} else {
if ("$templateVariableValue".StartsWith('[')) {
if ("$templateVariableValue".EndsWith(']')) {
return "$templateVariableValue" -replace '^\[' -replace '\]$'
} else {
return $templateVariableValue
}
} else {
return "'" + "$templateVariableValue".Replace("'","\'") + "'"
}

return "$($templateObject.variables.$($match.Groups['VariableName']))".Replace("'","\'")
}
}

if ($expandedTemplateText -ne $TemplateText) {
$expandedTemplateObject = try { $expandedTemplateText | ConvertFrom-Json -ErrorAction Stop -ErrorVariable err } catch {
"$_" | Write-Debug
}
} else {
$expandedTemplateObject = $null
}
}

$out = [Ordered]@{}
foreach ($v in $WellKnownVariables) {
Expand Down
27 changes: 20 additions & 7 deletions task/arm-ttk/Find-JsonContent.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@
$OutObject[$prop.Name] = $prop.Value
}
$OutObject['ParentObject'] = $parent
$OutObject['PropertyName'] = if ($property) {$Property[-1]}

$OutObject['PSTypeName'] = 'JSON.Content'
$OutObject['PropertyName'] = if ($property) {$Property[-1] -replace '^\[\d+\]\.'}
$OutObject['JSONPath'] = @(
$np =0
foreach ($p in $property) {
Expand All @@ -90,7 +90,7 @@
if (-not $InputObject) { return }


$index = -1
$index = -1
foreach ($in in $InputObject) {
if (-not $in) { continue }
$index++
Expand All @@ -106,7 +106,11 @@
($NotLike -and $in.$key -notlike $Value) -or
($NotMatch -and $in.$key -notmatch $Value) -or
($in.$key -eq $Value -and -not ($NotLike -or $NotMatch))) {
$property += $key
if ($InputObject -is [Collections.IList] -and $Property) {
$property += "[$index].$($key)"
} else {
$property += $key
}
. $outputMatch $in
}
}
Expand All @@ -124,8 +128,17 @@
} else {
$key
})
$property += $matchingKeys
. $OutputMatch $in
$propertyList = @() + $Property
foreach ($k in $matchingKeys) {
if ($InputObject -is [Collections.IList] -and $Property) {
$property += "[$index].$($k)"
} else {
$property += $k
}

. $OutputMatch $in
$property = $propertyList
}
}
elseif (
($NotMatch -and $propertyNames -notmatch $Key) -or
Expand Down Expand Up @@ -154,7 +167,7 @@
@(if ($Property) {
$property
}
if ($InputObject.Count) {
if ($InputObject -is [Collections.IList]) {
"[$index]"
})
foreach ($prop in $in.psobject.properties) {
Expand Down
10 changes: 8 additions & 2 deletions task/arm-ttk/Import-Json.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,20 @@
if (-not $resolvedPath) { return }

$convertProblems = $null
try {
[IO.File]::ReadAllText("$resolvedPath") |
ConvertFrom-Json -ErrorAction SilentlyContinue -ErrorVariable ConvertProblems
} catch {
$convertProblems = $_
}

if ($convertProblems) {
if ($convertProblems[0].InnerException) {
$PSCmdlet.WriteError([Management.Automation.ErrorRecord]::new($convertProblems[0].InnerException, 'Import.Json.Failed', 'InvalidOperation', $FilePath))
$PSCmdlet.WriteError([Management.Automation.ErrorRecord]::new(
[Exception]::new("Import failed for '$filePath': $($convertProblems[0].InnerException.Message)",$convertProblems[0].InnerException)
, 'Import.Json.Failed', 'InvalidOperation', $FilePath))
} else {
$PSCmdlet.WriteError([Management.Automation.ErrorRecord]::new("$($convertProblems)", 'Import.Json.Failed', 'InvalidOperation', $FilePath))
$PSCmdlet.WriteError([Management.Automation.ErrorRecord]::new("Import failed for '$filePath': $($convertProblems)", 'Import.Json.Failed', 'InvalidOperation', $FilePath))
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions task/arm-ttk/Regex/CreateUIDefinition/Output.regex.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<#
.Synopsis
Matches an Output containing an element
.Description
Matches a CreateUIDefinition output that contains an element name (references to steps or basics)
#>
param(
# The name of the control. By default, matches any control not in steps.
[string]
$ControlName = '\w+',

# The name of the step.
[string]
$StepName
)


if ($StepName) {
@"
steps\(
\s{0,}
'(?<StepName>$StepName)'
\s{0,}
\)
(?:
\.
(?<Property>\w+)
){0,}?(?=\z|\.${ControlName})
\.${ControlName}
"@
} else {
@"
basics\(
\s{0,}
'(?<ControlName>$ControlName)'
\s{0,}
\)
(?:
\.
(?<Property>\w+)
){0,}
"@
}
Loading

0 comments on commit 88fd790

Please sign in to comment.