Skip to content

Commit

Permalink
Merge pull request #11 from ddelnano/add-list-dhcp-leases-client-code
Browse files Browse the repository at this point in the history
Add list dhcp leases client code
  • Loading branch information
ddelnano authored May 1, 2020
2 parents e0cf616 + 17a7772 commit a217572
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
terraform-provider-mikrotik
dist/
vendor
66 changes: 65 additions & 1 deletion client/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package client
import (
"fmt"
"log"
"strconv"
"strings"

"github.com/go-routeros/routeros/proto"
Expand All @@ -13,6 +14,8 @@ type DhcpLease struct {
Address string
MacAddress string
Comment string
Hostname string
Dynamic bool
}

func (client Mikrotik) AddDhcpLease(address, macaddress, name string) (*DhcpLease, error) {
Expand All @@ -34,6 +37,57 @@ func (client Mikrotik) AddDhcpLease(address, macaddress, name string) (*DhcpLeas
return client.FindDhcpLease(id)
}

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

if err != nil {
return nil, err
}
cmd := []string{"/ip/dhcp-server/lease/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 dhcp leases: %v", r)

leases := []DhcpLease{}
for _, reply := range r.Re {
id := ""
address := ""
macaddress := ""
comment := ""
hostname := ""
for _, item := range reply.List {
if item.Key == ".id" {
id = item.Value
}
if item.Key == "address" {
address = item.Value
}
if item.Key == "mac-address" {
macaddress = item.Value
}
if item.Key == "comment" {
comment = item.Value
}
if item.Key == "host-name" {
hostname = item.Value
}
}
lease := DhcpLease{
Id: id,
Address: address,
MacAddress: macaddress,
Comment: comment,
Hostname: hostname,
}
leases = append(leases, lease)
}
return leases, nil
}

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

Expand Down Expand Up @@ -67,6 +121,8 @@ func (client Mikrotik) FindDhcpLease(id string) (*DhcpLease, error) {
address := ""
macaddress := ""
comment := ""
hostname := ""
dynamic := false
for _, pair := range sentence.List {
if pair.Key == "address" {
address = pair.Value
Expand All @@ -77,17 +133,25 @@ func (client Mikrotik) FindDhcpLease(id string) (*DhcpLease, error) {
if pair.Key == "comment" {
comment = pair.Value
}
if pair.Key == "host-name" {
hostname = pair.Value
}
if pair.Key == "dynamic" {
dynamic, _ = strconv.ParseBool(pair.Value)
}
}

return &DhcpLease{
Id: id,
MacAddress: macaddress,
Address: address,
Comment: comment,
Hostname: hostname,
Dynamic: dynamic,
}, nil
}

func (client Mikrotik) UpdateDhcpLease(id, address, macaddress, comment string) (*DhcpLease, error) {
func (client Mikrotik) UpdateDhcpLease(id, address, macaddress, comment string, dynamic bool) (*DhcpLease, error) {
c, err := client.getMikrotikClient()

if err != nil {
Expand Down
21 changes: 20 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@ module github.com/ddelnano/terraform-provider-mikrotik
go 1.12

require (
9fans.net/go v0.0.2 // indirect
cloud.google.com/go/storage v1.0.0 // indirect
github.com/alecthomas/gometalinter v3.0.0+incompatible // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/davidrjenni/reftools v0.0.0-20191222082827-65925cf01315 // indirect
github.com/fatih/gomodifytags v1.4.0 // indirect
github.com/fatih/motion v1.1.0 // indirect
github.com/fatih/structtag v1.2.0 // indirect
github.com/go-routeros/routeros v0.0.0-20190719172022-0819accf8221
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/hcl2 v0.0.0-20190909202536-66c59f909e25 // indirect
github.com/hashicorp/hil v0.0.0-20190212132231-97b3a9cdfa93 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.0.0
golang.org/x/tools v0.0.0-20190911225940-c7d52e45e2f2 // indirect
github.com/josharian/impl v0.0.0-20191119165012-6b9658ad00c7 // indirect
github.com/jstemmer/gotags v1.4.1 // indirect
github.com/kisielk/errcheck v1.2.0 // indirect
github.com/klauspost/asmfmt v1.2.1 // indirect
github.com/koron/iferr v0.0.0-20180615142939-bb332a3b1d91 // indirect
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/nsf/gocode v0.0.0-20190302080247-5bee97b48836 // indirect
github.com/pelletier/go-toml v1.7.0 // indirect
github.com/rogpeppe/godef v1.1.2 // indirect
github.com/zmb3/gogetdoc v0.0.0-20190228002656-b37376c5da6a // indirect
golang.org/x/tools v0.0.0-20200501155019-2658dc0cadb5 // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
)
Loading

0 comments on commit a217572

Please sign in to comment.