Skip to content

Commit

Permalink
Add interface peer & test
Browse files Browse the repository at this point in the history
  • Loading branch information
DawoudMahmud committed May 31, 2023
1 parent bd098a2 commit ac2fa48
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 0 deletions.
96 changes: 96 additions & 0 deletions client/interface_peer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package client

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

type InterfacePeer struct {
Id string `mikrotik:".id"`
AllowedAddress string `mikrotik:"allowed-address"`
Comment string `mikrotik:"comment"`
Disabled bool `mikrotik:"disabled"`
EndpointAddress string `mikrotik:"endpoint-address"`
EndpointPort int `mikrotik:"endpoint-port"`
Interface string `mikrotik:"interface"`
PersistentKeepalive int `mikrotik:"persistent-keepalive"`
PresharedKey string `mikrotik:"preshared-key"`
PublicKey string `mikrotik:"public-key"`
CurrentEndpointAddress string `mikrotik:"current-endpoint-address,readonly"`
CurrentEndpointPort string `mikrotik:"current-endpoint-port,readonly"`
LastHandshake string `mikrotik:"last-handshake,readonly"`
Rx string `mikrotik:"rx,readonly"`
Tx string `mikrotik:"tx,readonly"`
}

func (i *InterfacePeer) ActionToCommand(action Action) string {
return map[Action]string{
Add: "/interface/wireguard/peers/add", //is this correct?
Find: "/interface/wireguard/peers/print",
List: "/interface/wireguard/peers/print",
Update: "/interface/wireguard/peers/set",
Delete: "/interface/wireguard/peers/remove",
}[action]
}

func (i *InterfacePeer) IDField() string {
return ".id"
}

func (i *InterfacePeer) ID() string {
return i.Id
}

func (i *InterfacePeer) SetID(id string) {
i.Id = id
}

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

func (i *InterfacePeer) FindField() string {
return ".id"
}

func (i *InterfacePeer) FindFieldValue() string {
return i.Id
}

func (i *InterfacePeer) DeleteField() string {
return "numbers"
}

func (i *InterfacePeer) DeleteFieldValue() string {
return i.Id
}

func (client Mikrotik) AddInterfacePeer(i *InterfacePeer) (*InterfacePeer, error) {
res, err := client.Add(i)
if err != nil {
return nil, err
}

return res.(*InterfacePeer), nil
}

func (client Mikrotik) FindInterfacePeer(id string) (*InterfacePeer, error) {
res, err := client.Find(&InterfacePeer{Id: id})
if err != nil {
return nil, err
}

return res.(*InterfacePeer), nil
}

func (client Mikrotik) UpdateInterfacePeer(i *InterfacePeer) (*InterfacePeer, error) {
res, err := client.Update(i)
if err != nil {
return nil, err
}

return res.(*InterfacePeer), nil
}

func (client Mikrotik) DeleteInterfacePeer(id string) error {
return client.Delete(&InterfacePeer{Id: id})
}
62 changes: 62 additions & 0 deletions client/interface_peer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package client

import (
"errors"
"reflect"
"testing"
)

func TestFindInterfacePeer_onNonExistantInterfacePeer(t *testing.T) {
SkipInterfaceWireguardIfUnsupported(t)
c := NewClient(GetConfigFromEnv())

id := "Interface peer does not exist"
_, err := c.FindInterfacePeer(id)

if _, ok := err.(*NotFound); !ok {
t.Errorf("Expecting to receive NotFound error for Interface peer `%s`, instead error was nil.", id)
}
}

func TestAddFindDeleteInterfacePeer(t *testing.T) {
SkipInterfaceWireguardIfUnsupported(t)
c := NewClient(GetConfigFromEnv())

id := "new_interface_peer"
interfacePeer := &InterfacePeer{
Id: id,
Disabled: false,
Comment: "new interface from test",
}

created, err := c.Add(interfacePeer)
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}
defer func() {
err = c.Delete(interfacePeer)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
expected := &NotFound{}
if _, err := c.Find(interfacePeer); err == nil || !errors.As(err, &expected) {
t.Error(err)
}
}()
findInterface := &InterfacePeer{}
findInterface.Id = id
found, err := c.Find(findInterface)
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}

if _, ok := found.(Resource); !ok {
t.Error("expected found resource to implement Resource interface, but it doesn't")
return
}
if !reflect.DeepEqual(created, found) {
t.Error("expected created and found resources to be equal, but they don't")
}
}

0 comments on commit ac2fa48

Please sign in to comment.