Skip to content

Commit

Permalink
api: Support encode/decode OVF to JSON
Browse files Browse the repository at this point in the history
This patch adds support for encoding an `ovf.Envelope` to JSON
using Golang's `encoding/json` package. The resulting output can
be safely decoded back to an `ovf.Envelope`. This decoded envelope
is deep-equal to the one originally decoded from XML.

This feature allows clients to format an OVF envelope as YAML, such
as when part of a Kubernetes API.

Signed-off-by: akutz <akutz@vmware.com>
  • Loading branch information
akutz committed Dec 19, 2024
1 parent 04ddb84 commit 0af06a3
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 191 deletions.
174 changes: 87 additions & 87 deletions ovf/cim.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,104 +70,104 @@ Source: http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2.24.0/CIM_VirtualSystem
*/

type CIMVirtualSystemSettingData struct {
ElementName string `xml:"ElementName"`
InstanceID string `xml:"InstanceID"`

AutomaticRecoveryAction *uint8 `xml:"AutomaticRecoveryAction"`
AutomaticShutdownAction *uint8 `xml:"AutomaticShutdownAction"`
AutomaticStartupAction *uint8 `xml:"AutomaticStartupAction"`
AutomaticStartupActionDelay *string `xml:"AutomaticStartupActionDelay>Interval"`
AutomaticStartupActionSequenceNumber *uint16 `xml:"AutomaticStartupActionSequenceNumber"`
Caption *string `xml:"Caption"`
ConfigurationDataRoot *string `xml:"ConfigurationDataRoot"`
ConfigurationFile *string `xml:"ConfigurationFile"`
ConfigurationID *string `xml:"ConfigurationID"`
CreationTime *string `xml:"CreationTime"`
Description *string `xml:"Description"`
LogDataRoot *string `xml:"LogDataRoot"`
Notes []string `xml:"Notes"`
RecoveryFile *string `xml:"RecoveryFile"`
SnapshotDataRoot *string `xml:"SnapshotDataRoot"`
SuspendDataRoot *string `xml:"SuspendDataRoot"`
SwapFileDataRoot *string `xml:"SwapFileDataRoot"`
VirtualSystemIdentifier *string `xml:"VirtualSystemIdentifier"`
VirtualSystemType *string `xml:"VirtualSystemType"`
ElementName string `xml:"ElementName" json:"elementName"`
InstanceID string `xml:"InstanceID" json:"instanceID"`

AutomaticRecoveryAction *uint8 `xml:"AutomaticRecoveryAction" json:"automaticRecoveryAction,omitempty"`
AutomaticShutdownAction *uint8 `xml:"AutomaticShutdownAction" json:"automaticShutdownAction,omitempty"`
AutomaticStartupAction *uint8 `xml:"AutomaticStartupAction" json:"automaticStartupAction,omitempty"`
AutomaticStartupActionDelay *string `xml:"AutomaticStartupActionDelay>Interval" json:"automaticStartupActionDelay,omitempty"`
AutomaticStartupActionSequenceNumber *uint16 `xml:"AutomaticStartupActionSequenceNumber" json:"automaticStartupActionSequenceNumber,omitempty"`
Caption *string `xml:"Caption" json:"caption,omitempty"`
ConfigurationDataRoot *string `xml:"ConfigurationDataRoot" json:"configurationDataRoot,omitempty"`
ConfigurationFile *string `xml:"ConfigurationFile" json:"configurationFile,omitempty"`
ConfigurationID *string `xml:"ConfigurationID" json:"configurationID,omitempty"`
CreationTime *string `xml:"CreationTime" json:"creationTime,omitempty"`
Description *string `xml:"Description" json:"description,omitempty"`
LogDataRoot *string `xml:"LogDataRoot" json:"logDataRoot,omitempty"`
Notes []string `xml:"Notes" json:"notes,omitempty"`
RecoveryFile *string `xml:"RecoveryFile" json:"recoveryFile,omitempty"`
SnapshotDataRoot *string `xml:"SnapshotDataRoot" json:"snapshotDataRoot,omitempty"`
SuspendDataRoot *string `xml:"SuspendDataRoot" json:"suspendDataRoot,omitempty"`
SwapFileDataRoot *string `xml:"SwapFileDataRoot" json:"swapFileDataRoot,omitempty"`
VirtualSystemIdentifier *string `xml:"VirtualSystemIdentifier" json:"virtualSystemIdentifier,omitempty"`
VirtualSystemType *string `xml:"VirtualSystemType" json:"virtualSystemType,omitempty"`
}

