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 Interface Wireguard Support #153

Closed
Closed
Show file tree
Hide file tree
Changes from 7 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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
terraform-provider-mikrotik
dist/
vendor
vendor
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})
}
73 changes: 73 additions & 0 deletions client/interface_wireguard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
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
}

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")
}
defer func() {
err = c.Delete(found.(Resource))
if err != nil {
t.Errorf("expected no error, got %v", err)
}

_, err = c.Find(findInterface)
if err == nil {
t.Errorf("expected error, got nothing")
return
}

target := &NotFound{}
if !errors.As(err, &target) {
t.Errorf("expected error to be of type %T, got %T", &NotFound{}, err)
}
}()
}
40 changes: 40 additions & 0 deletions docs/resources/interface_wireguard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# mikrotik_interface_wireguard (Resource)
Creates a Mikrotik interface_wireguard.

!> This resource is supported for RouterOS v7+.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like these docs were edited by hand. They must be generated with the tfplugindocs tool. See my comment in the mikrotik/resource_interface_wireguard.go file on how to add the RouterOS v7 information to the autogenerated file.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the default resource templating isn't appropriate, you can create resource specific templates as well (like the bgp_peer resource).


## Example Usage
```terraform
resource "mikrotik_interface_wireguard" "interface_wireguard" {
name = "interface_wireguard-name"
}
```

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

### Required

- `name` (String) Name of the tunnel.

### Optional

- `comment` (String) Short description of the tunnel.
- `disabled` (Boolean) Enables/disables the tunnel. Default: `false`
- `listen_port` (Number) Port for WireGuard service to listen on for incoming sessions. Default: `13231`
- `mtu` (Number) Layer3 Maximum transmission unit. Default: `1420`
- `private_key` (String) A base64 private key. If not specified, it will be automatically generated upon interface creation.
-


### Read-Only

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

## Import
Import is supported using the following syntax:
```shell
terraform import mikrotik_interface_wireguard.interface_wireguard interface_wireguard-name
```
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.18
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.0.1
github.com/hashicorp/terraform-plugin-go v0.14.2
github.com/hashicorp/terraform-plugin-framework v1.2.0
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upgrading these dependencies are not needed for adding the new resources as far as I can tell, so they should be done in another PR (if needed). Please let me know if you have trouble preventing the go toolchain from making these changes automatically and exclude the go.sum and go.mod changes from this PR.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After reviewing this and running the tests locally, I realized this was necessary for the changes you made. Because our existing code has significant acceptance test coverage, upgrading this in place is fine. However, with most projects bumping the core sdk can be a large change that is usually best done prior to the dependent change (adding the resource functionality that needs it).

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 All @@ -30,17 +30,18 @@ require (
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
github.com/hashicorp/go-hclog v1.2.1 // indirect
github.com/hashicorp/go-hclog v1.4.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.6 // indirect
github.com/hashicorp/go-plugin v1.4.8 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hashicorp/hc-install v0.4.0 // indirect
github.com/hashicorp/hcl/v2 v2.13.0 // indirect
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.17.2 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
github.com/hashicorp/terraform-plugin-framework-validators v0.10.0
github.com/hashicorp/terraform-plugin-log v0.8.0 // indirect
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
Expand All @@ -61,13 +62,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
16 changes: 16 additions & 0 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