libvirt.org/libvirt-go-xml@v7.4.0+incompatible/domain_snapshot.go (about)

     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  
    26  package libvirtxml
    27  
    28  import "encoding/xml"
    29  
    30  type DomainSnapshotDisk struct {
    31  	Name     string            `xml:"name,attr"`
    32  	Snapshot string            `xml:"snapshot,attr,omitempty"`
    33  	Driver   *DomainDiskDriver `xml:"driver"`
    34  	Source   *DomainDiskSource `xml:"source"`
    35  }
    36  
    37  type DomainSnapshotDisks struct {
    38  	Disks []DomainSnapshotDisk `xml:"disk"`
    39  }
    40  
    41  type DomainSnapshotMemory struct {
    42  	Snapshot string `xml:"snapshot,attr"`
    43  	File     string `xml:"file,attr,omitempty"`
    44  }
    45  
    46  type DomainSnapshotParent struct {
    47  	Name string `xml:"name"`
    48  }
    49  
    50  type DomainSnapshot struct {
    51  	XMLName      xml.Name              `xml:"domainsnapshot"`
    52  	Name         string                `xml:"name,omitempty"`
    53  	Description  string                `xml:"description,omitempty"`
    54  	State        string                `xml:"state,omitempty"`
    55  	CreationTime string                `xml:"creationTime,omitempty"`
    56  	Parent       *DomainSnapshotParent `xml:"parent"`
    57  	Memory       *DomainSnapshotMemory `xml:"memory"`
    58  	Disks        *DomainSnapshotDisks  `xml:"disks"`
    59  	Domain       *Domain               `xml:"domain"`
    60  	Active       *uint                 `xml:"active"`
    61  }
    62  
    63  type domainSnapshotDisk DomainSnapshotDisk
    64  
    65  func (a *DomainSnapshotDisk) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    66  	start.Name.Local = "disk"
    67  	if a.Source != nil {
    68  		if a.Source.File != nil {
    69  			start.Attr = append(start.Attr, xml.Attr{
    70  				xml.Name{Local: "type"}, "file",
    71  			})
    72  		} else if a.Source.Block != nil {
    73  			start.Attr = append(start.Attr, xml.Attr{
    74  				xml.Name{Local: "type"}, "block",
    75  			})
    76  		} else if a.Source.Dir != nil {
    77  			start.Attr = append(start.Attr, xml.Attr{
    78  				xml.Name{Local: "type"}, "dir",
    79  			})
    80  		} else if a.Source.Network != nil {
    81  			start.Attr = append(start.Attr, xml.Attr{
    82  				xml.Name{Local: "type"}, "network",
    83  			})
    84  		} else if a.Source.Volume != nil {
    85  			start.Attr = append(start.Attr, xml.Attr{
    86  				xml.Name{Local: "type"}, "volume",
    87  			})
    88  		}
    89  	}
    90  	disk := domainSnapshotDisk(*a)
    91  	return e.EncodeElement(disk, start)
    92  }
    93  
    94  func (a *DomainSnapshotDisk) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
    95  	typ, ok := getAttr(start.Attr, "type")
    96  	if !ok {
    97  		typ = "file"
    98  	}
    99  	a.Source = &DomainDiskSource{}
   100  	if typ == "file" {
   101  		a.Source.File = &DomainDiskSourceFile{}
   102  	} else if typ == "block" {
   103  		a.Source.Block = &DomainDiskSourceBlock{}
   104  	} else if typ == "network" {
   105  		a.Source.Network = &DomainDiskSourceNetwork{}
   106  	} else if typ == "dir" {
   107  		a.Source.Dir = &DomainDiskSourceDir{}
   108  	} else if typ == "volume" {
   109  		a.Source.Volume = &DomainDiskSourceVolume{}
   110  	}
   111  	disk := domainSnapshotDisk(*a)
   112  	err := d.DecodeElement(&disk, &start)
   113  	if err != nil {
   114  		return err
   115  	}
   116  	*a = DomainSnapshotDisk(disk)
   117  	return nil
   118  }
   119  
   120  func (s *DomainSnapshot) Unmarshal(doc string) error {
   121  	return xml.Unmarshal([]byte(doc), s)
   122  }
   123  
   124  func (s *DomainSnapshot) Marshal() (string, error) {
   125  	doc, err := xml.MarshalIndent(s, "", "  ")
   126  	if err != nil {
   127  		return "", err
   128  	}
   129  	return string(doc), nil
   130  }