Skip to content

Commit

Permalink
virtual switch part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
mattieserver committed Sep 11, 2024
1 parent d384ba7 commit 4c1759a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
9 changes: 7 additions & 2 deletions internal/configparser/fortios.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ func convertVirtualSwitch(virtutalSwitches *[]model.FortigateVirtualSwitch, devi
for _, member := range *virtutalSwitches {
var vswitch model.FortigateInterface
vswitch.Name = member.Name
vswitch.InterfaceType = "bridge"
vswitch.InterfaceType = "virtual-switch"
vswitch.Description = "virtual-switch"
vswitch.Members = member.Members
*deviceInterfaces = append(*deviceInterfaces, vswitch)
for _, vswitchMember := range member.Members {
Expand Down Expand Up @@ -220,6 +221,10 @@ func parseSingleInterface(interfaceData []string, results *[]model.FortigateInte
return
}

if alias == "''" {
alias = ""
}

switch interfaceType {
case "aggregate":
var aggr model.FortigateInterface
Expand Down Expand Up @@ -252,7 +257,7 @@ func parseSingleInterface(interfaceData []string, results *[]model.FortigateInte
func createVlan(name string, alias string, vdom string, vlanId string, parentName string, description string) model.FortigateInterface {
var vid model.FortigateInterface
vid.InterfaceType = "vlan"
if alias != "" && alias == "''" {
if alias != "" {
vid.Name = alias
} else {
vid.Name = name
Expand Down
23 changes: 17 additions & 6 deletions internal/httphelper/netbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type interfacePatchData struct {
Enabled *bool `json:"enabled,omitempty"`
Parent int `json:"parent,omitempty"`
Lag int `json:"lag,omitempty"`
Bridge int `json:"bridge,omitempty"`
InterfaceType string `json:"type,omitempty"`
Mode string `json:"mode,omitempty"`
UntaggedVlan int `json:"untagged_vlan,omitempty"`
Expand All @@ -38,6 +39,7 @@ type interfacePostData struct {
UntaggedVlan int `json:"untagged_vlan,omitempty"`
Mode string `json:"mode,omitempty"`
Parent int `json:"parent,omitempty"`
Bridge int `json:"bridge,omitempty"`
Lag int `json:"lag,omitempty"`
}

Expand Down Expand Up @@ -192,7 +194,12 @@ func (e *NetboxHTTPClient) updateInterface(port model.NetboxInterfaceUpdateCreat
if port.Parent != "" {
if port.ParentId != "" {
if port.PortType == "physical" {
patchData.Lag, _ = strconv.Atoi(port.ParentId)
if port.ParentType == "virtual-switch" {
patchData.Bridge, _ = strconv.Atoi(port.ParentId)
} else if port.ParentType == "aggregate" {
patchData.Lag, _ = strconv.Atoi(port.ParentId)
}

} else {
patchData.Parent, _ = strconv.Atoi(port.ParentId)
}
Expand Down Expand Up @@ -263,10 +270,10 @@ func (e *NetboxHTTPClient) createInterface(port model.NetboxInterfaceUpdateCreat

if port.PortType == "aggregate" {
postData.InterfaceType = "lag"
}

if port.PortType == "vlan" {
} else if port.PortType == "vlan" {
postData.InterfaceType = "virtual"
} else if port.PortType == "virtual-switch" {
postData.InterfaceType = "bridge"
}

if port.VlanId != "" {
Expand Down Expand Up @@ -308,7 +315,11 @@ func (e *NetboxHTTPClient) createInterface(port model.NetboxInterfaceUpdateCreat
if port.Parent != "" {
if port.ParentId != "" {
if port.PortType == "physical" {
postData.Lag, _ = strconv.Atoi(port.ParentId)
if port.ParentType == "virtual-switch" {
postData.Bridge, _ = strconv.Atoi(port.ParentId)
} else if port.ParentType == "aggregate" {
postData.Lag, _ = strconv.Atoi(port.ParentId)
}
} else {
postData.Parent, _ = strconv.Atoi(port.ParentId)
}
Expand All @@ -325,7 +336,7 @@ func (e *NetboxHTTPClient) createInterface(port model.NetboxInterfaceUpdateCreat
_, err := TokenAuthHTTPPost(requestURL, e.apikey, &e.client, data)
if err != nil {
slog.Error(err.Error())

}
}

Expand Down
17 changes: 8 additions & 9 deletions internal/model/DeviceInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ type NetboxInterface struct {
} `json:"type"`
Enabled bool `json:"enabled"`
Parent interface{} `json:"parent"`
Bridge struct{
ID int `json:"id"`
Name string `json:"name"`
Bridge struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"bridge"`
Lag struct {
Lag struct {
ID int `json:"id"`
URL string `json:"url"`
Display string `json:"display"`
Expand Down Expand Up @@ -203,8 +203,8 @@ type NetboxDevice struct {
}

type FortigateVirtualSwitch struct {
Name string
Members []string
Name string
Members []string
}

type NetboxInterfaceUpdateCreate struct {
Expand All @@ -216,11 +216,11 @@ type NetboxInterfaceUpdateCreate struct {
Description string
Mode string
Parent string
ParentId string
ParentId string
ParentType string
VlanMode string
VlanId string
InterfaceId string

}

type NetboxVlan struct {
Expand Down Expand Up @@ -261,4 +261,3 @@ type NetboxVlan struct {
LastUpdated time.Time `json:"last_updated"`
PrefixCount int `json:"prefix_count"`
}

24 changes: 20 additions & 4 deletions internal/netboxparser/fortitonetbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/mattieserver/netbox-oxidized-sync/internal/model"
)

const (
virtualSwitchName = "virtual-switch"
lagName = "aggregate"
)

func getParentID(parentName string, netboxDeviceInterfaces *[]model.NetboxInterface) string {
for _, netboxParentInterface := range *netboxDeviceInterfaces {
if strings.EqualFold(netboxParentInterface.Name, parentName) {
Expand All @@ -28,23 +33,28 @@ func processPort(port model.FortigateInterface, allMembers map[string]int, forti
InterfaceId: strconv.Itoa(netboxInterface.ID),
}

if port.InterfaceType == "aggregate" && netboxInterface.Type.Value != "lag" {
if port.InterfaceType == lagName && netboxInterface.Type.Value != "lag" {
matched.PortTypeUpdate = "lag"
}
if port.InterfaceType == virtualSwitchName && netboxInterface.Type.Value != "bridge" {
matched.PortTypeUpdate = "bridge"
}
if !strings.EqualFold(port.Description, netboxInterface.Description) {
matched.Description = port.Description
}
if port.InterfaceType == "physical" && len(allMembers) > 0 {
if parentIndex, ok := allMembers[port.Name]; ok {
if (*fortiInterfaces)[parentIndex].InterfaceType == "aggregate" {
if (*fortiInterfaces)[parentIndex].InterfaceType == lagName {
matched.ParentType = lagName
if netboxInterface.Lag.ID == 0 {
matched.Parent = (*fortiInterfaces)[parentIndex].Name
} else {
if !strings.EqualFold(netboxInterface.Lag.Name, (*fortiInterfaces)[parentIndex].Name) {
matched.Parent = (*fortiInterfaces)[parentIndex].Name
}
}
} else if (*fortiInterfaces)[parentIndex].InterfaceType == "bridge" {
} else if (*fortiInterfaces)[parentIndex].InterfaceType == virtualSwitchName {
matched.ParentType = virtualSwitchName
if netboxInterface.Bridge.ID == 0 {
matched.Parent = (*fortiInterfaces)[parentIndex].Name
} else {
Expand Down Expand Up @@ -101,7 +111,7 @@ func processPort(port model.FortigateInterface, allMembers map[string]int, forti
matched.Status = "enabled"
}
}
} else if port.InterfaceType == "aggregate" && len(port.Members) > 0 {
} else if port.InterfaceType == lagName && len(port.Members) > 0 {
if len(port.Members) != 1 && port.Members[0] != "" {
matched.Mode = "create"
matched.PortType = port.InterfaceType
Expand All @@ -127,6 +137,12 @@ func processPort(port model.FortigateInterface, allMembers map[string]int, forti
matched.VlanId = port.VlanId
matched.Parent = port.Parent
matched.ParentId = getParentID(matched.Parent, netboxDeviceInterfaces)
} else if port.InterfaceType == virtualSwitchName {
matched.Mode = "create"
matched.Name = port.Name
matched.Description = port.Description
matched.PortType = port.InterfaceType
matched.DeviceId = deviceId
}
} else {
if matched.Description != "" || matched.Status != "" || matched.PortTypeUpdate != "" || matched.Parent != "" || matched.VlanMode != "" {
Expand Down

0 comments on commit 4c1759a

Please sign in to comment.