Skip to content

Commit

Permalink
Fixed juniper (ELS) switch port configuration
Browse files Browse the repository at this point in the history
Updated test to represent the actual expected switch configuration result
  • Loading branch information
msniveau committed Sep 20, 2023
1 parent 88a1977 commit 7c11f02
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 44 deletions.
55 changes: 32 additions & 23 deletions pkg/vendors/juniper-els/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package juniper_els
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"github.com/Juniper/go-netconf/netconf"
"github.com/g-portal/switchmgr-go/pkg/models"
Expand Down Expand Up @@ -134,27 +133,36 @@ func (s *JuniperELS) isUplink(inter string) bool {
return false
}

const EditPortConfigurationTemplate = `<interfaces>
<interface operation="replace">
<name>{{ .Name }}</name>
<description>{{ .Description }}</description>
{{if .UntaggedVLAN}}<native-vlan-id>{{ .UntaggedVLAN }}</native-vlan-id>{{end}}
<unit>
<name>0</name>
<family>
<ethernet-switching>
<interface-mode>trunk</interface-mode>
<vlan>
{{range .TaggedVLANs }}<members>{{ . }}</members>{{end}}
</vlan>
<storm-control>
<profile-name>default</profile-name>
</storm-control>
</ethernet-switching>
</family>
</unit>
</interface>
</interfaces>`
const EditPortConfigurationTemplate = `<edit-config>
<target>
<candidate/>
</target>
<default-operation>merge</default-operation>
<test-option>test-then-set</test-option>
<config>
<configuration>
<interfaces>
<interface operation="replace">
<name>{{ .Name }}</name>
<description>{{ .Description }}</description>
{{if .UntaggedVLAN}}<native-vlan-id>{{ .UntaggedVLAN }}</native-vlan-id>{{end}}
<unit>
<name>0</name>
<family>
<ethernet-switching>
<interface-mode>trunk</interface-mode>
{{if gt (len .TaggedVLANs) 0}}<vlan>
{{range .TaggedVLANs }}<members>{{ . }}</members>{{end}}
</vlan>{{end}}
</ethernet-switching>
</family>
</unit>
</interface>
</interfaces>
</configuration>
</config>
</edit-config>
`

// ConfigureInterface configures a single interface. It returns true if the
// configuration has changed. If the interface is an uplink, it will return
Expand All @@ -171,6 +179,7 @@ func (j *JuniperELS) ConfigureInterface(update *models.UpdateInterface) (bool, e
if err != nil {
return false, err
}

if !swport.Differs(update) {
return false, nil
}
Expand All @@ -184,7 +193,7 @@ func (j *JuniperELS) ConfigureInterface(update *models.UpdateInterface) (bool, e
update.TaggedVLANs = utils.UniqueVlanIDs(update.TaggedVLANs)

if len(update.TaggedVLANs) == 0 && update.UntaggedVLAN != nil && *update.UntaggedVLAN == 0 {
return false, errors.New("switch port has no vlans to configure")
return false, fmt.Errorf("switch port has no vlans to configure")
}

var tpl bytes.Buffer
Expand Down
51 changes: 30 additions & 21 deletions pkg/vendors/juniper-els/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,36 @@ func TestGetInterface(t *testing.T) {
}
}

const EditPortConfigurationExpected = `<interfaces>
<interface operation="replace">
<name>eth0</name>
<description>example interface</description>
<native-vlan-id>1337</native-vlan-id>
<unit>
<name>0</name>
<family>
<ethernet-switching>
<interface-mode>trunk</interface-mode>
<vlan>
<members>1</members><members>2</members><members>3</members>
</vlan>
<storm-control>
<profile-name>default</profile-name>
</storm-control>
</ethernet-switching>
</family>
</unit>
</interface>
</interfaces>`
const EditPortConfigurationExpected = `<edit-config>
<target>
<candidate/>
</target>
<default-operation>merge</default-operation>
<test-option>test-then-set</test-option>
<config>
<configuration>
<interfaces>
<interface operation="replace">
<name>eth0</name>
<description>example interface</description>
<native-vlan-id>1337</native-vlan-id>
<unit>
<name>0</name>
<family>
<ethernet-switching>
<interface-mode>trunk</interface-mode>
<vlan>
<members>1</members><members>2</members><members>3</members>
</vlan>
</ethernet-switching>
</family>
</unit>
</interface>
</interfaces>
</configuration>
</config>
</edit-config>
`

func TestConfigureInterfaceTemplate(t *testing.T) {
var tpl bytes.Buffer
Expand Down

0 comments on commit 7c11f02

Please sign in to comment.