refactor
This commit is contained in:
parent
49563af412
commit
555c553686
18 changed files with 481 additions and 742 deletions
69
dns/service/vultr/service.go
Normal file
69
dns/service/vultr/service.go
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
package vultr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net"
|
||||
|
||||
"dnsupdater/dns"
|
||||
|
||||
"github.com/vultr/govultr/v3"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
api govultr.DomainRecordService
|
||||
}
|
||||
|
||||
func New(token string) Service {
|
||||
ctx := context.Background()
|
||||
config := &oauth2.Config{}
|
||||
ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: token})
|
||||
client := govultr.NewClient(oauth2.NewClient(ctx, ts))
|
||||
|
||||
return Service{
|
||||
api: client.DomainRecord,
|
||||
}
|
||||
}
|
||||
|
||||
func Factory(args map[string]any) (any, error) {
|
||||
t, ok := args["token"]
|
||||
if !ok {
|
||||
return nil, errors.New("did not find token")
|
||||
}
|
||||
|
||||
token, ok := t.(string)
|
||||
if !ok {
|
||||
return nil, errors.New("token must be a string")
|
||||
}
|
||||
|
||||
return New(token), nil
|
||||
}
|
||||
|
||||
func (p Service) List(domain_name string) (dns.RecordList, error) {
|
||||
fetchedRecords, _, _, err := p.api.List(context.Background(), domain_name, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
records := dns.RecordList{}
|
||||
for _, record := range fetchedRecords {
|
||||
|
||||
if record.Type != "A" {
|
||||
continue
|
||||
}
|
||||
|
||||
records.Add(dns.Record{
|
||||
Id: record.ID,
|
||||
Name: record.Name,
|
||||
Ip: net.ParseIP(record.Data),
|
||||
})
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (p Service) Update(domain, recordID, ip string) error {
|
||||
return p.api.Update(context.Background(), domain, recordID, &govultr.DomainRecordReq{
|
||||
Data: ip,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue