From 53b72f39a8d85d3ec38dac940f2b541d000f9b96 Mon Sep 17 00:00:00 2001 From: Philipp Hug Date: Mon, 27 Nov 2017 19:04:24 +0100 Subject: [PATCH] GANDI: Registrar support (#274) * GANDI: add registrar support for changing nameservers. #87 * Update docs with Gandi registrar support #87 * Updated dependencies for Gandi Registrar Support #87 --- docs/_includes/matrix.html | 4 +- providers/gandi/gandiProvider.go | 40 ++++++++++++++++++- providers/gandi/protocol.go | 9 +++++ .../prasmussen/gandi-api/domain/domain.go | 3 +- .../domain/nameservers/nameservers.go | 24 +++++++++++ .../gandi-api/domain/zone/record/record.go | 2 +- vendor/vendor.json | 38 ++++++++++-------- 7 files changed, 98 insertions(+), 22 deletions(-) create mode 100644 vendor/github.com/prasmussen/gandi-api/domain/nameservers/nameservers.go diff --git a/docs/_includes/matrix.html b/docs/_includes/matrix.html index 40df3fb03..5f1c617ff 100644 --- a/docs/_includes/matrix.html +++ b/docs/_includes/matrix.html @@ -89,8 +89,8 @@ - - + + diff --git a/providers/gandi/gandiProvider.go b/providers/gandi/gandiProvider.go index f8f6b7d81..131765f5c 100644 --- a/providers/gandi/gandiProvider.go +++ b/providers/gandi/gandiProvider.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "log" + "sort" "github.com/StackExchange/dnscontrol/models" "github.com/StackExchange/dnscontrol/providers" @@ -31,8 +32,9 @@ var docNotes = providers.DocumentationNotes{ } func init() { - providers.RegisterDomainServiceProviderType("GANDI", newGandi, providers.CanUsePTR, + providers.RegisterDomainServiceProviderType("GANDI", newDsp, providers.CanUsePTR, providers.CanUseSRV, docNotes, providers.CantUseNOPURGE) + providers.RegisterRegistrarType("GANDI", newReg) } type GandiApi struct { @@ -150,7 +152,15 @@ func (c *GandiApi) GetDomainCorrections(dc *models.DomainConfig) ([]*models.Corr return corrections, nil } -func newGandi(m map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) { +func newDsp(conf map[string]string, metadata json.RawMessage) (providers.DNSServiceProvider, error) { + return newGandi(conf, metadata) +} + +func newReg(conf map[string]string) (providers.Registrar, error) { + return newGandi(conf, nil) +} + +func newGandi(m map[string]string, metadata json.RawMessage) (*GandiApi, error) { api := &GandiApi{} api.ApiKey = m["apikey"] if api.ApiKey == "" { @@ -159,3 +169,29 @@ func newGandi(m map[string]string, metadata json.RawMessage) (providers.DNSServi return api, nil } + +func (c *GandiApi) GetRegistrarCorrections(dc *models.DomainConfig) ([]*models.Correction, error) { + domaininfo, err := c.getDomainInfo(dc.Name) + if err != nil { + return nil, err + } + sort.Strings(domaininfo.Nameservers) + found := strings.Join(domaininfo.Nameservers, ",") + desiredNs := []string{} + for _, d := range dc.Nameservers { + desiredNs = append(desiredNs, d.Name) + } + sort.Strings(desiredNs) + desired := strings.Join(desiredNs, ",") + if found != desired { + return []*models.Correction{ + { + Msg: fmt.Sprintf("Change Nameservers from '%s' to '%s'", found, desired), + F: func() (err error) { + _, err = c.setDomainNameservers(dc.Name, desiredNs) + return + }}, + }, nil + } + return nil, nil +} diff --git a/providers/gandi/protocol.go b/providers/gandi/protocol.go index f7a886271..28581a05f 100644 --- a/providers/gandi/protocol.go +++ b/providers/gandi/protocol.go @@ -5,9 +5,11 @@ import ( gandiclient "github.com/prasmussen/gandi-api/client" gandidomain "github.com/prasmussen/gandi-api/domain" + gandinameservers "github.com/prasmussen/gandi-api/domain/nameservers" gandizone "github.com/prasmussen/gandi-api/domain/zone" gandirecord "github.com/prasmussen/gandi-api/domain/zone/record" gandiversion "github.com/prasmussen/gandi-api/domain/zone/version" + gandioperation "github.com/prasmussen/gandi-api/operation" "github.com/StackExchange/dnscontrol/models" "github.com/miekg/dns/dnsutil" @@ -39,6 +41,13 @@ func (c *GandiApi) fetchDomainInfo(fqdn string) (*gandidomain.DomainInfo, error) return domain.Info(fqdn) } +// setDomainNameservers updates the nameservers of a domain. +func (c *GandiApi) setDomainNameservers(fqdn string, nameservers []string) (*gandioperation.OperationInfo, error) { + gc := gandiclient.New(c.ApiKey, gandiclient.Production) + nameserversapi := gandinameservers.New(gc) + return nameserversapi.Set(fqdn, nameservers) +} + // getRecordsForDomain returns a list of records for a zone. func (c *GandiApi) getZoneRecords(zoneid int64, origin string) ([]*models.RecordConfig, error) { gc := gandiclient.New(c.ApiKey, gandiclient.Production) diff --git a/vendor/github.com/prasmussen/gandi-api/domain/domain.go b/vendor/github.com/prasmussen/gandi-api/domain/domain.go index 7c3edfa96..83b955ec6 100644 --- a/vendor/github.com/prasmussen/gandi-api/domain/domain.go +++ b/vendor/github.com/prasmussen/gandi-api/domain/domain.go @@ -16,7 +16,8 @@ func New(c *client.Client) *Domain { // Check the availability of some domain func (self *Domain) Available(name string) (string, error) { var result map[string]interface{} - params := []interface{}{self.Key, name} + domain := []string{name} + params := []interface{}{self.Key, domain} if err := self.Call("domain.available", params, &result); err != nil { return "", err } diff --git a/vendor/github.com/prasmussen/gandi-api/domain/nameservers/nameservers.go b/vendor/github.com/prasmussen/gandi-api/domain/nameservers/nameservers.go new file mode 100644 index 000000000..7dd4a5224 --- /dev/null +++ b/vendor/github.com/prasmussen/gandi-api/domain/nameservers/nameservers.go @@ -0,0 +1,24 @@ +package zone + +import ( + "github.com/prasmussen/gandi-api/client" + "github.com/prasmussen/gandi-api/operation" +) + +type Nameservers struct { + *client.Client +} + +func New(c *client.Client) *Nameservers { + return &Nameservers{c} +} + +// Set the current zone of a domain +func (self *Nameservers) Set(domainName string, nameservers []string) (*operation.OperationInfo, error) { + var res map[string]interface{} + params := []interface{}{self.Key, domainName, nameservers} + if err := self.Call("domain.nameservers.set", params, &res); err != nil { + return nil, err + } + return operation.ToOperationInfo(res), nil +} diff --git a/vendor/github.com/prasmussen/gandi-api/domain/zone/record/record.go b/vendor/github.com/prasmussen/gandi-api/domain/zone/record/record.go index 880ba6e0e..b06f7b500 100644 --- a/vendor/github.com/prasmussen/gandi-api/domain/zone/record/record.go +++ b/vendor/github.com/prasmussen/gandi-api/domain/zone/record/record.go @@ -65,7 +65,7 @@ func (self *Record) Add(args RecordAdd) (*RecordInfo, error) { } // Remove a record from a zone/version -func (self *Record) Delete(zoneId, version, recordId int64) (bool, error) { +func (self *Record) Delete(zoneId, version int64, recordId string) (bool, error) { var res int64 deleteArgs := map[string]interface{}{"id": recordId} params := []interface{}{self.Key, zoneId, version, deleteArgs} diff --git a/vendor/vendor.json b/vendor/vendor.json index 1fad977df..e15f78741 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -335,44 +335,50 @@ { "checksumSHA1": "nS4kKHjMlJpQg3sixqelpCg1jyk=", "path": "github.com/prasmussen/gandi-api/client", - "revision": "931915c3a2ca2661373f0b25bbedfee9e1958d2b", - "revisionTime": "2017-09-08T20:15:21Z" + "revision": "a23dc46a847ad0f56c9d66cd84ffeeb002f2c6d3", + "revisionTime": "2017-11-24T17:13:09Z" }, { - "checksumSHA1": "tT8xSQc+QM3/JPs16WjDtTFY7j0=", + "checksumSHA1": "US0LGupyHvXgr2iDH7kw4f7KOcc=", "path": "github.com/prasmussen/gandi-api/domain", - "revision": "931915c3a2ca2661373f0b25bbedfee9e1958d2b", - "revisionTime": "2017-09-08T20:15:21Z" + "revision": "a23dc46a847ad0f56c9d66cd84ffeeb002f2c6d3", + "revisionTime": "2017-11-24T17:13:09Z" + }, + { + "checksumSHA1": "QCTGEMnU3WqIX51TqbRvAuwu4Wo=", + "path": "github.com/prasmussen/gandi-api/domain/nameservers", + "revision": "a23dc46a847ad0f56c9d66cd84ffeeb002f2c6d3", + "revisionTime": "2017-11-24T17:13:09Z" }, { "checksumSHA1": "/t4nBKtJF6W3PHzTBNAzgcw54GU=", "path": "github.com/prasmussen/gandi-api/domain/zone", - "revision": "931915c3a2ca2661373f0b25bbedfee9e1958d2b", - "revisionTime": "2017-09-08T20:15:21Z" + "revision": "a23dc46a847ad0f56c9d66cd84ffeeb002f2c6d3", + "revisionTime": "2017-11-24T17:13:09Z" }, { - "checksumSHA1": "wers/RRtpv1SXRpnUg6ptC1nKoM=", + "checksumSHA1": "wmLJwLc3SlIGhio+vWofJq4XSTU=", "path": "github.com/prasmussen/gandi-api/domain/zone/record", - "revision": "931915c3a2ca2661373f0b25bbedfee9e1958d2b", - "revisionTime": "2017-09-08T20:15:21Z" + "revision": "a23dc46a847ad0f56c9d66cd84ffeeb002f2c6d3", + "revisionTime": "2017-11-24T17:13:09Z" }, { "checksumSHA1": "avzpVkEeXzDiaNLsl4agXWm9tm0=", "path": "github.com/prasmussen/gandi-api/domain/zone/version", - "revision": "931915c3a2ca2661373f0b25bbedfee9e1958d2b", - "revisionTime": "2017-09-08T20:15:21Z" + "revision": "a23dc46a847ad0f56c9d66cd84ffeeb002f2c6d3", + "revisionTime": "2017-11-24T17:13:09Z" }, { "checksumSHA1": "txVNPkzE0Jkag20VZ1hLj/Td+O4=", "path": "github.com/prasmussen/gandi-api/operation", - "revision": "931915c3a2ca2661373f0b25bbedfee9e1958d2b", - "revisionTime": "2017-09-08T20:15:21Z" + "revision": "a23dc46a847ad0f56c9d66cd84ffeeb002f2c6d3", + "revisionTime": "2017-11-24T17:13:09Z" }, { "checksumSHA1": "FwIh1Rk/XNANaxCz3Fw/vQ3Hu5E=", "path": "github.com/prasmussen/gandi-api/util", - "revision": "931915c3a2ca2661373f0b25bbedfee9e1958d2b", - "revisionTime": "2017-09-08T20:15:21Z" + "revision": "a23dc46a847ad0f56c9d66cd84ffeeb002f2c6d3", + "revisionTime": "2017-11-24T17:13:09Z" }, { "checksumSHA1": "NLTyYVX4dn8jV1iad7p494AiZ8E=",