This repository was archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (98 loc) · 2.85 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"time"
)
var (
netbox = flag.String("netbox", "https://netbox.local", "Netbox BaseURL")
netboxAPIToken = flag.String("netbox-api-token", "", "Mandatory: Netbox API Token")
netboxDevice = flag.String("netbox-device", "", "Device String to search for")
tld = flag.String("tld", ".local", "Default TLD for devices")
)
// NetboxResult the whole Json Reply
type NetboxResult struct {
Count int `json:"count"`
Next interface{} `json:"next"`
Previous interface{} `json:"previous"`
Results []NetboxResultList `json:"results"`
}
// NetboxResultList all Results of the json call
type NetboxResultList struct {
ID int `json:"id"`
Name string `json:"name,omitempty"`
Address string `json:"address,omitempty"`
}
func main() {
flag.Parse()
if *netboxAPIToken == "" {
panic("Please provide a netbox-api-token")
}
netboxClient := http.Client{
Timeout: time.Second * 2,
}
devices := make(map[string]bool)
devices = getAllDevices(netboxClient, *netboxDevice)
for device := range devices {
fmt.Println(fmt.Sprintf("%s%s", device, *tld))
}
}
func getAllDevices(netboxClient http.Client, searchString string) (netboxDevices map[string]bool) {
deviceMap := make(map[string]bool)
deviceURL := fmt.Sprintf("%s/api/dcim/devices/?q=%s", *netbox, searchString)
vmURL := fmt.Sprintf("%s/api/virtualization/virtual-machines/?q=%s", *netbox, searchString)
// Query devices
req, err := http.NewRequest(http.MethodGet, deviceURL, nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Token %s", *netboxAPIToken))
res, err := netboxClient.Do(req)
if err != nil {
panic(err)
}
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
panic(err)
}
var netboxDeviceList NetboxResult
bodyErr := json.Unmarshal(body, &netboxDeviceList)
if bodyErr != nil {
fmt.Println("Json Unmarshal failed with: ", err)
}
for i := 0; i < len(netboxDeviceList.Results); i++ {
if _, device := deviceMap[netboxDeviceList.Results[i].Name]; !device {
deviceMap[netboxDeviceList.Results[i].Name] = true
}
}
// Query VMs
req, err = http.NewRequest(http.MethodGet, vmURL, nil)
if err != nil {
panic(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Token %s", *netboxAPIToken))
res, err = netboxClient.Do(req)
if err != nil {
panic(err)
}
body, err = ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
panic(err)
}
var netboxVMList NetboxResult
bodyErr = json.Unmarshal(body, &netboxVMList)
if bodyErr != nil {
fmt.Println("Json Unmarshal failed with: ", err)
}
for i := 0; i < len(netboxVMList.Results); i++ {
if _, device := deviceMap[netboxVMList.Results[i].Name]; !device {
deviceMap[netboxVMList.Results[i].Name] = true
}
}
return (deviceMap)
}