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

Add Wireguard Interface resource (continuation) #159

Merged
merged 9 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ jobs:
# Test against latest stable release, v6 beta and v7 beta
routeros: ["6.48.3", "6.49beta54"]
legacy_bgp_support: [true]
interface_wireguard_support: [false]
include:
- experimental: true
go: 1.18
os: ubuntu-latest
routeros: "latest"
legacy_bgp_support: false
interface_wireguard_support: true

steps:
- name: Set up Go
uses: actions/setup-go@v1
Expand Down Expand Up @@ -52,6 +55,7 @@ jobs:
MIKROTIK_PASSWORD: ''
TF_ACC: 1
LEGACY_BGP_SUPPORT: ${{ matrix.legacy_bgp_support }}
INTERFACE_WIREGUARD_SUPPORT: ${{ matrix.interface_wireguard_support }}

- name: Run client tests
run: make testclient
Expand All @@ -61,6 +65,7 @@ jobs:
MIKROTIK_PASSWORD: ''
TF_ACC: 1
LEGACY_BGP_SUPPORT: ${{ matrix.legacy_bgp_support }}
INTERFACE_WIREGUARD_SUPPORT: ${{ matrix.interface_wireguard_support }}

services:
routeros:
Expand Down
13 changes: 13 additions & 0 deletions client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ func IsLegacyBgpSupported() bool {
}
return false
}

func SkipInterfaceWireguardIfUnsupported(t *testing.T) {
if !IsInterfaceWireguardSupported() {
t.Skip()
}
}

func IsInterfaceWireguardSupported() bool {
if os.Getenv("INTERFACE_WIREGUARD_SUPPORT") == "true" {
return true
}
return false
}
90 changes: 90 additions & 0 deletions client/interface_wireguard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package client

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

type InterfaceWireguard struct {
Id string `mikrotik:".id"`
Name string `mikrotik:"name"`
Comment string `mikrotik:"comment"`
Disabled bool `mikrotik:"disabled"`
ListenPort int `mikrotik:"listen-port"`
Mtu int `mikrotik:"mtu"`
PrivateKey string `mikrotik:"private-key"`
PublicKey string `mikrotik:"public-key,readonly"` //read only property
Running bool `mikrotik:"running,readonly"` //read only property
}

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

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

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

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

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

func (i *InterfaceWireguard) FindField() string {
return "name"
}

func (i *InterfaceWireguard) FindFieldValue() string {
return i.Name
}

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

func (i *InterfaceWireguard) DeleteFieldValue() string {
return i.Name
}

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

return res.(*InterfaceWireguard), nil
}

func (client Mikrotik) FindInterfaceWireguard(name string) (*InterfaceWireguard, error) {
res, err := client.Find(&InterfaceWireguard{Name: name})
if err != nil {
return nil, err
}

return res.(*InterfaceWireguard), nil
}

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

return res.(*InterfaceWireguard), nil
}

func (client Mikrotik) DeleteInterfaceWireguard(name string) error {
return client.Delete(&InterfaceWireguard{Name: name})
}
65 changes: 65 additions & 0 deletions client/interface_wireguard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package client

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

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

name := "Interface wireguard does not exist"
_, err := c.FindInterfaceWireguard(name)

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

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

name := "new_interface_wireguard"
interfaceWireguard := &InterfaceWireguard{
Name: name,
Disabled: false,
ListenPort: 10000,
Mtu: 10001,
PrivateKey: "YOi0P0lTTiN8hAQvuRET23Srb+U7C52iOZokj0CCSkM=",
Comment: "new interface from test",
}

created, err := c.Add(interfaceWireguard)
if err != nil {
t.Errorf("expected no error, got %v", err)
return
}
defer func() {
err = c.Delete(interfaceWireguard)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
expected := &NotFound{}
if _, err := c.Find(interfaceWireguard); err == nil || !errors.As(err, &expected) {
t.Error(err)
}
}()
findInterface := &InterfaceWireguard{}
findInterface.Name = name
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")
}
}
27 changes: 27 additions & 0 deletions docs/resources/interface_wireguard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# mikrotik_interface_wireguard (Resource)
Creates a Mikrotik interface wireguard only supported by RouterOS v7+.



<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the interface wireguard.

### Optional

- `comment` (String) Comment associated with interface wireguard.
- `disabled` (Boolean) Boolean for whether or not the interface wireguard is disabled.
- `listen_port` (Number) Port for WireGuard service to listen on for incoming sessions.
- `mtu` (Number) Layer3 Maximum transmission unit.
- `private_key` (String, Sensitive) A base64 private key. If not specified, it will be automatically generated upon interface creation.

### Read-Only

- `id` (String) Identifier of this resource assigned by RouterOS
- `public_key` (String) A base64 public key is calculated from the private key.
- `running` (Boolean) Whether the interface is running.


5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/ddelnano/terraform-provider-mikrotik/client v0.0.0-00010101000000-000000000000
github.com/hashicorp/terraform-plugin-docs v0.13.0
github.com/hashicorp/terraform-plugin-framework v1.2.0
github.com/hashicorp/terraform-plugin-framework-validators v0.10.0
github.com/hashicorp/terraform-plugin-go v0.14.3
github.com/hashicorp/terraform-plugin-mux v0.8.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.20.0
Expand Down Expand Up @@ -63,13 +64,13 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/vmihailenco/tagparser v0.1.2 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20200711021454-869866162049 // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
Expand Down
9 changes: 6 additions & 3 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
1 change: 1 addition & 0 deletions mikrotik/provider_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func (p *ProviderFramework) DataSources(ctx context.Context) []func() datasource
func (p *ProviderFramework) Resources(ctx context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewSchedulerResource,
NewInterfaceWireguardResource,
}
}

Expand Down
Loading