/*
Source: http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2.24.0/CIM_ResourceAllocationSettingData.xsd
*/

type CIMResourceAllocationSettingData struct {
ElementName string `xml:"ElementName"`
InstanceID string `xml:"InstanceID"`

ResourceType *CIMResourceType `xml:"ResourceType"`
OtherResourceType *string `xml:"OtherResourceType"`
ResourceSubType *string `xml:"ResourceSubType"`

AddressOnParent *string `xml:"AddressOnParent"`
Address *string `xml:"Address"`
AllocationUnits *string `xml:"AllocationUnits"`
AutomaticAllocation *bool `xml:"AutomaticAllocation"`
AutomaticDeallocation *bool `xml:"AutomaticDeallocation"`
Caption *string `xml:"Caption"`
Connection []string `xml:"Connection"`
ConsumerVisibility *uint16 `xml:"ConsumerVisibility"`
Description *string `xml:"Description"`
HostResource []string `xml:"HostResource"`
Limit *uint64 `xml:"Limit"`
MappingBehavior *uint `xml:"MappingBehavior"`
Parent *string `xml:"Parent"`
PoolID *string `xml:"PoolID"`
Reservation *uint64 `xml:"Reservation"`
VirtualQuantity *uint `xml:"VirtualQuantity"`
VirtualQuantityUnits *string `xml:"VirtualQuantityUnits"`
Weight *uint `xml:"Weight"`
ElementName string `xml:"ElementName" json:"elementName,omitempty"`
InstanceID string `xml:"InstanceID" json:"instanceID,omitempty"`

ResourceType *CIMResourceType `xml:"ResourceType" json:"resourceType,omitempty"`
OtherResourceType *string `xml:"OtherResourceType" json:"otherResourceType,omitempty"`
ResourceSubType *string `xml:"ResourceSubType" json:"resourceSubType,omitempty"`

AddressOnParent *string `xml:"AddressOnParent" json:"addressOnParent,omitempty"`
Address *string `xml:"Address" json:"address,omitempty"`
AllocationUnits *string `xml:"AllocationUnits" json:"allocationUnits,omitempty"`
AutomaticAllocation *bool `xml:"AutomaticAllocation" json:"automaticAllocation,omitempty"`
AutomaticDeallocation *bool `xml:"AutomaticDeallocation" json:"automaticDeallocation,omitempty"`
Caption *string `xml:"Caption" json:"caption,omitempty"`
Connection []string `xml:"Connection" json:"connection,omitempty"`
ConsumerVisibility *uint16 `xml:"ConsumerVisibility" json:"consumerVisibility,omitempty"`
Description *string `xml:"Description" json:"description,omitempty"`
HostResource []string `xml:"HostResource" json:"hostResource,omitempty"`
Limit *uint64 `xml:"Limit" json:"limit,omitempty"`
MappingBehavior *uint `xml:"MappingBehavior" json:"mappingBehavior,omitempty"`
Parent *string `xml:"Parent" json:"parent,omitempty"`
PoolID *string `xml:"PoolID" json:"poolID,omitempty"`
Reservation *uint64 `xml:"Reservation" json:"reservation,omitempty"`
VirtualQuantity *uint `xml:"VirtualQuantity" json:"virtualQuantity,omitempty"`
VirtualQuantityUnits *string `xml:"VirtualQuantityUnits" json:"virtualQuantityUnits,omitempty"`
Weight *uint `xml:"Weight" json:"weight,omitempty"`
}

