Skip to content

Commit 9a8d9b6

Browse files
committed
review: 3
1 parent 439df49 commit 9a8d9b6

File tree

4 files changed

+74
-77
lines changed

4 files changed

+74
-77
lines changed

providers/dns/nicru/internal/client.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/xml"
77
"errors"
8+
"fmt"
89
"net/http"
910
"net/url"
1011
"strconv"
@@ -27,14 +28,12 @@ type OauthConfiguration struct {
2728
Password string
2829
}
2930

30-
func NewOauthClient(config *OauthConfiguration) (*http.Client, error) {
31+
func NewOauthClient(ctx context.Context, config *OauthConfiguration) (*http.Client, error) {
3132
err := validateAuthOptions(config)
3233
if err != nil {
3334
return nil, err
3435
}
3536

36-
ctx := context.TODO()
37-
3837
oauth2Config := oauth2.Config{
3938
ClientID: config.OAuth2ClientID,
4039
ClientSecret: config.OAuth2SecretID,
@@ -47,7 +46,7 @@ func NewOauthClient(config *OauthConfiguration) (*http.Client, error) {
4746

4847
oauth2Token, err := oauth2Config.PasswordCredentialsToken(ctx, config.Username, config.Password)
4948
if err != nil {
50-
return nil, err
49+
return nil, fmt.Errorf("failed to create oauth2 token: %w", err)
5150
}
5251

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

124123
var records []*RR
125124
for _, zone := range apiResponse.Data.Zone {
126-
records = append(records, zone.Rr...)
125+
records = append(records, zone.RR...)
127126
}
128127

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

148147
func (c *Client) AddTxtRecord(zoneName string, name string, content string, ttl int) (*Response, error) {
149-
request := &Request{RrList: &RrList{Rr: []*RR{{
148+
request := &Request{RRList: &RrList{RR: []*RR{{
150149
Name: name,
151-
Ttl: strconv.Itoa(ttl),
150+
TTL: strconv.Itoa(ttl),
152151
Type: "TXT",
153152
Txt: &Txt{String: content},
154153
}}}}
@@ -236,7 +235,7 @@ func (c *Client) add(zoneName string, request *Request) (*Response, error) {
236235
}
237236

238237
if apiResponse.Status != successStatus {
239-
return nil, errors.New(describeError(apiResponse.Errors.Error))
238+
return nil, apiResponse.Errors.Error
240239
}
241240

242241
return apiResponse, nil

providers/dns/nicru/internal/types.go

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,34 @@ import (
88
type Request struct {
99
XMLName xml.Name `xml:"request" json:"xml_name,omitempty"`
1010
Text string `xml:",chardata" json:"text,omitempty"`
11-
RrList *RrList `xml:"rr-list" json:"rr_list,omitempty"`
11+
RRList *RrList `xml:"rr-list" json:"rr_list,omitempty"`
1212
}
1313

1414
type RrList struct {
1515
Text string `xml:",chardata" json:"text,omitempty"`
16-
Rr []*RR `xml:"rr" json:"rr,omitempty"`
16+
RR []*RR `xml:"rr" json:"rr,omitempty"`
1717
}
1818

1919
type RR struct {
20-
Text string `xml:",chardata" json:"text,omitempty"`
21-
ID string `xml:"id,attr,omitempty" json:"id,omitempty"`
22-
Name string `xml:"name" json:"name,omitempty"`
23-
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
24-
Ttl string `xml:"ttl" json:"ttl,omitempty"`
25-
Type string `xml:"type" json:"type,omitempty"`
26-
Soa *Soa `xml:"soa" json:"soa,omitempty"`
27-
A *Address `xml:"a" json:"a,omitempty"`
28-
AAAA *Address `xml:"aaaa" json:"aaaa,omitempty"`
29-
Cname *Cname `xml:"cname" json:"cname,omitempty"`
30-
Ns *Ns `xml:"ns" json:"ns,omitempty"`
31-
Mx *Mx `xml:"mx" json:"mx,omitempty"`
32-
Srv *Srv `xml:"srv" json:"srv,omitempty"`
33-
Ptr *Ptr `xml:"ptr" json:"ptr,omitempty"`
34-
Txt *Txt `xml:"txt" json:"txt,omitempty"`
35-
Dname *Dname `xml:"dname" json:"dname,omitempty"`
36-
Hinfo *Hinfo `xml:"hinfo" json:"hinfo,omitempty"`
37-
Naptr *Naptr `xml:"naptr" json:"naptr,omitempty"`
38-
Rp *Rp `xml:"rp" json:"rp,omitempty"`
39-
}
40-
41-
type Address string
42-
43-
func (address *Address) String() string {
44-
return string(*address)
20+
Text string `xml:",chardata" json:"text,omitempty"`
21+
ID string `xml:"id,attr,omitempty" json:"id,omitempty"`
22+
Name string `xml:"name" json:"name,omitempty"`
23+
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
24+
TTL string `xml:"ttl" json:"ttl,omitempty"`
25+
Type string `xml:"type" json:"type,omitempty"`
26+
Soa *Soa `xml:"soa" json:"soa,omitempty"`
27+
A *string `xml:"a" json:"a,omitempty"`
28+
AAAA *string `xml:"aaaa" json:"aaaa,omitempty"`
29+
CName *CName `xml:"cname" json:"cname,omitempty"`
30+
Ns *Ns `xml:"ns" json:"ns,omitempty"`
31+
Mx *Mx `xml:"mx" json:"mx,omitempty"`
32+
Srv *Srv `xml:"srv" json:"srv,omitempty"`
33+
Ptr *Ptr `xml:"ptr" json:"ptr,omitempty"`
34+
Txt *Txt `xml:"txt" json:"txt,omitempty"`
35+
DName *DName `xml:"dname" json:"dname,omitempty"`
36+
HInfo *HInfo `xml:"hinfo" json:"hinfo,omitempty"`
37+
Naptr *Naptr `xml:"naptr" json:"naptr,omitempty"`
38+
RP *RP `xml:"rp" json:"rp,omitempty"`
4539
}
4640

4741
type Service struct {
@@ -54,28 +48,28 @@ type Service struct {
5448
Name string `xml:"name,attr" json:"name,omitempty"`
5549
Payer string `xml:"payer,attr" json:"payer,omitempty"`
5650
Tariff string `xml:"tariff,attr" json:"tariff,omitempty"`
57-
RrLimit string `xml:"rr-limit,attr" json:"rr_limit,omitempty"`
58-
RrNum string `xml:"rr-num,attr" json:"rr_num,omitempty"`
51+
RRLimit string `xml:"rr-limit,attr" json:"rr_limit,omitempty"`
52+
RRNum string `xml:"rr-num,attr" json:"rr_num,omitempty"`
5953
}
6054

6155
type Soa struct {
6256
Text string `xml:",chardata" json:"text,omitempty"`
63-
Mname *Mname `xml:"mname" json:"mname,omitempty"`
64-
Rname *Rname `xml:"rname" json:"rname,omitempty"`
57+
MName *MName `xml:"mname" json:"mname,omitempty"`
58+
RName *RName `xml:"rname" json:"rname,omitempty"`
6559
Serial string `xml:"serial" json:"serial,omitempty"`
6660
Refresh string `xml:"refresh" json:"refresh,omitempty"`
6761
Retry string `xml:"retry" json:"retry,omitempty"`
6862
Expire string `xml:"expire" json:"expire,omitempty"`
6963
Minimum string `xml:"minimum" json:"minimum,omitempty"`
7064
}
7165

72-
type Mname struct {
66+
type MName struct {
7367
Text string `xml:",chardata" json:"text,omitempty"`
7468
Name string `xml:"name" json:"name,omitempty"`
7569
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
7670
}
7771

78-
type Rname struct {
72+
type RName struct {
7973
Text string `xml:",chardata" json:"text,omitempty"`
8074
Name string `xml:"name" json:"name,omitempty"`
8175
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
@@ -116,10 +110,10 @@ type Ptr struct {
116110
Name string `xml:"name" json:"name,omitempty"`
117111
}
118112

119-
type Hinfo struct {
113+
type HInfo struct {
120114
Text string `xml:",chardata" json:"text,omitempty"`
121115
Hardware string `xml:"hardware" json:"hardware,omitempty"`
122-
Os string `xml:"os" json:"os,omitempty"`
116+
OS string `xml:"os" json:"os,omitempty"`
123117
}
124118

125119
type Naptr struct {
@@ -137,29 +131,29 @@ type Replacement struct {
137131
Name string `xml:"name" json:"name,omitempty"`
138132
}
139133

140-
type Rp struct {
134+
type RP struct {
141135
Text string `xml:",chardata" json:"text,omitempty"`
142-
MboxDname *MboxDname `xml:"mbox-dname" json:"mbox_dname,omitempty"`
143-
TxtDname *TxtDname `xml:"txt-dname" json:"txt_dname,omitempty"`
136+
MboxDName *MboxDName `xml:"mbox-dname" json:"mbox_dname,omitempty"`
137+
TxtDName *TxtDName `xml:"txt-dname" json:"txt_dname,omitempty"`
144138
}
145139

146-
type MboxDname struct {
140+
type MboxDName struct {
147141
Text string `xml:",chardata" json:"text,omitempty"`
148142
Name string `xml:"name" json:"name,omitempty"`
149143
}
150144

151-
type TxtDname struct {
145+
type TxtDName struct {
152146
Text string `xml:",chardata" json:"text,omitempty"`
153147
Name string `xml:"name" json:"name,omitempty"`
154148
}
155149

156-
type Cname struct {
150+
type CName struct {
157151
Text string `xml:",chardata" json:"text,omitempty"`
158152
Name string `xml:"name" json:"name,omitempty"`
159153
IdnName string `xml:"idn-name,omitempty" json:"idn_name,omitempty"`
160154
}
161155

162-
type Dname struct {
156+
type DName struct {
163157
Text string `xml:",chardata" json:"text,omitempty"`
164158
Name string `xml:"name" json:"name,omitempty"`
165159
}
@@ -180,13 +174,13 @@ type Zone struct {
180174
Name string `xml:"name,attr" json:"name,omitempty"`
181175
Payer string `xml:"payer,attr" json:"payer,omitempty"`
182176
Service string `xml:"service,attr" json:"service,omitempty"`
183-
Rr []*RR `xml:"rr" json:"rr,omitempty"`
177+
RR []*RR `xml:"rr" json:"rr,omitempty"`
184178
}
185179

186180
type Revision struct {
187181
Text string `xml:",chardata" json:"text,omitempty"`
188182
Date string `xml:"date,attr" json:"date,omitempty"`
189-
Ip string `xml:"ip,attr" json:"ip,omitempty"`
183+
IP string `xml:"ip,attr" json:"ip,omitempty"`
190184
Number string `xml:"number,attr" json:"number,omitempty"`
191185
}
192186

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

198-
func describeError(e Error) string {
199-
return fmt.Sprintf(`%s (code %s)`, e.Text, e.Code)
192+
func (e Error) Error() string {
193+
return fmt.Sprintf("%s (code %s)", e.Text, e.Code)
200194
}
201195

202196
type Response struct {
@@ -214,6 +208,6 @@ type Data struct {
214208
Text string `xml:",chardata" json:"text,omitempty"`
215209
Service []*Service `xml:"service" json:"service,omitempty"`
216210
Zone []*Zone `xml:"zone" json:"zone,omitempty"`
217-
Address []*Address `xml:"address" json:"address,omitempty"`
211+
Address []*string `xml:"address" json:"address,omitempty"`
218212
Revision []*Revision `xml:"revision" json:"revision,omitempty"`
219213
}

providers/dns/nicru/nicru.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
package nicru
33

44
import (
5+
"context"
56
"errors"
67
"fmt"
78
"time"
@@ -11,12 +12,13 @@ import (
1112
"github.com/go-acme/lego/v4/providers/dns/nicru/internal"
1213
)
1314

15+
// Environment variables names.
1416
const (
1517
envNamespace = "NIC_RU_"
1618

1719
EnvUsername = envNamespace + "USER"
1820
EnvPassword = envNamespace + "PASSWORD"
19-
EnvServiceId = envNamespace + "SERVICE_ID"
21+
EnvServiceID = envNamespace + "SERVICE_ID"
2022
EnvSecret = envNamespace + "SECRET"
2123
EnvServiceName = envNamespace + "SERVICE_NAME"
2224

@@ -34,7 +36,7 @@ type Config struct {
3436
TTL int
3537
Username string
3638
Password string
37-
ServiceId string
39+
ServiceID string
3840
Secret string
3941
Domain string
4042
ServiceName string
@@ -59,15 +61,15 @@ type DNSProvider struct {
5961

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

6769
config := NewDefaultConfig()
6870
config.Username = values[EnvUsername]
6971
config.Password = values[EnvPassword]
70-
config.ServiceId = values[EnvServiceId]
72+
config.ServiceID = values[EnvServiceID]
7173
config.Secret = values[EnvSecret]
7274
config.ServiceName = values[EnvServiceName]
7375

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

8385
clientCfg := &internal.OauthConfiguration{
84-
OAuth2ClientID: config.ServiceId,
86+
OAuth2ClientID: config.ServiceID,
8587
OAuth2SecretID: config.Secret,
8688
Username: config.Username,
8789
Password: config.Password,
8890
}
8991

90-
oauthClient, err := internal.NewOauthClient(clientCfg)
92+
oauthClient, err := internal.NewOauthClient(context.Background(), clientCfg)
9193
if err != nil {
9294
return nil, fmt.Errorf("nicru: %w", err)
9395
}

0 commit comments

Comments
 (0)