71 lines
1.3 KiB
Go
71 lines
1.3 KiB
Go
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 {
|
|
req := &govultr.DomainRecordReq{
|
|
Data: ip,
|
|
}
|
|
|
|
return p.api.Update(context.Background(), domain, recordID, req)
|
|
}
|