Skip to content

Commit 2de95d1

Browse files
committed
remove image remote http format sniffing based on HTTP Range
Instead: - Use the file extension - Only auto-detect if no format is specified, unlike the current implmentation which always autodetects and errors on inconsistency of specified vs auto-detected. Closes: #958
1 parent c6406ac commit 2de95d1

File tree

2 files changed

+12
-40
lines changed

2 files changed

+12
-40
lines changed

libvirt/resource_libvirt_volume.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ func resourceLibvirtVolumeCreate(ctx context.Context, d *schema.ResourceData, me
127127
if _, ok := d.GetOk("size"); ok {
128128
return diag.Errorf("'size' can't be specified when also 'source' is given (the size will be set to the size of the source image")
129129
}
130+
130131
if _, ok := d.GetOk("base_volume_id"); ok {
131132
return diag.Errorf("'base_volume_id' can't be specified when also 'source' is given")
132133
}
@@ -139,17 +140,16 @@ func resourceLibvirtVolumeCreate(ctx context.Context, d *schema.ResourceData, me
139140
return diag.FromErr(err)
140141
}
141142

142-
// figure out the format of the image
143-
isQCOW2, err := img.IsQCOW2()
144-
if err != nil {
145-
return diag.Errorf("error while determining image type for %s: %s", img.String(), err)
146-
}
147-
if isQCOW2 {
148-
volumeDef.Target.Format.Type = "qcow2"
149-
}
143+
// if no format is given, autodetect
144+
if !isFormatGiven {
145+
isQCOW2, err := img.IsQCOW2()
146+
if err != nil {
147+
return diag.Errorf("error while determining image type for %s: %s", img.String(), err)
148+
}
150149

151-
if isFormatGiven && isQCOW2 && givenFormat != "qcow2" {
152-
return diag.Errorf("format other than QCOW2 explicitly specified for image detected as QCOW2 image: %s", img.String())
150+
if isQCOW2 {
151+
volumeDef.Target.Format.Type = "qcow2"
152+
}
153153
}
154154

155155
// update the image in the description, even if the file has not changed

libvirt/volume_image.go

+2-30
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http"
88
"net/url"
99
"os"
10+
"path"
1011
"path/filepath"
1112
"runtime"
1213
"strconv"
@@ -126,37 +127,8 @@ func (i *httpImage) Size() (uint64, error) {
126127
return uint64(length), nil
127128
}
128129

129-
//nolint:mnd
130130
func (i *httpImage) IsQCOW2() (bool, error) {
131-
client := &http.Client{}
132-
req, _ := http.NewRequest("GET", i.url.String(), nil)
133-
req.Header.Set("Range", "bytes=0-7")
134-
response, err := client.Do(req)
135-
if err != nil {
136-
return false, err
137-
}
138-
defer response.Body.Close()
139-
140-
if response.StatusCode != http.StatusPartialContent {
141-
return false, fmt.Errorf(
142-
"can't retrieve partial header of resource to determine file type: %s - %s",
143-
i.url.String(),
144-
response.Status)
145-
}
146-
147-
header, err := io.ReadAll(response.Body)
148-
if err != nil {
149-
return false, err
150-
}
151-
152-
if len(header) < 8 {
153-
return false, fmt.Errorf(
154-
"can't retrieve read header of resource to determine file type: %s - %d bytes read",
155-
i.url.String(),
156-
len(header))
157-
}
158-
159-
return isQCOW2Header(header)
131+
return strings.ToLower(strings.TrimPrefix(path.Ext(i.url.Path), ".")) == "qcow2", nil
160132
}
161133

162134
func (i *httpImage) Import(uploader func(io.Reader) error, vol libvirtxml.StorageVolume) error {

0 commit comments

Comments
 (0)