From a737ac4d9d83278e01a767eff8ce4b0245007546 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Sat, 8 Apr 2023 18:11:56 +0200 Subject: [PATCH] Add Static Route API --- internal/api/network/static_routes.go | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 internal/api/network/static_routes.go diff --git a/internal/api/network/static_routes.go b/internal/api/network/static_routes.go new file mode 100644 index 0000000..1ce276b --- /dev/null +++ b/internal/api/network/static_routes.go @@ -0,0 +1,59 @@ +package network + +import ( + "context" + "fmt" + + "nfsense.net/nfsense/internal/definitions" +) + +type GetStaticRoutesResult struct { + StaticRoutes []definitions.StaticRoute +} + +func (f *Network) GetStaticRoutes(ctx context.Context, params struct{}) (GetStaticRoutesResult, error) { + return GetStaticRoutesResult{ + StaticRoutes: f.ConfigManager.GetPendingConfig().Network.StaticRoutes, + }, nil +} + +func (f *Network) CreateStaticRoute(ctx context.Context, params definitions.StaticRoute) (struct{}, error) { + t, conf := f.ConfigManager.StartTransaction() + defer t.Discard() + + conf.Network.StaticRoutes = append(conf.Network.StaticRoutes, params) + return struct{}{}, t.Commit() +} + +type UpdateStaticRouteParameters struct { + Index uint + definitions.StaticRoute +} + +func (f *Network) UpdateStaticRoute(ctx context.Context, params UpdateStaticRouteParameters) (struct{}, error) { + if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.DestinationNATRules) { + return struct{}{}, fmt.Errorf("StaticRoute does not Exist") + } + + t, conf := f.ConfigManager.StartTransaction() + defer t.Discard() + + conf.Network.StaticRoutes = append(conf.Network.StaticRoutes, params.StaticRoute) + return struct{}{}, t.Commit() +} + +type DeleteStaticRouteParameters struct { + Index uint +} + +func (f *Network) DeleteStaticRoute(ctx context.Context, params DeleteStaticRouteParameters) (struct{}, error) { + if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.DestinationNATRules) { + return struct{}{}, fmt.Errorf("StaticRoute does not Exist") + } + + t, conf := f.ConfigManager.StartTransaction() + defer t.Discard() + + conf.Network.StaticRoutes = append(conf.Network.StaticRoutes[:params.Index], conf.Network.StaticRoutes[params.Index+1:]...) + return struct{}{}, t.Commit() +}