/*
Source: http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2.24.0/CIM_StorageAllocationSettingData.xsd
*/
type CIMStorageAllocationSettingData struct {
ElementName string `xml:"ElementName"`
InstanceID string `xml:"InstanceID"`

ResourceType *CIMResourceType `xml:"ResourceType"`
OtherResourceType *string `xml:"OtherResourceType"`
ResourceSubType *string `xml:"ResourceSubType"`

Access *uint16 `xml:"Access"`
Address *string `xml:"Address"`
AddressOnParent *string `xml:"AddressOnParent"`
AllocationUnits *string `xml:"AllocationUnits"`
AutomaticAllocation *bool `xml:"AutomaticAllocation"`
AutomaticDeallocation *bool `xml:"AutomaticDeallocation"`
Caption *string `xml:"Caption"`
ChangeableType *uint16 `xml:"ChangeableType"`
ComponentSetting []types.AnyType `xml:"ComponentSetting"`
ConfigurationName *string `xml:"ConfigurationName"`
Connection []string `xml:"Connection"`
ConsumerVisibility *uint16 `xml:"ConsumerVisibility"`
Description *string `xml:"Description"`
Generation *uint64 `xml:"Generation"`
HostExtentName *string `xml:"HostExtentName"`
HostExtentNameFormat *uint16 `xml:"HostExtentNameFormat"`
HostExtentNameNamespace *uint16 `xml:"HostExtentNameNamespace"`
HostExtentStartingAddress *uint64 `xml:"HostExtentStartingAddress"`
HostResource []string `xml:"HostResource"`
HostResourceBlockSize *uint64 `xml:"HostResourceBlockSize"`
Limit *uint64 `xml:"Limit"`
MappingBehavior *uint `xml:"MappingBehavior"`
OtherHostExtentNameFormat *string `xml:"OtherHostExtentNameFormat"`
OtherHostExtentNameNamespace *string `xml:"OtherHostExtentNameNamespace"`
Parent *string `xml:"Parent"`
PoolID *string `xml:"PoolID"`
Reservation *uint64 `xml:"Reservation"`
SoID *string `xml:"SoID"`
SoOrgID *string `xml:"SoOrgID"`
VirtualQuantity *uint `xml:"VirtualQuantity"`
VirtualQuantityUnits *string `xml:"VirtualQuantityUnits"`
VirtualResourceBlockSize *uint64 `xml:"VirtualResourceBlockSize"`
Weight *uint `xml:"Weight"`
ElementName string `xml:"ElementName" json:"elementName"`
InstanceID string `xml:"InstanceID" json:"instanceID"`

ResourceType *CIMResourceType `xml:"ResourceType" json:"resourceType,omitempty"`
OtherResourceType *string `xml:"OtherResourceType" json:"otherResourceType,omitempty"`
ResourceSubType *string `xml:"ResourceSubType" json:"resourceSubType,omitempty"`

Access *uint16 `xml:"Access" json:"access,omitempty"`
Address *string `xml:"Address" json:"address,omitempty"`
AddressOnParent *string `xml:"AddressOnParent" json:"addressOnParent,omitempty"`
AllocationUnits *string `xml:"AllocationUnits" json:"allocationUnits,omitempty"`
AutomaticAllocation *bool `xml:"AutomaticAllocation" json:"automaticAllocation,omitempty"`
AutomaticDeallocation *bool `xml:"AutomaticDeallocation" json:"automaticDeallocation,omitempty"`
Caption *string `xml:"Caption" json:"caption,omitempty"`
ChangeableType *uint16 `xml:"ChangeableType" json:"changeableType,omitempty"`
ComponentSetting []types.AnyType `xml:"ComponentSetting" json:"componentSetting,omitempty"`
ConfigurationName *string `xml:"ConfigurationName" json:"configurationName,omitempty"`
Connection []string `xml:"Connection" json:"connection,omitempty"`
ConsumerVisibility *uint16 `xml:"ConsumerVisibility" json:"consumerVisibility,omitempty"`
Description *string `xml:"Description" json:"description,omitempty"`
Generation *uint64 `xml:"Generation" json:"generation,omitempty"`
HostExtentName *string `xml:"HostExtentName" json:"hostExtentName,omitempty"`
HostExtentNameFormat *uint16 `xml:"HostExtentNameFormat" json:"hostExtentNameFormat,omitempty"`
HostExtentNameNamespace *uint16 `xml:"HostExtentNameNamespace" json:"hostExtentNameNamespace,omitempty"`
HostExtentStartingAddress *uint64 `xml:"HostExtentStartingAddress" json:"hostExtentStartingAddress,omitempty"`
HostResource []string `xml:"HostResource" json:"hostResource,omitempty"`
HostResourceBlockSize *uint64 `xml:"HostResourceBlockSize" json:"hostResourceBlockSize,omitempty"`
Limit *uint64 `xml:"Limit" json:"limit,omitempty"`
MappingBehavior *uint `xml:"MappingBehavior" json:"mappingBehavior,omitempty"`
OtherHostExtentNameFormat *string `xml:"OtherHostExtentNameFormat" json:"otherHostExtentNameFormat,omitempty"`
OtherHostExtentNameNamespace *string `xml:"OtherHostExtentNameNamespace" json:"otherHostExtentNameNamespace,omitempty"`
Parent *string `xml:"Parent" json:"parent,omitempty"`
PoolID *string `xml:"PoolID" json:"poolID,omitempty"`
Reservation *uint64 `xml:"Reservation" json:"reservation,omitempty"`
SoID *string `xml:"SoID" json:"soID,omitempty"`
SoOrgID *string `xml:"SoOrgID" json:"soOrgID,omitempty"`
VirtualQuantity *uint `xml:"VirtualQuantity" json:"virtualQuantity,omitempty"`
VirtualQuantityUnits *string `xml:"VirtualQuantityUnits" json:"virtualQuantityUnits,omitempty"`
VirtualResourceBlockSize *uint64 `xml:"VirtualResourceBlockSize" json:"virtualResourceBlockSize,omitempty"`
Weight *uint `xml:"Weight" json:"weight,omitempty"`
}
24 changes: 12 additions & 12 deletions ovf/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,28 +44,28 @@ const (
)

