forked from Telmate/terraform-provider-proxmox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
provisioner.go
73 lines (64 loc) · 1.7 KB
/
provisioner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package proxmox
import (
"context"
"fmt"
"time"
pxapi "github.com/RomanenkoDenys/proxmox-api-go/proxmox"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
// Provisioner - Terrafrom properties for proxmox-provisioner
func Provisioner() terraform.ResourceProvisioner {
return &schema.Provisioner{
Schema: map[string]*schema.Schema{
"action": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"net1": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
ApplyFunc: applyFn,
}
}
var currentClient *pxapi.Client
func applyFn(ctx context.Context) error {
data := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
state := ctx.Value(schema.ProvRawStateKey).(*terraform.InstanceState)
connInfo := state.Ephemeral.ConnInfo
act := data.Get("action").(string)
targetNode, _, vmID, err := parseResourceId(state.ID)
if err != nil {
return err
}
vmr := pxapi.NewVmRef(vmID)
vmr.SetNode(targetNode)
client := currentClient
if client == nil {
client, err = getClient(connInfo["pm_api_url"], connInfo["pm_user"], connInfo["pm_password"], connInfo["pm_otp"], connInfo["pm_tls_insecure"] == "true")
if err != nil {
return err
}
currentClient = client
}
switch act {
case "sshbackward":
return pxapi.RemoveSshForwardUsernet(vmr, client)
case "reconnect":
err = pxapi.RemoveSshForwardUsernet(vmr, client)
if err != nil {
return err
}
time.Sleep(10 * time.Second)
vmParams := map[string]interface{}{
"net1": data.Get("net1").(string),
}
_, err = client.SetVmConfig(vmr, vmParams)
time.Sleep(10 * time.Second)
return err
default:
return fmt.Errorf("Unkown action: %s", act)
}
}