github.com/vmware/govmomi@v0.37.2/ovf/env.go (about)

     1  /*
     2  Copyright (c) 2015 VMware, Inc. All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package ovf
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  
    23  	"github.com/vmware/govmomi/vim25/xml"
    24  )
    25  
    26  const (
    27  	ovfEnvHeader = `<Environment
    28  		xmlns="http://schemas.dmtf.org/ovf/environment/1"
    29  		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    30  		xmlns:oe="http://schemas.dmtf.org/ovf/environment/1"
    31  		xmlns:ve="http://www.vmware.com/schema/ovfenv"
    32  		oe:id=""
    33  		ve:esxId="%s">`
    34  	ovfEnvPlatformSection = `<PlatformSection>
    35  		<Kind>%s</Kind>
    36  		<Version>%s</Version>
    37  		<Vendor>%s</Vendor>
    38  		<Locale>%s</Locale>
    39  		</PlatformSection>`
    40  	ovfEnvPropertyHeader = `<PropertySection>`
    41  	ovfEnvPropertyEntry  = `<Property oe:key="%s" oe:value="%s"/>`
    42  	ovfEnvPropertyFooter = `</PropertySection>`
    43  	ovfEnvFooter         = `</Environment>`
    44  )
    45  
    46  type Env struct {
    47  	XMLName xml.Name `xml:"http://schemas.dmtf.org/ovf/environment/1 Environment"`
    48  	ID      string   `xml:"id,attr"`
    49  	EsxID   string   `xml:"http://www.vmware.com/schema/ovfenv esxId,attr"`
    50  
    51  	Platform *PlatformSection `xml:"PlatformSection"`
    52  	Property *PropertySection `xml:"PropertySection"`
    53  }
    54  
    55  type PlatformSection struct {
    56  	Kind    string `xml:"Kind"`
    57  	Version string `xml:"Version"`
    58  	Vendor  string `xml:"Vendor"`
    59  	Locale  string `xml:"Locale"`
    60  }
    61  
    62  type PropertySection struct {
    63  	Properties []EnvProperty `xml:"Property"`
    64  }
    65  
    66  type EnvProperty struct {
    67  	Key   string `xml:"key,attr"`
    68  	Value string `xml:"value,attr"`
    69  }
    70  
    71  // Marshal marshals Env to xml by using xml.Marshal.
    72  func (e Env) Marshal() (string, error) {
    73  	x, err := xml.Marshal(e)
    74  	if err != nil {
    75  		return "", err
    76  	}
    77  
    78  	return fmt.Sprintf("%s%s", xml.Header, x), nil
    79  }
    80  
    81  // MarshalManual manually marshals Env to xml suitable for a vApp guest.
    82  // It exists to overcome the lack of expressiveness in Go's XML namespaces.
    83  func (e Env) MarshalManual() string {
    84  	var buffer bytes.Buffer
    85  
    86  	buffer.WriteString(xml.Header)
    87  	buffer.WriteString(fmt.Sprintf(ovfEnvHeader, e.EsxID))
    88  	buffer.WriteString(fmt.Sprintf(ovfEnvPlatformSection, e.Platform.Kind, e.Platform.Version, e.Platform.Vendor, e.Platform.Locale))
    89  
    90  	buffer.WriteString(fmt.Sprint(ovfEnvPropertyHeader))
    91  	for _, p := range e.Property.Properties {
    92  		buffer.WriteString(fmt.Sprintf(ovfEnvPropertyEntry, p.Key, p.Value))
    93  	}
    94  	buffer.WriteString(fmt.Sprint(ovfEnvPropertyFooter))
    95  
    96  	buffer.WriteString(fmt.Sprint(ovfEnvFooter))
    97  
    98  	return buffer.String()
    99  }