2017-02-09 04:53:24 +00:00
|
|
|
package proxmox
|
|
|
|
|
|
|
|
import (
|
2017-05-24 17:18:06 +00:00
|
|
|
"fmt"
|
2017-02-09 04:53:24 +00:00
|
|
|
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-05-24 17:18:06 +00:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2017-02-14 23:24:54 +00:00
|
|
|
"sync"
|
2017-02-09 04:53:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type providerConfiguration struct {
|
2017-07-21 01:35:01 +00:00
|
|
|
Client *pxapi.Client
|
|
|
|
MaxParallel int
|
|
|
|
CurrentParallel int
|
|
|
|
MaxVmId int
|
|
|
|
Mutex *sync.Mutex
|
|
|
|
Cond *sync.Cond
|
2017-02-09 04:53:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Provider() *schema.Provider {
|
|
|
|
return &schema.Provider{
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"pm_user": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PM_USER", nil),
|
|
|
|
Description: "username, maywith with @pam",
|
|
|
|
},
|
|
|
|
"pm_password": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PM_PASS", nil),
|
|
|
|
Description: "secret",
|
2017-02-15 23:32:37 +00:00
|
|
|
Sensitive: true,
|
2017-02-09 04:53:24 +00:00
|
|
|
},
|
|
|
|
"pm_api_url": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PM_API_URL", nil),
|
|
|
|
Description: "https://host.fqdn:8006/api2/json",
|
|
|
|
},
|
2017-07-21 01:35:01 +00:00
|
|
|
"pm_parallel": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Default: 4,
|
|
|
|
},
|
2017-02-09 04:53:24 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
ResourcesMap: map[string]*schema.Resource{
|
2017-02-09 21:36:31 +00:00
|
|
|
"proxmox_vm_qemu": resourceVmQemu(),
|
2017-02-09 04:53:24 +00:00
|
|
|
// TODO - storage_iso
|
|
|
|
// TODO - bridge
|
|
|
|
// TODO - vm_qemu_template
|
|
|
|
},
|
|
|
|
|
|
|
|
ConfigureFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
2017-05-24 17:18:06 +00:00
|
|
|
client, err := getClient(d.Get("pm_api_url").(string), d.Get("pm_user").(string), d.Get("pm_password").(string))
|
2017-02-09 04:53:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-21 01:35:01 +00:00
|
|
|
var mut sync.Mutex
|
2017-02-09 04:53:24 +00:00
|
|
|
return &providerConfiguration{
|
2017-07-21 01:35:01 +00:00
|
|
|
Client: client,
|
|
|
|
MaxParallel: d.Get("pm_parallel").(int),
|
|
|
|
CurrentParallel: 0,
|
|
|
|
MaxVmId: 0,
|
|
|
|
Mutex: &mut,
|
|
|
|
Cond: sync.NewCond(&mut),
|
2017-02-09 04:53:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2017-02-14 23:24:54 +00:00
|
|
|
|
2017-05-24 17:18:06 +00:00
|
|
|
func getClient(pm_api_url string, pm_user string, pm_password string) (*pxapi.Client, error) {
|
|
|
|
client, _ := pxapi.NewClient(pm_api_url, nil, nil)
|
|
|
|
err := client.Login(pm_user, pm_password)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, nil
|
|
|
|
}
|
|
|
|
|
2017-07-21 01:35:01 +00:00
|
|
|
func nextVmId(pconf *providerConfiguration) (nextId int, err error) {
|
|
|
|
pconf.Mutex.Lock()
|
|
|
|
if pconf.MaxVmId == 0 {
|
|
|
|
pconf.MaxVmId, err = pxapi.MaxVmId(pconf.Client)
|
2017-02-14 23:24:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 01:35:01 +00:00
|
|
|
pconf.MaxVmId++
|
|
|
|
nextId = pconf.MaxVmId
|
|
|
|
pconf.Mutex.Unlock()
|
2017-02-14 23:24:54 +00:00
|
|
|
return nextId, nil
|
|
|
|
}
|
2017-05-24 17:18:06 +00:00
|
|
|
|
2017-07-21 01:35:01 +00:00
|
|
|
func pmParallelBegin(pconf *providerConfiguration) {
|
|
|
|
pconf.Mutex.Lock()
|
|
|
|
for pconf.CurrentParallel >= pconf.MaxParallel {
|
|
|
|
pconf.Cond.Wait()
|
|
|
|
}
|
|
|
|
pconf.CurrentParallel++
|
|
|
|
pconf.Mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func pmParallelEnd(pconf *providerConfiguration) {
|
|
|
|
pconf.Mutex.Lock()
|
|
|
|
pconf.CurrentParallel--
|
|
|
|
pconf.Cond.Signal()
|
|
|
|
pconf.Mutex.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-05-24 17:18:06 +00:00
|
|
|
func resourceId(targetNode string, resType string, vmId int) string {
|
|
|
|
return fmt.Sprintf("%s/%s/%d", targetNode, resType, vmId)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rxRsId = regexp.MustCompile("([^/]+)/([^/]+)/(\\d+)")
|
|
|
|
|
|
|
|
func parseResourceId(resId string) (targetNode string, resType string, vmId int, err error) {
|
|
|
|
idMatch := rxRsId.FindStringSubmatch(resId)
|
|
|
|
if idMatch == nil {
|
|
|
|
err = fmt.Errorf("Invalid resource id: %s", resId)
|
|
|
|
}
|
|
|
|
targetNode = idMatch[1]
|
|
|
|
resType = idMatch[2]
|
|
|
|
vmId, err = strconv.Atoi(idMatch[3])
|
|
|
|
return
|
|
|
|
}
|