From 0fc1cb12c753822e98421a8907cc0865662087e7 Mon Sep 17 00:00:00 2001 From: djpbessems Date: Tue, 11 May 2021 15:13:14 +0200 Subject: [PATCH] Remove ReferencedDisks;Add missing resourceType=17 XML items --- scripts/Update-OvfConfiguration.ps1 | 64 +++++++++++------------------ scripts/Update-OvfConfiguration.yml | 27 +++++------- 2 files changed, 35 insertions(+), 56 deletions(-) diff --git a/scripts/Update-OvfConfiguration.ps1 b/scripts/Update-OvfConfiguration.ps1 index 499a003..3a9d36a 100644 --- a/scripts/Update-OvfConfiguration.ps1 +++ b/scripts/Update-OvfConfiguration.ps1 @@ -49,42 +49,12 @@ $XML = [xml](Get-Content @GetContentSplat) $NS = [System.Xml.XmlNamespaceManager]$XML.NameTable [void]$NS.AddNamespace('ns', $XML.DocumentElement.xmlns) [void]$NS.AddNamespace('ovf', $XML.DocumentElement.ovf) +[void]$NS.AddNamespace('rasd', $XML.DocumentElement.rasd) -ForEach ($ReferencedDisk in $OVFConfig.Disks.Referenced) { - $XMLDisk = $XML.SelectSingleNode("//ns:DiskSection/ns:Disk[@ovf:diskId='$($ReferencedDisk.Id)']", $NS) - If ($XMLDisk.Count -eq 1) { - $FactorMap = @{ - 'KB' = [Math]::Pow(2,10) - 'MB' = [Math]::Pow(2,20) - 'GB' = [Math]::Pow(2,30) - 'TB' = [Math]::Pow(2,40) - 'PB' = [Math]::Pow(2,50) - } +# Create copy of default resourceType=17 (=Harddisk) node +$XMLDiskTemplate = $XML.SelectSingleNode("//ns:VirtualHardwareSection/ns:Item/rasd:ResourceType[.='17']", $NS).ParentNode.CloneNode($True) - If (($FactorMap.Keys -contains $ReferencedDisk.UnitSize)) { - # Convert new capacity to same scale as existing capacity - 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 - } - } -} -ForEach ($DynamicDisk in $OVFConfig.Disks.Dynamic) { +ForEach ($Disk in $OVFConfig.DynamicDisks) { # Determine next free available diskId $XMLDisks = $XML.SelectNodes("//ns:DiskSection/ns:Disk[contains(@ovf:diskId,'vmdisk')]", $NS) $DiskId = $XMLDisks.Count + 1 @@ -94,7 +64,7 @@ ForEach ($DynamicDisk in $OVFConfig.Disks.Dynamic) { $XMLDisk = $XML.CreateElement('Disk', $XML.DocumentElement.xmlns) $XMLDiskAttrUnits = $XML.CreateAttribute('capacityAllocationUnits', $XML.DocumentElement.ovf) - Switch ($DynamicDisk.UnitSize) { + Switch ($Disk.UnitSize) { 'KB' { $Powers = 10 } @@ -133,22 +103,36 @@ ForEach ($DynamicDisk in $OVFConfig.Disks.Dynamic) { [void]$XML.SelectSingleNode('//ns:DiskSection', $NS).AppendChild($XMLDisk) + # Add new resourceType nodes + $XMLDiskItem = $XMLDiskTemplate.CloneNode($True) + $XMLDiskItem.SelectSingleNode('rasd:AddressOnParent', $NS).InnerText = ($DiskId - 1) + $XMLDiskItem.SelectSingleNode('rasd:ElementName', $NS).InnerText = "Hard Disk $($DiskId)" + $XMLDiskItem.SelectSingleNode('rasd:HostResource', $NS).InnerText = "ovf:/disk/vmdisk$($DiskId)" + # Determine next free available InstanceID + $InstanceIDs = $XML.SelectNodes('//ns:VirtualHardwareSection/ns:Item/rasd:InstanceID', $NS) + $InstanceID = 1 + While ($InstanceIDs.InnerText -contains $InstanceID) { + $InstanceID++ + } + $XMLDiskItem.SelectSingleNode('rasd:InstanceID', $NS).InnerText = $InstanceID + $XML.SelectSingleNode('//ns:VirtualHardwareSection', $NS).AppendChild($XMLDiskItem) + $OVFConfig.PropertyCategories[0].ProductProperties += @{ Key = "vmconfig.disksize.$($DiskId)" - Type = If ([boolean]$DynamicDisk.Constraints.Minimum -or [boolean]$DynamicDisk.Constraints.Maximum) { - "Int($($DynamicDisk.Constraints.Minimum)..$($DynamicDisk.Constraints.Maximum))" + Type = If ([boolean]$Disk.Constraints.Minimum -or [boolean]$Disk.Constraints.Maximum) { + "Int($($Disk.Constraints.Minimum)..$($Disk.Constraints.Maximum))" } Else { 'Int' } Label = "Disk $($DiskId) size*" - Description = "$($DynamicDisk.Description) (in $($DynamicDisk.UnitSize))".Trim() - DefaultValue = "$($DynamicDisk.Constraints.Minimum)" + Description = "$($Disk.Description) (in $($Disk.UnitSize))".Trim() + DefaultValue = "$($Disk.Constraints.Minimum)" Configurations = '*' UserConfigurable = 'true' } } -Write-Host "Inserted $($OVFConfig.Disks.Dynamic.Count) new nodes into 'DiskSection'" +Write-Host "Inserted $($OVFConfig.DynamicDisks.Count) new node(s) into 'DiskSection' and 'VirtualHardwareSection' respectively" If ($OVFConfig.DeploymentConfigurations.Count -gt 0) { $XMLSection = $XML.CreateElement('DeploymentOptionSection', $XML.DocumentElement.xmlns) diff --git a/scripts/Update-OvfConfiguration.yml b/scripts/Update-OvfConfiguration.yml index 120dc65..5e1f1da 100644 --- a/scripts/Update-OvfConfiguration.yml +++ b/scripts/Update-OvfConfiguration.yml @@ -23,22 +23,17 @@ DeploymentConfigurations: Size: CPU: 4 Memory: 8192 -Disks: - Referenced: - - Id: vmdisk1 - UnitSize: GB - Capacity: 50 - Dynamic: - - Description: Data - UnitSize: GB - Constraints: - Minimum: 10 - Maximum: 500 - - Description: Scratch - UnitSize: GB - Constraints: - Minimum: 5 - Maximum: '' +DynamicDisks: +- Description: Data + UnitSize: GB + Constraints: + Minimum: 10 + Maximum: 500 +- Description: Scratch + UnitSize: GB + Constraints: + Minimum: 5 + Maximum: '' PropertyCategories: - Name: 0) Deployment information ProductProperties: