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