Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed juniper (ELS) switch port configuration #13

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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