Skip to content

Commit

Permalink
review: 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 12, 2023
1 parent 439df49 commit 9a8d9b6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 77 deletions.
15 changes: 7 additions & 8 deletions providers/dns/nicru/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/xml"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
Expand All @@ -27,14 +28,12 @@ type OauthConfiguration struct {
Password string
}

func NewOauthClient(config *OauthConfiguration) (*http.Client, error) {
func NewOauthClient(ctx context.Context, config *OauthConfiguration) (*http.Client, error) {
err := validateAuthOptions(config)
if err != nil {
return nil, err
}

ctx := context.TODO()

oauth2Config := oauth2.Config{
ClientID: config.OAuth2ClientID,
ClientSecret: config.OAuth2SecretID,
Expand All @@ -47,7 +46,7 @@ func NewOauthClient(config *OauthConfiguration) (*http.Client, error) {

oauth2Token, err := oauth2Config.PasswordCredentialsToken(ctx, config.Username, config.Password)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to create oauth2 token: %w", err)
}

return oauth2Config.Client(ctx, oauth2Token), nil
Expand Down Expand Up @@ -123,7 +122,7 @@ func (c *Client) GetRecords(fqdn string) ([]*RR, error) {

var records []*RR
for _, zone := range apiResponse.Data.Zone {
records = append(records, zone.Rr...)
records = append(records, zone.RR...)
}

return records, nil
Expand All @@ -146,9 +145,9 @@ func (c *Client) GetTXTRecords(fqdn string) ([]*Txt, error) {
}

func (c *Client) AddTxtRecord(zoneName string, name string, content string, ttl int) (*Response, error) {
request := &Request{RrList: &RrList{Rr: []*RR{{
request := &Request{RRList: &RrList{RR: []*RR{{
Name: name,
Ttl: strconv.Itoa(ttl),
TTL: strconv.Itoa(ttl),
Type: "TXT",
Txt: &Txt{String: content},
}}}}
Expand Down Expand Up @@ -236,7 +235,7 @@ func (c *Client) add(zoneName string, request *Request) (*Response, error) {
}

if apiResponse.Status != successStatus {
return nil, errors.New(describeError(apiResponse.Errors.Error))
return nil, apiResponse.Errors.Error
}

return apiResponse, nil
Expand Down
88 changes: 41 additions & 47 deletions providers/dns/nicru/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,34 @@ import (
type Request struct {
XMLName xml.Name `xml:"request" json:"xml_name,omitempty"`
Text string `xml:",chardata" json:"text,omitempty"`
RrList *RrList `xml:"rr-list" json:"rr_list,omitempty"`
RRList *RrList `xml:"rr-list" json:"rr_list,omitempty"`
}

type RrList struct {
Text string `xml:",chardata" json:"text,omitempty"`
Rr []*RR `xml:"rr" json:"rr,omitempty"`
RR []*RR `xml:"rr" json:"rr,omitempty"`
}

type RR struct {
Text string `xml:",chardata" json:"text,omitempty"`
ID string `xml:"id,attr,omitempty" json:"id,omitempty"`
Name string `xml:"name" json:"name,omitempty"`
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
Ttl string `xml:"ttl" json:"ttl,omitempty"`
Type string `xml:"type" json:"type,omitempty"`
Soa *Soa `xml:"soa" json:"soa,omitempty"`
A *Address `xml:"a" json:"a,omitempty"`
AAAA *Address `xml:"aaaa" json:"aaaa,omitempty"`
Cname *Cname `xml:"cname" json:"cname,omitempty"`
Ns *Ns `xml:"ns" json:"ns,omitempty"`
Mx *Mx `xml:"mx" json:"mx,omitempty"`
Srv *Srv `xml:"srv" json:"srv,omitempty"`
Ptr *Ptr `xml:"ptr" json:"ptr,omitempty"`
Txt *Txt `xml:"txt" json:"txt,omitempty"`
Dname *Dname `xml:"dname" json:"dname,omitempty"`
Hinfo *Hinfo `xml:"hinfo" json:"hinfo,omitempty"`
Naptr *Naptr `xml:"naptr" json:"naptr,omitempty"`
Rp *Rp `xml:"rp" json:"rp,omitempty"`
}

type Address string

func (address *Address) String() string {
return string(*address)
Text string `xml:",chardata" json:"text,omitempty"`
ID string `xml:"id,attr,omitempty" json:"id,omitempty"`
Name string `xml:"name" json:"name,omitempty"`
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
TTL string `xml:"ttl" json:"ttl,omitempty"`
Type string `xml:"type" json:"type,omitempty"`
Soa *Soa `xml:"soa" json:"soa,omitempty"`
A *string `xml:"a" json:"a,omitempty"`
AAAA *string `xml:"aaaa" json:"aaaa,omitempty"`
CName *CName `xml:"cname" json:"cname,omitempty"`
Ns *Ns `xml:"ns" json:"ns,omitempty"`
Mx *Mx `xml:"mx" json:"mx,omitempty"`
Srv *Srv `xml:"srv" json:"srv,omitempty"`
Ptr *Ptr `xml:"ptr" json:"ptr,omitempty"`
Txt *Txt `xml:"txt" json:"txt,omitempty"`
DName *DName `xml:"dname" json:"dname,omitempty"`
HInfo *HInfo `xml:"hinfo" json:"hinfo,omitempty"`
Naptr *Naptr `xml:"naptr" json:"naptr,omitempty"`
RP *RP `xml:"rp" json:"rp,omitempty"`
}

type Service struct {
Expand All @@ -54,28 +48,28 @@ type Service struct {
Name string `xml:"name,attr" json:"name,omitempty"`
Payer string `xml:"payer,attr" json:"payer,omitempty"`
Tariff string `xml:"tariff,attr" json:"tariff,omitempty"`
RrLimit string `xml:"rr-limit,attr" json:"rr_limit,omitempty"`
RrNum string `xml:"rr-num,attr" json:"rr_num,omitempty"`
RRLimit string `xml:"rr-limit,attr" json:"rr_limit,omitempty"`
RRNum string `xml:"rr-num,attr" json:"rr_num,omitempty"`
}

type Soa struct {
Text string `xml:",chardata" json:"text,omitempty"`
Mname *Mname `xml:"mname" json:"mname,omitempty"`
Rname *Rname `xml:"rname" json:"rname,omitempty"`
MName *MName `xml:"mname" json:"mname,omitempty"`
RName *RName `xml:"rname" json:"rname,omitempty"`
Serial string `xml:"serial" json:"serial,omitempty"`
Refresh string `xml:"refresh" json:"refresh,omitempty"`
Retry string `xml:"retry" json:"retry,omitempty"`
Expire string `xml:"expire" json:"expire,omitempty"`
Minimum string `xml:"minimum" json:"minimum,omitempty"`
}

type Mname struct {
type MName struct {
Text string `xml:",chardata" json:"text,omitempty"`
Name string `xml:"name" json:"name,omitempty"`
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
}

type Rname struct {
type RName struct {
Text string `xml:",chardata" json:"text,omitempty"`
Name string `xml:"name" json:"name,omitempty"`
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
Expand Down Expand Up @@ -116,10 +110,10 @@ type Ptr struct {
Name string `xml:"name" json:"name,omitempty"`
}

type Hinfo struct {
type HInfo struct {
Text string `xml:",chardata" json:"text,omitempty"`
Hardware string `xml:"hardware" json:"hardware,omitempty"`
Os string `xml:"os" json:"os,omitempty"`
OS string `xml:"os" json:"os,omitempty"`
}

type Naptr struct {
Expand All @@ -137,29 +131,29 @@ type Replacement struct {
Name string `xml:"name" json:"name,omitempty"`
}

type Rp struct {
type RP struct {
Text string `xml:",chardata" json:"text,omitempty"`
MboxDname *MboxDname `xml:"mbox-dname" json:"mbox_dname,omitempty"`
TxtDname *TxtDname `xml:"txt-dname" json:"txt_dname,omitempty"`
MboxDName *MboxDName `xml:"mbox-dname" json:"mbox_dname,omitempty"`
TxtDName *TxtDName `xml:"txt-dname" json:"txt_dname,omitempty"`
}

type MboxDname struct {
type MboxDName struct {
Text string `xml:",chardata" json:"text,omitempty"`
Name string `xml:"name" json:"name,omitempty"`
}

type TxtDname struct {
type TxtDName struct {
Text string `xml:",chardata" json:"text,omitempty"`
Name string `xml:"name" json:"name,omitempty"`
}

type Cname struct {
type CName struct {
Text string `xml:",chardata" json:"text,omitempty"`
Name string `xml:"name" json:"name,omitempty"`
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
}

type Dname struct {
type DName struct {
Text string `xml:",chardata" json:"text,omitempty"`
Name string `xml:"name" json:"name,omitempty"`
}
Expand All @@ -180,13 +174,13 @@ type Zone struct {
Name string `xml:"name,attr" json:"name,omitempty"`
Payer string `xml:"payer,attr" json:"payer,omitempty"`
Service string `xml:"service,attr" json:"service,omitempty"`
Rr []*RR `xml:"rr" json:"rr,omitempty"`
RR []*RR `xml:"rr" json:"rr,omitempty"`
}

type Revision struct {
Text string `xml:",chardata" json:"text,omitempty"`
Date string `xml:"date,attr" json:"date,omitempty"`
Ip string `xml:"ip,attr" json:"ip,omitempty"`
IP string `xml:"ip,attr" json:"ip,omitempty"`
Number string `xml:"number,attr" json:"number,omitempty"`
}

Expand All @@ -195,8 +189,8 @@ type Error struct {
Code string `xml:"code,attr" json:"code,omitempty"`
}

func describeError(e Error) string {
return fmt.Sprintf(`%s (code %s)`, e.Text, e.Code)
func (e Error) Error() string {
return fmt.Sprintf("%s (code %s)", e.Text, e.Code)
}

type Response struct {
Expand All @@ -214,6 +208,6 @@ type Data struct {
Text string `xml:",chardata" json:"text,omitempty"`
Service []*Service `xml:"service" json:"service,omitempty"`
Zone []*Zone `xml:"zone" json:"zone,omitempty"`
Address []*Address `xml:"address" json:"address,omitempty"`
Address []*string `xml:"address" json:"address,omitempty"`
Revision []*Revision `xml:"revision" json:"revision,omitempty"`
}
14 changes: 8 additions & 6 deletions providers/dns/nicru/nicru.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package nicru

import (
"context"
"errors"
"fmt"
"time"
Expand All @@ -11,12 +12,13 @@ import (
"github.com/go-acme/lego/v4/providers/dns/nicru/internal"
)

// Environment variables names.
const (
envNamespace = "NIC_RU_"

EnvUsername = envNamespace + "USER"
EnvPassword = envNamespace + "PASSWORD"
EnvServiceId = envNamespace + "SERVICE_ID"
EnvServiceID = envNamespace + "SERVICE_ID"
EnvSecret = envNamespace + "SECRET"
EnvServiceName = envNamespace + "SERVICE_NAME"

Expand All @@ -34,7 +36,7 @@ type Config struct {
TTL int
Username string
Password string
ServiceId string
ServiceID string
Secret string
Domain string
ServiceName string
Expand All @@ -59,15 +61,15 @@ type DNSProvider struct {

// NewDNSProvider returns a DNSProvider instance configured for RU Center.
func NewDNSProvider() (*DNSProvider, error) {
values, err := env.Get(EnvUsername, EnvPassword, EnvServiceId, EnvSecret, EnvServiceName)
values, err := env.Get(EnvUsername, EnvPassword, EnvServiceID, EnvSecret, EnvServiceName)
if err != nil {
return nil, fmt.Errorf("nicru: %w", err)
}

config := NewDefaultConfig()
config.Username = values[EnvUsername]
config.Password = values[EnvPassword]
config.ServiceId = values[EnvServiceId]
config.ServiceID = values[EnvServiceID]
config.Secret = values[EnvSecret]
config.ServiceName = values[EnvServiceName]

Expand All @@ -81,13 +83,13 @@ func NewDNSProviderConfig(config *Config) (*DNSProvider, error) {
}

clientCfg := &internal.OauthConfiguration{
OAuth2ClientID: config.ServiceId,
OAuth2ClientID: config.ServiceID,
OAuth2SecretID: config.Secret,
Username: config.Username,
Password: config.Password,
}

oauthClient, err := internal.NewOauthClient(clientCfg)
oauthClient, err := internal.NewOauthClient(context.Background(), clientCfg)
if err != nil {
return nil, fmt.Errorf("nicru: %w", err)
}
Expand Down
Loading

0 comments on commit 9a8d9b6

Please sign in to comment.