Append new nodes into XML parent node; Fix capacity comparison.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Danny Bessems 2021-05-10 23:13:04 +02:00
parent a7ded04317
commit d1413040fe

View File

@ -53,31 +53,34 @@ $NS = [System.Xml.XmlNamespaceManager]$XML.NameTable
ForEach ($ReferencedDisk in $OVFConfig.Disks.Referenced) { ForEach ($ReferencedDisk in $OVFConfig.Disks.Referenced) {
$XMLDisk = $XML.SelectSingleNode("//ns:DiskSection/ns:Disk[@ovf:diskId='$($ReferencedDisk.Id)']", $NS) $XMLDisk = $XML.SelectSingleNode("//ns:DiskSection/ns:Disk[@ovf:diskId='$($ReferencedDisk.Id)']", $NS)
If ($XMLDisk.Count -eq 1) { If ($XMLDisk.Count -eq 1) {
Switch ($ReferencedDisk.UnitSize) { $FactorMap = @{
'KB' { 'KB' = [Math]::Pow(2,10)
$CapacityInBytes = $ReferencedDisk.Capacity * 1KB 'MB' = [Math]::Pow(2,20)
} 'GB' = [Math]::Pow(2,30)
'MB' { 'TB' = [Math]::Pow(2,40)
$CapacityInBytes = $ReferencedDisk.Capacity * 1MB 'PB' = [Math]::Pow(2,50)
}
'GB' {
$CapacityInBytes = $ReferencedDisk.Capacity * 1GB
}
'TB' {
$CapacityInBytes = $ReferencedDisk.Capacity * 1TB
}
'PB' {
$CapacityInBytes = $ReferencedDisk.Capacity * 1PB
}
default {
# Invalid UnitSize; skipping modification of existing disk
Continue
}
} }
# Determine if specified size is equal or larger than allocated size If (($FactorMap.Keys -contains $ReferencedDisk.UnitSize)) {
If ($XMLDisk.populatedSize -le $CapacityInBytes) { # Convert new capacity to same scale as existing capacity
[void]$XMLDisk.SetAttribute('ovf:capacity', $CapacityInBytes) If ($XMLDisk.capacityAllocationUnits -match 'byte \* 2\^(\d+)') {
$NewSize = [Math]::Truncate($ReferencedDisk.Capacity * $FactorMap[$ReferencedDisk.UnitSize] / [Math]::Pow(2,$Matches[1]))
}
ElseIf ($XMLDisk.capacityAllocationUnits -eq 'byte') {
$NewSize = $ReferencedDisk.Capacity * $FactorMap[$ReferencedDisk.UnitSize]
}
Else {
Throw "Unexpected value '$($XMLDisk.capacityAllocationUnits)' for property 'capacityAllocationUnits'; aborting."
}
# Determine if specified capacity is equal or larger than current & allocated capacity
If ($XMLDisk.populatedSize -le $NewSize -and $XMLDisk.capacity -le $NewSize) {
[void]$XMLDisk.SetAttribute('ovf:capacity', $NewSize)
Write-Host "Updated 'Disk' with new property value"
}
}
Else {
# Invalid UnitSize; skipping modification of existing disk
Continue
} }
} }
} }
@ -128,6 +131,8 @@ ForEach ($DynamicDisk in $OVFConfig.Disks.Dynamic) {
[void]$XMLDisk.Attributes.Append($XMLDiskAttrCapacity) [void]$XMLDisk.Attributes.Append($XMLDiskAttrCapacity)
[void]$XMLDisk.Attributes.Append($XMLDiskAttrPopulated) [void]$XMLDisk.Attributes.Append($XMLDiskAttrPopulated)
[void]$XML.SelectSingleNode('//ns:DiskSection', $NS).AppendChild($XMLDisk)
$OVFConfig.PropertyCategories[0].ProductProperties += @{ $OVFConfig.PropertyCategories[0].ProductProperties += @{
Key = "vmconfig.disksize.$($DiskId)" Key = "vmconfig.disksize.$($DiskId)"
Type = If ([boolean]$DynamicDisk.Constraints.Minimum -or [boolean]$DynamicDisk.Constraints.Maximum) { Type = If ([boolean]$DynamicDisk.Constraints.Minimum -or [boolean]$DynamicDisk.Constraints.Maximum) {
@ -143,6 +148,7 @@ ForEach ($DynamicDisk in $OVFConfig.Disks.Dynamic) {
UserConfigurable = 'true' UserConfigurable = 'true'
} }
} }
Write-Host "Inserted $($OVFConfig.Disks.Dynamic.Count) new nodes into 'DiskSection'"
If ($OVFConfig.DeploymentConfigurations.Count -gt 0) { If ($OVFConfig.DeploymentConfigurations.Count -gt 0) {
$XMLSection = $XML.CreateElement('DeploymentOptionSection', $XML.DocumentElement.xmlns) $XMLSection = $XML.CreateElement('DeploymentOptionSection', $XML.DocumentElement.xmlns)