-
Notifications
You must be signed in to change notification settings - Fork 3
/
resource_mac_allow.go
119 lines (100 loc) · 2.59 KB
/
resource_mac_allow.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
package main
import (
"crypto/rand"
"encoding/hex"
"errors"
"log"
"net"
"github.com/hashicorp/terraform/helper/schema"
)
// https://docs.microsoft.com/en-us/powershell/module/dhcpserver/Add-DhcpServerv4Filter?view=win10-ps
func resourceMacAllow() *schema.Resource {
return &schema.Resource{
Create: resourceServerCreate,
Read: resourceServerRead,
Update: resourceServerUpdate,
Delete: resourceServerDelete,
Schema: map[string]*schema.Schema{
"mac": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: nil,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"mac_windows": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceServerCreate(d *schema.ResourceData, m interface{}) error {
c := m.(*Communicator)
c.Connect()
var mac net.HardwareAddr
if d.Get("mac").(string) == "" {
log.Printf("[DEBUG] generate a mac address")
mac = GenerateMac(c.GetAllAllowedMacAddress())
d.Set("mac", mac.String())
log.Println("[DEBUG] Generated mac" + mac.String())
}
var err error
log.Printf("[DEBUG] validate a mac address")
mac, err = net.ParseMAC(d.Get("mac").(string))
if err != nil {
log.Printf("[DEBUG] invalid mac address")
return errors.New("Invalid mac Address")
}
d.Set("mac_windows", NormalizeMacWindows(mac.String()))
error := c.AddFilterAllowAddress(d.Get("mac_windows").(string), d.Get("description").(string))
if error != nil {
return error
}
d.SetId(mac.String())
return nil
}
func resourceServerRead(d *schema.ResourceData, m interface{}) error {
return nil
}
func resourceServerUpdate(d *schema.ResourceData, m interface{}) error {
return resourceServerRead(d, m)
}
func resourceServerDelete(d *schema.ResourceData, m interface{}) error {
c := m.(*Communicator)
c.Connect()
return c.RemoveFilterAllowAddress(d.Get("mac_windows").(string))
}
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func GenerateMac(except []string) net.HardwareAddr {
mac, _ := net.ParseMAC(subGenerate("00", 5))
if containsMac(NormalizeMacWindows(mac.String()), except) {
return GenerateMac(except)
}
return mac
}
func containsMac(mac string, macs []string) bool {
for _, n := range macs {
if mac == n {
return true
}
}
return false
}
func subGenerate(before string, count int) string {
if count == 0 {
return before
}
sub, _ := randomHex(1)
new := before + ":" + sub
return subGenerate(new, count-1)
}