-
Notifications
You must be signed in to change notification settings - Fork 544
/
provider.go
451 lines (412 loc) · 14.1 KB
/
provider.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package proxmox
import (
"context"
"crypto/tls"
"fmt"
"net/url"
"os"
"sort"
"strconv"
"strings"
"sync"
pveSDK "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/Telmate/terraform-provider-proxmox/v2/proxmox/Internal/validator"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
schemaPmUser = "pm_user"
schemaPmPassword = "pm_password"
schemaPmApiUrl = "pm_api_url"
schemaPmApiTokenID = "pm_api_token_id"
schemaPmApiTokenSecret = "pm_api_token_secret"
schemaPmParallel = "pm_parallel"
schemaPmTlsInsecure = "pm_tls_insecure"
schemaPmHttpHeaders = "pm_http_headers"
schemaPmLogEnable = "pm_log_enable"
schemaPmLogLevels = "pm_log_levels"
schemaPmLogFile = "pm_log_file"
schemaPmTimeout = "pm_timeout"
schemaPmDangerouslyIgnoreUnknownAttributes = "pm_dangerously_ignore_unknown_attributes"
schemaPmDebug = "pm_debug"
schemaPmProxyServer = "pm_proxy_server"
schemaPmOTP = "pm_otp"
)
type providerConfiguration struct {
Client *pveSDK.Client
MaxParallel int
CurrentParallel int
MaxVMID int
Mutex *sync.Mutex
Cond *sync.Cond
LogFile string
LogLevels map[string]string
DangerouslyIgnoreUnknownAttributes bool
}
// Provider - Terrafrom properties for proxmox
func Provider() *schema.Provider {
pmOTPprompt := schema.Schema{
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_OTP", ""),
Description: "OTP 2FA code (if required)",
}
if os.Getenv("PM_OTP_PROMPT") == "1" {
pmOTPprompt = schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("PM_OTP", nil),
Description: "OTP 2FA code (if required)",
}
}
return &schema.Provider{
Schema: map[string]*schema.Schema{
schemaPmUser: {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_USER", nil),
Description: "Username e.g. myuser or myuser@pam",
},
schemaPmPassword: {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_PASS", nil),
Description: "Password to authenticate into proxmox",
Sensitive: true,
},
schemaPmApiUrl: {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_API_URL", ""),
ValidateFunc: func(v interface{}, k string) (warns []string, errs []error) {
value := v.(string)
if value == "" {
errs = append(errs, fmt.Errorf("you must specify an endpoint for the Proxmox Virtual Environment API (valid: https://host:port)"))
return
}
_, err := url.ParseRequestURI(value)
if err != nil {
errs = append(errs, fmt.Errorf("you must specify an endpoint for the Proxmox Virtual Environment API (valid: https://host:port)"))
return
}
return
},
Description: "https://host.fqdn:8006/api2/json",
},
schemaPmApiTokenID: {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_API_TOKEN_ID", nil),
Description: "API TokenID e.g. root@pam!mytesttoken",
},
schemaPmApiTokenSecret: {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_API_TOKEN_SECRET", nil),
Description: "The secret uuid corresponding to a TokenID",
Sensitive: true,
},
schemaPmParallel: {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateDiagFunc: func(i interface{}, k cty.Path) diag.Diagnostics {
v, ok := i.(int)
if !ok {
return diag.Errorf(validator.ErrorUint, k)
}
if v < 1 {
return diag.Errorf(schemaPmParallel + " must be greater than 0")
}
if v > 1 { // TODO actually fix the parallelism! workaround for #1136
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "setting " + schemaPmParallel + " greater than 1 is currently not recommended when using dynamic guest id allocation"}}
}
return nil
},
},
schemaPmTlsInsecure: {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_TLS_INSECURE", false), // we assume it's a production environment.
Description: "By default, every TLS connection is verified to be secure. This option allows terraform to proceed and operate on servers considered insecure. For example if you're connecting to a remote host and you do not have the CA cert that issued the proxmox api url's certificate.",
},
schemaPmHttpHeaders: {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_HTTP_HEADERS", nil),
Description: "Set custom http headers e.g. Key,Value,Key1,Value1",
},
schemaPmLogEnable: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Enable provider logging to get proxmox API logs",
},
schemaPmLogLevels: {
Type: schema.TypeMap,
Optional: true,
Description: "Configure the logging level to display; trace, debug, info, warn, etc",
},
schemaPmLogFile: {
Type: schema.TypeString,
Optional: true,
Default: "terraform-plugin-proxmox.log",
Description: "Write logs to this specific file",
},
schemaPmTimeout: {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_TIMEOUT", 1200),
Description: "How many seconds to wait for operations for both provider and api-client, default is 20m",
},
schemaPmDangerouslyIgnoreUnknownAttributes: {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_DANGEROUSLY_IGNORE_UNKNOWN_ATTRIBUTES", false),
Description: "By default this provider will exit if an unknown attribute is found. This is to prevent the accidential destruction of VMs or Data when something in the proxmox API has changed/updated and is not confirmed to work with this provider. Set this to true at your own risk. It may allow you to proceed in cases when the provider refuses to work, but be aware of the danger in doing so.",
},
schemaPmDebug: {
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_DEBUG", false),
Description: "Enable or disable the verbose debug output from proxmox api",
},
schemaPmProxyServer: {
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("PM_PROXY", nil),
Description: "Proxy Server passed to Api client(useful for debugging). Syntax: http://proxy:port",
},
schemaPmOTP: &pmOTPprompt,
},
ResourcesMap: map[string]*schema.Resource{
"proxmox_vm_qemu": resourceVmQemu(),
"proxmox_lxc": resourceLxc(),
"proxmox_lxc_disk": resourceLxcDisk(),
"proxmox_pool": resourcePool(),
"proxmox_cloud_init_disk": resourceCloudInitDisk(),
"proxmox_storage_iso": resourceStorageIso(),
// TODO - proxmox_bridge
// TODO - proxmox_vm_qemu_template
},
DataSourcesMap: map[string]*schema.Resource{
"proxmox_ha_groups": DataHAGroup(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
client, err := getClient(
d.Get(schemaPmApiUrl).(string),
d.Get(schemaPmUser).(string),
d.Get(schemaPmPassword).(string),
d.Get(schemaPmApiTokenID).(string),
d.Get(schemaPmApiTokenSecret).(string),
d.Get(schemaPmOTP).(string),
d.Get(schemaPmTlsInsecure).(bool),
d.Get(schemaPmHttpHeaders).(string),
d.Get(schemaPmTimeout).(int),
d.Get(schemaPmDebug).(bool),
d.Get(schemaPmProxyServer).(string),
)
if err != nil {
return nil, err
}
// permission check
minimumPermissions := []string{
"Datastore.AllocateSpace",
"Datastore.Audit",
"Pool.Allocate",
"Sys.Audit",
"Sys.Console",
"Sys.Modify",
"VM.Allocate",
"VM.Audit",
"VM.Clone",
"VM.Config.CDROM",
"VM.Config.Cloudinit",
"VM.Config.CPU",
"VM.Config.Disk",
"VM.Config.HWType",
"VM.Config.Memory",
"VM.Config.Network",
"VM.Config.Options",
"VM.Migrate",
"VM.Monitor",
"VM.PowerMgmt",
}
var id string
if result, getok := d.GetOk(schemaPmApiTokenID); getok {
id = result.(string)
id = strings.Split(id, "!")[0]
} else if result, getok := d.GetOk(schemaPmUser); getok {
id = result.(string)
}
userID, err := pveSDK.NewUserID(id)
if err != nil {
return nil, err
}
ctx := context.Background()
permlist, err := client.GetUserPermissions(ctx, userID, "/")
if err != nil {
return nil, err
}
sort.Strings(permlist)
sort.Strings(minimumPermissions)
permDiff := permissions_check(permlist, minimumPermissions)
if len(permDiff) == 0 {
// look to see what logging we should be outputting according to the provider configuration
logLevels := make(map[string]string)
for logger, level := range d.Get(schemaPmLogLevels).(map[string]interface{}) {
levelAsString, ok := level.(string)
if ok {
logLevels[logger] = levelAsString
} else {
return nil, fmt.Errorf("invalid logging level %v for %v. Be sure to use a string", level, logger)
}
}
// actually configure logging
// note that if enable is false here, the configuration will squash all output
ConfigureLogger(
d.Get(schemaPmLogEnable).(bool),
d.Get(schemaPmLogFile).(string),
logLevels,
)
var mut sync.Mutex
return &providerConfiguration{
Client: client,
MaxParallel: d.Get(schemaPmParallel).(int),
CurrentParallel: 0,
MaxVMID: -1,
Mutex: &mut,
Cond: sync.NewCond(&mut),
LogFile: d.Get(schemaPmLogFile).(string),
LogLevels: logLevels,
DangerouslyIgnoreUnknownAttributes: d.Get(schemaPmDangerouslyIgnoreUnknownAttributes).(bool),
}, nil
}
err = fmt.Errorf("permissions for user/token %s are not sufficient, please provide also the following permissions that are missing: %v", userID.ToString(), permDiff)
return nil, err
}
func getClient(pm_api_url string,
pm_user string,
pm_password string,
pm_api_token_id string,
pm_api_token_secret string,
pm_otp string,
pm_tls_insecure bool,
pm_http_headers string,
pm_timeout int,
pm_debug bool,
pm_proxy_server string) (*pveSDK.Client, error) {
tlsconf := &tls.Config{InsecureSkipVerify: true}
if !pm_tls_insecure {
tlsconf = nil
}
var err error
if pm_password != "" && pm_api_token_secret != "" {
err = fmt.Errorf("password and API token secret both exist, choose one or the other")
}
if pm_password == "" && pm_api_token_secret == "" {
err = fmt.Errorf("password and API token do not exist, one of these must exist")
}
if strings.Contains(pm_user, "!") && pm_password != "" {
err = fmt.Errorf("you appear to be using an API TokenID username with your password")
}
if !strings.Contains(pm_api_token_id, "!") {
err = fmt.Errorf("your API TokenID username should contain a !, check your API credentials")
}
client, _ := pveSDK.NewClient(pm_api_url, nil, pm_http_headers, tlsconf, pm_proxy_server, pm_timeout)
*pveSDK.Debug = pm_debug
// User+Pass authentication
if pm_user != "" && pm_password != "" {
err = client.Login(context.Background(), pm_user, pm_password, pm_otp)
}
// API authentication
if pm_api_token_id != "" && pm_api_token_secret != "" {
// Unsure how to get an err for this
client.SetAPIToken(pm_api_token_id, pm_api_token_secret)
}
if err != nil {
return nil, err
}
return client, nil
}
func nextVmId(pconf *providerConfiguration) (nextId int, err error) {
pconf.Mutex.Lock()
defer pconf.Mutex.Unlock()
nextId, err = pconf.Client.GetNextID(context.Background(), 0)
if err != nil {
return 0, err
}
pconf.MaxVMID = nextId
return nextId, nil
}
type pmApiLockHolder struct {
locked bool
pconf *providerConfiguration
}
func (lock *pmApiLockHolder) lock() {
if lock.locked {
return
}
lock.locked = true
pconf := lock.pconf
pconf.Mutex.Lock()
for pconf.CurrentParallel >= pconf.MaxParallel {
pconf.Cond.Wait()
}
pconf.CurrentParallel++
pconf.Mutex.Unlock()
}
func (lock *pmApiLockHolder) unlock() {
if !lock.locked {
return
}
lock.locked = false
pconf := lock.pconf
pconf.Mutex.Lock()
pconf.CurrentParallel--
pconf.Cond.Signal()
pconf.Mutex.Unlock()
}
func pmParallelBegin(pconf *providerConfiguration) *pmApiLockHolder {
lock := &pmApiLockHolder{
pconf: pconf,
locked: false,
}
lock.lock()
return lock
}
func resourceId(targetNode pveSDK.NodeName, resType string, vmId int) string {
return fmt.Sprintf("%s/%s/%d", targetNode.String(), resType, vmId)
}
func parseResourceId(resId string) (targetNode string, resType string, vmId int, err error) {
// create a logger for this function
logger, _ := CreateSubLogger("parseResourceId")
if !rxRsId.MatchString(resId) {
return "", "", -1, fmt.Errorf("invalid resource format: %s. Must be <node>/<type>/<vmid>", resId)
}
idMatch := rxRsId.FindStringSubmatch(resId)
targetNode = idMatch[1]
resType = idMatch[2]
vmId, err = strconv.Atoi(idMatch[3])
if err != nil {
logger.Info().Str("error", err.Error()).Msgf("failed to get vmId")
}
return
}
func clusterResourceId(resType string, resId string) string {
return fmt.Sprintf("%s/%s", resType, resId)
}
func parseClusterResourceId(resId string) (resType string, id string, err error) {
if !rxClusterRsId.MatchString(resId) {
return "", "", fmt.Errorf("invalid resource format: %s. Must be <type>/<resourceid>", resId)
}
idMatch := rxClusterRsId.FindStringSubmatch(resId)
return idMatch[1], idMatch[2], nil
}