| 1 |
#!powershell |
| 2 |
|
| 3 |
# Copyright: (c) 2018, Ansible Project |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
#Requires -Module Ansible.ModuleUtils.Legacy |
| 7 |
#Requires -Module Ansible.ModuleUtils.Backup |
| 8 |
|
| 9 |
Set-StrictMode -Version 2 |
| 10 |
|
| 11 |
function Copy-Xml($dest, $src, $xmlorig) { |
| 12 |
if ($src.get_NodeType() -eq "Text") { |
| 13 |
$dest.set_InnerText($src.get_InnerText()) |
| 14 |
} |
| 15 |
|
| 16 |
if ($src.get_HasAttributes()) { |
| 17 |
foreach ($attr in $src.get_Attributes()) { |
| 18 |
$dest.SetAttribute($attr.get_Name(), $attr.get_Value()) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
if ($src.get_HasChildNodes()) { |
| 23 |
foreach ($childnode in $src.get_ChildNodes()) { |
| 24 |
if ($childnode.get_NodeType() -eq "Element") { |
| 25 |
$newnode = $xmlorig.CreateElement($childnode.get_Name(), $xmlorig.get_DocumentElement().get_NamespaceURI()) |
| 26 |
Copy-Xml -dest $newnode -src $childnode -xmlorig $xmlorig |
| 27 |
$dest.AppendChild($newnode) | Out-Null |
| 28 |
} |
| 29 |
elseif ($childnode.get_NodeType() -eq "Text") { |
| 30 |
$dest.set_InnerText($childnode.get_InnerText()) |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
function Compare-XmlDoc($actual, $expected) { |
| 37 |
if ($actual.get_Name() -ne $expected.get_Name()) { |
| 38 |
throw "Actual name not same as expected: actual=" + $actual.get_Name() + ", expected=" + $expected.get_Name() |
| 39 |
} |
| 40 |
##attributes... |
| 41 |
|
| 42 |
if (($actual.get_NodeType() -eq "Element") -and ($expected.get_NodeType() -eq "Element")) { |
| 43 |
if ($actual.get_HasAttributes() -and $expected.get_HasAttributes()) { |
| 44 |
if ($actual.get_Attributes().Count -ne $expected.get_Attributes().Count) { |
| 45 |
throw "attribute mismatch for actual=" + $actual.get_Name() |
| 46 |
} |
| 47 |
for ($i = 0; $i -lt $expected.get_Attributes().Count; $i = $i + 1) { |
| 48 |
if ($expected.get_Attributes()[$i].get_Name() -ne $actual.get_Attributes()[$i].get_Name()) { |
| 49 |
throw "attribute name mismatch for actual=" + $actual.get_Name() |
| 50 |
} |
| 51 |
if ($expected.get_Attributes()[$i].get_Value() -ne $actual.get_Attributes()[$i].get_Value()) { |
| 52 |
throw "attribute value mismatch for actual=" + $actual.get_Name() |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if (($actual.get_HasAttributes() -and !$expected.get_HasAttributes()) -or (!$actual.get_HasAttributes() -and $expected.get_HasAttributes())) { |
| 58 |
throw "attribute presence mismatch for actual=" + $actual.get_Name() |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
##children |
| 63 |
if ($expected.get_ChildNodes().Count -ne $actual.get_ChildNodes().Count) { |
| 64 |
throw "child node mismatch. for actual=" + $actual.get_Name() |
| 65 |
} |
| 66 |
|
| 67 |
for ($i = 0; $i -lt $expected.get_ChildNodes().Count; $i = $i + 1) { |
| 68 |
if (-not $actual.get_ChildNodes()[$i]) { |
| 69 |
throw "actual missing child nodes. for actual=" + $actual.get_Name() |
| 70 |
} |
| 71 |
Compare-XmlDoc $expected.get_ChildNodes()[$i] $actual.get_ChildNodes()[$i] |
| 72 |
} |
| 73 |
|
| 74 |
if ($expected.get_InnerText()) { |
| 75 |
if ($expected.get_InnerText() -ne $actual.get_InnerText()) { |
| 76 |
throw "inner text mismatch for actual=" + $actual.get_Name() |
| 77 |
} |
| 78 |
} |
| 79 |
elseif ($actual.get_InnerText()) { |
| 80 |
throw "actual has inner text but expected does not for actual=" + $actual.get_Name() |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
function Save-ChangedXml($xmlorig, $result, $message, $check_mode, $backup) { |
| 86 |
$result.changed = $true |
| 87 |
if (-Not $check_mode) { |
| 88 |
if ($backup) { |
| 89 |
$result.backup_file = Backup-File -path $dest -WhatIf:$check_mode |
| 90 |
} |
| 91 |
$xmlorig.Save($dest) |
| 92 |
$result.msg = $message |
| 93 |
} |
| 94 |
else { |
| 95 |
$result.msg += " check mode" |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
$params = Parse-Args $args -supports_check_mode $true |
| 100 |
$check_mode = Get-AnsibleParam -obj $params -name "_ansible_check_mode" -type "bool" -default $false |
| 101 |
|
| 102 |
$debug_level = Get-AnsibleParam -obj $params -name "_ansible_verbosity" -type "int" |
| 103 |
$debug = $debug_level -gt 2 |
| 104 |
|
| 105 |
$dest = Get-AnsibleParam $params "path" -type "path" -FailIfEmpty $true -aliases "dest", "file" |
| 106 |
$fragment = Get-AnsibleParam $params "fragment" -type "str" -aliases "xmlstring" |
| 107 |
$xpath = Get-AnsibleParam $params "xpath" -type "str" -FailIfEmpty $true |
| 108 |
$backup = Get-AnsibleParam $params "backup" -type "bool" -Default $false |
| 109 |
$type = Get-AnsibleParam $params "type" -type "str" -Default "element" -ValidateSet "element", "attribute", "text" |
| 110 |
$attribute = Get-AnsibleParam $params "attribute" -type "str" -FailIfEmpty ($type -eq "attribute") |
| 111 |
$state = Get-AnsibleParam $params "state" -type "str" -Default "present" |
| 112 |
$count = Get-AnsibleParam $params "count" -type "bool" -Default $false |
| 113 |
|
| 114 |
$result = @{ |
| 115 |
changed = $false |
| 116 |
} |
| 117 |
|
| 118 |
If (-Not (Test-Path -LiteralPath $dest -PathType Leaf)) { |
| 119 |
Fail-Json $result "Specified path $dest does not exist or is not a file." |
| 120 |
} |
| 121 |
|
| 122 |
$xmlorig = New-Object -TypeName System.Xml.XmlDocument |
| 123 |
$xmlorig.XmlResolver = $null |
| 124 |
Try { |
| 125 |
$xmlorig.Load($dest) |
| 126 |
} |
| 127 |
Catch { |
| 128 |
Fail-Json $result "Failed to parse file at '$dest' as an XML document: $($_.Exception.Message)" |
| 129 |
} |
| 130 |
|
| 131 |
$namespaceMgr = New-Object System.Xml.XmlNamespaceManager $xmlorig.NameTable |
| 132 |
$namespace = $xmlorig.DocumentElement.NamespaceURI |
| 133 |
$localname = $xmlorig.DocumentElement.LocalName |
| 134 |
|
| 135 |
$namespaceMgr.AddNamespace($xmlorig.$localname.SchemaInfo.Prefix, $namespace) |
| 136 |
|
| 137 |
$nodeList = $xmlorig.SelectNodes($xpath, $namespaceMgr) |
| 138 |
$nodeListCount = $nodeList.get_Count() |
| 139 |
if ($count) { |
| 140 |
$result.count = $nodeListCount |
| 141 |
if (-not $fragment) { |
| 142 |
Exit-Json $result |
| 143 |
} |
| 144 |
} |
| 145 |
## Exit early if xpath did not match any nodes |
| 146 |
if ($nodeListCount -eq 0) { |
| 147 |
$result.msg = "The supplied xpath did not match any nodes. If this is unexpected, check your xpath is valid for the xml file at supplied path." |
| 148 |
Exit-Json $result |
| 149 |
} |
| 150 |
|
| 151 |
$changed = $false |
| 152 |
$result.msg = "not changed" |
| 153 |
|
| 154 |
if ($type -eq "element") { |
| 155 |
if ($state -eq "absent") { |
| 156 |
|
| 157 |
$removals = [System.Collections.Generic.List[String]]@() |
| 158 |
|
| 159 |
foreach ($node in $nodeList) { |
| 160 |
# there are some nodes that match xpath, delete without comparing them to fragment |
| 161 |
if (-Not $check_mode) { |
| 162 |
[void]$node.get_ParentNode().RemoveChild($node) |
| 163 |
$changed = $true |
| 164 |
} |
| 165 |
|
| 166 |
if ($debug) { |
| 167 |
$removals.Add($node.get_OuterXml()) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
if ($removals) { |
| 172 |
$result.removed = $removals -join ", " |
| 173 |
} |
| 174 |
} |
| 175 |
else { |
| 176 |
# state -eq 'present' |
| 177 |
$xmlfragment = $null |
| 178 |
Try { |
| 179 |
$xmlfragment = [xml]$fragment |
| 180 |
} |
| 181 |
Catch { |
| 182 |
Fail-Json $result "Failed to parse fragment as XML: $($_.Exception.Message)" |
| 183 |
} |
| 184 |
|
| 185 |
foreach ($node in $nodeList) { |
| 186 |
$candidate = $xmlorig.CreateElement($xmlfragment.get_DocumentElement().get_Name(), $xmlorig.get_DocumentElement().get_NamespaceURI()) |
| 187 |
Copy-Xml -dest $candidate -src $xmlfragment.DocumentElement -xmlorig $xmlorig |
| 188 |
|
| 189 |
if ($node.get_NodeType() -eq "Document") { |
| 190 |
$node = $node.get_DocumentElement() |
| 191 |
} |
| 192 |
|
| 193 |
if ($node.ChildNodes.Count -eq 0) { |
| 194 |
$elements = @($node) |
| 195 |
} |
| 196 |
else { |
| 197 |
$elements = $node.get_ChildNodes() |
| 198 |
} |
| 199 |
|
| 200 |
[bool]$present = $false |
| 201 |
[bool]$changed = $false |
| 202 |
if ($elements.get_Count()) { |
| 203 |
if ($debug) { |
| 204 |
$err = @() |
| 205 |
$result.err = { $err }.Invoke() |
| 206 |
} |
| 207 |
foreach ($element in $elements) { |
| 208 |
try { |
| 209 |
Compare-XmlDoc $candidate $element |
| 210 |
$present = $true |
| 211 |
break |
| 212 |
} |
| 213 |
catch { |
| 214 |
if ($debug) { |
| 215 |
$result.err.Add($_.Exception.ToString()) |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
if (-Not $present -and ($state -eq "present")) { |
| 220 |
[void]$node.AppendChild($candidate) |
| 221 |
$result.msg = $result.msg + "xml added " |
| 222 |
$changed = $true |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
elseif ($type -eq "text") { |
| 229 |
foreach ($node in $nodeList) { |
| 230 |
if ($node.get_InnerText() -ne $fragment) { |
| 231 |
$node.set_InnerText($fragment) |
| 232 |
$changed = $true |
| 233 |
} |
| 234 |
} |
| 235 |
} |
| 236 |
elseif ($type -eq "attribute") { |
| 237 |
foreach ($node in $nodeList) { |
| 238 |
if ($state -eq 'present') { |
| 239 |
if ($node.NodeType -eq 'Attribute') { |
| 240 |
if ($node.Name -eq $attribute -and $node.Value -ne $fragment ) { |
| 241 |
# this is already the attribute with the right name, so just set its value to match fragment |
| 242 |
$node.Value = $fragment |
| 243 |
$changed = $true |
| 244 |
} |
| 245 |
} |
| 246 |
else { |
| 247 |
# assume NodeType is Element |
| 248 |
if (!$node.HasAttribute($attribute) -or ($node.$attribute -ne $fragment)) { |
| 249 |
if (!$node.HasAttribute($attribute)) { |
| 250 |
# add attribute to Element if missing |
| 251 |
$node.SetAttributeNode($attribute, $xmlorig.get_DocumentElement().get_NamespaceURI()) |
| 252 |
} |
| 253 |
#set the attribute into the element |
| 254 |
$node.SetAttribute($attribute, $fragment) |
| 255 |
$changed = $true |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
elseif ($state -eq 'absent') { |
| 260 |
if ($node.NodeType -eq 'Attribute') { |
| 261 |
$attrNode = [System.Xml.XmlAttribute]$node |
| 262 |
$parent = $attrNode.OwnerElement |
| 263 |
$parent.RemoveAttribute($attribute) |
| 264 |
$changed = $true |
| 265 |
} |
| 266 |
else { |
| 267 |
# element node processing |
| 268 |
if ($node.Name -eq $attribute ) { |
| 269 |
# note not caring about the state of 'fragment' at this point |
| 270 |
$node.RemoveAttribute($attribute) |
| 271 |
$changed = $true |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
else { |
| 276 |
Add-Warning $result "Unexpected state when processing attribute $($attribute), add was $add, state was $state" |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
if ($changed) { |
| 281 |
if ($state -eq "absent") { |
| 282 |
$summary = "$type removed" |
| 283 |
} |
| 284 |
else { |
| 285 |
$summary = "$type changed" |
| 286 |
} |
| 287 |
Save-ChangedXml -xmlorig $xmlorig -result $result -message $summary -check_mode $check_mode -backup $backup |
| 288 |
} |
| 289 |
|
| 290 |
Exit-Json $result |