mirror of
https://github.com/speatzle/nfsense.git
synced 2025-05-11 02:48:21 +00:00
Add Create, Update and Move API Methods
This commit is contained in:
parent
d89edc1a6a
commit
ddc17caba5
3 changed files with 192 additions and 3 deletions
|
@ -2,12 +2,13 @@ package firewall
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetDestinationNATRulesResult struct {
|
||||
DestinationNATRules []definitions.DestinationNATRule
|
||||
DestinationNATRules []definitions.DestinationNATRule `json:"destination_nat_rules"`
|
||||
}
|
||||
|
||||
func (f *Firewall) GetDestinationNATRules(ctx context.Context, params struct{}) (GetDestinationNATRulesResult, error) {
|
||||
|
@ -15,3 +16,65 @@ func (f *Firewall) GetDestinationNATRules(ctx context.Context, params struct{})
|
|||
DestinationNATRules: f.ConfigManager.GetPendingConfig().Firewall.DestinationNATRules,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type CreateDestinationNATRuleParameters struct {
|
||||
DestinationNATRule definitions.DestinationNATRule `json:"destination_nat_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) CreateDestinationNATRule(ctx context.Context, params CreateDestinationNATRuleParameters) (struct{}, error) {
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.DestinationNATRules = append(conf.Firewall.DestinationNATRules, params.DestinationNATRule)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type UpdateDestinationNATRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
DestinationNATRule definitions.DestinationNATRule `json:"destination_nat_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) UpdateDestinationNATRule(ctx context.Context, params UpdateDestinationNATRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.DestinationNATRules) {
|
||||
return struct{}{}, fmt.Errorf("DestinationNATRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.DestinationNATRules[params.Index] = params.DestinationNATRule
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type MoveDestinationNATRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
ToIndex uint64 `json:"to_index"`
|
||||
}
|
||||
|
||||
func (f *Firewall) MoveDestinationNATRule(ctx context.Context, params DeleteDestinationNATRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.DestinationNATRules) {
|
||||
return struct{}{}, fmt.Errorf("DestinationNATRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.DestinationNATRules = append(conf.Firewall.DestinationNATRules[:params.Index], conf.Firewall.DestinationNATRules[params.Index+1:]...)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type DeleteDestinationNATRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
}
|
||||
|
||||
func (f *Firewall) DeleteDestinationNATRule(ctx context.Context, params DeleteDestinationNATRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.DestinationNATRules) {
|
||||
return struct{}{}, fmt.Errorf("DestinationNATRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.DestinationNATRules = append(conf.Firewall.DestinationNATRules[:params.Index], conf.Firewall.DestinationNATRules[params.Index+1:]...)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
|
|
@ -2,12 +2,13 @@ package firewall
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetForwardRulesResult struct {
|
||||
ForwardRules []definitions.ForwardRule
|
||||
ForwardRules []definitions.ForwardRule `json:"forward_rules"`
|
||||
}
|
||||
|
||||
func (f *Firewall) GetForwardRules(ctx context.Context, params struct{}) (GetForwardRulesResult, error) {
|
||||
|
@ -15,3 +16,65 @@ func (f *Firewall) GetForwardRules(ctx context.Context, params struct{}) (GetFor
|
|||
ForwardRules: f.ConfigManager.GetPendingConfig().Firewall.ForwardRules,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type CreateForwardRuleParameters struct {
|
||||
ForwardRule definitions.ForwardRule `json:"forward_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) CreateForwardRule(ctx context.Context, params CreateForwardRuleParameters) (struct{}, error) {
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.ForwardRules = append(conf.Firewall.ForwardRules, params.ForwardRule)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type UpdateForwardRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
ForwardRule definitions.ForwardRule `json:"forward_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) UpdateForwardRule(ctx context.Context, params UpdateForwardRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.ForwardRules) {
|
||||
return struct{}{}, fmt.Errorf("ForwardRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.ForwardRules[params.Index] = params.ForwardRule
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type MoveForwardRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
ToIndex uint64 `json:"to_index"`
|
||||
}
|
||||
|
||||
func (f *Firewall) MoveForwardRule(ctx context.Context, params DeleteForwardRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.ForwardRules) {
|
||||
return struct{}{}, fmt.Errorf("ForwardRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.ForwardRules = append(conf.Firewall.ForwardRules[:params.Index], conf.Firewall.ForwardRules[params.Index+1:]...)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type DeleteForwardRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
}
|
||||
|
||||
func (f *Firewall) DeleteForwardRule(ctx context.Context, params DeleteForwardRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.ForwardRules) {
|
||||
return struct{}{}, fmt.Errorf("ForwardRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.ForwardRules = append(conf.Firewall.ForwardRules[:params.Index], conf.Firewall.ForwardRules[params.Index+1:]...)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
|
|
@ -2,12 +2,13 @@ package firewall
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"nfsense.net/nfsense/internal/definitions"
|
||||
)
|
||||
|
||||
type GetSourceNATRulesResult struct {
|
||||
SourceNATRules []definitions.SourceNATRule
|
||||
SourceNATRules []definitions.SourceNATRule `json:"source_nat_rules"`
|
||||
}
|
||||
|
||||
func (f *Firewall) GetSourceNATRules(ctx context.Context, params struct{}) (GetSourceNATRulesResult, error) {
|
||||
|
@ -15,3 +16,65 @@ func (f *Firewall) GetSourceNATRules(ctx context.Context, params struct{}) (GetS
|
|||
SourceNATRules: f.ConfigManager.GetPendingConfig().Firewall.SourceNATRules,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type CreateSourceNATRuleParameters struct {
|
||||
SourceNATRule definitions.SourceNATRule `json:"source_nat_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) CreateSourceNATRule(ctx context.Context, params CreateSourceNATRuleParameters) (struct{}, error) {
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.SourceNATRules = append(conf.Firewall.SourceNATRules, params.SourceNATRule)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type UpdateSourceNATRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
SourceNATRule definitions.SourceNATRule `json:"source_nat_rule"`
|
||||
}
|
||||
|
||||
func (f *Firewall) UpdateSourceNATRule(ctx context.Context, params UpdateSourceNATRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.SourceNATRules) {
|
||||
return struct{}{}, fmt.Errorf("SourceNATRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.SourceNATRules[params.Index] = params.SourceNATRule
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type MoveSourceNATRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
ToIndex uint64 `json:"to_index"`
|
||||
}
|
||||
|
||||
func (f *Firewall) MoveSourceNATRule(ctx context.Context, params DeleteSourceNATRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.SourceNATRules) {
|
||||
return struct{}{}, fmt.Errorf("SourceNATRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.SourceNATRules = append(conf.Firewall.SourceNATRules[:params.Index], conf.Firewall.SourceNATRules[params.Index+1:]...)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
||||
type DeleteSourceNATRuleParameters struct {
|
||||
Index uint64 `json:"index"`
|
||||
}
|
||||
|
||||
func (f *Firewall) DeleteSourceNATRule(ctx context.Context, params DeleteSourceNATRuleParameters) (struct{}, error) {
|
||||
if int(params.Index) >= len(f.ConfigManager.GetPendingConfig().Firewall.SourceNATRules) {
|
||||
return struct{}{}, fmt.Errorf("SourceNATRule does not Exist")
|
||||
}
|
||||
|
||||
t, conf := f.ConfigManager.StartTransaction()
|
||||
defer t.Discard()
|
||||
|
||||
conf.Firewall.SourceNATRules = append(conf.Firewall.SourceNATRules[:params.Index], conf.Firewall.SourceNATRules[params.Index+1:]...)
|
||||
return struct{}{}, t.Commit()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue