[management] Enforce peer or peer groups requirement for network routers (#5894)

This commit is contained in:
Bethuel Mmbaga
2026-04-16 13:12:19 +03:00
committed by GitHub
parent 95bc01e48f
commit 08f624507d
4 changed files with 65 additions and 20 deletions

View File

@@ -21,11 +21,7 @@ type NetworkRouter struct {
}
func NewNetworkRouter(accountID string, networkID string, peer string, peerGroups []string, masquerade bool, metric int, enabled bool) (*NetworkRouter, error) {
if peer != "" && len(peerGroups) > 0 {
return nil, errors.New("peer and peerGroups cannot be set at the same time")
}
return &NetworkRouter{
r := &NetworkRouter{
ID: xid.New().String(),
AccountID: accountID,
NetworkID: networkID,
@@ -34,7 +30,25 @@ func NewNetworkRouter(accountID string, networkID string, peer string, peerGroup
Masquerade: masquerade,
Metric: metric,
Enabled: enabled,
}, nil
}
if err := r.Validate(); err != nil {
return nil, err
}
return r, nil
}
func (n *NetworkRouter) Validate() error {
if n.Peer != "" && len(n.PeerGroups) > 0 {
return errors.New("peer and peer_groups cannot be set at the same time")
}
if n.Peer == "" && len(n.PeerGroups) == 0 {
return errors.New("either peer or peer_groups must be provided")
}
return nil
}
func (n *NetworkRouter) ToAPIResponse() *api.NetworkRouter {

View File

@@ -38,7 +38,7 @@ func TestNewNetworkRouter(t *testing.T) {
expectedError: false,
},
{
name: "Valid with no peer or peerGroups",
name: "Invalid with no peer or peerGroups",
networkID: "network-3",
accountID: "account-3",
peer: "",
@@ -46,7 +46,18 @@ func TestNewNetworkRouter(t *testing.T) {
masquerade: true,
metric: 300,
enabled: true,
expectedError: false,
expectedError: true,
},
{
name: "Invalid with empty peerGroups slice",
networkID: "network-5",
accountID: "account-5",
peer: "",
peerGroups: []string{},
masquerade: true,
metric: 500,
enabled: true,
expectedError: true,
},
// Invalid cases