diff options
author | Flavio Castelli <fcastelli@suse.com> | 2017-05-27 00:08:27 +0200 |
---|---|---|
committer | Flavio Castelli <fcastelli@suse.com> | 2017-05-27 00:08:27 +0200 |
commit | 392b153595f220abef45da262ec4ffab4fcb969a (patch) | |
tree | 1bba877887c891fa7c3d9d7da3dc94f10db62f60 /libvirt/utils_volume.go | |
parent | 2817006523fead9169dbbdf06212df620b72aad8 (diff) | |
download | terraform-provider-libvirt-392b153595f220abef45da262ec4ffab4fcb969a.tar terraform-provider-libvirt-392b153595f220abef45da262ec4ffab4fcb969a.tar.gz |
DRY code
Provide a generic function to return a copier to be used with
`img.Import`. This reduces code duplication.
Signed-off-by: Flavio Castelli <fcastelli@suse.com>
Diffstat (limited to 'libvirt/utils_volume.go')
-rw-r--r-- | libvirt/utils_volume.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/libvirt/utils_volume.go b/libvirt/utils_volume.go index 594d1f68..67418985 100644 --- a/libvirt/utils_volume.go +++ b/libvirt/utils_volume.go @@ -9,6 +9,8 @@ import ( "os" "strconv" "strings" + + libvirt "github.com/dmacvicar/libvirt-go" ) // network transparent image @@ -131,3 +133,23 @@ func newImage(source string) (image, error) { return nil, fmt.Errorf("Don't know how to read from '%s': %s", url.String(), err) } } + +func newCopier(virConn *libvirt.VirConnection, volume libvirt.VirStorageVol, size uint64) func(src io.Reader) error { + copier := func(src io.Reader) error { + stream, err := libvirt.NewVirStream(virConn, 0) + if err != nil { + return err + } + defer stream.Close() + + volume.Upload(stream, 0, size, 0) + + n, err := io.Copy(stream, src) + if err != nil { + return err + } + log.Printf("%d bytes uploaded\n", n) + return nil + } + return copier +} |