diff options
author | Flavio Castelli <fcastelli@suse.com> | 2016-06-12 23:29:30 +0200 |
---|---|---|
committer | Flavio Castelli <fcastelli@suse.com> | 2016-06-16 18:15:48 +0200 |
commit | 9a145accf7eb88591e8445bd2c8a2174c1c1cbe6 (patch) | |
tree | 08b3d86d241eafaa13b99711c6da51a81052a530 | |
parent | cf45c4af0ec47fd183d4eda339fd97bfcb0bfc35 (diff) | |
download | terraform-provider-libvirt-9a145accf7eb88591e8445bd2c8a2174c1c1cbe6.tar terraform-provider-libvirt-9a145accf7eb88591e8445bd2c8a2174c1c1cbe6.tar.gz |
Allow to specify the base volume by using a name
It's now possible to specify a base volume by using the
`base_volume_name`. This is an approach done also by other Terraform
providers: either use an "internal" ID or a human-friendly name.
This makes easier to share the same base volume between different
terraform projects.
Signed-off-by: Flavio Castelli <fcastelli@suse.com>
-rw-r--r-- | docs/providers/libvirt/r/volume.html.markdown | 3 | ||||
-rw-r--r-- | libvirt/resource_libvirt_volume.go | 48 |
2 files changed, 49 insertions, 2 deletions
diff --git a/docs/providers/libvirt/r/volume.html.markdown b/docs/providers/libvirt/r/volume.html.markdown index d7f18783..f76c718b 100644 --- a/docs/providers/libvirt/r/volume.html.markdown +++ b/docs/providers/libvirt/r/volume.html.markdown @@ -34,4 +34,5 @@ The following arguments are supported: * `size` - (Optional) The size of the volume in bytes (if you don't like this, help fix [this issue](https://github.com/hashicorp/terraform/issues/3287). If `source` is specified, `size` will be set to the source image file size. * `base_volume_id` - (Optional) The backing volume (CoW) to use for this volume. - +* `base_volume_name` - (Optional) The name of the backing volume (CoW) to use for this volume. Note well: when `base_volume_pool` is not specified the volume is going to be searched inside of `pool`. +* `base_volume_pool` - (Optional) The name of the pool containing the volume defined by `base_volume_name`. diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go index 588774d1..af565e15 100644 --- a/libvirt/resource_libvirt_volume.go +++ b/libvirt/resource_libvirt_volume.go @@ -136,6 +136,16 @@ func volumeCommonSchema() map[string]*schema.Schema { Optional: true, ForceNew: true, }, + "base_volume_pool": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "base_volume_name": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, } } @@ -196,7 +206,11 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("'size' can't be specified when also 'source' is given (the size will be set to the size of the source image.") } if _, ok := d.GetOk("base_volume_id"); ok { - return fmt.Errorf("'base_volume_id' can't be specified when also 'source' is given (the size will be set to the size of the base image.") + return fmt.Errorf("'base_volume_id' can't be specified when also 'source' is given.") + } + + if _, ok := d.GetOk("base_volume_name"); ok { + return fmt.Errorf("'base_volume_name' can't be specified when also 'source' is given.") } img, err := newImage(url.(string)) @@ -227,6 +241,10 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("'size' can't be specified when also 'base_volume_id' is given (the size will be set to the size of the backing image.") } + if _, ok := d.GetOk("base_volume_name"); ok { + return fmt.Errorf("'base_volume_name' can't be specified when also 'base_volume_id' is given.") + } + volumeDef.BackingStore = new(defBackingStore) volumeDef.BackingStore.Format.Type = "qcow2" baseVolume, err := virConn.LookupStorageVolByKey(baseVolumeId.(string)) @@ -240,6 +258,34 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error volumeDef.BackingStore.Path = baseVolPath } + if baseVolumeName, ok := d.GetOk("base_volume_name"); ok { + if _, ok := d.GetOk("size"); ok { + return fmt.Errorf("'size' can't be specified when also 'base_volume_name' is given (the size will be set to the size of the backing image.") + } + + baseVolumePool := pool + if _, ok := d.GetOk("base_volume_pool"); ok { + baseVolumePoolName := d.Get("base_volume_pool").(string) + baseVolumePool, err = virConn.LookupStoragePoolByName(baseVolumePoolName) + if err != nil { + return fmt.Errorf("can't find storage pool '%s'", baseVolumePoolName) + } + defer baseVolumePool.Free() + } + + volumeDef.BackingStore = new(defBackingStore) + volumeDef.BackingStore.Format.Type = "qcow2" + baseVolume, err := baseVolumePool.LookupStorageVolByName(baseVolumeName.(string)) + if err != nil { + return fmt.Errorf("Can't retrieve volume %s", baseVolumeName.(string)) + } + baseVolPath, err := baseVolume.GetPath() + if err != nil { + return fmt.Errorf("can't get name for base image '%s'", baseVolumeName) + } + volumeDef.BackingStore.Path = baseVolPath + } + volumeDefXml, err := xml.Marshal(volumeDef) if err != nil { return fmt.Errorf("Error serializing libvirt volume: %s", err) |