env.go 2.8 KB

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