@@ -20,45 +20,56 @@ type netboxResult struct {
20
20
}
21
21
22
22
type interfacePatchData struct {
23
- Description string `json:"description,omitempty"`
24
- Enabled * bool `json:"enabled,omitempty"`
25
- Parent int `json:"parent,omitempty"`
26
- Lag int `json:"lag,omitempty"`
27
- Bridge int `json:"bridge,omitempty"`
28
- InterfaceType string `json:"type,omitempty"`
29
- Mode string `json:"mode,omitempty"`
30
- UntaggedVlan int `json:"untagged_vlan,omitempty"`
23
+ Description string `json:"description,omitempty"`
24
+ Enabled * bool `json:"enabled,omitempty"`
25
+ Parent int `json:"parent,omitempty"`
26
+ Lag int `json:"lag,omitempty"`
27
+ Bridge int `json:"bridge,omitempty"`
28
+ InterfaceType string `json:"type,omitempty"`
29
+ Mode string `json:"mode,omitempty"`
30
+ UntaggedVlan int `json:"untagged_vlan,omitempty"`
31
+ Tags []string `json:"tags,omitempty"`
31
32
}
32
33
33
34
type interfacePostData struct {
34
- Device int `json:"device"`
35
- Name string `json:"name"`
36
- InterfaceType string `json:"type"`
37
- Description string `json:"description,omitempty"`
38
- Enabled * bool `json:"enabled,omitempty"`
39
- UntaggedVlan int `json:"untagged_vlan,omitempty"`
40
- Mode string `json:"mode,omitempty"`
41
- Parent int `json:"parent,omitempty"`
42
- Bridge int `json:"bridge,omitempty"`
43
- Lag int `json:"lag,omitempty"`
35
+ Device int `json:"device"`
36
+ Name string `json:"name"`
37
+ InterfaceType string `json:"type"`
38
+ Description string `json:"description,omitempty"`
39
+ Enabled * bool `json:"enabled,omitempty"`
40
+ UntaggedVlan int `json:"untagged_vlan,omitempty"`
41
+ Mode string `json:"mode,omitempty"`
42
+ Parent int `json:"parent,omitempty"`
43
+ Bridge int `json:"bridge,omitempty"`
44
+ Lag int `json:"lag,omitempty"`
45
+ Tags []string `json:"tags,omitempty"`
44
46
}
45
47
46
48
type vlanPostData struct {
47
- SiteId int `json:"site,omitempty"`
48
- TenantId int `json:"tenant,omitempty"`
49
- VlanId int `json:"vid"`
50
- Name string `json:"name"`
49
+ SiteId int `json:"site,omitempty"`
50
+ TenantId int `json:"tenant,omitempty"`
51
+ VlanId int `json:"vid"`
52
+ Name string `json:"name"`
53
+ Tags []string `json:"tags,omitempty"`
54
+ }
55
+
56
+ type tagPostData struct {
57
+ Name string `json:"name"`
58
+ Slug string `json:"slug"`
59
+ Color string `json:"color,omitempty"`
60
+ Description string `json:"description,omitempty"`
51
61
}
52
62
53
63
type netboxData interface {
54
- model.NetboxInterface | model.NetboxDevice | model.NetboxVlan
64
+ model.NetboxInterface | model.NetboxDevice | model.NetboxVlan | model. NetboxTag
55
65
}
56
66
57
67
type NetboxHTTPClient struct {
58
68
apikey string
59
69
baseurl string
60
70
client http.Client
61
71
rolesfilter string
72
+ defaultTag model.NetboxTag
62
73
}
63
74
64
75
func NewNetbox (baseurl string , apikey string , roles string ) NetboxHTTPClient {
@@ -80,10 +91,68 @@ func NewNetbox(baseurl string, apikey string, roles string) NetboxHTTPClient {
80
91
rolesfilter = sb .String ()
81
92
}
82
93
83
- e := NetboxHTTPClient {apikey , baseurl , * client , rolesfilter }
94
+ e := NetboxHTTPClient {apikey , baseurl , * client , rolesfilter , model. NetboxTag {} }
84
95
return e
85
96
}
86
97
98
+ func (e * NetboxHTTPClient ) GetManagedTag (tagName string ) {
99
+ tag , err := getNetboxTagByName (tagName , e )
100
+ if err != nil {
101
+ slog .Error ("Error getting tags" , err )
102
+ }
103
+ if tag .ID == 0 {
104
+ newTag := e .createNetboxTag (tagName )
105
+ e .defaultTag = newTag
106
+ } else {
107
+ e .defaultTag = tag
108
+ }
109
+ }
110
+
111
+ func getNetboxTagByName (tagName string , e * NetboxHTTPClient ) (model.NetboxTag , error ) {
112
+ requestURL := fmt .Sprintf ("%s/api/extras/tags/" , e .baseurl )
113
+ tags , err := apiRequest [model.NetboxTag ](requestURL , e )
114
+ if err != nil {
115
+ return model.NetboxTag {}, err
116
+ }
117
+ for _ , iface := range tags {
118
+ if iface .Name == tagName {
119
+ return iface , nil
120
+ }
121
+ }
122
+ return model.NetboxTag {}, nil
123
+ }
124
+
125
+ func slugify (input string ) string {
126
+ var result string
127
+
128
+ result = strings .ToLower (input )
129
+ result = strings .Replace (result , " " , "-" , - 1 )
130
+
131
+ return result
132
+ }
133
+
134
+ func (e * NetboxHTTPClient ) createNetboxTag (tagName string ) model.NetboxTag {
135
+ var postData tagPostData
136
+ postData .Name = tagName
137
+ postData .Slug = slugify (tagName )
138
+ postData .Description = "Auto generated tag to track objects created by the oxidized sync"
139
+ postData .Color = "72599f"
140
+
141
+ data , _ := json .Marshal (postData )
142
+ requestURL := fmt .Sprintf ("%s/api/extras/tags/" , e .baseurl )
143
+ resBody , err := TokenAuthHTTPPost (requestURL , e .apikey , & e .client , data )
144
+ if err != nil {
145
+ slog .Error (err .Error ())
146
+ }
147
+
148
+ var result model.NetboxTag
149
+ err = json .Unmarshal (resBody , & result )
150
+ if err != nil {
151
+ slog .Error (err .Error ())
152
+ }
153
+ return result
154
+ }
155
+
87
156
func loopAPIRequest (path string , e * NetboxHTTPClient ) (netboxResult , error ) {
88
157
resBody , err := TokenAuthHTTPGet (path , e .apikey , & e .client )
89
158
if err != nil {
@@ -165,6 +234,7 @@ func (e *NetboxHTTPClient) createVlan(SiteId int, TenantId int, VlanId int, Name
165
234
postData .SiteId = SiteId
166
235
postData .VlanId = VlanId
167
236
postData .TenantId = TenantId
237
+ postData .Tags = []string {strconv .Itoa (e .defaultTag .ID )}
168
238
169
239
data , _ := json .Marshal (postData )
170
240
requestURL := fmt .Sprintf ("%s/api/ipam/vlans/" , e .baseurl )
@@ -232,6 +302,10 @@ func (e *NetboxHTTPClient) updateInterface(port model.NetboxInterfaceUpdateCreat
232
302
if port .PortTypeUpdate == "virtual" {
233
303
patchData .InterfaceType = "virtual"
234
304
}
305
+ if port .PortTypeUpdate == "bridge" {
306
+ patchData .InterfaceType = "bridge"
307
+ }
308
+
235
309
}
236
310
if port .VlanMode != "" {
237
311
patchData .Mode = port .VlanMode
@@ -248,6 +322,17 @@ func (e *NetboxHTTPClient) updateInterface(port model.NetboxInterfaceUpdateCreat
248
322
}
249
323
}
250
324
325
+ if len (port .Tags ) != 0 {
326
+ for _ ,tag := range port .Tags {
327
+ if tag != strconv .Itoa (e .defaultTag .ID ) {
328
+ patchData .Tags = append (patchData .Tags , tag )
329
+ }
330
+ }
331
+ patchData .Tags = append (patchData .Tags , strconv .Itoa (e .defaultTag .ID ))
332
+ } else {
333
+ patchData .Tags = []string {strconv .Itoa (e .defaultTag .ID )}
334
+ }
335
+
251
336
data , _ := json .Marshal (patchData )
252
337
requestURL := fmt .Sprintf ("%s/%s%s/" , e .baseurl , "api/dcim/interfaces/" , port .InterfaceId )
253
338
_ , err := TokenAuthHTTPPatch (requestURL , e .apikey , & e .client , data )
@@ -331,6 +416,8 @@ func (e *NetboxHTTPClient) createInterface(port model.NetboxInterfaceUpdateCreat
331
416
}
332
417
}
333
418
419
+ postData .Tags = []string {strconv .Itoa (e .defaultTag .ID )}
420
+
334
421
data , _ := json .Marshal (postData )
335
422
requestURL := fmt .Sprintf ("%s/%s" , e .baseurl , "api/dcim/interfaces/" )
336
423
_ , err := TokenAuthHTTPPost (requestURL , e .apikey , & e .client , data )
0 commit comments