Simplify code (different method for XML attributes)
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Danny Bessems 2021-05-12 11:38:39 +02:00
parent ddf8b2e7cf
commit 8340b97a4d
1 changed files with 110 additions and 80 deletions

View File

@ -63,7 +63,7 @@ ForEach ($Disk in $OVFConfig.DynamicDisks) {
}
$XMLDisk = $XML.CreateElement('Disk', $XML.DocumentElement.xmlns)
$XMLDiskAttrUnits = $XML.CreateAttribute('capacityAllocationUnits', $XML.DocumentElement.ovf)
# $XMLDiskAttrUnits = $XML.CreateAttribute('capacityAllocationUnits', $XML.DocumentElement.ovf)
Switch ($Disk.UnitSize) {
'KB' {
$Powers = 10
@ -85,21 +85,26 @@ ForEach ($Disk in $OVFConfig.DynamicDisks) {
Continue
}
}
$XMLDiskAttrUnits.Value = "byte * 2^$($Powers)"
$XMLDiskAttrFormat = $XML.CreateAttribute('format', $XML.DocumentElement.ovf)
$XMLDiskAttrFormat.Value = 'http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized'
$XMLDiskId = $XML.CreateAttribute('diskId', $XML.DocumentElement.ovf)
$XMLDiskId.Value = "vmdisk$($DiskId)"
$XMLDiskAttrCapacity = $XML.CreateAttribute('capacity', $XML.DocumentElement.ovf)
$XMLDiskAttrCapacity.Value = '${{vmconfig.disksize.{0}}}' -f $DiskId
$XMLDiskAttrPopulated = $XML.CreateAttribute('populatedSize', $XML.DocumentElement.ovf)
$XMLDiskAttrPopulated.Value = 0
# $XMLDiskAttrUnits.Value = "byte * 2^$($Powers)"
[void]$XMLExtraConfig.SetAttribute('capacityAllocationUnits', $NS.LookupNamespace('ovf'), "byte * 2^$($Powers)")
# $XMLDiskAttrFormat = $XML.CreateAttribute('format', $XML.DocumentElement.ovf)
# $XMLDiskAttrFormat.Value = 'http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized'
[void]$XMLExtraConfig.SetAttribute('format', $NS.LookupNamespace('ovf'), 'http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized')
# $XMLDiskId = $XML.CreateAttribute('diskId', $XML.DocumentElement.ovf)
# $XMLDiskId.Value = "vmdisk$($DiskId)"
[void]$XMLExtraConfig.SetAttribute('diskId', $NS.LookupNamespace('ovf'), "vmdisk$($DiskId)")
# $XMLDiskAttrCapacity = $XML.CreateAttribute('capacity', $XML.DocumentElement.ovf)
# $XMLDiskAttrCapacity.Value = '${{vmconfig.disksize.{0}}}' -f $DiskId
[void]$XMLExtraConfig.SetAttribute('capacity', $NS.LookupNamespace('ovf'), '${{vmconfig.disksize.{0}}}' -f $DiskId)
# $XMLDiskAttrPopulated = $XML.CreateAttribute('populatedSize', $XML.DocumentElement.ovf)
# $XMLDiskAttrPopulated.Value = 0
[void]$XMLExtraConfig.SetAttribute('populatedSize', $NS.LookupNamespace('ovf'), 0)
[void]$XMLDisk.Attributes.Append($XMLDiskAttrUnits)
[void]$XMLDisk.Attributes.Append($XMLDiskAttrFormat)
[void]$XMLDisk.Attributes.Append($XMLDiskId)
[void]$XMLDisk.Attributes.Append($XMLDiskAttrCapacity)
[void]$XMLDisk.Attributes.Append($XMLDiskAttrPopulated)
# [void]$XMLDisk.Attributes.Append($XMLDiskAttrUnits)
# [void]$XMLDisk.Attributes.Append($XMLDiskAttrFormat)
# [void]$XMLDisk.Attributes.Append($XMLDiskId)
# [void]$XMLDisk.Attributes.Append($XMLDiskAttrCapacity)
# [void]$XMLDisk.Attributes.Append($XMLDiskAttrPopulated)
[void]$XML.SelectSingleNode('//ns:DiskSection', $NS).AppendChild($XMLDisk)
@ -143,8 +148,9 @@ If ($OVFConfig.DeploymentConfigurations.Count -gt 0) {
ForEach ($Configuration in $OVFConfig.DeploymentConfigurations) {
$XMLConfig = $XML.CreateElement('Configuration', $XML.DocumentElement.xmlns)
$XMLConfigAttrId = $XML.CreateAttribute('id', $XML.DocumentElement.ovf)
$XMLConfigAttrId.Value = $Configuration.Id
# $XMLConfigAttrId = $XML.CreateAttribute('id', $XML.DocumentElement.ovf)
# $XMLConfigAttrId.Value = $Configuration.Id
[void]$XMLExtraConfig.SetAttribute('id', $NS.LookupNamespace('ovf'), $Configuration.Id)
$XMLConfigLabel = $XML.CreateElement('Label', $XML.DocumentElement.xmlns)
$XMLConfigLabel.InnerText = $Configuration.Label
@ -152,7 +158,7 @@ If ($OVFConfig.DeploymentConfigurations.Count -gt 0) {
$XMLConfigDescription = $XML.CreateElement('Description', $XML.DocumentElement.xmlns)
$XMLConfigDescription.InnerText = $Configuration.Description
[void]$XMLConfig.Attributes.Append($XMLConfigAttrId)
# [void]$XMLConfig.Attributes.Append($XMLConfigAttrId)
[void]$XMLConfig.AppendChild($XMLConfigLabel)
[void]$XMLConfig.AppendChild($XMLConfigDescription)
@ -187,22 +193,26 @@ If ($OVFConfig.DeploymentConfigurations.Count -gt 0) {
}
}
$XMLAttrTransport = $XML.CreateAttribute('transport', $XML.DocumentElement.ovf)
$XMLAttrTransport.Value = 'com.vmware.guestInfo'
[void]$XML.SelectSingleNode('//ns:VirtualHardwareSection', $NS).Attributes.Append($XMLAttrTransport)
# $XMLAttrTransport = $XML.CreateAttribute('transport', $XML.DocumentElement.ovf)
# $XMLAttrTransport.Value = 'com.vmware.guestInfo'
# [void]$XML.SelectSingleNode('//ns:VirtualHardwareSection', $NS).Attributes.Append($XMLAttrTransport)
[void]$XML.SelectSingleNode('//ns:VirtualHardwareSection', $NS).SetAttribute('transport', $NS.LookupNamespace('ovf'), 'com.vmware.guestInfo')
ForEach ($ExtraConfig in $OVFConfig.AdvancedOptions) {
$XMLExtraConfig = $XML.CreateElement('vmw:ExtraConfig', $XML.DocumentElement.vmw)
$XMLExtraConfigAttrRequired = $XML.CreateAttribute('required', $XML.DocumentElement.ovf)
$XMLExtraConfigAttrRequired.Value = "$([boolean]$ExtraConfig.Required)".ToLower()
$XMLExtraConfigAttrKey = $XML.CreateAttribute('key', $XML.DocumentElement.vmw)
$XMLExtraConfigAttrKey.Value = $ExtraConfig.Key
$XMLExtraConfigAttrValue = $XML.CreateAttribute('value', $XML.DocumentElement.vmw)
$XMLExtraConfigAttrValue.Value = $ExtraConfig.Value
# $XMLExtraConfigAttrRequired = $XML.CreateAttribute('required', $XML.DocumentElement.ovf)
# $XMLExtraConfigAttrRequired.Value = "$([boolean]$ExtraConfig.Required)".ToLower()
# $XMLExtraConfigAttrKey = $XML.CreateAttribute('key', $XML.DocumentElement.vmw)
# $XMLExtraConfigAttrKey.Value = $ExtraConfig.Key
# $XMLExtraConfigAttrValue = $XML.CreateAttribute('value', $XML.DocumentElement.vmw)
# $XMLExtraConfigAttrValue.Value = $ExtraConfig.Value
[void]$XMLExtraConfig.Attributes.Append($XMLExtraConfigAttrRequired)
[void]$XMLExtraConfig.Attributes.Append($XMLExtraConfigAttrKey)
[void]$XMLExtraConfig.Attributes.Append($XMLExtraConfigAttrValue)
# [void]$XMLExtraConfig.Attributes.Append($XMLExtraConfigAttrRequired)
[void]$XMLExtraConfig.SetAttribute('required', $NS.LookupNamespace('ovf'), "$([boolean]$ExtraConfig.Required)".ToLower())
# [void]$XMLExtraConfig.Attributes.Append($XMLExtraConfigAttrKey)
[void]$XMLExtraConfig.SetAttribute('key', $NS.LookupNamespace('vmw'), $ExtraConfig.Key)
# [void]$XMLExtraConfig.Attributes.Append($XMLExtraConfigAttrValue)
[void]$XMLExtraConfig.SetAttribute('value', $NS.LookupNamespace('vmw'), $ExtraConfig.Value)
[void]$XML.SelectSingleNode('//ns:VirtualHardwareSection', $NS).AppendChild($XMLExtraConfig)
}
Write-Host "Added $($OVFConfig.AdvancedOptions.Count) 'vmw:ExtraConfig' nodes"
@ -234,15 +244,18 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
ForEach ($Property in $Category.ProductProperties) {
$XMLProperty = $XML.CreateElement('Property', $XML.DocumentElement.xmlns)
$XMLPropertyAttrKey = $XML.CreateAttribute('key', $XML.DocumentElement.ovf)
$XMLPropertyAttrKey.Value = $Property.Key
$XMLPropertyAttrType = $XML.CreateAttribute('type', $XML.DocumentElement.ovf)
# $XMLPropertyAttrKey = $XML.CreateAttribute('key', $XML.DocumentElement.ovf)
# $XMLPropertyAttrKey.Value = $Property.Key
$XMLProperty.SetAttribute('key', $NS.LookupNamespace('ovf'), $Property.Key)
# $XMLPropertyAttrType = $XML.CreateAttribute('type', $XML.DocumentElement.ovf)
Switch -regex ($Property.Type) {
'^boolean' {
$XMLPropertyAttrType.Value = 'boolean'
# $XMLPropertyAttrType.Value = 'boolean'
$XMLProperty.SetAttribute('type', $NS.LookupNamespace('ovf'), 'boolean')
}
'^int' {
$XMLPropertyAttrType.Value = 'uint16'
# $XMLPropertyAttrType.Value = 'uint16'
$XMLProperty.SetAttribute('type', $NS.LookupNamespace('ovf'), 'uint16')
$Qualifiers = @()
If ($Property.Type -match '^int\((\d*)\.\.(\d*)\)') {
If ($Matches[1]) {
@ -251,22 +264,27 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
If ($Matches[2]) {
$Qualifiers += "MaxValue($($Matches[2]))"
}
$XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
$XMLPropertyAttrQualifiers.Value = $Qualifiers -join ' '
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
# $XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
# $XMLPropertyAttrQualifiers.Value = $Qualifiers -join ' '
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
$XMLProperty.SetAttribute('qualifiers', $NS.LookupNamespace('ovf'), $Qualifiers -join ' ')
}
}
'^ip' {
$XMLPropertyAttrType.Value = 'string'
$XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.vmw)
$XMLPropertyAttrQualifiers.Value = 'Ip'
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
# $XMLPropertyAttrType.Value = 'string'
$XMLProperty.SetAttribute('type', $NS.LookupNamespace('ovf'), 'string')
# $XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.vmw)
# $XMLPropertyAttrQualifiers.Value = 'Ip'
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
$XMLProperty.SetAttribute('qualifiers', $NS.LookupNamespace('vmw'), 'Ip')
}
'^password' {
$XMLPropertyAttrType.Value = 'string'
$XMLPropertyAttrPassword = $XML.CreateAttribute('password', $XML.DocumentElement.ovf)
$XMLPropertyAttrPassword.Value = 'true'
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrPassword)
# $XMLPropertyAttrType.Value = 'string'
$XMLProperty.SetAttribute('type', $NS.LookupNamespace('ovf'), 'string')
# $XMLPropertyAttrPassword = $XML.CreateAttribute('password', $XML.DocumentElement.ovf)
# $XMLPropertyAttrPassword.Value = 'true'
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrPassword)
$XMLProperty.SetAttribute('password', $NS.LookupNamespace('ovf'), 'true')
$Qualifiers = @()
If ($Property.Type -match '^password\((\d*)\.\.(\d*)\)') {
@ -276,13 +294,15 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
If ($Matches[2]) {
$Qualifiers += "MaxLen($($Matches[2]))"
}
$XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
$XMLPropertyAttrQualifiers.Value = $Qualifiers -join ' '
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
# $XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
# $XMLPropertyAttrQualifiers.Value = $Qualifiers -join ' '
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
$XMLProperty.SetAttribute('qualifiers', $NS.LookupNamespace('ovf'), $Qualifiers -join ' ')
}
}
'^string' {
$XMLPropertyAttrType.Value = 'string'
# $XMLPropertyAttrType.Value = 'string'
$XMLProperty.SetAttribute('type', $NS.LookupNamespace('ovf'), 'string')
$Qualifiers = @()
If ($Property.Type -match '^string\((\d*)\.\.(\d*)\)') {
If ($Matches[1]) {
@ -291,28 +311,33 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
If ($Matches[2]) {
$Qualifiers += "MaxLen($($Matches[2]))"
}
$XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
$XMLPropertyAttrQualifiers.Value = $Qualifiers -join ' '
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
# $XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
# $XMLPropertyAttrQualifiers.Value = $Qualifiers -join ' '
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
$XMLProperty.SetAttribute('qualifiers', $NS.LookupNamespace('ovf'), $Qualifiers -join ' ')
} ElseIf ($Property.Type -match '^string\[(.*)\]') {
$XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
$XMLPropertyAttrQualifiers.Value = "ValueMap{$($Matches[1] -replace '","', '", "')}"
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
# $XMLPropertyAttrQualifiers = $XML.CreateAttribute('qualifiers', $XML.DocumentElement.ovf)
# $XMLPropertyAttrQualifiers.Value = "ValueMap{$($Matches[1] -replace '","', '", "')}"
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrQualifiers)
$XMLProperty.SetAttribute('qualifiers', $NS.LookupNamespace('ovf'), "ValueMap{$($Matches[1] -replace '","', '", "')}")
}
}
}
$XMLPropertyAttrUserConfigurable = $XML.CreateAttribute('userConfigurable', $XML.DocumentElement.ovf)
$XMLPropertyAttrUserConfigurable.Value = "$([boolean]$Property.UserConfigurable)".ToLower()
$XMLPropertyAttrValue = $XML.CreateAttribute('value', $XML.DocumentElement.ovf)
# $XMLPropertyAttrUserConfigurable = $XML.CreateAttribute('userConfigurable', $XML.DocumentElement.ovf)
# $XMLPropertyAttrUserConfigurable.Value = "$([boolean]$Property.UserConfigurable)".ToLower()
$XMLProperty.SetAttribute('userConfigurable', $NS.LookupNamespace('ovf'), "$([boolean]$Property.UserConfigurable)".ToLower())
# $XMLPropertyAttrValue = $XML.CreateAttribute('value', $XML.DocumentElement.ovf)
If ($Property.Type -eq 'boolean') {
$XMLPropertyAttrValue.Value = "$([boolean]$Property.DefaultValue)".ToLower()
# $XMLPropertyAttrValue.Value = "$([boolean]$Property.DefaultValue)".ToLower()
$XMLProperty.SetAttribute('value', $NS.LookupNamespace('ovf'), "$([boolean]$Property.DefaultValue)".ToLower())
} Else {
$XMLPropertyAttrValue.Value = $Property.DefaultValue
# $XMLPropertyAttrValue.Value = $Property.DefaultValue
$XMLProperty.SetAttribute('value', $NS.LookupNamespace('ovf'), $Property.DefaultValue)
}
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrKey)
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrType)
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrUserConfigurable)
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrValue)
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrKey)
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrType)
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrUserConfigurable)
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrValue)
If ($Property.Label) {
$XMLPropertyLabel = $XML.CreateElement('Label', $XML.DocumentElement.xmlns)
@ -326,30 +351,35 @@ ForEach ($Category in $OVFConfig.PropertyCategories) {
}
If (($Property.Configurations.Count -eq 1) -and ($Property.Configurations -eq '*')) {
$XMLPropertyAttrConfiguration = $XML.CreateAttribute('configuration', $XML.DocumentElement.ovf)
$XMLPropertyAttrConfiguration.Value = $OVFConfig.DeploymentConfigurations.Id -join ' '
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrConfiguration)
# $XMLPropertyAttrConfiguration = $XML.CreateAttribute('configuration', $XML.DocumentElement.ovf)
# $XMLPropertyAttrConfiguration.Value = $OVFConfig.DeploymentConfigurations.Id -join ' '
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrConfiguration)
$XMLProperty.SetAttribute('configuration', $NS.LookupNamespace('ovf'), $OVFConfig.DeploymentConfigurations.Id -join ' ')
} ElseIf ($Property.Configurations.Count -gt 0) {
$XMLPropertyAttrConfiguration = $XML.CreateAttribute('configuration', $XML.DocumentElement.ovf)
$XMLPropertyAttrConfiguration.Value = $Property.Configurations -join ' '
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrConfiguration)
# $XMLPropertyAttrConfiguration = $XML.CreateAttribute('configuration', $XML.DocumentElement.ovf)
# $XMLPropertyAttrConfiguration.Value = $Property.Configurations -join ' '
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrConfiguration)
$XMLProperty.SetAttribute('configuration', $NS.LookupNamespace('ovf'), $Property.Configurations -join ' ')
}
If ($Property.Value.Count -eq 1) {
$XMLPropertyAttrValue = $XML.CreateAttribute('value', $XML.DocumentElement.ovf)
$XMLPropertyAttrValue.Value = $Property.Value
[void]$XMLProperty.Attributes.Append($XMLPropertyAttrValue)
# $XMLPropertyAttrValue = $XML.CreateAttribute('value', $XML.DocumentElement.ovf)
# $XMLPropertyAttrValue.Value = $Property.Value
# [void]$XMLProperty.Attributes.Append($XMLPropertyAttrValue)
$XMLProperty.SetAttribute('value', $NS.LookupNamespace('ovf'), $Property.Value)
} ElseIf ($Property.Value.Count -gt 1) {
ForEach ($Value in $Property.Value) {
$XMLValue = $XML.CreateElement('Value', $XML.DocumentElement.xmlns)
$XMLValueAttrValue = $XML.CreateAttribute('value', $XML.DocumentElement.ovf)
$XMLValueAttrValue.Value = $Value
$XMLValueAttrConfiguration = $XML.CreateAttribute('configuration', $XML.DocumentElement.ovf)
$XMLValueAttrConfiguration.Value = $Value
# $XMLValueAttrValue = $XML.CreateAttribute('value', $XML.DocumentElement.ovf)
# $XMLValueAttrValue.Value = $Value
$XMLProperty.SetAttribute('value', $NS.LookupNamespace('ovf'), $Value)
# $XMLValueAttrConfiguration = $XML.CreateAttribute('configuration', $XML.DocumentElement.ovf)
# $XMLValueAttrConfiguration.Value = $Value
$XMLProperty.SetAttribute('configuration', $NS.LookupNamespace('ovf'), $Value)
[void]$XMLValue.Attributes.Append($XMLValueAttrValue)
[void]$XMLValue.Attributes.Append($XMLValueAttrConfiguration)
# [void]$XMLValue.Attributes.Append($XMLValueAttrValue)
# [void]$XMLValue.Attributes.Append($XMLValueAttrConfiguration)
[void]$XMLProperty.AppendChild($XMLValue)
}