forked from mrxinu/gosolar
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ipam.go
191 lines (163 loc) · 5.37 KB
/
ipam.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
package gosolar
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
)
// IPAddress is a struct for basic unmarshalling of an IPAddress object
type IPAddress struct {
IPNodeID int `json:"IpNodeId"`
Address string `json:"IPAddress"`
Status int `json:"Status"`
StatusString string `json:StatusString`
Comments string `json:Comments`
//TransientCount string `json"Transient"`
}
var statuses = []string{"Used", "Available", "Reserved", "Transient", "Blocked"}
// GetFirstAvailableIP returns the first available ip in the subnet
// TODO: change to return error, so terraform can handle not found IPs
func (c *Client) GetFirstAvailableIP(subnetAddress string, subnetCIDR string) IPAddress {
// We need to format the body as an array...
body := []string{
subnetAddress,
subnetCIDR,
}
res, err := c.Invoke("IPAM.SubnetManagement", "GetFirstAvailableIp", body)
// We have to do some fancy slicing to get rid of quotes. Fuck this api.
bodyString := string(res)[1 : len(string(res))-1]
// IP Not found. Or there's a problem
if err != nil {
log.Infof("ResponseString %s", bodyString)
log.Fatal(err)
}
var ip IPAddress
ip = IPAddress{
Address: bodyString,
}
return ip
}
// GetIP returns a full ip address object for ips
// TODO: change to return error, so terraform can handle not found IPs
func (c *Client) GetIP(ipAddress string) IPAddress {
query := `SELECT TOP 1
IpNodeId,
IPAddress,
Status,
Comments
FROM IPAM.IpNode WHERE IPAddress = @ipAddress`
parameters := map[string]interface{}{
"ipAddress": ipAddress,
}
res, err := c.Query(query, parameters)
// run the query without with the parameters map above
bodyString := string(res)
if err != nil {
log.Infof("ResponseString %s", bodyString)
log.Fatal(err)
}
var ip []IPAddress
if err := json.Unmarshal(res, &ip); err != nil {
log.Infof("ResponseString %s", bodyString)
log.Fatal(err)
}
// return empty if not found.
if len(ip) < 1 {
return IPAddress{}
}
//ip[0].StatusString = statuses[ip[0].Status-1]
return ip[0]
}
// Set-SwisObject $swis -Uri 'swis://localhost/Orion/IPAM.IPNode/IPAddress=1.1.1.1' -Properties @{ Alias = 'test1' }
//Invoke-SwisVerb $swis IPAM.SubnetManagement GetFirstAvailableIp @("199.10.1.0", "24")
//Invoke-SwisVerb $swis IPAM.SubnetManagement ChangeIPStatus @("199.10.1.1", "Used")
//Update: Set-SwisObject $swis -Uri 'swis://localhost/Orion/IPAM.IPNode/IpNodeId=2' -Properties @{ Status = 'Used', Comment = "Reserved by terraform." }
// ReserveIP will set the IP Status to "Used"
// TODO: change to return error, so terraform can handle not found IPs
func (c *Client) ReserveIP(ipAddress string) IPAddress {
// We need to format the body as an array...
body := []string{
ipAddress,
"Used",
}
// Create: New-SwisObject $swis -EntityType 'IPAM.IPNode' -Properties @{ SubnetId=22; IPAddress='10.20.30.40' }
result, err := c.Invoke("IPAM.SubnetManagement", "ChangeIPStatus", body)
resultString := string(result)
if err != nil {
log.Info(resultString)
log.Fatal(err)
}
return c.GetIP(ipAddress)
}
// ReserveIPForHostname will set the IP Status to "Used" and set the alias to hostname
func (c *Client) ReserveIPForHostname(ipAddress string, hostname string) IPAddress {
// We need to format the body as an array...
body := []string{
ipAddress,
"Used",
}
// Create: New-SwisObject $swis -EntityType 'IPAM.IPNode' -Properties @{ SubnetId=22; IPAddress='10.20.30.40' }
result, err := c.Invoke("IPAM.SubnetManagement", "ChangeIPStatus", body)
ipResult := []IPAddress{}
json.Unmarshal(result, &ipResult)
if len(ipResult) < 1 {
log.Fatal("Problem reserving IP.")
}
ipObj := ipResult[0]
c.AddHostnameAliastoIPNode(ipObj.Address, hostname)
if err != nil {
log.Info(ipResult)
log.Fatal(err)
}
return c.GetIP(ipAddress)
}
// ReleaseIP will set the IP Status to "Unused"
func (c *Client) ReleaseIP(ipAddress string) IPAddress {
// We need to format the body as an array...
body := []string{
ipAddress,
"Available",
}
result, err := c.Invoke("IPAM.SubnetManagement", "ChangeIPStatus", body)
resultString := string(result)
if err != nil {
log.Info(resultString)
log.Fatal(err)
}
return c.GetIP(ipAddress)
}
// CommentOnIPNode puts comments on a ip node object
// https://localhost:17778/SolarWinds/InformationService/v3/Json/swis://--SERVERNAME--/Orion/IPAM.IPNode/IPNodeID=%s
func (c *Client) CommentOnIPNode(ipAddress string, comment string) IPAddress {
ipAddr := c.GetIP(ipAddress)
body := map[string]interface{}{
"Comments": comment,
}
log.Info(ipAddr)
uri := fmt.Sprintf("swis://localhost/Orion/IPAM.IPNode/IpNodeId=%d", ipAddr.IPNodeID)
log.Info(uri)
result, err := c.Update(uri, body)
resultString := string(result)
if err != nil {
log.Info(resultString)
log.Fatal(err)
}
return c.GetIP(ipAddress)
}
// CommentOnIPNode puts comments on a ip node object
// https://localhost:17778/SolarWinds/InformationService/v3/Json/swis://--SERVERNAME--/Orion/IPAM.IPNode/IPNodeID=%s
func (c *Client) AddHostnameAliastoIPNode(ipAddress string, hostname string) IPAddress {
ipAddr := c.GetIP(ipAddress)
body := map[string]interface{}{
"Alias": hostname,
}
log.Info(ipAddr)
uri := fmt.Sprintf("swis://localhost/Orion/IPAM.IPNode/IpNodeId=%d", ipAddr.IPNodeID)
log.Info(uri)
result, err := c.Update(uri, body)
resultString := string(result)
if err != nil {
log.Info(resultString)
log.Fatal(err)
}
return c.GetIP(ipAddress)
}