Skip to content

Commit fbfb3a6

Browse files
committed
Extract AdminState type and fix lint issues
Signed-off-by: Vasyl Paliuha <vasyl.paliuha@sap.com>
1 parent 6b5a84d commit fbfb3a6

3 files changed

Lines changed: 20 additions & 14 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package srlinux
5+
6+
type AdminSt string
7+
8+
const (
9+
AdminStEnable AdminSt = "enable"
10+
AdminStDisable AdminSt = "disable"
11+
)

‎internal/provider/nokia/srlinux/dhcprelay.go‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ const (
1818
// dhcpRelayDefaultSubinterfaceIndex is the subinterface a relay attaches to
1919
// when the interface name carries no ".<index>" suffix.
2020
dhcpRelayDefaultSubinterfaceIndex = 0
21-
22-
// dhcpRelayAdminStateEnable is the admin-state value that enables the relay.
23-
// It is also the platform default, so it must be set explicitly to avoid a
24-
// false diff against the device on every reconcile.
25-
dhcpRelayAdminStateEnable = "enable"
2621
)
2722

2823
var _ provider.DHCPRelayProvider = (*Provider)(nil)
@@ -62,7 +57,7 @@ func (p *Provider) EnsureDHCPRelay(ctx context.Context, req *provider.DHCPRelayR
6257
sb.Update(&DHCPRelay{
6358
Interface: name,
6459
SubinterfaceIndex: index,
65-
AdminState: dhcpRelayAdminStateEnable,
60+
AdminState: AdminStEnable,
6661
Server: req.DHCPRelay.Spec.Servers,
6762
NetworkInstance: networkInstance,
6863
})
@@ -75,21 +70,21 @@ func (p *Provider) EnsureDHCPRelay(ctx context.Context, req *provider.DHCPRelayR
7570
// subinterface index. SRLinux names a subinterface "<interface>.<index>";
7671
// without a suffix the relay attaches to the base subinterface.
7772
func splitSubinterface(name string) (string, int, error) {
78-
i := strings.LastIndex(name, ".")
79-
if i == -1 {
73+
base, suffix, found := strings.CutLast(name, ".")
74+
if !found {
8075
return name, dhcpRelayDefaultSubinterfaceIndex, nil
8176
}
82-
index, err := strconv.Atoi(name[i+1:])
77+
index, err := strconv.Atoi(suffix)
8378
if err != nil {
8479
return "", 0, fmt.Errorf("invalid subinterface index in interface name %q: %w", name, err)
8580
}
86-
return name[:i], index, nil
81+
return base, index, nil
8782
}
8883

8984
type DHCPRelay struct {
9085
Interface string `json:"-"`
9186
SubinterfaceIndex int `json:"-"`
92-
AdminState string `json:"admin-state,omitempty"`
87+
AdminState AdminSt `json:"admin-state,omitempty"`
9388
Server []string `json:"server,omitempty"`
9489
NetworkInstance string `json:"network-instance,omitempty"`
9590
}

‎internal/provider/nokia/srlinux/dhcprelay_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ func TestDHCPRelayJSONNetworkInstance(t *testing.T) {
8888
}{
8989
{
9090
name: "empty network-instance is omitted",
91-
relay: &DHCPRelay{AdminState: dhcpRelayAdminStateEnable, Server: []string{"192.0.2.1"}},
91+
relay: &DHCPRelay{AdminState: AdminStEnable, Server: []string{"192.0.2.1"}},
9292
wantKey: false,
9393
},
9494
{
9595
name: "set network-instance is present",
96-
relay: &DHCPRelay{AdminState: dhcpRelayAdminStateEnable, Server: []string{"192.0.2.1"}, NetworkInstance: "shared-services"},
96+
relay: &DHCPRelay{AdminState: AdminStEnable, Server: []string{"192.0.2.1"}, NetworkInstance: "shared-services"},
9797
wantKey: true,
9898
},
9999
}
@@ -225,7 +225,7 @@ func TestDeleteDHCPRelay(t *testing.T) {
225225
err := p.DeleteDHCPRelay(t.Context(), &provider.DeleteDHCPRelayRequest{
226226
DHCPRelay: &corev1alpha1.DHCPRelay{
227227
Spec: corev1alpha1.DHCPRelaySpec{
228-
InterfaceRefs: []corev1alpha1.LocalObjectReference{{Name: "eth1"}},
228+
InterfaceRefs: []corev1alpha1.LocalObjectReference{{Name: "eth1"}}, //nolint:staticcheck
229229
},
230230
},
231231
})

0 commit comments

Comments
 (0)