Signed-off-by: Alexey Slaykovsky <aslaikov@xxxxxxxxxx> --- domain.go | 11 ++- domain_snapshot.go | 79 ++++++++++++++++++++ domain_snapshot_test.go | 190 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 278 insertions(+), 2 deletions(-) create mode 100644 domain_snapshot.go create mode 100644 domain_snapshot_test.go diff --git a/domain.go b/domain.go index 8afe32d..161ba33 100644 --- a/domain.go +++ b/domain.go @@ -43,11 +43,18 @@ type DomainDiskDriver struct { Type string `xml:"type,attr"` } +type DomainDiskTarget struct { + Dev string `xml:"dev,attr"` + Bus string `xml:"bus,attr"` +} + type DomainDisk struct { Type string `xml:"type,attr"` Device string `xml:"device,attr"` + Snapshot string `xml:"snapshot,attr,omitempty"` Driver DomainDiskDriver `xml:"driver"` FileSource DomainDiskFileSource `xml:"source"` + Target *DomainDiskTarget `xml:"target"` } type DomainInterfaceMAC struct { @@ -123,7 +130,7 @@ type DomainDeviceList struct { type DomainMemory struct { Value string `xml:",chardata"` - Unit string `xml:"unit,attr"` + Unit string `xml:"unit,attr,omitempty"` } type DomainOSType struct { @@ -191,7 +198,7 @@ type DomainOS struct { type Domain struct { XMLName xml.Name `xml:"domain"` - Type string `xml:"type,attr"` + Type string `xml:"type,attr,omitempty"` Name string `xml:"name"` UUID *string `xml:"uuid"` Memory *DomainMemory `xml:"memory"` diff --git a/domain_snapshot.go b/domain_snapshot.go new file mode 100644 index 0000000..226cd09 --- /dev/null +++ b/domain_snapshot.go @@ -0,0 +1,79 @@ +/* + * This file is part of the libvirt-go-xml project + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * Copyright (C) 2017 Red Hat, Inc. + * + */ + +package libvirtxml + +import "encoding/xml" + +type DomainSnapshotDiskDriver struct { + Type string `xml:"type,attr"` +} + +type DomainSnapshotDiskSource struct { + File string `xml:"file,attr"` +} + +type DomainSnapshotDisk struct { + Name string `xml:"name,attr"` + Snapshot string `xml:"snapshot,attr,omitempty"` + Driver *DomainSnapshotDiskDriver `xml:"driver"` + Source *DomainSnapshotDiskSource `xml:"source"` +} + +type DomainSnapshotDisks struct { + Disks []DomainSnapshotDisk `xml:"disk"` +} + +type DomainSnapshotMemory struct { + Snapshot string `xml:"snapshot,attr"` +} + +type DomainSnapshotParent struct { + Name string `xml:"name"` +} + +type DomainSnapshot struct { + XMLName xml.Name `xml:"domainsnapshot"` + Name string `xml:"name,omitempty"` + Description string `xml:"description,omitempty"` + State string `xml:"state,omitempty"` + CreationTime string `xml:"creationTime,omitempty"` + Parent *DomainSnapshotParent `xml:"parent"` + Memory *DomainSnapshotMemory `xml:"memory"` + Disks *DomainSnapshotDisks `xml:"disks"` + Domain *Domain `xml:"domain"` +} + +func (s *DomainSnapshot) Unmarshal(doc string) error { + return xml.Unmarshal([]byte(doc), s) +} + +func (s *DomainSnapshot) Marshal() (string, error) { + doc, err := xml.MarshalIndent(s, "", " ") + if err != nil { + return "", err + } + return string(doc), nil +} diff --git a/domain_snapshot_test.go b/domain_snapshot_test.go new file mode 100644 index 0000000..e33bc6b --- /dev/null +++ b/domain_snapshot_test.go @@ -0,0 +1,190 @@ +/* + * This file is part of the libvirt-go-xml project + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * Copyright (C) 2017 Red Hat, Inc. + * + */ + +package libvirtxml + +import ( + "strings" + "testing" +) + +var domainSnapshotTestData = []struct { + Object *DomainSnapshot + Expected []string +}{ + { + Object: &DomainSnapshot{ + Description: "Snapshot", + Disks: &DomainSnapshotDisks{ + []DomainSnapshotDisk{ + DomainSnapshotDisk{ + Name: "/old", + Source: &DomainSnapshotDiskSource{ + File: "/new", + }, + }, + DomainSnapshotDisk{ + Name: "vdb", + Snapshot: "no", + }, + }, + }, + }, + Expected: []string{ + `<domainsnapshot>`, + ` <description>Snapshot</description>`, + ` <disks>`, + ` <disk name="/old">`, + ` <source file="/new"></source>`, + ` </disk>`, + ` <disk name="vdb" snapshot="no"></disk>`, + ` </disks>`, + `</domainsnapshot>`, + }, + }, + { + Object: &DomainSnapshot{ + Name: "1270477159", + Description: "Snapshot of OS install and updates", + State: "running", + CreationTime: "1270477159", + Parent: &DomainSnapshotParent{ + Name: "bare-os-install", + }, + Memory: &DomainSnapshotMemory{ + Snapshot: "no", + }, + Disks: &DomainSnapshotDisks{ + Disks: []DomainSnapshotDisk{ + DomainSnapshotDisk{ + Name: "vda", + Snapshot: "external", + Driver: &DomainSnapshotDiskDriver{ + Type: "qcow2", + }, + Source: &DomainSnapshotDiskSource{ + File: "/path/to/new", + }, + }, + DomainSnapshotDisk{ + Name: "vdb", + Snapshot: "no", + }, + }, + }, + Domain: &Domain{ + Name: "fedora", + Memory: &DomainMemory{ + Value: "1048576", + }, + Devices: &DomainDeviceList{ + Disks: []DomainDisk{ + DomainDisk{ + Type: "file", + Device: "disk", + Driver: DomainDiskDriver{ + Name: "qemu", + Type: "raw", + }, + FileSource: DomainDiskFileSource{ + File: "/path/to/old", + }, + Target: &DomainDiskTarget{ + Dev: "vda", + Bus: "virtio", + }, + }, + DomainDisk{ + Type: "file", + Device: "disk", + Snapshot: "external", + Driver: DomainDiskDriver{ + Name: "qemu", + Type: "raw", + }, + FileSource: DomainDiskFileSource{ + File: "/path/to/old2", + }, + Target: &DomainDiskTarget{ + Dev: "vdb", + Bus: "virtio", + }, + }, + }, + }, + }, + }, + Expected: []string{ + `<domainsnapshot>`, + ` <name>1270477159</name>`, + ` <description>Snapshot of OS install and updates</description>`, + ` <state>running</state>`, + ` <creationTime>1270477159</creationTime>`, + ` <parent>`, + ` <name>bare-os-install</name>`, + ` </parent>`, + ` <memory snapshot="no"></memory>`, + ` <disks>`, + ` <disk name="vda" snapshot="external">`, + ` <driver type="qcow2"></driver>`, + ` <source file="/path/to/new"></source>`, + ` </disk>`, + ` <disk name="vdb" snapshot="no"></disk>`, + ` </disks>`, + ` <domain>`, + ` <name>fedora</name>`, + ` <memory>1048576</memory>`, + ` <devices>`, + ` <disk type="file" device="disk">`, + ` <driver name="qemu" type="raw"></driver>`, + ` <source file="/path/to/old"></source>`, + ` <target dev="vda" bus="virtio"></target>`, + ` </disk>`, + ` <disk type="file" device="disk" snapshot="external">`, + ` <driver name="qemu" type="raw"></driver>`, + ` <source file="/path/to/old2"></source>`, + ` <target dev="vdb" bus="virtio"></target>`, + ` </disk>`, + ` </devices>`, + ` </domain>`, + `</domainsnapshot>`, + }, + }, +} + +func TestDomainSnapshot(t *testing.T) { + for _, test := range domainSnapshotTestData { + doc, err := test.Object.Marshal() + if err != nil { + t.Fatal(err) + } + + expect := strings.Join(test.Expected, "\n") + + if doc != expect { + t.Fatal("Bad xml:\n", string(doc), "\n does not match\n", expect, "\n") + } + } +} -- 2.11.0 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list