fix parallel VMID grabbing

This commit is contained in:
Grant Gongaware 2017-02-14 15:24:54 -08:00
parent 624169e7b7
commit 679fd58542
2 changed files with 20 additions and 2 deletions

View File

@ -3,6 +3,7 @@ package proxmox
import ( import (
pxapi "github.com/Telmate/proxmox-api-go/proxmox" pxapi "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"sync"
) )
type providerConfiguration struct { type providerConfiguration struct {
@ -54,3 +55,20 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
Client: client, Client: client,
}, nil }, nil
} }
var mutex = &sync.Mutex{}
var maxVmId = 0
func nextVmId(client *pxapi.Client) (nextId int, err error) {
mutex.Lock()
if maxVmId == 0 {
maxVmId, err = pxapi.MaxVmId(client)
if err != nil {
return 0, err
}
}
maxVmId++
nextId = maxVmId
mutex.Unlock()
return nextId, nil
}

View File

@ -127,11 +127,11 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error {
} }
// get unique id // get unique id
maxid, err := pxapi.MaxVmId(client) nextid, err := nextVmId(client)
if err != nil { if err != nil {
return err return err
} }
vmr := pxapi.NewVmRef(maxid + 1) vmr := pxapi.NewVmRef(nextid)
vmr.SetNode(d.Get("target_node").(string)) vmr.SetNode(d.Get("target_node").(string))
// check if ISO or clone // check if ISO or clone