domain_snapshot.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * This file is part of the libvirt-go-xml project
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. *
  22. * Copyright (C) 2017 Red Hat, Inc.
  23. *
  24. */
  25. package libvirtxml
  26. import "encoding/xml"
  27. type DomainSnapshotDiskDriver struct {
  28. Type string `xml:"type,attr,omitempty"`
  29. }
  30. type DomainSnapshotDisk struct {
  31. Name string `xml:"name,attr"`
  32. Snapshot string `xml:"snapshot,attr,omitempty"`
  33. Driver *DomainSnapshotDiskDriver `xml:"driver"`
  34. Source *DomainDiskSource `xml:"source"`
  35. }
  36. type DomainSnapshotDisks struct {
  37. Disks []DomainSnapshotDisk `xml:"disk"`
  38. }
  39. type DomainSnapshotMemory struct {
  40. Snapshot string `xml:"snapshot,attr"`
  41. File string `xml:"file,attr,omitempty"`
  42. }
  43. type DomainSnapshotParent struct {
  44. Name string `xml:"name"`
  45. }
  46. type DomainSnapshot struct {
  47. XMLName xml.Name `xml:"domainsnapshot"`
  48. Name string `xml:"name,omitempty"`
  49. Description string `xml:"description,omitempty"`
  50. State string `xml:"state,omitempty"`
  51. CreationTime string `xml:"creationTime,omitempty"`
  52. Parent *DomainSnapshotParent `xml:"parent"`
  53. Memory *DomainSnapshotMemory `xml:"memory"`
  54. Disks *DomainSnapshotDisks `xml:"disks"`
  55. Domain *Domain `xml:"domain"`
  56. Active *uint `xml:"active"`
  57. }
  58. type domainSnapshotDisk DomainSnapshotDisk
  59. func (a *DomainSnapshotDisk) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  60. start.Name.Local = "disk"
  61. if a.Source != nil {
  62. if a.Source.File != nil {
  63. start.Attr = append(start.Attr, xml.Attr{
  64. xml.Name{Local: "type"}, "file",
  65. })
  66. } else if a.Source.Block != nil {
  67. start.Attr = append(start.Attr, xml.Attr{
  68. xml.Name{Local: "type"}, "block",
  69. })
  70. } else if a.Source.Dir != nil {
  71. start.Attr = append(start.Attr, xml.Attr{
  72. xml.Name{Local: "type"}, "dir",
  73. })
  74. } else if a.Source.Network != nil {
  75. start.Attr = append(start.Attr, xml.Attr{
  76. xml.Name{Local: "type"}, "network",
  77. })
  78. } else if a.Source.Volume != nil {
  79. start.Attr = append(start.Attr, xml.Attr{
  80. xml.Name{Local: "type"}, "volume",
  81. })
  82. }
  83. }
  84. disk := domainSnapshotDisk(*a)
  85. return e.EncodeElement(disk, start)
  86. }
  87. func (a *DomainSnapshotDisk) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
  88. typ, ok := getAttr(start.Attr, "type")
  89. if !ok {
  90. typ = "file"
  91. }
  92. a.Source = &DomainDiskSource{}
  93. if typ == "file" {
  94. a.Source.File = &DomainDiskSourceFile{}
  95. } else if typ == "block" {
  96. a.Source.Block = &DomainDiskSourceBlock{}
  97. } else if typ == "network" {
  98. a.Source.Network = &DomainDiskSourceNetwork{}
  99. } else if typ == "dir" {
  100. a.Source.Dir = &DomainDiskSourceDir{}
  101. } else if typ == "volume" {
  102. a.Source.Volume = &DomainDiskSourceVolume{}
  103. }
  104. disk := domainSnapshotDisk(*a)
  105. err := d.DecodeElement(&disk, &start)
  106. if err != nil {
  107. return err
  108. }
  109. *a = DomainSnapshotDisk(disk)
  110. return nil
  111. }
  112. func (s *DomainSnapshot) Unmarshal(doc string) error {
  113. return xml.Unmarshal([]byte(doc), s)
  114. }
  115. func (s *DomainSnapshot) Marshal() (string, error) {
  116. doc, err := xml.MarshalIndent(s, "", " ")
  117. if err != nil {
  118. return "", err
  119. }
  120. return string(doc), nil
  121. }