forked from ddelnano/terraform-provider-mikrotik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
3944598
commit 9e63d78
Showing
7 changed files
with
178 additions
and
226 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 |
---|---|---|
@@ -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 | ||
} |
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
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 |
---|---|---|
@@ -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= |
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
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
Oops, something went wrong.