-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from maksym-nazarenko/refactor/dns_record
Refactor/dns record
- Loading branch information
Showing
5 changed files
with
56 additions
and
191 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
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,87 +1,87 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"github.com/ddelnano/terraform-provider-mikrotik/client/types" | ||
"github.com/go-routeros/routeros" | ||
) | ||
|
||
type DnsRecord struct { | ||
Id string `mikrotik:".id"` | ||
Name string `mikrotik:"name"` | ||
Ttl int `mikrotik:"ttl,ttlToSeconds"` | ||
Address string `mikrotik:"address"` | ||
Comment string `mikrotik:"comment"` | ||
Id string `mikrotik:".id"` | ||
Name string `mikrotik:"name"` | ||
Ttl types.MikrotikDuration `mikrotik:"ttl"` | ||
Address string `mikrotik:"address"` | ||
Comment string `mikrotik:"comment"` | ||
} | ||
|
||
func (client Mikrotik) AddDnsRecord(d *DnsRecord) (*DnsRecord, error) { | ||
c, err := client.getMikrotikClient() | ||
func (d *DnsRecord) ActionToCommand(action Action) string { | ||
return map[Action]string{ | ||
Add: "/ip/dns/static/add", | ||
Find: "/ip/dns/static/print", | ||
List: "/ip/dns/static/print", | ||
Update: "/ip/dns/static/set", | ||
Delete: "/ip/dns/static/remove", | ||
}[action] | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
cmd := Marshal("/ip/dns/static/add", d) | ||
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd) | ||
r, err := c.RunArgs(cmd) | ||
log.Printf("[DEBUG] /ip/dns/static/add returned %v", r) | ||
func (d *DnsRecord) IDField() string { | ||
return ".id" | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
func (d *DnsRecord) ID() string { | ||
return d.Id | ||
} | ||
|
||
return client.FindDnsRecord(d.Name) | ||
func (d *DnsRecord) SetID(id string) { | ||
d.Id = id | ||
} | ||
|
||
func (client Mikrotik) FindDnsRecord(name string) (*DnsRecord, error) { | ||
c, err := client.getMikrotikClient() | ||
func (d *DnsRecord) AfterAddHook(r *routeros.Reply) { | ||
d.Id = r.Done.Map["ret"] | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
cmd := []string{"/ip/dns/static/print", "?name=" + name} | ||
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd) | ||
r, err := c.RunArgs(cmd) | ||
func (d *DnsRecord) FindField() string { | ||
return "name" | ||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
func (d *DnsRecord) FindFieldValue() string { | ||
return d.Name | ||
} | ||
|
||
log.Printf("[DEBUG] Found dns record: %v", r) | ||
func (d *DnsRecord) DeleteField() string { | ||
return "numbers" | ||
} | ||
|
||
record := DnsRecord{} | ||
err = Unmarshal(*r, &record) | ||
func (d *DnsRecord) DeleteFieldValue() string { | ||
return d.Id | ||
} | ||
|
||
func (client Mikrotik) AddDnsRecord(d *DnsRecord) (*DnsRecord, error) { | ||
res, err := client.Add(d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if record.Name == "" { | ||
return nil, NewNotFound(fmt.Sprintf("dns record `%s` not found", name)) | ||
return res.(*DnsRecord), nil | ||
} | ||
|
||
func (client Mikrotik) FindDnsRecord(name string) (*DnsRecord, error) { | ||
res, err := client.Find(&DnsRecord{Name: name}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &record, nil | ||
return res.(*DnsRecord), nil | ||
} | ||
|
||
func (client Mikrotik) UpdateDnsRecord(d *DnsRecord) (*DnsRecord, error) { | ||
c, err := client.getMikrotikClient() | ||
|
||
res, err := client.Update(d) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cmd := Marshal("/ip/dns/static/set", d) | ||
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd) | ||
_, err = c.RunArgs(cmd) | ||
|
||
return client.FindDnsRecord(d.Name) | ||
return res.(*DnsRecord), nil | ||
} | ||
|
||
func (client Mikrotik) DeleteDnsRecord(id string) error { | ||
c, err := client.getMikrotikClient() | ||
|
||
if err != nil { | ||
return err | ||
} | ||
cmd := []string{"/ip/dns/static/remove", "=numbers=" + id} | ||
log.Printf("[INFO] Running the mikrotik command: `%s`", cmd) | ||
_, err = c.RunArgs(cmd) | ||
return err | ||
return client.Delete(&DnsRecord{Id: id}) | ||
} |
This file was deleted.
Oops, something went wrong.
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