-
Notifications
You must be signed in to change notification settings - Fork 29
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
Changes from 7 commits
90090ff
e931905
62d7d6d
dc12c8f
30d259c
70fdd97
6e3f449
1c8793c
bc5aef0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
terraform-provider-mikrotik | ||
dist/ | ||
vendor | ||
vendor |
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}) | ||
} |
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) | ||
} | ||
}() | ||
} |
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+. | ||
|
||
## 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 | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
) | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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= |
There was a problem hiding this comment.
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 themikrotik/resource_interface_wireguard.go
file on how to add the RouterOS v7 information to the autogenerated file.There was a problem hiding this comment.
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).