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

refactor client Bridge resource to use interface-based implementation #169

Merged
merged 1 commit into from
Jun 27, 2023
Merged
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
102 changes: 53 additions & 49 deletions client/bridge.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package client

import (
"fmt"
"log"
"github.com/go-routeros/routeros"
)

// Bridge defines /bridge resource
Expand All @@ -14,72 +13,77 @@ type Bridge struct {
Comment string `mikrotik:"comment"`
}

func (client Mikrotik) AddBridge(r *Bridge) (*Bridge, error) {
c, err := client.getMikrotikClient()
if err != nil {
return nil, err
}
cmd := Marshal("/interface/bridge/add", r)
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
response, err := c.RunArgs(cmd)
log.Printf("[DEBUG] /interface/bridge/add returned %v", response)
if err != nil {
return nil, err
}
var _ Resource = (*Bridge)(nil)

return client.FindBridge(r.Name)
func (b *Bridge) ActionToCommand(a Action) string {
return map[Action]string{
Add: "/interface/bridge/add",
Find: "/interface/bridge/print",
Update: "/interface/bridge/set",
Delete: "/interface/bridge/remove",
}[a]
}

func (client Mikrotik) FindBridge(id string) (*Bridge, error) {
c, err := client.getMikrotikClient()
func (b *Bridge) IDField() string {
return ".id"
}

if err != nil {
return nil, err
}
cmd := []string{"/interface/bridge/print", "?name=" + id}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)
if err != nil {
return nil, err
}
log.Printf("[DEBUG] Found bridge: %v", r)
func (b *Bridge) ID() string {
return b.Id
}

record := Bridge{}
err = Unmarshal(*r, &record)
if err != nil {
return nil, err
}
if record.Name == "" {
return nil, NewNotFound(fmt.Sprintf("bridge `%s` not found", id))
}
func (b *Bridge) SetID(id string) {
b.Id = id
}

func (b *Bridge) AfterAddHook(r *routeros.Reply) {
b.Id = r.Done.Map["ret"]
}

func (b *Bridge) FindField() string {
return "name"
}

func (b *Bridge) FindFieldValue() string {
return b.Name
}

return &record, nil
func (b *Bridge) DeleteField() string {
return "numbers"
}

func (client Mikrotik) UpdateBridge(r *Bridge) (*Bridge, error) {
c, err := client.getMikrotikClient()
func (b *Bridge) DeleteFieldValue() string {
return b.Name
}

// Typed wrappers
func (c Mikrotik) AddBridge(r *Bridge) (*Bridge, error) {
res, err := c.Add(r)
if err != nil {
return nil, err
}
cmd := Marshal("/interface/bridge/set", r)
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
_, err = c.RunArgs(cmd)

return res.(*Bridge), nil
}

func (c Mikrotik) UpdateBridge(r *Bridge) (*Bridge, error) {
res, err := c.Update(r)
if err != nil {
return nil, err
}

return client.FindBridge(r.Name)
return res.(*Bridge), nil
}

func (client Mikrotik) DeleteBridge(id string) error {
c, err := client.getMikrotikClient()
func (c Mikrotik) FindBridge(name string) (*Bridge, error) {
res, err := c.Find(&Bridge{Name: name})
if err != nil {
return err
return nil, err
}
cmd := []string{"/interface/bridge/remove", "=numbers=" + id}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
_, err = c.RunArgs(cmd)

return err
return res.(*Bridge), nil
}

func (c Mikrotik) DeleteBridge(name string) error {
return c.Delete(&Bridge{Name: name})
}