-
-
Notifications
You must be signed in to change notification settings - Fork 108
/
selector.go
87 lines (73 loc) · 2.49 KB
/
selector.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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package firewall
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/bpg/terraform-provider-proxmox/proxmox/firewall"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf"
resource "github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/vm"
)
const (
mkSelectorNodeName = "node_name"
mkSelectorVMID = "vm_id"
mkSelectorContainerID = "container_id"
)
func selectorSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
mkSelectorNodeName: {
Type: schema.TypeString,
Optional: true,
Description: "The name of the node.",
},
mkSelectorVMID: {
Type: schema.TypeInt,
Optional: true,
Description: "The ID of the VM to manage the firewall for.",
RequiredWith: []string{mkSelectorNodeName},
ValidateDiagFunc: resource.VMIDValidator(),
},
mkSelectorContainerID: {
Type: schema.TypeInt,
Optional: true,
Description: "The ID of the container to manage the firewall for.",
RequiredWith: []string{mkSelectorNodeName},
ValidateDiagFunc: resource.VMIDValidator(),
},
}
}
func selectorSchemaMandatory() map[string]*schema.Schema {
s := selectorSchema()
s[mkSelectorNodeName].Optional = false
s[mkSelectorNodeName].Required = true
// required attributes can't be included in RequiredWith
s[mkSelectorVMID].RequiredWith = nil
s[mkSelectorContainerID].RequiredWith = nil
return s
}
func selectFirewallAPI(
f func(context.Context, firewall.API, *schema.ResourceData) diag.Diagnostics,
) func(context.Context, *schema.ResourceData, interface{}) diag.Diagnostics {
return func(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(proxmoxtf.ProviderConfiguration)
api, err := config.GetClient()
if err != nil {
return diag.FromErr(err)
}
var fwAPI firewall.API = api.Cluster().Firewall()
if nn, ok := d.GetOk(mkSelectorNodeName); ok {
nodeName := nn.(string)
nodeAPI := api.Node(nodeName)
if v, ok := d.GetOk(mkSelectorVMID); ok {
fwAPI = nodeAPI.VM(v.(int)).Firewall()
} else if v, ok := d.GetOk(mkSelectorContainerID); ok {
fwAPI = nodeAPI.Container(v.(int)).Firewall()
}
}
return f(ctx, fwAPI, d)
}
}