|
Packit |
63bb0d |
/*
|
|
Packit |
63bb0d |
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
Licensed under the Apache License, Version 2.0 (the "License");
|
|
Packit |
63bb0d |
you may not use this file except in compliance with the License.
|
|
Packit |
63bb0d |
You may obtain a copy of the License at
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
Unless required by applicable law or agreed to in writing, software
|
|
Packit |
63bb0d |
distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
63bb0d |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
63bb0d |
See the License for the specific language governing permissions and
|
|
Packit |
63bb0d |
limitations under the License.
|
|
Packit |
63bb0d |
*/
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
package ovf
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
import (
|
|
Packit |
63bb0d |
"bytes"
|
|
Packit |
63bb0d |
"fmt"
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
"github.com/vmware/govmomi/vim25/xml"
|
|
Packit |
63bb0d |
)
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
const (
|
|
Packit |
63bb0d |
ovfEnvHeader = `
|
|
Packit |
63bb0d |
xmlns="http://schemas.dmtf.org/ovf/environment/1"
|
|
Packit |
63bb0d |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
Packit |
63bb0d |
xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"
|
|
Packit |
63bb0d |
xmlns:ve="http://www.vmware.com/schema/ovfenv"
|
|
Packit |
63bb0d |
oe:id=""
|
|
Packit |
63bb0d |
ve:esxId="%s">`
|
|
Packit |
63bb0d |
ovfEnvPlatformSection = `<PlatformSection>
|
|
Packit |
63bb0d |
<Kind>%s</Kind>
|
|
Packit |
63bb0d |
<Version>%s</Version>
|
|
Packit |
63bb0d |
<Vendor>%s</Vendor>
|
|
Packit |
63bb0d |
<Locale>%s</Locale>
|
|
Packit |
63bb0d |
</PlatformSection>`
|
|
Packit |
63bb0d |
ovfEnvPropertyHeader = `<PropertySection>`
|
|
Packit |
63bb0d |
ovfEnvPropertyEntry = `<Property oe:key="%s" oe:value="%s"/>`
|
|
Packit |
63bb0d |
ovfEnvPropertyFooter = `</PropertySection>`
|
|
Packit |
63bb0d |
ovfEnvFooter = `</Environment>`
|
|
Packit |
63bb0d |
)
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
type Env struct {
|
|
Packit |
63bb0d |
XMLName xml.Name `xml:"http://schemas.dmtf.org/ovf/environment/1 Environment"`
|
|
Packit |
63bb0d |
ID string `xml:"id,attr"`
|
|
Packit |
63bb0d |
EsxID string `xml:"http://www.vmware.com/schema/ovfenv esxId,attr"`
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
Platform *PlatformSection `xml:"PlatformSection"`
|
|
Packit |
63bb0d |
Property *PropertySection `xml:"PropertySection"`
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
type PlatformSection struct {
|
|
Packit |
63bb0d |
Kind string `xml:"Kind"`
|
|
Packit |
63bb0d |
Version string `xml:"Version"`
|
|
Packit |
63bb0d |
Vendor string `xml:"Vendor"`
|
|
Packit |
63bb0d |
Locale string `xml:"Locale"`
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
type PropertySection struct {
|
|
Packit |
63bb0d |
Properties []EnvProperty `xml:"Property"`
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
type EnvProperty struct {
|
|
Packit |
63bb0d |
Key string `xml:"key,attr"`
|
|
Packit |
63bb0d |
Value string `xml:"value,attr"`
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// Marshal marshals Env to xml by using xml.Marshal.
|
|
Packit |
63bb0d |
func (e Env) Marshal() (string, error) {
|
|
Packit |
63bb0d |
x, err := xml.Marshal(e)
|
|
Packit |
63bb0d |
if err != nil {
|
|
Packit |
63bb0d |
return "", err
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
return fmt.Sprintf("%s%s", xml.Header, x), nil
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
// MarshalManual manually marshals Env to xml suitable for a vApp guest.
|
|
Packit |
63bb0d |
// It exists to overcome the lack of expressiveness in Go's XML namespaces.
|
|
Packit |
63bb0d |
func (e Env) MarshalManual() string {
|
|
Packit |
63bb0d |
var buffer bytes.Buffer
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
buffer.WriteString(xml.Header)
|
|
Packit |
63bb0d |
buffer.WriteString(fmt.Sprintf(ovfEnvHeader, e.EsxID))
|
|
Packit |
63bb0d |
buffer.WriteString(fmt.Sprintf(ovfEnvPlatformSection, e.Platform.Kind, e.Platform.Version, e.Platform.Vendor, e.Platform.Locale))
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
buffer.WriteString(fmt.Sprint(ovfEnvPropertyHeader))
|
|
Packit |
63bb0d |
for _, p := range e.Property.Properties {
|
|
Packit |
63bb0d |
buffer.WriteString(fmt.Sprintf(ovfEnvPropertyEntry, p.Key, p.Value))
|
|
Packit |
63bb0d |
}
|
|
Packit |
63bb0d |
buffer.WriteString(fmt.Sprint(ovfEnvPropertyFooter))
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
buffer.WriteString(fmt.Sprint(ovfEnvFooter))
|
|
Packit |
63bb0d |
|
|
Packit |
63bb0d |
return buffer.String()
|
|
Packit |
63bb0d |
}
|