type Env struct {
XMLName xml.Name `xml:"http://schemas.dmtf.org/ovf/environment/1 Environment"`
ID string `xml:"id,attr"`
EsxID string `xml:"http://www.vmware.com/schema/ovfenv esxId,attr"`
XMLName xml.Name `xml:"http://schemas.dmtf.org/ovf/environment/1 Environment" json:"xmlName"`
ID string `xml:"id,attr" json:"id"`
EsxID string `xml:"http://www.vmware.com/schema/ovfenv esxId,attr" json:"esxID"`

Platform *PlatformSection `xml:"PlatformSection"`
Property *PropertySection `xml:"PropertySection"`
Platform *PlatformSection `xml:"PlatformSection" json:"platformSection,omitempty"`
Property *PropertySection `xml:"PropertySection" json:"propertySection,omitempty"`
}

type PlatformSection struct {
Kind string `xml:"Kind"`
Version string `xml:"Version"`
Vendor string `xml:"Vendor"`
Locale string `xml:"Locale"`
Kind string `xml:"Kind" json:"kind,omitempty"`
Version string `xml:"Version" json:"version,omitempty"`
Vendor string `xml:"Vendor" json:"vendor,omitempty"`
Locale string `xml:"Locale" json:"locale,omitempty"`
}

type PropertySection struct {
Properties []EnvProperty `xml:"Property"`
Properties []EnvProperty `xml:"Property" json:"property,omitempty"`
}

type EnvProperty struct {
Key string `xml:"key,attr"`
Value string `xml:"value,attr"`
Key string `xml:"key,attr" json:"key"`
Value string `xml:"value,attr" json:"value,omitempty"`
}

// Marshal marshals Env to xml by using xml.Marshal.
Expand Down
Loading

0 comments on commit 0af06a3

Please sign in to comment.