forked from StackExchange/dnscontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensrsProvider.go
144 lines (114 loc) · 3.48 KB
/
opensrsProvider.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package opensrs
import (
"encoding/json"
"errors"
"fmt"
"sort"
"strings"
"github.com/StackExchange/dnscontrol/models"
"github.com/StackExchange/dnscontrol/providers"
opensrs "github.com/philhug/opensrs-go/opensrs"
)
var docNotes = providers.DocumentationNotes{
providers.DocCreateDomains: providers.Cannot(),
providers.DocOfficiallySupported: providers.Cannot(),
providers.CanUseTLSA: providers.Cannot(),
}
func init() {
providers.RegisterRegistrarType("OPENSRS", newReg)
}
var defaultNameServerNames = []string{
"ns1.systemdns.com",
"ns2.systemdns.com",
"ns3.systemdns.com",
}
type OpenSRSApi struct {
UserName string // reseller user name
ApiKey string // API Key
BaseURL string // An alternate base URI
client *opensrs.Client // Client
}
func (c *OpenSRSApi) GetNameservers(domainName string) ([]*models.Nameserver, error) {
return models.StringsToNameservers(defaultNameServerNames), nil
}
func (c *OpenSRSApi) GetRegistrarCorrections(dc *models.DomainConfig) ([]*models.Correction, error) {
corrections := []*models.Correction{}
nameServers, err := c.getNameservers(dc.Name)
if err != nil {
return nil, err
}
sort.Strings(nameServers)
actual := strings.Join(nameServers, ",")
expectedSet := []string{}
for _, ns := range dc.Nameservers {
expectedSet = append(expectedSet, ns.Name)
}
sort.Strings(expectedSet)
expected := strings.Join(expectedSet, ",")
if actual != expected {
return []*models.Correction{
{
Msg: fmt.Sprintf("Update nameservers %s -> %s", actual, expected),
F: c.updateNameserversFunc(expectedSet, dc.Name),
},
}, nil
}
return corrections, nil
}
// OpenSRS calls
func (c *OpenSRSApi) getClient() *opensrs.Client {
return c.client
}
// Returns the name server names that should be used. If the domain is registered
// then this method will return the delegation name servers. If this domain
// is hosted only, then it will return the default OpenSRS name servers.
func (c *OpenSRSApi) getNameservers(domainName string) ([]string, error) {
client := c.getClient()
status, err := client.Domains.GetDomain(domainName, "status", 1)
if err != nil {
return nil, err
}
if status.Attributes.LockState == "0" {
dom, err := client.Domains.GetDomain(domainName, "nameservers", 1)
if err != nil {
return nil, err
}
return dom.Attributes.NameserverList.ToString(), nil
} else {
return nil, errors.New("Domain is locked")
}
}
// Returns a function that can be invoked to change the delegation of the domain to the given name server names.
func (c *OpenSRSApi) updateNameserversFunc(nameServerNames []string, domainName string) func() error {
return func() error {
client := c.getClient()
_, err := client.Domains.UpdateDomainNameservers(domainName, nameServerNames)
if err != nil {
return err
}
return nil
}
}
// constructors
func newReg(conf map[string]string) (providers.Registrar, error) {
return newProvider(conf, nil)
}
func newProvider(m map[string]string, metadata json.RawMessage) (*OpenSRSApi, error) {
api := &OpenSRSApi{}
api.ApiKey = m["apikey"]
if api.ApiKey == "" {
return nil, fmt.Errorf("OpenSRS apikey must be provided.")
}
api.UserName = m["username"]
if api.UserName == "" {
return nil, fmt.Errorf("OpenSRS username key must be provided.")
}
if m["baseurl"] != "" {
api.BaseURL = m["baseurl"]
}
api.client = opensrs.NewClient(opensrs.NewApiKeyMD5Credentials(api.UserName, api.ApiKey))
if api.BaseURL != "" {
api.client.BaseURL = api.BaseURL
}
return api, nil
}