diff options
author | Dario Maiocchi <dmaiocchi@suse.com> | 2017-11-10 19:24:14 +0100 |
---|---|---|
committer | Dario Maiocchi <dmaiocchi@suse.com> | 2017-11-16 11:26:28 +0100 |
commit | 69abc17d32412ff2480afeeff0e87de2d5684225 (patch) | |
tree | 2421ebcd4c78533fa68a2cdbd510e5cf436d713a | |
parent | c738e395f0d57147cfa08e385574d171dff640e5 (diff) | |
download | terraform-provider-libvirt-69abc17d32412ff2480afeeff0e87de2d5684225.tar terraform-provider-libvirt-69abc17d32412ff2480afeeff0e87de2d5684225.tar.gz |
error strings should not be capitalized or end
with punctuation or a newline
-rw-r--r-- | libvirt/resource_cloud_init.go | 360 | ||||
-rw-r--r-- | libvirt/resource_libvirt_coreos_ignition.go | 6 | ||||
-rw-r--r-- | libvirt/resource_libvirt_domain.go | 16 | ||||
-rw-r--r-- | libvirt/resource_libvirt_network.go | 10 | ||||
-rw-r--r-- | libvirt/resource_libvirt_volume.go | 42 | ||||
-rw-r--r-- | libvirt/volume_def.go | 8 |
6 files changed, 332 insertions, 110 deletions
diff --git a/libvirt/resource_cloud_init.go b/libvirt/resource_cloud_init.go index 6221ebc5..86c72ac7 100644 --- a/libvirt/resource_cloud_init.go +++ b/libvirt/resource_cloud_init.go @@ -1,119 +1,341 @@ package libvirt import ( + "encoding/xml" "fmt" "log" + "net/http" + "strconv" "github.com/hashicorp/terraform/helper/schema" + libvirt "github.com/libvirt/libvirt-go" ) -func resourceCloudInit() *schema.Resource { - return &schema.Resource{ - Create: resourceCloudInitCreate, - Read: resourceCloudInitRead, - Delete: resourceCloudInitDelete, - Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "pool": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - Default: "default", - ForceNew: true, - }, - "local_hostname": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "user_data": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "ssh_authorized_key": &schema.Schema{ - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, +func volumeCommonSchema() map[string]*schema.Schema { + return map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "pool": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Default: "default", + ForceNew: true, + }, + "source": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "size": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + Computed: true, + ForceNew: true, + }, + "format": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "base_volume_id": &schema.Schema{ + Type: schema.TypeString, + 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, }, } } -func resourceCloudInitCreate(d *schema.ResourceData, meta interface{}) error { - log.Printf("[DEBUG] creating cloudinit") +func resourceLibvirtVolume() *schema.Resource { + return &schema.Resource{ + Create: resourceLibvirtVolumeCreate, + Read: resourceLibvirtVolumeRead, + Delete: resourceLibvirtVolumeDelete, + Schema: volumeCommonSchema(), + } +} + +func remoteImageSize(url string) (int, error) { + response, err := http.Head(url) + if err != nil { + return 0, err + } + length, err := strconv.Atoi(response.Header.Get("Content-Length")) + if err != nil { + return 0, err + } + return length, nil +} + +func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") + } + + poolName := "default" + if _, ok := d.GetOk("pool"); ok { + poolName = d.Get("pool").(string) + } + + PoolSync.AcquireLock(poolName) + defer PoolSync.ReleaseLock(poolName) + + pool, err := virConn.LookupStoragePoolByName(poolName) + if err != nil { + return fmt.Errorf("can't find storage pool '%s'", poolName) } + defer pool.Free() + + // Refresh the pool of the volume so that libvirt knows it is + // not longer in use. + WaitForSuccess("Error refreshing pool for volume", func() error { + return pool.Refresh(0) + }) - cloudInit := newCloudInitDef() - cloudInit.Metadata.LocalHostname = d.Get("local_hostname").(string) - cloudInit.UserDataRaw = d.Get("user_data").(string) + volumeDef := newDefVolume() - if _, ok := d.GetOk("ssh_authorized_key"); ok { - sshKey := d.Get("ssh_authorized_key").(string) - cloudInit.UserData.SSHAuthorizedKeys = append( - cloudInit.UserData.SSHAuthorizedKeys, - sshKey) + if name, ok := d.GetOk("name"); ok { + volumeDef.Name = name.(string) } - cloudInit.Name = d.Get("name").(string) - cloudInit.PoolName = d.Get("pool").(string) + volumeFormat := "qcow2" + if _, ok := d.GetOk("format"); ok { + volumeFormat = d.Get("format").(string) + } + volumeDef.Target.Format.Type = volumeFormat + + var ( + img image + volume *libvirt.StorageVol = nil + ) + + // an source image was given, this mean we can't choose size + if source, ok := d.GetOk("source"); ok { + // source and size conflict + 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") + } + if _, ok := d.GetOk("base_volume_id"); ok { + 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") + } - log.Printf("[INFO] cloudInit: %+v", cloudInit) + // 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 + } + + // update the image in the description, even if the file has not changed + if size, err := img.Size(); err != nil { + return err + } else { + log.Printf("Image %s image is: %d bytes", img, size) + volumeDef.Capacity.Unit = "B" + volumeDef.Capacity.Value = size + } + } else { + _, noSize := d.GetOk("size") + _, noBaseVol := d.GetOk("base_volume_id") + + if noSize && noBaseVol { + return fmt.Errorf("'size' needs to be specified if no 'source' or 'base_volume_id' is given") + } + volumeDef.Capacity.Value = uint64(d.Get("size").(int)) + } - key, err := cloudInit.CreateAndUpload(virConn) + if baseVolumeId, ok := d.GetOk("base_volume_id"); ok { + if _, ok := d.GetOk("size"); ok { + 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") + } + + volume = nil + baseVolume, err := virConn.LookupStorageVolByKey(baseVolumeId.(string)) + if err != nil { + return fmt.Errorf("Can't retrieve volume %s", baseVolumeId.(string)) + } + backingStoreDef, err := newDefBackingStoreFromLibvirt(baseVolume) + if err != nil { + return fmt.Errorf("Could not retrieve backing store %s", baseVolumeId.(string)) + } + volumeDef.BackingStore = &backingStoreDef + } + + 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") + } + + volume = nil + 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() + } + baseVolume, err := baseVolumePool.LookupStorageVolByName(baseVolumeName.(string)) + if err != nil { + return fmt.Errorf("Can't retrieve volume %s", baseVolumeName.(string)) + } + backingStoreDef, err := newDefBackingStoreFromLibvirt(baseVolume) + if err != nil { + return fmt.Errorf("Could not retrieve backing store %s", baseVolumeName.(string)) + } + volumeDef.BackingStore = &backingStoreDef + } + + if volume == nil { + volumeDefXml, err := xml.Marshal(volumeDef) + if err != nil { + return fmt.Errorf("Error serializing libvirt volume: %s", err) + } + + // create the volume + v, err := pool.StorageVolCreateXML(string(volumeDefXml), 0) + if err != nil { + return fmt.Errorf("Error creating libvirt volume: %s", err) + } + volume = v + defer volume.Free() + } + + // we use the key as the id + key, err := volume.GetKey() if err != nil { - return err + return fmt.Errorf("Error retrieving volume key: %s", err) } d.SetId(key) // make sure we record the id even if the rest of this gets interrupted - d.Partial(true) // make sure we record the id even if the rest of this gets interrupted + d.Partial(true) d.Set("id", key) d.SetPartial("id") - // TODO: at this point we have collected more things than the ID, so let's save as many things as we can d.Partial(false) - return resourceCloudInitRead(d, meta) + log.Printf("[INFO] Volume ID: %s", d.Id()) + + // upload source if present + if _, ok := d.GetOk("source"); ok { + err = img.Import(newCopier(virConn, volume, volumeDef.Capacity.Value), volumeDef) + if err != nil { + return fmt.Errorf("Error while uploading source %s: %s", img.String(), err) + } + } + + return resourceLibvirtVolumeRead(d, meta) } -func resourceCloudInitRead(d *schema.ResourceData, meta interface{}) error { +func resourceLibvirtVolumeRead(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") + } + + volume, err := virConn.LookupStorageVolByKey(d.Id()) + if err != nil { + virErr := err.(libvirt.Error) + if virErr.Code != libvirt.ERR_NO_STORAGE_VOL { + return fmt.Errorf("Can't retrieve volume %s", d.Id()) + } + + log.Printf("[INFO] Volume %s not found, attempting to start its pool") + + volId := d.Id() + volPoolName := d.Get("pool").(string) + volPool, err := virConn.LookupStoragePoolByName(volPoolName) + if err != nil { + return fmt.Errorf("Error retrieving pool %s for volume %s: %s", volPoolName, volId, err) + } + defer volPool.Free() + + active, err := volPool.IsActive() + if err != nil { + return fmt.Errorf("error retrieving status of pool %s for volume %s: %s", volPoolName, volId, err) + } + if active { + return fmt.Errorf("can't retrieve volume %s", d.Id()) + } + + err = volPool.Create(0) + if err != nil { + return fmt.Errorf("error starting pool %s: %s", volPoolName, err) + } + + // attempt a new lookup + volume, err = virConn.LookupStorageVolByKey(d.Id()) + if err != nil { + return fmt.Errorf("second attempt: Can't retrieve volume %s", d.Id()) + } + } + defer volume.Free() + + volName, err := volume.GetName() + if err != nil { + return fmt.Errorf("error retrieving volume name: %s", err) } - ci, err := newCloudInitDefFromRemoteISO(virConn, d.Id()) + volPool, err := volume.LookupPoolByVolume() if err != nil { - return fmt.Errorf("Error while retrieving remote ISO: %s", err) + return fmt.Errorf("error retrieving pool for volume: %s", err) } - d.Set("pool", ci.PoolName) - d.Set("name", ci.Name) - d.Set("local_hostname", ci.Metadata.LocalHostname) - d.Set("user_data", ci.UserDataRaw) + defer volPool.Free() - if len(ci.UserData.SSHAuthorizedKeys) == 1 { - d.Set("ssh_authorized_key", ci.UserData.SSHAuthorizedKeys[0]) + volPoolName, err := volPool.GetName() + if err != nil { + return fmt.Errorf("error retrieving pool name: %s", err) + } + + d.Set("pool", volPoolName) + d.Set("name", volName) + + info, err := volume.GetInfo() + if err != nil { + return fmt.Errorf("error retrieving volume name: %s", err) } + d.Set("size", info.Capacity) return nil } -func resourceCloudInitDelete(d *schema.ResourceData, meta interface{}) error { +func resourceLibvirtVolumeDelete(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") - } - - key, err := getCloudInitVolumeKeyFromTerraformID(d.Id()) - if err != nil { - return err + return fmt.Errorf("the libvirt connection was nil") } - return RemoveVolume(virConn, key) + return RemoveVolume(virConn, d.Id()) } diff --git a/libvirt/resource_libvirt_coreos_ignition.go b/libvirt/resource_libvirt_coreos_ignition.go index 7762a183..58af44ed 100644 --- a/libvirt/resource_libvirt_coreos_ignition.go +++ b/libvirt/resource_libvirt_coreos_ignition.go @@ -36,7 +36,7 @@ func resourceIgnitionCreate(d *schema.ResourceData, meta interface{}) error { log.Printf("[DEBUG] creating ignition file") virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } ignition := newIgnitionDef() @@ -66,7 +66,7 @@ func resourceIgnitionCreate(d *schema.ResourceData, meta interface{}) error { func resourceIgnitionRead(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } ign, err := newIgnitionDefFromRemoteVol(virConn, d.Id()) @@ -83,7 +83,7 @@ func resourceIgnitionRead(d *schema.ResourceData, meta interface{}) error { func resourceIgnitionDelete(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } key, err := getIgnitionVolumeKeyFromTerraformID(d.Id()) diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 40bed468..3ca8d43f 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -192,7 +192,7 @@ func resourceLibvirtDomainExists(d *schema.ResourceData, meta interface{}) (bool virConn := meta.(*Client).libvirt if virConn == nil { - return false, fmt.Errorf("The libvirt connection was nil.") + return false, fmt.Errorf("the libvirt connection was nil") } domain, err := virConn.LookupDomainByUUIDString(d.Id()) @@ -212,7 +212,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } domainDef, err := newDomainDefForConnection(virConn) @@ -284,7 +284,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error if firmware, ok := d.GetOk("firmware"); ok { firmwareFile := firmware.(string) if _, err := os.Stat(firmwareFile); os.IsNotExist(err) { - return fmt.Errorf("Could not find firmware file '%s'.", firmwareFile) + return fmt.Errorf("could not find firmware file '%s'", firmwareFile) } domainDef.OS.Loader = &libvirtxml.DomainLoader{ Path: firmwareFile, @@ -298,13 +298,13 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error nvramFile := nvramMap["file"].(string) if _, err := os.Stat(nvramFile); os.IsNotExist(err) { - return fmt.Errorf("Could not find nvram file '%s'.", nvramFile) + return fmt.Errorf("could not find nvram file '%s'", nvramFile) } nvramTemplateFile := "" if nvramTemplate, ok := nvramMap["template"]; ok { nvramTemplateFile = nvramTemplate.(string) if _, err := os.Stat(nvramTemplateFile); os.IsNotExist(err) { - return fmt.Errorf("Could not find nvram template file '%s'.", nvramTemplateFile) + return fmt.Errorf("could not find nvram template file '%s'", nvramTemplateFile) } } domainDef.OS.NVRam = &libvirtxml.DomainNVRam{ @@ -705,7 +705,7 @@ func resourceLibvirtDomainUpdate(d *schema.ResourceData, meta interface{}) error virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } domain, err := virConn.LookupDomainByUUIDString(d.Id()) if err != nil { @@ -805,7 +805,7 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } domain, err := virConn.LookupDomainByUUIDString(d.Id()) @@ -1024,7 +1024,7 @@ func resourceLibvirtDomainDelete(d *schema.ResourceData, meta interface{}) error virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } log.Printf("[DEBUG] Deleting domain %s", d.Id()) diff --git a/libvirt/resource_libvirt_network.go b/libvirt/resource_libvirt_network.go index f1cc46de..88dcac7a 100644 --- a/libvirt/resource_libvirt_network.go +++ b/libvirt/resource_libvirt_network.go @@ -113,7 +113,7 @@ func dnsForwarderSchema() map[string]*schema.Schema { func resourceLibvirtNetworkExists(d *schema.ResourceData, meta interface{}) (bool, error) { virConn := meta.(*Client).libvirt if virConn == nil { - return false, fmt.Errorf("The libvirt connection was nil.") + return false, fmt.Errorf("the libvirt connection was nil") } network, err := virConn.LookupNetworkByUUIDString(d.Id()) if err != nil { @@ -132,7 +132,7 @@ func resourceLibvirtNetworkExists(d *schema.ResourceData, meta interface{}) (boo func resourceLibvirtNetworkUpdate(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } network, err := virConn.LookupNetworkByUUIDString(d.Id()) if err != nil { @@ -165,7 +165,7 @@ func resourceLibvirtNetworkCreate(d *schema.ResourceData, meta interface{}) erro // see https://libvirt.org/formatnetwork.html virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } networkDef := newNetworkDef() @@ -337,7 +337,7 @@ func resourceLibvirtNetworkCreate(d *schema.ResourceData, meta interface{}) erro func resourceLibvirtNetworkRead(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } network, err := virConn.LookupNetworkByUUIDString(d.Id()) @@ -396,7 +396,7 @@ func resourceLibvirtNetworkRead(d *schema.ResourceData, meta interface{}) error func resourceLibvirtNetworkDelete(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } log.Printf("[DEBUG] Deleting network ID %s", d.Id()) diff --git a/libvirt/resource_libvirt_volume.go b/libvirt/resource_libvirt_volume.go index 234772db..8a16bf0b 100644 --- a/libvirt/resource_libvirt_volume.go +++ b/libvirt/resource_libvirt_volume.go @@ -82,7 +82,7 @@ func remoteImageSize(url string) (int, error) { func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } poolName := "default" @@ -101,7 +101,7 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error // Refresh the pool of the volume so that libvirt knows it is // not longer in use. - WaitForSuccess("Error refreshing pool for volume", func() error { + WaitForSuccess("error refreshing pool for volume", func() error { return pool.Refresh(0) }) @@ -126,25 +126,25 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error if source, ok := d.GetOk("source"); ok { // source and size conflict 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.") + 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.") + 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.") + 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) + 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) + return fmt.Errorf("could not get a volume definition from XML for %s: %s", volumeDef.Name, err) } } } @@ -166,18 +166,18 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error _, noBaseVol := d.GetOk("base_volume_id") if noSize && noBaseVol { - return fmt.Errorf("'size' needs to be specified if no 'source' or 'base_volume_id' is given.") + return fmt.Errorf("'size' needs to be specified if no 'source' or 'base_volume_id' is given") } volumeDef.Capacity.Value = uint64(d.Get("size").(int)) } if baseVolumeId, ok := d.GetOk("base_volume_id"); ok { if _, ok := d.GetOk("size"); ok { - 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.") + 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.") + return fmt.Errorf("'base_volume_name' can't be specified when also 'base_volume_id' is given") } volume = nil @@ -194,7 +194,7 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error 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.") + 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") } volume = nil @@ -262,7 +262,7 @@ func resourceLibvirtVolumeCreate(d *schema.ResourceData, meta interface{}) error func resourceLibvirtVolumeRead(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } volume, err := virConn.LookupStorageVolByKey(d.Id()) @@ -284,39 +284,39 @@ func resourceLibvirtVolumeRead(d *schema.ResourceData, meta interface{}) error { active, err := volPool.IsActive() if err != nil { - return fmt.Errorf("Error retrieving status of pool %s for volume %s: %s", volPoolName, volId, err) + return fmt.Errorf("error retrieving status of pool %s for volume %s: %s", volPoolName, volId, err) } if active { - return fmt.Errorf("Can't retrieve volume %s", d.Id()) + return fmt.Errorf("can't retrieve volume %s", d.Id()) } err = volPool.Create(0) if err != nil { - return fmt.Errorf("Error starting pool %s: %s", volPoolName, err) + return fmt.Errorf("error starting pool %s: %s", volPoolName, err) } // attempt a new lookup volume, err = virConn.LookupStorageVolByKey(d.Id()) if err != nil { - return fmt.Errorf("Second attempt: Can't retrieve volume %s", d.Id()) + return fmt.Errorf("second attempt: Can't retrieve volume %s", d.Id()) } } defer volume.Free() volName, err := volume.GetName() if err != nil { - return fmt.Errorf("Error retrieving volume name: %s", err) + return fmt.Errorf("error retrieving volume name: %s", err) } volPool, err := volume.LookupPoolByVolume() if err != nil { - return fmt.Errorf("Error retrieving pool for volume: %s", err) + return fmt.Errorf("error retrieving pool for volume: %s", err) } defer volPool.Free() volPoolName, err := volPool.GetName() if err != nil { - return fmt.Errorf("Error retrieving pool name: %s", err) + return fmt.Errorf("error retrieving pool name: %s", err) } d.Set("pool", volPoolName) @@ -324,7 +324,7 @@ func resourceLibvirtVolumeRead(d *schema.ResourceData, meta interface{}) error { info, err := volume.GetInfo() if err != nil { - return fmt.Errorf("Error retrieving volume name: %s", err) + return fmt.Errorf("error retrieving volume name: %s", err) } d.Set("size", info.Capacity) @@ -334,7 +334,7 @@ func resourceLibvirtVolumeRead(d *schema.ResourceData, meta interface{}) error { func resourceLibvirtVolumeDelete(d *schema.ResourceData, meta interface{}) error { virConn := meta.(*Client).libvirt if virConn == nil { - return fmt.Errorf("The libvirt connection was nil.") + return fmt.Errorf("the libvirt connection was nil") } return RemoveVolume(virConn, d.Id()) diff --git a/libvirt/volume_def.go b/libvirt/volume_def.go index 6d867240..f520a164 100644 --- a/libvirt/volume_def.go +++ b/libvirt/volume_def.go @@ -38,15 +38,15 @@ func newDefVolumeFromXML(s string) (libvirtxml.StorageVolume, error) { func newDefVolumeFromLibvirt(volume *libvirt.StorageVol) (libvirtxml.StorageVolume, error) { name, err := volume.GetName() if err != nil { - return libvirtxml.StorageVolume{}, fmt.Errorf("could not get name for volume: %s.", err) + return libvirtxml.StorageVolume{}, fmt.Errorf("could not get name for volume: %s", err) } volumeDefXml, err := volume.GetXMLDesc(0) if err != nil { - return libvirtxml.StorageVolume{}, fmt.Errorf("could not get XML description for volume %s: %s.", name, err) + return libvirtxml.StorageVolume{}, fmt.Errorf("could not get XML description for volume %s: %s", name, err) } volumeDef, err := newDefVolumeFromXML(volumeDefXml) if err != nil { - return libvirtxml.StorageVolume{}, fmt.Errorf("could not get a volume definition from XML for %s: %s.", volumeDef.Name, err) + return libvirtxml.StorageVolume{}, fmt.Errorf("could not get a volume definition from XML for %s: %s", volumeDef.Name, err) } return volumeDef, nil } @@ -54,7 +54,7 @@ func newDefVolumeFromLibvirt(volume *libvirt.StorageVol) (libvirtxml.StorageVolu func newDefBackingStoreFromLibvirt(baseVolume *libvirt.StorageVol) (libvirtxml.StorageVolumeBackingStore, error) { baseVolumeDef, err := newDefVolumeFromLibvirt(baseVolume) if err != nil { - return libvirtxml.StorageVolumeBackingStore{}, fmt.Errorf("could not get volume: %s.", err) + return libvirtxml.StorageVolumeBackingStore{}, fmt.Errorf("could not get volume: %s", err) } baseVolPath, err := baseVolume.GetPath() if err != nil { |