-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd098a2
commit ac2fa48
Showing
2 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |