libvirt.org/go/libvirtxml@v1.10003.0/domain_snapshot.go (about)

     1  /*
     2   * This file is part of the libvirt-go-xml-module 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 DomainSnapshotInactiveDomain struct {
    51  	XMLName xml.Name `xml:"inactiveDomain"`
    52  	Domain
    53  }
    54  
    55  type DomainSnapshotCookie struct {
    56  	XML string `xml:",innerxml"`
    57  }
    58  
    59  type DomainSnapshot struct {
    60  	XMLName        xml.Name                      `xml:"domainsnapshot"`
    61  	Name           string                        `xml:"name,omitempty"`
    62  	Description    string                        `xml:"description,omitempty"`
    63  	State          string                        `xml:"state,omitempty"`
    64  	CreationTime   string                        `xml:"creationTime,omitempty"`
    65  	Parent         *DomainSnapshotParent         `xml:"parent"`
    66  	Memory         *DomainSnapshotMemory         `xml:"memory"`
    67  	Disks          *DomainSnapshotDisks          `xml:"disks"`
    68  	Domain         *Domain                       `xml:"domain"`
    69  	InactiveDomain *DomainSnapshotInactiveDomain `xml:"inactiveDomain"`
    70  	Active         *uint                         `xml:"active"`
    71  	Cookie         *DomainSnapshotCookie         `xml:"cookie"`
    72  }
    73  
    74  type domainSnapshotDisk DomainSnapshotDisk
    75  
    76  func (a *DomainSnapshotDisk) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
    77  	start.Name.Local = "disk"
    78  	if a.Source != nil {
    79  		if a.Source.File != nil {
    80  			start.Attr = append(start.Attr, xml.Attr{
    81  				xml.Name{Local: "type"}, "file",
    82  			})
    83  		} else if a.Source.Block != nil {
    84  			start.Attr = append(start.Attr, xml.Attr{
    85  				xml.Name{Local: "type"}, "block",
    86  			})
    87  		} else if a.Source.Dir != nil {
    88  			start.Attr = append(start.Attr, xml.Attr{
    89  				xml.Name{Local: "type"}, "dir",
    90  			})
    91  		} else if a.Source.Network != nil {
    92  			start.Attr = append(start.Attr, xml.Attr{
    93  				xml.Name{Local: "type"}, "network",
    94  			})
    95  		} else if a.Source.Volume != nil {
    96  			start.Attr = append(start.Attr, xml.Attr{
    97  				xml.Name{Local: "type"}, "volume",
    98  			})
    99  		}
   100  	}
   101  	disk := domainSnapshotDisk(*a)
   102  	return e.EncodeElement(disk, start)
   103  }
   104  
   105  func (a *DomainSnapshotDisk) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
   106  	typ, ok := getAttr(start.Attr, "type")
   107  	if !ok {
   108  		typ = "file"
   109  	}
   110  	a.Source = &DomainDiskSource{}
   111  	if typ == "file" {
   112  		a.Source.File = &DomainDiskSourceFile{}
   113  	} else if typ == "block" {
   114  		a.Source.Block = &DomainDiskSourceBlock{}
   115  	} else if typ == "network" {
   116  		a.Source.Network = &DomainDiskSourceNetwork{}
   117  	} else if typ == "dir" {
   118  		a.Source.Dir = &DomainDiskSourceDir{}
   119  	} else if typ == "volume" {
   120  		a.Source.Volume = &DomainDiskSourceVolume{}
   121  	}
   122  	disk := domainSnapshotDisk(*a)
   123  	err := d.DecodeElement(&disk, &start)
   124  	if err != nil {
   125  		return err
   126  	}
   127  	*a = DomainSnapshotDisk(disk)
   128  	return nil
   129  }
   130  
   131  func (s *DomainSnapshot) Unmarshal(doc string) error {
   132  	return xml.Unmarshal([]byte(doc), s)
   133  }
   134  
   135  func (s *DomainSnapshot) Marshal() (string, error) {
   136  	doc, err := xml.MarshalIndent(s, "", "  ")
   137  	if err != nil {
   138  		return "", err
   139  	}
   140  	return string(doc), nil
   141  }