Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Jefajers authored Feb 23, 2024
1 parent bbadd47 commit e03dc3e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
56 changes: 32 additions & 24 deletions src/functions/Invoke-AzOpsPush.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -343,32 +343,40 @@
}
if ($DeleteSetContents -and $deleteSet) {
Write-AzOpsMessage -LogLevel Important -LogString 'Invoke-AzOpsPush.Change.Delete'
# Unique delimiter used to join, split and replace data in DeleteSetContents
$delimiter = (New-Guid).Guid
# Transform $DeleteSetContents for further processing
$DeleteSetContents = $DeleteSetContents -join $delimiter -split "$delimiter-- " -replace $delimiter,""
# Process each $deleteSet $item
foreach ($item in $deleteSet) {
Write-AzOpsMessage -LogLevel Important -LogString 'Invoke-AzOpsPush.Change.Delete.File' -LogStringValues $item
# Processing each $deleteSet, compare it to each $DeleteSetContents
foreach ($content in $DeleteSetContents) {
if ($content.Contains($item)) {
# Transform original first line in content with missing delimiter
if ($content.StartsWith("-- ")) {
$jsonValue = $content.replace("-- $item", "")
}
# Transform remaining content
else {
$jsonValue = $content.replace($item, "")
# Iterate through each line in $DeleteSetContents
for ($i = 0; $i -lt $DeleteSetContents.Count; $i++) {
$line = $DeleteSetContents[$i].Trim()
# Check if the line starts with '-- ' and matches any filename in $deleteSet
if ($line -match '^-- (.+)$') {
$fileName = $matches[1]
if ($deleteSet -contains $fileName) {
Write-AzOpsMessage -LogLevel Important -LogString 'Invoke-AzOpsPush.Change.Delete.File' -LogStringValues $fileName
# Collect lines until the next line starting with '--'
$objectLines = @($line)
$i++
while ($i -lt $DeleteSetContents.Count) {
$currentLine = $DeleteSetContents[$i].Trim()
# Check if the line starts with '-- ' followed by any filename in $deleteSet
if ($currentLine -match '^-- (.+)$' -and $deleteSet -contains $matches[1]) {
$i--
Write-AzOpsMessage -LogLevel InternalComment -LogString 'Invoke-AzOpsPush.Change.Delete.NextTempFile' -LogStringValues $currentLine
break # Exit the loop if the line starts with '-- ' and matches a filename in $deleteSet
}
$objectLines += $currentLine
$i++
}
# When processed as designed there is no file present in the running branch. To run a removal AzOps re-creates the file and content based on $DeleteSetContents momentarily for processing, it is disregarded afterwards.
if (-not(Test-Path -Path (Split-Path -Path $item))) {
Write-AzOpsMessage -LogLevel InternalComment -LogString 'Invoke-AzOpsPush.Change.Delete.TempFile' -LogStringValues $item
New-Item -Path (Split-Path -Path $item) -ItemType Directory | Out-Null
# When processed as designed there is no file present in the running branch.
# To run a removal AzOps re-creates the file and content based on $DeleteSetContents momentarily for processing, it is disregarded afterwards.
if (-not(Test-Path -Path (Split-Path -Path $fileName))) {
Write-AzOpsMessage -LogLevel InternalComment -LogString 'Invoke-AzOpsPush.Change.Delete.TempFile' -LogStringValues $fileName
New-Item -Path (Split-Path -Path $fileName) -ItemType Directory | Out-Null
}
# Update item
Write-AzOpsMessage -LogLevel InternalComment -LogString 'Invoke-AzOpsPush.Change.Delete.SetTempFileContent' -LogStringValues $item, $jsonValue
Set-Content -Path $item -Value $jsonValue
# Create $fileName and set $content
$objectLines = $objectLines[1..$objectLines.Count]
$content = $objectLines.replace("-- $fileName", "") -join "`r`n"
Write-AzOpsMessage -LogLevel InternalComment -LogString 'Invoke-AzOpsPush.Change.Delete.SetTempFileContent' -LogStringValues $fileName, $content
Set-Content -Path $fileName -Value $content
$i-- # Move back one step to process the next line properly
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/localized/en-us/Strings.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@
'Invoke-AzOpsPush.Change.AddModify' = 'Adding or modifying:' #
'Invoke-AzOpsPush.Change.AddModify.File' = ' {0}' # $item
'Invoke-AzOpsPush.Change.Delete' = 'Deleting:' #
'Invoke-AzOpsPush.Change.Delete.File' = ' {0}' # $item
'Invoke-AzOpsPush.Change.Delete.TempFile' = 'Creating temporary file for deletion processing: {0}' # $item
'Invoke-AzOpsPush.Change.Delete.SetTempFileContent' = 'Set temporary file content: {{1}}, in {{0}}' # $item, $jsonValue
'Invoke-AzOpsPush.Change.Delete.File' = ' {0}' # $fileName
'Invoke-AzOpsPush.Change.Delete.TempFile' = 'Creating temporary file dir for deletion processing: {0}' # $fileName
'Invoke-AzOpsPush.Change.Delete.NextTempFile' = 'Exiting while loop, next file detected in $DeleteSetContents for deletion processing based on this content line: [{0}]' # $currentLine
'Invoke-AzOpsPush.Change.Delete.SetTempFileContent' = 'Set temporary file content: [{1}], in [{0}]' # $fileName, $jsonValue
'Invoke-AzOpsPush.Deletion.Failed' = 'Deletion of resources {0}, has failed using templates: {1}, {2}, this could be due to delayed deletion acceptance from Azure, please investigate and take action.' # $fail.FullyQualifiedResourceId, $fail.TemplateFilePath, $fail.TemplateParameterFilePath
'Invoke-AzOpsPush.Deletion.Retry' = 'Deletion of {0} resources unsuccessful, initiate final retry combination.' # $retry.Count
'Invoke-AzOpsPush.Deploy.ProviderFeature' = 'Invoking new state deployment - *.providerfeatures.json for a file {0}' # $addition
Expand Down

0 comments on commit e03dc3e

Please sign in to comment.