diff options
author | Thomas Hipp <thipp@suse.de> | 2017-08-10 12:48:55 +0200 |
---|---|---|
committer | Flavio Castelli <flavio@castelli.me> | 2017-08-11 14:42:24 +0200 |
commit | 7fbe405ecb15e4ab22da4a74368aafcc6b0c2ea8 (patch) | |
tree | 337160720552859360c190bf890cd0dc96f8297c /libvirt/utils_volume.go | |
parent | b76cf35ad27d3f0e49684ea58920230ebd447883 (diff) | |
download | terraform-provider-libvirt-7fbe405ecb15e4ab22da4a74368aafcc6b0c2ea8.tar terraform-provider-libvirt-7fbe405ecb15e4ab22da4a74368aafcc6b0c2ea8.tar.gz |
use libvirt-go-xml for volumes
Signed-off-by: Thomas Hipp <thipp@suse.de>
Diffstat (limited to 'libvirt/utils_volume.go')
-rw-r--r-- | libvirt/utils_volume.go | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/libvirt/utils_volume.go b/libvirt/utils_volume.go index 83596465..289ded4e 100644 --- a/libvirt/utils_volume.go +++ b/libvirt/utils_volume.go @@ -9,14 +9,16 @@ import ( "os" "strconv" "strings" + "time" libvirt "github.com/libvirt/libvirt-go" + "github.com/libvirt/libvirt-go-xml" ) // network transparent image type image interface { Size() (uint64, error) - Import(func(io.Reader) error, defVolume) error + Import(func(io.Reader) error, libvirtxml.StorageVolume) error String() string } @@ -41,8 +43,7 @@ func (i *localImage) Size() (uint64, error) { return uint64(fi.Size()), nil } -func (i *localImage) Import(copier func(io.Reader) error, vol defVolume) error { - +func (i *localImage) Import(copier func(io.Reader) error, vol libvirtxml.StorageVolume) error { file, err := os.Open(i.path) defer file.Close() if err != nil { @@ -53,9 +54,8 @@ func (i *localImage) Import(copier func(io.Reader) error, vol defVolume) error { return err } else { // we can skip the upload if the modification times are the same - if vol.Target.Timestamps != nil && vol.Target.Timestamps.Modification != nil { - modTime := UnixTimestamp{fi.ModTime()} - if modTime == *vol.Target.Timestamps.Modification { + if vol.Target.Timestamps != nil && vol.Target.Timestamps.Mtime != "" { + if fi.ModTime() == timeFromEpoch(vol.Target.Timestamps.Mtime) { log.Printf("Modification time is the same: skipping image copy") return nil } @@ -98,13 +98,12 @@ func (i *httpImage) Size() (uint64, error) { return uint64(length), nil } -func (i *httpImage) Import(copier func(io.Reader) error, vol defVolume) error { +func (i *httpImage) Import(copier func(io.Reader) error, vol libvirtxml.StorageVolume) error { client := &http.Client{} req, _ := http.NewRequest("GET", i.url.String(), nil) - if vol.Target.Timestamps != nil && vol.Target.Timestamps.Modification != nil { - t := vol.Target.Timestamps.Modification.UTC().Format(http.TimeFormat) - req.Header.Set("If-Modified-Since", t) + if vol.Target.Timestamps != nil && vol.Target.Timestamps.Mtime != "" { + req.Header.Set("If-Modified-Since", timeFromEpoch(vol.Target.Timestamps.Mtime).UTC().Format(http.TimeFormat)) } response, err := client.Do(req) defer response.Body.Close() @@ -155,3 +154,15 @@ func newCopier(virConn *libvirt.Connect, volume *libvirt.StorageVol, size uint64 } return copier } + +func timeFromEpoch(str string) time.Time { + var s, ns int + + ts := strings.Split(str, ".") + if len(ts) == 2 { + ns, _ = strconv.Atoi(ts[1]) + } + s, _ = strconv.Atoi(ts[0]) + + return time.Unix(int64(s), int64(ns)) +} |