diff --git a/proxmox/provider.go b/proxmox/provider.go index 82304ba..8ba9278 100644 --- a/proxmox/provider.go +++ b/proxmox/provider.go @@ -3,6 +3,7 @@ package proxmox import ( pxapi "github.com/Telmate/proxmox-api-go/proxmox" "github.com/hashicorp/terraform/helper/schema" + "sync" ) type providerConfiguration struct { @@ -54,3 +55,20 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { Client: client, }, 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 +} diff --git a/proxmox/resource_vm_qemu.go b/proxmox/resource_vm_qemu.go index ae3450b..195739f 100644 --- a/proxmox/resource_vm_qemu.go +++ b/proxmox/resource_vm_qemu.go @@ -127,11 +127,11 @@ func resourceVmQemuCreate(d *schema.ResourceData, meta interface{}) error { } // get unique id - maxid, err := pxapi.MaxVmId(client) + nextid, err := nextVmId(client) if err != nil { return err } - vmr := pxapi.NewVmRef(maxid + 1) + vmr := pxapi.NewVmRef(nextid) vmr.SetNode(d.Get("target_node").(string)) // check if ISO or clone