2017-02-09 04:53:24 +00:00
|
|
|
package proxmox
|
|
|
|
|
|
|
|
import (
|
2018-07-10 21:50:35 +00:00
|
|
|
"crypto/tls"
|
2017-05-24 17:18:06 +00:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
2017-02-14 23:24:54 +00:00
|
|
|
"sync"
|
2018-07-10 17:37:45 +00:00
|
|
|
|
|
|
|
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
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
|
2018-07-13 17:25:37 +00:00
|
|
|
MaxVMID int
|
2017-07-21 01:35:01 +00:00
|
|
|
Mutex *sync.Mutex
|
|
|
|
Cond *sync.Cond
|
2017-02-09 04:53:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 17:25:37 +00:00
|
|
|
// Provider - Terrafrom properties for proxmox
|
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,
|
|
|
|
},
|
2018-07-10 21:50:35 +00:00
|
|
|
"pm_tls_insecure": {
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
2019-08-02 12:01:52 +00:00
|
|
|
"pm_otp": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("PM_OTP", nil),
|
|
|
|
Description: "OTP 2FA code (if required)",
|
|
|
|
},
|
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(),
|
2019-06-03 07:01:19 +00:00
|
|
|
"proxmox_lxc": resourceLxc(),
|
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) {
|
2019-08-02 12:01:52 +00:00
|
|
|
client, err := getClient(d.Get("pm_api_url").(string), d.Get("pm_user").(string), d.Get("pm_password").(string), d.Get("pm_otp").(string), d.Get("pm_tls_insecure").(bool))
|
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,
|
2018-07-13 17:25:37 +00:00
|
|
|
MaxVMID: -1,
|
2017-07-21 01:35:01 +00:00
|
|
|
Mutex: &mut,
|
|
|
|
Cond: sync.NewCond(&mut),
|
2017-02-09 04:53:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2017-02-14 23:24:54 +00:00
|
|
|
|
2019-08-02 12:01:52 +00:00
|
|
|
func getClient(pm_api_url string, pm_user string, pm_password string, pm_otp string, pm_tls_insecure bool) (*pxapi.Client, error) {
|
2018-07-10 21:50:35 +00:00
|
|
|
tlsconf := &tls.Config{InsecureSkipVerify: true}
|
|
|
|
if !pm_tls_insecure {
|
|
|
|
tlsconf = nil
|
|
|
|
}
|
|
|
|
client, _ := pxapi.NewClient(pm_api_url, nil, tlsconf)
|
2019-08-02 12:01:52 +00:00
|
|
|
err := client.Login(pm_user, pm_password, pm_otp)
|
2017-05-24 17:18:06 +00:00
|
|
|
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()
|
2018-07-13 17:25:37 +00:00
|
|
|
pconf.MaxVMID, err = pconf.Client.GetNextID(pconf.MaxVMID + 1)
|
2018-07-11 22:14:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2017-02-14 23:24:54 +00:00
|
|
|
}
|
2018-07-13 17:25:37 +00:00
|
|
|
nextId = pconf.MaxVMID
|
2017-07-21 01:35:01 +00:00
|
|
|
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) {
|
2019-05-23 11:39:39 +00:00
|
|
|
if !rxRsId.MatchString(resId) {
|
|
|
|
return "", "", -1, fmt.Errorf("Invalid resource format: %s. Must be node/type/vmId", resId)
|
2017-05-24 17:18:06 +00:00
|
|
|
}
|
2019-05-23 11:39:39 +00:00
|
|
|
idMatch := rxRsId.FindStringSubmatch(resId)
|
2017-05-24 17:18:06 +00:00
|
|
|
targetNode = idMatch[1]
|
|
|
|
resType = idMatch[2]
|
|
|
|
vmId, err = strconv.Atoi(idMatch[3])
|
|
|
|
return
|
|
|
|
}
|