github.com/vmware/govmomi@v0.51.0/ovf/env.go (about) 1 // © Broadcom. All Rights Reserved. 2 // The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. 3 // SPDX-License-Identifier: Apache-2.0 4 5 package ovf 6 7 import ( 8 "bytes" 9 "fmt" 10 11 "github.com/vmware/govmomi/vim25/xml" 12 ) 13 14 const ( 15 ovfEnvHeader = `<Environment 16 xmlns="http://schemas.dmtf.org/ovf/environment/1" 17 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 18 xmlns:oe="http://schemas.dmtf.org/ovf/environment/1" 19 xmlns:ve="http://www.vmware.com/schema/ovfenv" 20 oe:id="" 21 ve:esxId="%s">` 22 ovfEnvPlatformSection = `<PlatformSection> 23 <Kind>%s</Kind> 24 <Version>%s</Version> 25 <Vendor>%s</Vendor> 26 <Locale>%s</Locale> 27 </PlatformSection>` 28 ovfEnvPropertyHeader = `<PropertySection>` 29 ovfEnvPropertyEntry = `<Property oe:key="%s" oe:value="%s"/>` 30 ovfEnvPropertyFooter = `</PropertySection>` 31 ovfEnvFooter = `</Environment>` 32 ) 33 34 type Env struct { 35 XMLName xml.Name `xml:"http://schemas.dmtf.org/ovf/environment/1 Environment" json:"xmlName"` 36 ID string `xml:"id,attr" json:"id"` 37 EsxID string `xml:"http://www.vmware.com/schema/ovfenv esxId,attr" json:"esxID"` 38 39 Platform *PlatformSection `xml:"PlatformSection" json:"platformSection,omitempty"` 40 Property *PropertySection `xml:"PropertySection" json:"propertySection,omitempty"` 41 } 42 43 type PlatformSection struct { 44 Kind string `xml:"Kind" json:"kind,omitempty"` 45 Version string `xml:"Version" json:"version,omitempty"` 46 Vendor string `xml:"Vendor" json:"vendor,omitempty"` 47 Locale string `xml:"Locale" json:"locale,omitempty"` 48 } 49 50 type PropertySection struct { 51 Properties []EnvProperty `xml:"Property" json:"property,omitempty"` 52 } 53 54 type EnvProperty struct { 55 Key string `xml:"key,attr" json:"key"` 56 Value string `xml:"value,attr" json:"value,omitempty"` 57 } 58 59 // Marshal marshals Env to xml by using xml.Marshal. 60 func (e Env) Marshal() (string, error) { 61 x, err := xml.Marshal(e) 62 if err != nil { 63 return "", err 64 } 65 66 return fmt.Sprintf("%s%s", xml.Header, x), nil 67 } 68 69 // MarshalManual manually marshals Env to xml suitable for a vApp guest. 70 // It exists to overcome the lack of expressiveness in Go's XML namespaces. 71 func (e Env) MarshalManual() string { 72 var buffer bytes.Buffer 73 74 buffer.WriteString(xml.Header) 75 buffer.WriteString(fmt.Sprintf(ovfEnvHeader, e.EsxID)) 76 buffer.WriteString(fmt.Sprintf(ovfEnvPlatformSection, e.Platform.Kind, e.Platform.Version, e.Platform.Vendor, e.Platform.Locale)) 77 78 buffer.WriteString(fmt.Sprint(ovfEnvPropertyHeader)) 79 for _, p := range e.Property.Properties { 80 buffer.WriteString(fmt.Sprintf(ovfEnvPropertyEntry, p.Key, p.Value)) 81 } 82 buffer.WriteString(fmt.Sprint(ovfEnvPropertyFooter)) 83 84 buffer.WriteString(fmt.Sprint(ovfEnvFooter)) 85 86 return buffer.String() 87 }