-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaddress.go
233 lines (218 loc) · 7.15 KB
/
address.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package phpipam
import (
"encoding/json"
"errors"
"net/http"
"net/url"
"strings"
)
// Address struct from phpipam
type Address struct {
Code int `json:"code"`
Success bool `json:"success"`
Data struct {
ID string `json:"id"`
SubnetID string `json:"subnetId"`
IP string `json:"ip"`
Hostname string `json:"hostname"`
} `json:"data"`
Message string `json:"message"`
}
// AddressSearch struct from phpipam
type AddressSearch struct {
Code int `json:"code"`
Success bool `json:"success"`
Data []struct {
ID string `json:"id"`
SubnetID string `json:"subnetId"`
IP string `json:"ip"`
Description string `json:"description"`
} `json:"data"`
Message string `json:"message"`
}
// AddressPing struct from phpipam
type AddressPing struct {
Code int `json:"code"`
Success bool `json:"success"`
Data struct {
ScanType string `json:"scan_type"`
ExitCode int `json:"exit_code"`
} `json:"data"`
Message string `json:"message"`
}
// AddressDelete struct from phpipam
type AddressDelete struct {
Code int `json:"code"`
Success bool `json:"success"`
Message string `json:"message"`
}
// AddressFirstFree struct from phpipam
type AddressFirstFree struct {
Code int `json:"code"`
Success bool `json:"success"`
Message string `json:"message"`
IP string `json:"ip"`
}
// AddressSearchIP struct from phpipam
type AddressSearchIP struct {
Code int `json:"code"`
Success bool `json:"success"`
Data []struct {
ID string `json:"id"`
SubnetID string `json:"subnetId"`
IP string `json:"ip"`
Hostname string `json:"hostname"`
} `json:"data"`
Message string `json:"message"`
}
// UpdateAddress struct from phpipam
type UpdateAddress struct {
Code int `json:"code"`
Success bool `json:"success"`
Message string `json:"message"`
}
// GetAddress Client pointer method to get phpipam address using addressID
// string, returns Address struct and error
func (c *Client) GetAddress(addressID string) (Address, error) {
var addressData Address
req, _ := http.NewRequest("GET", c.ServerURL+"/api/"+c.Application+"/addresses/"+addressID+"/", nil)
body, err := c.Do(req)
if err != nil {
return addressData, err
}
err = json.Unmarshal([]byte(body), &addressData)
if err != nil {
return addressData, err
}
if addressData.Code == 200 || addressData.Code == 404 {
// Accepted responses
} else {
return addressData, errors.New(addressData.Message)
}
return addressData, nil
}
// GetAddressSearch Client pointer method to search for phpiapm address using
// hostname string, returns AddressSearch struct and error
func (c *Client) GetAddressSearch(searchHostname string) (AddressSearch, error) {
var addressSearchData AddressSearch
req, _ := http.NewRequest("GET", c.ServerURL+"/api/"+c.Application+"/addresses/search_hostname/"+searchHostname+"/", nil)
body, err := c.Do(req)
if err != nil {
return addressSearchData, err
}
err = json.Unmarshal([]byte(body), &addressSearchData)
if err != nil {
return addressSearchData, err
}
if addressSearchData.Code == 200 || addressSearchData.Code == 404 {
// Accepted responses
} else {
return addressSearchData, errors.New(addressSearchData.Message)
}
return addressSearchData, nil
}
// GetAddressPing Client pointer method to get ping status about phpipam address
// using addressID string, returns AddressPring struct and error
func (c *Client) GetAddressPing(addressID string) (AddressPing, error) {
var addressPingData AddressPing
req, _ := http.NewRequest("GET", c.ServerURL+"/api/"+c.Application+"/addresses/"+addressID+"/ping/", nil)
body, err := c.Do(req)
if err != nil {
return addressPingData, err
}
err = json.Unmarshal([]byte(body), &addressPingData)
if err != nil {
return addressPingData, err
}
if addressPingData.Code == 200 || addressPingData.Code == 404 {
// Accepted responses
} else {
return addressPingData, errors.New(addressPingData.Message)
}
return addressPingData, nil
}
/*
DeleteAddress Client pointer method to delete a phpipam address using
addressID string, returns AddressDelete struct and error
*/
func (c *Client) DeleteAddress(addressID string) (AddressDelete, error) {
var addressDeleteData AddressDelete
req, _ := http.NewRequest("DELETE", c.ServerURL+"/api/"+c.Application+"/addresses/"+addressID+"/", nil)
body, err := c.Do(req)
if err != nil {
return addressDeleteData, err
}
err = json.Unmarshal([]byte(body), &addressDeleteData)
if err != nil {
return addressDeleteData, err
}
if addressDeleteData.Code != 200 {
return addressDeleteData, errors.New(addressDeleteData.Message)
}
return addressDeleteData, nil
}
/*
CreateAddressFirstFree Client pointer method to create the first avalible
phpipam address (starting from the top of the subnet) using subnetID string,
hostname string and owner string, returns AddressFirstFree struct and error
*/
func (c *Client) CreateAddressFirstFree(subnetID string, hostname string, owner string, description string) (AddressFirstFree, error) {
var addressFirstFreeData AddressFirstFree
var reqBody string
if len(description) > 0 {
reqBody = "hostname=" + hostname + "&owner=" + owner + "&description=" + url.QueryEscape(description)
} else {
reqBody = "hostname=" + hostname + "&owner=" + owner
}
req, _ := http.NewRequest("POST", c.ServerURL+"/api/"+c.Application+"/addresses/first_free/"+subnetID+"/", strings.NewReader(reqBody))
body, err := c.Do(req)
if err != nil {
return addressFirstFreeData, err
}
err = json.Unmarshal([]byte(body), &addressFirstFreeData)
if err != nil {
return addressFirstFreeData, err
}
if addressFirstFreeData.Code != 201 {
return addressFirstFreeData, errors.New(addressFirstFreeData.Message)
}
return addressFirstFreeData, nil
}
// GetAddressSearchIP Client pointer method to search for phpipam address using
// address string, returns AddressSearchIP struct and error
func (c *Client) GetAddressSearchIP(address string) (AddressSearchIP, error) {
var addressSearchIPData AddressSearchIP
req, _ := http.NewRequest("GET", c.ServerURL+"/api/"+c.Application+"/addresses/search/"+address+"/", nil)
body, err := c.Do(req)
if err != nil {
return addressSearchIPData, err
}
err = json.Unmarshal([]byte(body), &addressSearchIPData)
if err != nil {
return addressSearchIPData, err
}
if addressSearchIPData.Code != 200 {
return addressSearchIPData, errors.New(addressSearchIPData.Message)
}
return addressSearchIPData, nil
}
// PatchUpdateAddress Client pointer method to be able to update a phpipam
// address to a new hostname using hostname string and addressID string, returns
// UpdateAddress struct and error
func (c *Client) PatchUpdateAddress(hostname string, addressID string) (UpdateAddress, error) {
var updateAddressData UpdateAddress
reqBody := "hostname=" + hostname
req, _ := http.NewRequest("PATCH", c.ServerURL+"/api/"+c.Application+"/addresses/"+addressID+"/", strings.NewReader(reqBody))
body, err := c.Do(req)
if err != nil {
return updateAddressData, err
}
err = json.Unmarshal([]byte(body), &updateAddressData)
if err != nil {
return updateAddressData, err
}
if updateAddressData.Code != 200 {
return updateAddressData, errors.New(updateAddressData.Message)
}
return updateAddressData, nil
}