aboutsummaryrefslogtreecommitdiff
path: root/libvirt
diff options
context:
space:
mode:
Diffstat (limited to 'libvirt')
-rw-r--r--libvirt/resource_libvirt_domain.go8
-rw-r--r--libvirt/resource_libvirt_volume.go24
-rw-r--r--libvirt/resource_libvirt_volume_test.go27
-rw-r--r--libvirt/utils_libvirt.go9
4 files changed, 47 insertions, 21 deletions
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go
index 95ac6c52..616f919b 100644
--- a/libvirt/resource_libvirt_domain.go
+++ b/libvirt/resource_libvirt_domain.go
@@ -605,9 +605,9 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
if ip == nil {
return fmt.Errorf("Could not parse addresses '%s'", address)
}
- // TODO: we should check the IP is contained in the DHCP addresses served
+
log.Printf("[INFO] Adding IP/MAC/host=%s/%s/%s to %s", ip.String(), mac, hostname, networkName)
- if err := addHost(network, ip.String(), mac, hostname); err != nil {
+ if err := updateOrAddHost(network, ip.String(), mac, hostname); err != nil {
return err
}
}
@@ -738,7 +738,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error
for _, addressI := range addressesI.([]interface{}) {
address := addressI.(string)
log.Printf("[INFO] Finally adding IP/MAC/host=%s/%s/%s", address, mac, pending.hostname)
- addHost(pending.network, address, mac, pending.hostname)
+ updateOrAddHost(pending.network, address, mac, pending.hostname)
if err != nil {
return fmt.Errorf("Could not add IP/MAC/host=%s/%s/%s: %s", address, mac, pending.hostname, err)
}
@@ -836,7 +836,7 @@ func resourceLibvirtDomainUpdate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("Could not parse addresses '%s'", address)
}
log.Printf("[INFO] Updating IP/MAC/host=%s/%s/%s in '%s' network", ip.String(), mac, hostname, networkName)
- if err := updateHost(network, ip.String(), mac, hostname); err != nil {
+ if err := updateOrAddHost(network, ip.String(), mac, hostname); err != nil {
return err
}
}
diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go
index 7a7f6a40..4f9c3322 100644
--- a/libvirt/resource_libvirt_volume.go
+++ b/libvirt/resource_libvirt_volume.go
@@ -105,12 +105,15 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
return pool.Refresh(0)
})
- volumeDef := newDefVolume()
-
- if name, ok := d.GetOk("name"); ok {
- volumeDef.Name = name.(string)
+ // Check whether the storage volume already exists. Its name needs to be
+ // unique.
+ if _, err := pool.LookupStorageVolByName(d.Get("name").(string)); err == nil {
+ return fmt.Errorf("storage volume '%s' already exists", d.Get("name").(string))
}
+ volumeDef := newDefVolume()
+ volumeDef.Name = d.Get("name").(string)
+
volumeFormat := "qcow2"
if _, ok := d.GetOk("format"); ok {
volumeFormat = d.Get("format").(string)
@@ -136,19 +139,6 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("'base_volume_name' can't be specified when also 'source' is given")
}
- // Check if we already have this image in the pool
- if len(volumeDef.Name) > 0 {
- if v, err := pool.LookupStorageVolByName(volumeDef.Name); err != nil {
- log.Printf("could not find image %s in pool %s", volumeDef.Name, poolName)
- } else {
- volume = v
- volumeDef, err = newDefVolumeFromLibvirt(volume)
- if err != nil {
- return fmt.Errorf("could not get a volume definition from XML for %s: %s", volumeDef.Name, err)
- }
- }
- }
-
if img, err = newImage(source.(string)); err != nil {
return err
}
diff --git a/libvirt/resource_libvirt_volume_test.go b/libvirt/resource_libvirt_volume_test.go
index d185b65e..65c658f1 100644
--- a/libvirt/resource_libvirt_volume_test.go
+++ b/libvirt/resource_libvirt_volume_test.go
@@ -2,6 +2,7 @@ package libvirt
import (
"fmt"
+ "regexp"
"testing"
"github.com/hashicorp/terraform/helper/resource"
@@ -109,6 +110,32 @@ func TestAccLibvirtVolume_Basic(t *testing.T) {
})
}
+func TestAccLibvirtVolume_UniqueName(t *testing.T) {
+ const config = `
+ resource "libvirt_volume" "terraform-acceptance-test-1" {
+ name = "terraform-test"
+ size = 1073741824
+ }
+
+ resource "libvirt_volume" "terraform-acceptance-test-2" {
+ name = "terraform-test"
+ size = 1073741824
+ }
+ `
+
+ resource.Test(t, resource.TestCase{
+ PreCheck: func() { testAccPreCheck(t) },
+ Providers: testAccProviders,
+ CheckDestroy: testAccCheckLibvirtVolumeDestroy,
+ Steps: []resource.TestStep{
+ {
+ Config: config,
+ ExpectError: regexp.MustCompile(`storage volume 'terraform-test' already exists`),
+ },
+ },
+ })
+}
+
func TestAccLibvirtVolume_DownloadFromSource(t *testing.T) {
var volume libvirt.StorageVol
diff --git a/libvirt/utils_libvirt.go b/libvirt/utils_libvirt.go
index 22816769..0b1da1ef 100644
--- a/libvirt/utils_libvirt.go
+++ b/libvirt/utils_libvirt.go
@@ -46,6 +46,15 @@ func updateHost(n *libvirt.Network, ip, mac, name string) error {
return n.Update(libvirt.NETWORK_UPDATE_COMMAND_MODIFY, libvirt.NETWORK_SECTION_IP_DHCP_HOST, -1, xmlDesc, libvirt.NETWORK_UPDATE_AFFECT_CURRENT)
}
+// Tries to update first, if that fails, it will add it
+func updateOrAddHost(n *libvirt.Network, ip, mac, name string) error {
+ err := updateHost(n, ip, mac, name)
+ if virErr, ok := err.(libvirt.Error); ok && virErr.Code == libvirt.ERR_OPERATION_INVALID && virErr.Domain == libvirt.FROM_NETWORK {
+ return addHost(n, ip, mac, name)
+ }
+ return err
+}
+
func getHostArchitecture(virConn *libvirt.Connect) (string, error) {
type HostCapabilities struct {
XMLName xml.Name `xml:"capabilities"`