Skip to content

Commit

Permalink
Refactor/pool (ddelnano#184)
Browse files Browse the repository at this point in the history
* refactor MikroTik Pool to use interface-based implementation

* refactor pool resource

* correctly handle unsetting of next_pool field

* run docs generator

* merge 2 IP pool creation tests into one

* code cleanup

* sync go dependency files

* revert go.* files
  • Loading branch information
maksym-nazarenko authored Sep 16, 2023
1 parent 3944598 commit 9e63d78
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 226 deletions.
168 changes: 48 additions & 120 deletions client/pool.go
Original file line number Diff line number Diff line change
@@ -1,152 +1,80 @@
package client

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

type Pool struct {
Id string `mikrotik:".id"`
Name string `mikrotik:"name"`
Ranges string `mikrotik:"ranges"`
NextPool string `mikrotik:"next-pool"`
Comment string `mikrotik:"comment"`
Id string `mikrotik:".id" codegen:"id,mikrotikID,terraformID"`
Name string `mikrotik:"name" codegen:"name,required"`
Ranges string `mikrotik:"ranges" codegen:"ranges,required"`
NextPool string `mikrotik:"next-pool" codegen:"next_pool,optiona,computed"`
Comment string `mikrotik:"comment" codegen:"comment,optional,computed"`
}

func (client Mikrotik) AddPool(p *Pool) (*Pool, error) {
c, err := client.getMikrotikClient()
var _ Resource = (*Pool)(nil)

if err != nil {
return nil, err
}
cmd := Marshal("/ip/pool/add", p)
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)

log.Printf("[DEBUG] Pool creation response: `%v`", r)

if err != nil {
return nil, err
}

id := r.Done.Map["ret"]

return client.FindPool(id)
func (b *Pool) ActionToCommand(a Action) string {
return map[Action]string{
Add: "/ip/pool/add",
Find: "/ip/pool/print",
Update: "/ip/pool/set",
Delete: "/ip/pool/remove",
}[a]
}

func (client Mikrotik) ListPools() ([]Pool, error) {
c, err := client.getMikrotikClient()

if err != nil {
return nil, err
}
cmd := []string{"/ip/pool/print"}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)

if err != nil {
return nil, err
}
log.Printf("[DEBUG] Found pools: %v", r)

pools := []Pool{}

err = Unmarshal(*r, &pools)

if err != nil {
return nil, err
}

return pools, nil
func (b *Pool) IDField() string {
return ".id"
}

func (client Mikrotik) FindPool(id string) (*Pool, error) {
c, err := client.getMikrotikClient()

if err != nil {
return nil, err
}
cmd := []string{"/ip/pool/print", "?.id=" + id}

log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)

log.Printf("[DEBUG] Pool response: %v", r)

if err != nil {
return nil, err
}

pool := Pool{}
err = Unmarshal(*r, &pool)

if err != nil {
return nil, err
}
if pool.Id == "" {
return nil, NewNotFound(fmt.Sprintf("pool `%s` not found", id))
}

return &pool, nil
func (b *Pool) ID() string {
return b.Id
}

func (client Mikrotik) FindPoolByName(name string) (*Pool, error) {
c, err := client.getMikrotikClient()

if err != nil {
return nil, err
}
cmd := []string{"/ip/pool/print", "?name=" + name}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
r, err := c.RunArgs(cmd)

log.Printf("[DEBUG] Pool response: %v", r)
func (b *Pool) SetID(id string) {
b.Id = id
}

if err != nil {
return nil, err
}
func (b *Pool) AfterAddHook(r *routeros.Reply) {
b.Id = r.Done.Map["ret"]
}

pool := Pool{}
err = Unmarshal(*r, &pool)
// Typed wrappers
func (c Mikrotik) AddPool(r *Pool) (*Pool, error) {
return r.processResourceErrorTuplePtr(c.Add(r))
}

if err != nil {
return nil, err
}
func (c Mikrotik) UpdatePool(r *Pool) (*Pool, error) {
return r.processResourceErrorTuplePtr(c.Update(r))
}

if pool.Name == "" {
return nil, NewNotFound(fmt.Sprintf("pool `%s` not found", name))
}
func (c Mikrotik) FindPool(id string) (*Pool, error) {
return Pool{}.processResourceErrorTuplePtr(c.Find(&Pool{Id: id}))
}

return &pool, nil
func (c Mikrotik) FindPoolByName(name string) (*Pool, error) {
return Pool{}.processResourceErrorTuplePtr(c.findByField(&Pool{}, "name", name))
}

func (client Mikrotik) UpdatePool(p *Pool) (*Pool, error) {
c, err := client.getMikrotikClient()
func (c Mikrotik) DeletePool(id string) error {
return c.Delete(&Pool{Id: id})
}

func (c Mikrotik) ListPools() ([]Pool, error) {
res, err := c.List(&Pool{})
if err != nil {
return nil, err
}

cmd := Marshal("/ip/pool/set", p)
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
_, err = c.RunArgs(cmd)

if err != nil {
return nil, err
returnSlice := make([]Pool, len(res))
for i, v := range res {
returnSlice[i] = *(v.(*Pool))
}

return client.FindPool(p.Id)
return returnSlice, nil
}

func (client Mikrotik) DeletePool(id string) error {
c, err := client.getMikrotikClient()

func (b Pool) processResourceErrorTuplePtr(r Resource, err error) (*Pool, error) {
if err != nil {
return err
return nil, err
}

cmd := []string{"/ip/pool/remove", "=.id=" + id}
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd)
_, err = c.RunArgs(cmd)
return err
return r.(*Pool), nil
}
2 changes: 1 addition & 1 deletion docs/resources/pool.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ resource "mikrotik_pool" "pool" {

### Read-Only

- `id` (String) The ID of this resource.
- `id` (String) ID of this resource.

## Import
Import is supported using the following syntax:
Expand Down
1 change: 1 addition & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
github.com/apparentlymart/go-textseg v1.0.0 h1:rRmlIsPEEhUTIKQb7T++Nz/A5Q6C9IuX2wFoYVvnCs0=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
1 change: 0 additions & 1 deletion mikrotik/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ func Provider(client *mt.Mikrotik) *schema.Provider {
"mikrotik_interface_list": resourceInterfaceList(),
"mikrotik_ip_address": resourceIpAddress(),
"mikrotik_firewall_filter_rule": resourceFirewallFilterRule(),
"mikrotik_pool": resourcePool(),
},
}

Expand Down
1 change: 1 addition & 0 deletions mikrotik/provider_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ func (p *ProviderFramework) Resources(ctx context.Context) []func() resource.Res
NewInterfaceWireguardPeerResource,
NewInterfaceWireguardResource,
NewIpv6AddressResource,
NewPoolResource,
NewSchedulerResource,
NewScriptResource,
NewVlanInterfaceResource,
Expand Down
Loading

0 comments on commit 9e63d78

Please sign in to comment.