terraform-provider-proxmox/proxmox/provider.go

76 lines
1.6 KiB
Go
Raw Normal View History

2017-02-09 04:53:24 +00:00
package proxmox
import (
pxapi "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/hashicorp/terraform/helper/schema"
2017-02-14 23:24:54 +00:00
"sync"
2017-02-09 04:53:24 +00:00
)
type providerConfiguration struct {
Client *pxapi.Client
}
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",
},
},
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) {
client, _ := pxapi.NewClient(d.Get("pm_api_url").(string), nil, nil)
err := client.Login(d.Get("pm_user").(string), d.Get("pm_password").(string))
if err != nil {
return nil, err
}
return &providerConfiguration{
Client: client,
}, nil
}
2017-02-14 23:24:54 +00:00
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
}