forked from claudusd/terraform-windows-network
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_dhcp_reservation.go
88 lines (76 loc) · 2.07 KB
/
resource_dhcp_reservation.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
package main
import (
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"net"
)
func resourceDHCPReservation() *schema.Resource {
return &schema.Resource{
Create: CreateDHCPReservation,
Read: ReadDHCPReservation,
Update: UpdateDHCPReservation,
Delete: DeleteDHCPReservation,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"mac": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"ip": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: nil,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"scope_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func CreateDHCPReservation(d *schema.ResourceData, m interface{}) error {
mac, err := net.ParseMAC(d.Get("mac").(string))
if err != nil {
return errors.New("Invalid mac Address")
}
c := m.(*Communicator)
c.Connect()
if d.Get("ip").(string) == "" {
ip, err := c.GetFreeIp(d.Get("scope_id").(string))
if err != nil {
return err
}
d.Set("ip", ip.String())
}
ipv4 := net.ParseIP(d.Get("ip").(string))
if ipv4 == nil {
return errors.New("Invalid ip Address")
}
c.AddDHCPReservation(NormalizeMacWindows(mac.String()), ipv4, d.Get("scope_id").(string), d.Get("description").(string), d.Get("name").(string))
d.SetId(mac.String() + "_" + ipv4.String())
return nil
}
func DeleteDHCPReservation(d *schema.ResourceData, m interface{}) error {
c := m.(*Communicator)
c.Connect()
c.RemoveDHCPReservation(NormalizeMacWindows(d.Get("mac").(string)), d.Get("scope_id").(string))
c.RemoveDHCPLease(d.Get("scope_id").(string), d.Get("mac").(string), d.Get("ip").(string))
return nil
}
func ReadDHCPReservation(d *schema.ResourceData, m interface{}) error {
return nil
}
func UpdateDHCPReservation(d *schema.ResourceData, m interface{}) error {
return nil
}