/
resource_ip6_mac.go
199 lines (168 loc) · 5.9 KB
/
resource_ip6_mac.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
package solidserver
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"log"
"net/url"
"strings"
)
func resourceip6mac() *schema.Resource {
return &schema.Resource{
Create: resourceip6macCreate,
Read: resourceip6macRead,
Delete: resourceip6macDelete,
Exists: resourceip6macExists,
Schema: map[string]*schema.Schema{
"space": {
Type: schema.TypeString,
Description: "The name of the space into which mapping the IP and the MAC address.",
Required: true,
ForceNew: true,
},
"address": {
Type: schema.TypeString,
Description: "The IP v6 address to map with the MAC address.",
ValidateFunc: resourceip6addressrequestvalidateformat,
Required: true,
ForceNew: true,
},
"mac": {
Type: schema.TypeString,
Description: "The MAC Address o map with the IP v6 address.",
ValidateFunc: resourceipmacrequestvalidateformat,
DiffSuppressFunc: resourcediffsuppresslowercase,
Required: true,
ForceNew: true,
},
},
}
}
func resourceip6macExists(d *schema.ResourceData, meta interface{}) (bool, error) {
s := meta.(*SOLIDserver)
// Building parameters
parameters := url.Values{}
parameters.Add("ip6_id", d.Id())
log.Printf("[DEBUG] Checking existence of IP v6 address (oid): %s; associated to the mac: %s\n", d.Id(), d.Get("mac").(string))
// Sending the read request
resp, body, err := s.Request("get", "rest/ip6_address6_info", ¶meters)
if err == nil {
var buf [](map[string]interface{})
json.Unmarshal([]byte(body), &buf)
// Checking the answer
if (resp.StatusCode == 200 || resp.StatusCode == 201) && len(buf) > 0 {
if ip6Mac, ip6MacExist := buf[0]["ip6_mac_addr"].(string); ip6MacExist {
if strings.ToLower(ip6Mac) == strings.ToLower(d.Get("mac").(string)) {
return true, nil
}
// Log the error
log.Printf("[DEBUG] SOLIDServer - Unable to find the IP v6 address (oid): %s; associated to the mac (%s)\n", d.Id(), d.Get("mac").(string))
}
} else {
if len(buf) > 0 {
if errMsg, errExist := buf[0]["errmsg"].(string); errExist {
// Log the error
log.Printf("[DEBUG] SOLIDServer - Unable to find IP v6 address (oid): %s (%s)\n", d.Id(), errMsg)
}
} else {
// Log the error
log.Printf("[DEBUG] SOLIDServer - Unable to find IP v6 address (oid): %s\n", d.Id())
}
}
// Unset local ID
d.SetId("")
}
return false, err
}
func resourceip6macCreate(d *schema.ResourceData, meta interface{}) error {
s := meta.(*SOLIDserver)
// Building parameters
parameters := url.Values{}
parameters.Add("site_name", d.Get("space").(string))
parameters.Add("add_flag", "edit_only")
parameters.Add("hostaddr", d.Get("address").(string))
parameters.Add("ip6_mac_addr", strings.ToLower(d.Get("mac").(string)))
parameters.Add("keep_class_parameters", "1")
// Sending the creation request
resp, body, err := s.Request("put", "rest/ip6_address6_add", ¶meters)
if err == nil {
var buf [](map[string]interface{})
json.Unmarshal([]byte(body), &buf)
// Checking the answer
if (resp.StatusCode == 200 || resp.StatusCode == 201) && len(buf) > 0 {
if oid, oidExist := buf[0]["ret_oid"].(string); oidExist {
log.Printf("[DEBUG] SOLIDServer - Created IP MAC association (oid) %s\n", oid)
d.SetId(oid)
return nil
}
} else {
return fmt.Errorf("SOLIDServer - Failed to create IP MAC association between %s and %s\n", d.Get("address").(string), d.Get("mac").(string))
}
}
// Reporting a failure
return err
}
func resourceip6macDelete(d *schema.ResourceData, meta interface{}) error {
s := meta.(*SOLIDserver)
// Building parameters
parameters := url.Values{}
parameters.Add("site_name", d.Get("space").(string))
parameters.Add("add_flag", "edit_only")
parameters.Add("hostaddr", d.Get("address").(string))
parameters.Add("ip6_mac_addr", "")
parameters.Add("keep_class_parameters", "1")
// Sending the creation request
resp, body, err := s.Request("put", "rest/ip6_address6_add", ¶meters)
if err == nil {
var buf [](map[string]interface{})
json.Unmarshal([]byte(body), &buf)
// Checking the answer
if (resp.StatusCode == 200 || resp.StatusCode == 201) && len(buf) > 0 {
if oid, oidExist := buf[0]["ret_oid"].(string); oidExist {
log.Printf("[DEBUG] SOLIDServer - Deleted IP MAC association (oid) %s\n", oid)
d.SetId("")
return nil
}
} else {
return fmt.Errorf("SOLIDServer - Failed to delete IP MAC association between %s and %s\n", d.Get("address").(string), d.Get("mac").(string))
}
}
// Reporting a failure
return err
}
func resourceip6macRead(d *schema.ResourceData, meta interface{}) error {
s := meta.(*SOLIDserver)
// Building parameters
parameters := url.Values{}
parameters.Add("ip6_id", d.Id())
log.Printf("[DEBUG] Reading information about IP v6 address (oid): %s; associated to the mac: %s\n", d.Id(), d.Get("mac").(string))
// Sending the read request
resp, body, err := s.Request("get", "rest/ip6_address6_info", ¶meters)
if err == nil {
var buf [](map[string]interface{})
json.Unmarshal([]byte(body), &buf)
// Checking the answer
if (resp.StatusCode == 200 || resp.StatusCode == 201) && len(buf) > 0 {
if ip6Mac, ip6MacExist := buf[0]["ip6_mac_addr"].(string); ip6MacExist {
if strings.ToLower(ip6Mac) == strings.ToLower(d.Get("mac").(string)) {
return nil
}
// Log the error
log.Printf("[DEBUG] SOLIDServer - Unable to find the IP v6 address (oid): %s; associated to the mac (%s)\n", d.Id(), d.Get("mac").(string))
}
} else {
if len(buf) > 0 {
if errMsg, errExist := buf[0]["errmsg"].(string); errExist {
// Log the error
log.Printf("[DEBUG] SOLIDServer - Unable to find IP v6 address (oid): %s (%s)\n", d.Id(), errMsg)
}
} else {
// Log the error
log.Printf("[DEBUG] SOLIDServer - Unable to find IP v6 address (oid): %s\n", d.Id())
}
}
// Unset local ID
d.SetId("")
}
return err
}