aboutsummaryrefslogtreecommitdiff
path: root/clients/netbox/model.go
blob: be4767d39254c64d006c04ab805ed63d5014b1df (plain)
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
package netbox

import (
	"encoding/json"
	"fmt"
	"net"
)

type ApiError struct {
	Status int
	Detail string `json:"detail"`
}

func (e *ApiError) Error() string {
	return fmt.Sprintf("Netbox API Error (Status %d): %s", e.Status, e.Detail)
}

var _ error = (*ApiError)(nil)

type LabeledInt struct {
	Value int    `json:"value"`
	Label string `json:"label"`
}

type LabeledString struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

type Role struct {
	ID      int    `json:"ID"`
	Url     string `json:"url"`
	Display string `json:"display"`
	Name    string `json:"name"`
	Slug    string `json:"slub"`
}

type Tag struct {
	ID      int    `json:"id"`
	Url     string `json:"url"`
	Display string `json:"display"`
	Name    string `json:"name"`
	Slug    string `json:"slug"`
	Color   string `json:"color"`
}

type SiteList struct {
	Count    int     `json:"count"`
	Next     string  `json:"next"`
	Previous string  `json:"previous"`
	Results  []*Site `json:"results"`
}

type Site struct {
	ID          int           `json:"id"`
	Url         string        `json:"url"`
	Display     string        `json:"display"`
	Name        string        `json:"name"`
	Slug        string        `json:"slug"`
	Facility    string        `json:"facility"`
	Description string        `json:"description"`
	Timezone    string        `json:"time_zone"`
	ASN         int           `json:"asn"`
	Status      LabeledString `json:"status"`
}

type PrefixList struct {
	Count    int       `json:"count"`
	Next     string    `json:"next"`
	Previous string    `json:"previous"`
	Results  []*Prefix `json:"results"`
}

type Prefix struct {
	ID           int                    `json:"ID"`
	Url          string                 `json:"url"`
	Display      string                 `json:"display"`
	Prefix       string                 `json:"prefix"`
	IsPool       bool                   `json:"is_pool"`
	Description  string                 `json:"description"`
	Created      string                 `json:"created"`
	LastUpdated  string                 `json:"last_updated"`
	Children     int                    `json:"children"`
	Depth        int                    `json:"_depth"`
	Family       LabeledInt             `json:"family"`
	Status       LabeledString          `json:"status"`
	Site         *Site                  `json:"site"`
	Role         *Role                  `json:"role"`
	Tags         []*Tag                 `json:"tags"`
	CustomFields map[string]interface{} `json:"custom_fields"`
}

type ServiceList struct {
	Count    int        `json:"count"`
	Next     string     `json:"next"`
	Previous string     `json:"previous"`
	Results  []*Service `json:"results"`
}

type ServiceProtocol struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

type DeviceOrVM struct {
	ID      int    `json:"id"`
	Url     string `json:"url"`
	Name    string `json:"name"`
	Display string `json:"display"`
}

type ServiceIPAddress struct {
	ID      int    `json:"id"`
	Url     string `json:"url"`
	Display string `json:"display"`
	Family  int    `json:"family"`
	Address net.IP
	Network *net.IPNet
}

func (a *ServiceIPAddress) UnmarshalJSON(d []byte) error {
	type Alias ServiceIPAddress
	v := &struct {
		*Alias
		Address string `json:"address"`
	}{Alias: (*Alias)(a)}

	if err := json.Unmarshal(d, v); err != nil {
		return err
	}

	i, n, err := net.ParseCIDR(v.Address)
	if err != nil {
		return err
	}

	a.Address = i
	a.Network = n

	return nil
}

type Service struct {
	ID             int                    `json:"id" mapstructure:"id"`
	Url            string                 `json:"url" mapstructure:"url"`
	Name           string                 `json:"name" mapstructure:"name"`
	Display        string                 `json:"display" mapstructure:"display"`
	Device         *DeviceOrVM            `json:"device" mapstructure:"device"`
	VirtualMachine *DeviceOrVM            `json:"virtual_machine" mapstructure:"virtual_machine"`
	Ports          []int                  `json:"ports" mapstructure:"ports"`
	Protocol       *ServiceProtocol       `json:"protocol" mapstructure:"protocol"`
	Addresses      []*ServiceIPAddress    `json:"ipaddresses" mapstructure:"ipaddresses"`
	Description    string                 `json:"description" mapstructure:"description"`
	Created        string                 `json:"created" mapstructure:"created"`
	LastUpdated    string                 `json:"last_updated" mapstructure:"last_updated"`
	Tags           []*Tag                 `json:"tags" mapstructure:"tags"`
	CustomFields   map[string]interface{} `json:"custom_fields" mapstructure:"custom_fields"`
}