go mod vendor

This commit is contained in:
Megan Marsh 2020-07-15 14:20:09 -07:00
parent b725b9cab8
commit 629a8aec15
5 changed files with 84 additions and 14 deletions

View File

@ -13,6 +13,7 @@ import (
"mime/multipart" "mime/multipart"
"net" "net"
"net/http" "net/http"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -722,7 +723,28 @@ func (c *Client) DeleteVMDisks(
} }
func (c *Client) Upload(node string, storage string, contentType string, filename string, file io.Reader) error { func (c *Client) Upload(node string, storage string, contentType string, filename string, file io.Reader) error {
body, mimetype, err := createUploadBody(contentType, filename, file) var doStreamingIO bool
var fileSize int64
var contentLength int64
if f, ok := file.(*os.File); ok {
doStreamingIO = true
fileInfo, err := f.Stat()
if err != nil {
return err
}
fileSize = fileInfo.Size()
}
var body io.Reader
var mimetype string
var err error
if doStreamingIO {
body, mimetype, contentLength, err = createStreamedUploadBody(contentType, filename, fileSize, file)
} else {
body, mimetype, err = createUploadBody(contentType, filename, file)
}
if err != nil { if err != nil {
return err return err
} }
@ -735,6 +757,10 @@ func (c *Client) Upload(node string, storage string, contentType string, filenam
req.Header.Add("Content-Type", mimetype) req.Header.Add("Content-Type", mimetype)
req.Header.Add("Accept", "application/json") req.Header.Add("Accept", "application/json")
if doStreamingIO {
req.ContentLength = contentLength
}
resp, err := c.session.Do(req) resp, err := c.session.Do(req)
if err != nil { if err != nil {
return err return err
@ -780,6 +806,38 @@ func createUploadBody(contentType string, filename string, r io.Reader) (io.Read
return &buf, w.FormDataContentType(), nil return &buf, w.FormDataContentType(), nil
} }
// createStreamedUploadBody - Use MultiReader to create the multipart body from the file reader,
// avoiding allocation of large files in memory before upload (useful e.g. for Windows ISOs).
func createStreamedUploadBody(contentType string, filename string, fileSize int64, r io.Reader) (io.Reader, string, int64, error) {
var buf bytes.Buffer
w := multipart.NewWriter(&buf)
err := w.WriteField("content", contentType)
if err != nil {
return nil, "", 0, err
}
_, err = w.CreateFormFile("filename", filename)
if err != nil {
return nil, "", 0, err
}
headerSize := buf.Len()
err = w.Close()
if err != nil {
return nil, "", 0, err
}
mr := io.MultiReader(bytes.NewReader(buf.Bytes()[:headerSize]),
r,
bytes.NewReader(buf.Bytes()[headerSize:]))
contentLength := int64(buf.Len()) + fileSize
return mr, w.FormDataContentType(), contentLength, nil
}
// getStorageAndVolumeName - Extract disk storage and disk volume, since disk name is saved // getStorageAndVolumeName - Extract disk storage and disk volume, since disk name is saved
// in Proxmox with its storage. // in Proxmox with its storage.
func getStorageAndVolumeName( func getStorageAndVolumeName(

View File

@ -313,7 +313,9 @@ func (config configLxc) CreateLxc(vmr *VmRef, client *Client) (err error) {
// comma separated list of "key=value" pairs // comma separated list of "key=value" pairs
featuresParam := QemuDeviceParam{} featuresParam := QemuDeviceParam{}
featuresParam = featuresParam.createDeviceParam(config.Features, nil) featuresParam = featuresParam.createDeviceParam(config.Features, nil)
paramMap["features"] = strings.Join(featuresParam, ",") if len(featuresParam) > 0 {
paramMap["features"] = strings.Join(featuresParam, ",")
}
// build list of mountpoints // build list of mountpoints
// this does the same as for the feature list // this does the same as for the feature list

View File

@ -38,6 +38,7 @@ type ConfigQemu struct {
QemuVcpus int `json:"vcpus"` QemuVcpus int `json:"vcpus"`
QemuCpu string `json:"cpu"` QemuCpu string `json:"cpu"`
QemuNuma bool `json:"numa"` QemuNuma bool `json:"numa"`
QemuKVM bool `json:"kvm"`
Hotplug string `json:"hotplug"` Hotplug string `json:"hotplug"`
QemuIso string `json:"iso"` QemuIso string `json:"iso"`
FullClone *int `json:"fullclone"` FullClone *int `json:"fullclone"`
@ -94,6 +95,7 @@ func (config ConfigQemu) CreateVm(vmr *VmRef, client *Client) (err error) {
"cores": config.QemuCores, "cores": config.QemuCores,
"cpu": config.QemuCpu, "cpu": config.QemuCpu,
"numa": config.QemuNuma, "numa": config.QemuNuma,
"kvm": config.QemuKVM,
"hotplug": config.Hotplug, "hotplug": config.Hotplug,
"memory": config.Memory, "memory": config.Memory,
"boot": config.Boot, "boot": config.Boot,
@ -107,11 +109,11 @@ func (config ConfigQemu) CreateVm(vmr *VmRef, client *Client) (err error) {
if config.Balloon >= 1 { if config.Balloon >= 1 {
params["balloon"] = config.Balloon params["balloon"] = config.Balloon
} }
if config.QemuVcpus >= 1 { if config.QemuVcpus >= 1 {
params["vcpus"] = config.QemuVcpus params["vcpus"] = config.QemuVcpus
} }
if vmr.pool != "" { if vmr.pool != "" {
params["pool"] = vmr.pool params["pool"] = vmr.pool
} }
@ -214,6 +216,7 @@ func (config ConfigQemu) UpdateConfig(vmr *VmRef, client *Client) (err error) {
"cores": config.QemuCores, "cores": config.QemuCores,
"cpu": config.QemuCpu, "cpu": config.QemuCpu,
"numa": config.QemuNuma, "numa": config.QemuNuma,
"kvm": config.QemuKVM,
"hotplug": config.Hotplug, "hotplug": config.Hotplug,
"memory": config.Memory, "memory": config.Memory,
"boot": config.Boot, "boot": config.Boot,
@ -231,13 +234,13 @@ func (config ConfigQemu) UpdateConfig(vmr *VmRef, client *Client) (err error) {
} else { } else {
deleteParams = append(deleteParams, "balloon") deleteParams = append(deleteParams, "balloon")
} }
if config.QemuVcpus >= 1 { if config.QemuVcpus >= 1 {
configParams["vcpus"] = config.QemuVcpus configParams["vcpus"] = config.QemuVcpus
} else { } else {
deleteParams = append(deleteParams, "vcpus") deleteParams = append(deleteParams, "vcpus")
} }
if config.BootDisk != "" { if config.BootDisk != "" {
configParams["bootdisk"] = config.BootDisk configParams["bootdisk"] = config.BootDisk
} }
@ -307,11 +310,11 @@ func (config ConfigQemu) UpdateConfig(vmr *VmRef, client *Client) (err error) {
if config.Ipconfig2 != "" { if config.Ipconfig2 != "" {
configParams["ipconfig2"] = config.Ipconfig2 configParams["ipconfig2"] = config.Ipconfig2
} }
if len(deleteParams) > 0 { if len(deleteParams) > 0 {
configParams["delete"] = strings.Join(deleteParams, ", ") configParams["delete"] = strings.Join(deleteParams, ", ")
} }
_, err = client.SetVmConfig(vmr, configParams) _, err = client.SetVmConfig(vmr, configParams)
if err != nil { if err != nil {
log.Print(err) log.Print(err)
@ -326,7 +329,7 @@ func (config ConfigQemu) UpdateConfig(vmr *VmRef, client *Client) (err error) {
} }
func NewConfigQemuFromJson(io io.Reader) (config *ConfigQemu, err error) { func NewConfigQemuFromJson(io io.Reader) (config *ConfigQemu, err error) {
config = &ConfigQemu{QemuVlanTag: -1} config = &ConfigQemu{QemuVlanTag: -1, QemuKVM: true}
err = json.NewDecoder(io).Decode(config) err = json.NewDecoder(io).Decode(config)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
@ -449,6 +452,10 @@ func NewConfigQemuFromApi(vmr *VmRef, client *Client) (config *ConfigQemu, err e
if _, isSet := vmConfig["bootdisk"]; isSet { if _, isSet := vmConfig["bootdisk"]; isSet {
bootdisk = vmConfig["bootdisk"].(string) bootdisk = vmConfig["bootdisk"].(string)
} }
kvm := true
if _, isSet := vmConfig["kvm"]; isSet {
kvm = Itob(int(vmConfig["kvm"].(float64)))
}
scsihw := "lsi" scsihw := "lsi"
if _, isSet := vmConfig["scsihw"]; isSet { if _, isSet := vmConfig["scsihw"]; isSet {
scsihw = vmConfig["scsihw"].(string) scsihw = vmConfig["scsihw"].(string)
@ -470,6 +477,7 @@ func NewConfigQemuFromApi(vmr *VmRef, client *Client) (config *ConfigQemu, err e
QemuSockets: int(sockets), QemuSockets: int(sockets),
QemuCpu: cpu, QemuCpu: cpu,
QemuNuma: numa, QemuNuma: numa,
QemuKVM: kvm,
Hotplug: hotplug, Hotplug: hotplug,
QemuVlanTag: -1, QemuVlanTag: -1,
Boot: boot, Boot: boot,
@ -481,12 +489,12 @@ func NewConfigQemuFromApi(vmr *VmRef, client *Client) (config *ConfigQemu, err e
QemuNetworks: QemuDevices{}, QemuNetworks: QemuDevices{},
QemuSerials: QemuDevices{}, QemuSerials: QemuDevices{},
} }
if balloon >= 1 { if balloon >= 1 {
config.Balloon = int(balloon); config.Balloon = int(balloon)
} }
if vcpus >= 1 { if vcpus >= 1 {
config.QemuVcpus = int(vcpus); config.QemuVcpus = int(vcpus)
} }
if vmConfig["ide2"] != nil { if vmConfig["ide2"] != nil {

View File

@ -65,7 +65,9 @@ func ParamsToBody(params map[string]interface{}) (body []byte) {
default: default:
v = fmt.Sprintf("%v", intrV) v = fmt.Sprintf("%v", intrV)
} }
vals.Set(k, v) if v != "" {
vals.Set(k, v)
}
} }
body = bytes.NewBufferString(vals.Encode()).Bytes() body = bytes.NewBufferString(vals.Encode()).Bytes()
return return

2
vendor/modules.txt vendored
View File

@ -69,7 +69,7 @@ github.com/NaverCloudPlatform/ncloud-sdk-go-v2/services/server
github.com/PuerkitoBio/goquery github.com/PuerkitoBio/goquery
# github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d # github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d
github.com/StackExchange/wmi github.com/StackExchange/wmi
# github.com/Telmate/proxmox-api-go v0.0.0-20200225212220-a29566462efd # github.com/Telmate/proxmox-api-go v0.0.0-20200715182505-ec97c70ba887
github.com/Telmate/proxmox-api-go/proxmox github.com/Telmate/proxmox-api-go/proxmox
# github.com/agext/levenshtein v1.2.1 # github.com/agext/levenshtein v1.2.1
github.com/agext/levenshtein github.com/agext/levenshtein