-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetbox.go
140 lines (106 loc) · 3.89 KB
/
netbox.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
/*
Zabbix -> NetBox synchronization tool
Copyright (C) 2025 SUSE LLC <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"encoding/json"
"github.com/netbox-community/go-netbox/v4"
"net/http"
"os"
)
type site struct {
Name string
Slug string
Domain string
}
func nbConnect(url string, token string) (*netbox.APIClient, context.Context) {
return netbox.NewAPIClientFor(url, token), context.Background()
}
func getVirtualMachines(nb *netbox.APIClient, ctx context.Context) *netbox.PaginatedVirtualMachineWithConfigContextList {
result, _, err := nb.VirtualizationAPI.VirtualizationVirtualMachinesList(ctx).Execute()
handleError("Querying virtual machines", err)
Debug("getVirtualMachines() returns %v", result.Results)
return result
}
func getDevices(nb *netbox.APIClient, ctx context.Context) *netbox.PaginatedDeviceWithConfigContextList {
result, _, err := nb.DcimAPI.DcimDevicesList(ctx).Execute()
handleError("Querying devices", err)
Debug("getDevices() returns %v", result.Results)
return result
}
func getSites(nb *netbox.APIClient, ctx context.Context) []site {
result, _, err := nb.DcimAPI.DcimSitesList(ctx).Execute()
handleError("Querying sites", err)
Debug("getSites() returns %v", result.Results)
var sites []site
for _, object := range result.Results {
Debug("Processing site %+v", object)
var domain string
var has_domain bool
if value, ok := object.CustomFields["domain"]; ok {
domain, has_domain = value.(string)
}
if !has_domain {
continue
}
sites = append(sites, site{
Name: object.Name,
Slug: object.Slug,
Domain: domain,
})
}
return sites
}
func handleResponse(created interface{}, response *http.Response, err error) {
if err != nil {
Error("API returned: %s", err)
}
var body interface{}
jerr := json.NewDecoder(response.Body).Decode(&body)
handleError("Decoding response body", jerr)
if body != nil {
if err == nil {
Debug("%+v", body)
} else {
Error("%+v", body)
}
}
if err != nil || jerr != nil {
os.Exit(1)
}
Debug("Returned object: %+v", created)
}
func assignMacAddress(nb *netbox.APIClient, ctx context.Context, objid int32, address string, aobjtype string, aobjid int64) {
Info("Assigning MAC address object %d to %s object %d", objid, aobjtype, aobjid)
request := netbox.PatchedMACAddressRequest{
MacAddress: &address,
AssignedObjectType: *netbox.NewNullableString(&aobjtype),
AssignedObjectId: *netbox.NewNullableInt64(&aobjid),
}
Debug("Payload: %+v", request)
created, response, rerr := nb.DcimAPI.DcimMacAddressesPartialUpdate(ctx, objid).PatchedMACAddressRequest(request).Execute()
handleResponse(created, response, rerr)
}
func assignIpAddress(nb *netbox.APIClient, ctx context.Context, objid int32, address string, aobjtype string, aobjid int64) {
Info("Assigning IP address object %d to %s object %d", objid, aobjtype, aobjid)
request := netbox.PatchedWritableIPAddressRequest{
Address: &address,
AssignedObjectType: *netbox.NewNullableString(&aobjtype),
AssignedObjectId: *netbox.NewNullableInt64(&aobjid),
}
Debug("Payload: %+v", request)
created, response, rerr := nb.IpamAPI.IpamIpAddressesPartialUpdate(ctx, objid).PatchedWritableIPAddressRequest(request).Execute()
handleResponse(created, response, rerr)
}