summaryrefslogtreecommitdiff
path: root/libvirt
diff options
context:
space:
mode:
authorDuncan Mac-Vicar P <dmacvicar@suse.de>2016-03-10 23:43:17 +0100
committerDuncan Mac-Vicar P <dmacvicar@suse.de>2016-03-10 23:43:17 +0100
commit34649d5fe59659c056ffe31b6d28ea50003caf3d (patch)
treeca1267c444c5ac4f1402818c974d05790971aa1a /libvirt
parent653b575affed28a293680a9b860c82d98664bb8e (diff)
downloadterraform-provider-libvirt-34649d5fe59659c056ffe31b6d28ea50003caf3d.tar
terraform-provider-libvirt-34649d5fe59659c056ffe31b6d28ea50003caf3d.tar.gz
disk definition
Diffstat (limited to 'libvirt')
-rw-r--r--libvirt/config.go3
-rw-r--r--libvirt/disk_def.go45
-rw-r--r--libvirt/domain_def.go19
-rw-r--r--libvirt/resource_libvirt_domain.go97
-rw-r--r--libvirt/resource_libvirt_volume.go18
5 files changed, 96 insertions, 86 deletions
diff --git a/libvirt/config.go b/libvirt/config.go
index 75507d68..6217ad0a 100644
--- a/libvirt/config.go
+++ b/libvirt/config.go
@@ -1,7 +1,8 @@
package libvirt
import (
- libvirt "gopkg.in/alexzorin/libvirt-go.v2"
+ //libvirt "gopkg.in/alexzorin/libvirt-go.v2"
+ libvirt "github.com/dmacvicar/libvirt-go"
"log"
)
diff --git a/libvirt/disk_def.go b/libvirt/disk_def.go
new file mode 100644
index 00000000..afd3020f
--- /dev/null
+++ b/libvirt/disk_def.go
@@ -0,0 +1,45 @@
+package libvirt
+
+import (
+ "encoding/xml"
+ "github.com/hashicorp/terraform/helper/schema"
+)
+
+type defDisk struct {
+ XMLName xml.Name `xml:"disk"`
+ Type string `xml:"type,attr"`
+ Device string `xml:"device,attr"`
+ Format struct {
+ Type string `xml:"type,attr"`
+ } `xml:"format"`
+ Source struct {
+ Pool string `xml:"pool,attr"`
+ Volume string `xml:"volume,attr"`
+ } `xml:"source"`
+ Target struct {
+ Dev string `xml:"dev,attr"`
+ Bus string `xml:"bus,attr"`
+ } `xml:"target"`
+}
+
+func diskCommonSchema() map[string]*schema.Schema{
+ return map[string]*schema.Schema{
+ "volume_id": &schema.Schema{
+ Type: schema.TypeString,
+ Required: true,
+ ForceNew: true,
+ },
+ }
+}
+
+func newDefDisk() defDisk {
+ disk := defDisk{}
+ disk.Type = "volume"
+ disk.Device = "disk"
+ disk.Format.Type = "qcow2"
+
+ disk.Target.Dev = "sda"
+ disk.Target.Bus = "virtio"
+
+ return disk
+}
diff --git a/libvirt/domain_def.go b/libvirt/domain_def.go
index 660c6e5c..c3232246 100644
--- a/libvirt/domain_def.go
+++ b/libvirt/domain_def.go
@@ -4,23 +4,6 @@ import (
"encoding/xml"
)
-type defDisk struct {
- XMLName xml.Name `xml:"disk"`
- Type string `xml:"type,attr"`
- Device string `xml:"device,attr"`
- Format struct {
- Type string `xml:"type,attr"`
- } `xml:"format"`
- Source struct {
- Pool string `xml:"pool,attr"`
- Volume string `xml:"volume,attr"`
- } `xml:"source"`
- Target struct {
- Dev string `xml:"dev,attr"`
- Bus string `xml:"bus,attr"`
- } `xml:"target"`
-}
-
type defDomain struct {
XMLName xml.Name `xml:"domain"`
Name string `xml:"name"`
@@ -29,7 +12,7 @@ type defDomain struct {
Memory defMemory `xml:"memory"`
VCpu defVCpu `xml:"vcpu"`
Devices struct {
- RootDisk defDisk `xml:"disk"`
+ Disks []defDisk `xml:"disk"`
Spice struct {
Type string `xml:"type,attr"`
Autoport string `xml:"autoport,attr"`
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go
index e38f9130..e6d1d77a 100644
--- a/libvirt/resource_libvirt_domain.go
+++ b/libvirt/resource_libvirt_domain.go
@@ -5,11 +5,11 @@ import (
"fmt"
"log"
"strconv"
- "strings"
"time"
//"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
- libvirt "gopkg.in/alexzorin/libvirt-go.v2"
+ //libvirt "gopkg.in/alexzorin/libvirt-go.v2"
+ libvirt "github.com/dmacvicar/libvirt-go"
)
func resourceLibvirtDomain() *schema.Resource {
@@ -33,9 +33,13 @@ func resourceLibvirtDomain() *schema.Resource {
Optional: true,
Default: 512,
},
- "base_image": &schema.Schema{
- Type: schema.TypeString,
- Optional: true,
+ "disk": &schema.Schema{
+ Type: schema.TypeList,
+ Required: true,
+ ForceNew: true,
+ Elem: &schema.Resource{
+ Schema: diskCommonSchema(),
+ },
},
},
}
@@ -47,72 +51,41 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("The libvirt connection was nil.")
}
- // setup base image and disks
- baseImageSpec := d.Get("base_image").(string)
- // if the base image was specifies as $pool/$name
- components := strings.Split(baseImageSpec, "/")
- storagePool := "default"
- baseImage := ""
- if len(components) > 1 {
- storagePool = components[0]
- baseImage = components[1]
- } else {
- baseImage = components[0]
- }
-
- pool, err := virConn.LookupStoragePoolByName(storagePool)
- if err != nil {
- return fmt.Errorf("can't find storage pool '%s'", storagePool)
- }
- baseVol, err := pool.LookupStorageVolByName(baseImage)
- if err != nil {
- return fmt.Errorf("can't find image '%s' in pool '%s'", baseImage, storagePool)
- }
-
- // create the volume
- rootVolumeDef := defVolume{}
- rootVolumeDef.Name = "__terraform_" + d.Get("name").(string) + "-rootdisk"
- rootVolumeDef.Target.Format.Type = "qcow2"
- // use the base image as backing store
- rootVolumeDef.BackingStore = new(defBackingStore)
- rootVolumeDef.BackingStore.Format.Type = "qcow2"
- baseVolPath, err := baseVol.GetPath()
- if err != nil {
- return fmt.Errorf("can't get name for base image '%s'", baseImage)
- }
- rootVolumeDef.BackingStore.Path = baseVolPath
- rootVolumeDefXml, err := xml.Marshal(rootVolumeDef)
- if err != nil {
- return fmt.Errorf("Error serializing libvirt volume: %s", err)
- }
+ disksCount := d.Get("disk.#").(int)
+ disks := make([]defDisk, 0, disksCount)
+ for i := 0; i < disksCount; i++ {
+ prefix := fmt.Sprintf("disk.%d", i)
+ disk := newDefDisk()
- // create the volume
- rootVolume, err := pool.StorageVolCreateXML(string(rootVolumeDefXml), 0)
- if err != nil {
- return fmt.Errorf("Error creating libvirt volume: %s", err)
- }
+ volumeKey := d.Get(prefix + ".volume_id").(string)
+ diskVolume, err := virConn.LookupStorageVolByKey(volumeKey)
+ if err != nil {
+ return fmt.Errorf("Can't retrieve volume %s", volumeKey)
+ }
+ diskVolumeName, err := diskVolume.GetName()
+ if err != nil {
+ return fmt.Errorf("Error retrieving volume name: %s", err)
+ }
+ diskPool, err := diskVolume.LookupPoolByVolume()
+ if err != nil {
+ return fmt.Errorf("Error retrieving pool for volume: %s", err)
+ }
+ diskPoolName, err := diskPool.GetName()
+ if err != nil {
+ return fmt.Errorf("Error retrieving pool name: %s", err)
+ }
- // create the disk
- rootDisk := defDisk{}
- rootDisk.Type = "volume"
- rootDisk.Device = "disk"
- rootDisk.Format.Type = "qcow2"
+ disk.Source.Volume = diskVolumeName
+ disk.Source.Pool = diskPoolName
- rootVolumeName, err := rootVolume.GetName()
- if err != nil {
- return fmt.Errorf("Error retrieving volume name: %s", err)
+ disks = append(disks, disk)
}
- rootDisk.Source.Volume = rootVolumeName
- rootDisk.Source.Pool = storagePool
- rootDisk.Target.Dev = "sda"
- rootDisk.Target.Bus = "virtio"
-
domainDef := newDomainDef()
domainDef.Name = d.Get("name").(string)
domainDef.Memory.Amount = d.Get("memory").(int)
domainDef.VCpu.Amount = d.Get("vcpu").(int)
- domainDef.Devices.RootDisk = rootDisk
+ domainDef.Devices.Disks = disks
connectURI, err := virConn.GetURI()
if err != nil {
diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go
index b37a2867..59398308 100644
--- a/libvirt/resource_libvirt_volume.go
+++ b/libvirt/resource_libvirt_volume.go
@@ -9,7 +9,8 @@ import (
"strconv"
//"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
- libvirt "gopkg.in/alexzorin/libvirt-go.v2"
+ //libvirt "gopkg.in/alexzorin/libvirt-go.v2"
+ libvirt "github.com/dmacvicar/libvirt-go"
)
func volumeCommonSchema() map[string]*schema.Schema {
@@ -30,6 +31,7 @@ func volumeCommonSchema() map[string]*schema.Schema {
"size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
+ Computed: true,
},
"base_volume": &schema.Schema{
Type: schema.TypeString,
@@ -82,7 +84,7 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
// an existing image was given, this mean we can't choose size
if url, ok := d.GetOk("source"); ok {
// source and size conflict
- if d.Get("size").(int) != -1 {
+ if _, ok := d.GetOk("size"); ok {
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.")
}
@@ -93,15 +95,21 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
log.Printf("Remote image is: %d bytes", size)
volumeDef.Capacity.Unit = "B"
volumeDef.Capacity.Amount = size
-
} else {
- if _, ok := d.GetOk("size"); !ok {
- return fmt.Errorf("'size' needs to be specified if no 'source' is given.")
+ _, noSize := d.GetOk("size")
+ _, noBaseVol := d.GetOk("base_volume")
+
+ if noSize && noBaseVol {
+ return fmt.Errorf("'size' needs to be specified if no 'source' or 'base_vol' is given.")
}
volumeDef.Capacity.Amount = d.Get("size").(int)
}
if baseVolumeId, ok := d.GetOk("base_volume"); ok {
+ if _, ok := d.GetOk("size"); ok {
+ return fmt.Errorf("'size' can't be specified when also 'base_volume' is given (the size will be set to the size of the backing image.")
+ }
+
volumeDef.BackingStore = new(defBackingStore)
volumeDef.BackingStore.Format.Type = "qcow2"
baseVolume, err := virConn.LookupStorageVolByKey(baseVolumeId.(string))