Skip to content

Commit

Permalink
sakuracloud: update api client library (#1690)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamoto-febc authored Aug 4, 2022
1 parent 0aeafc6 commit d344d8b
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 344 deletions.
3 changes: 3 additions & 0 deletions .golangci.toml
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,6 @@
[[issues.exclude-rules]]
path = "providers/dns/dns_providers.go"
text = "Function name: NewDNSChallengeProviderByName,"
[[issues.exclude-rules]]
path = "providers/dns/sakuracloud/client.go"
text = "mu is a global variable"
2 changes: 1 addition & 1 deletion docs/content/dns/zz_gen_sakuracloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ More information [here]({{< ref "dns#configuration-and-credentials" >}}).
## More information

- [API documentation](https://developer.sakura.ad.jp/cloud/api/1.1/)
- [Go client](https://github.com/sacloud/libsacloud)
- [Go client](https://github.com/sacloud/iaas-api-go)

<!-- THIS DOCUMENTATION IS AUTO-GENERATED. PLEASE DO NOT EDIT. -->
<!-- providers/dns/sakuracloud/sakuracloud.toml -->
Expand Down
15 changes: 10 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ require (
github.com/mattn/go-isatty v0.0.14
github.com/miekg/dns v1.1.47
github.com/mimuret/golang-iij-dpf v0.7.1
github.com/mitchellh/mapstructure v1.4.1
github.com/mitchellh/mapstructure v1.5.0
github.com/namedotcom/go v0.0.0-20180403034216-08470befbe04
github.com/nrdcg/auroradns v1.1.0
github.com/nrdcg/desec v0.6.0
Expand All @@ -45,17 +45,18 @@ require (
github.com/ovh/go-ovh v1.1.0
github.com/pquerna/otp v1.3.0
github.com/rainycape/memcache v0.0.0-20150622160815-1031fa0ce2f2
github.com/sacloud/libsacloud v1.36.2
github.com/sacloud/api-client-go v0.2.0
github.com/sacloud/iaas-api-go v1.2.1
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20210127161313-bd30bebeac4f
github.com/softlayer/softlayer-go v1.0.3
github.com/stretchr/testify v1.7.5
github.com/stretchr/testify v1.8.0
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.287
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dnspod v1.0.287
github.com/transip/gotransip/v6 v6.6.1
github.com/urfave/cli/v2 v2.3.0
github.com/vinyldns/go-vinyldns v0.9.16
github.com/vultr/govultr/v2 v2.16.0
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e
golang.org/x/crypto v0.0.0-20220214200702-86341886e292
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/time v0.0.0-20210611083556-38a9dc6acbc6
Expand All @@ -73,13 +74,15 @@ require (
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/andres-erbsen/clock v0.0.0-20160526145045-9e14626cd129 // indirect
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deepmap/oapi-codegen v1.6.1 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/form3tech-oss/jwt-go v3.2.2+incompatible // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-errors/errors v1.0.1 // indirect
github.com/go-resty/resty/v2 v2.1.1-0.20191201195748-d7b97669fe48 // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
Expand All @@ -104,6 +107,8 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/sacloud/go-http v0.1.1 // indirect
github.com/sacloud/packages-go v0.0.3 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/smartystreets/assertions v1.0.1 // indirect
Expand All @@ -112,7 +117,7 @@ require (
github.com/spf13/cast v1.3.1 // indirect
github.com/stretchr/objx v0.4.0 // indirect
go.opencensus.io v0.22.3 // indirect
go.uber.org/ratelimit v0.0.0-20180316092928-c15da0234277 // indirect
go.uber.org/ratelimit v0.2.0 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
64 changes: 43 additions & 21 deletions go.sum

Large diffs are not rendered by default.

91 changes: 48 additions & 43 deletions providers/dns/sakuracloud/client.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,110 @@
package sakuracloud

import (
"errors"
"context"
"fmt"
"net/http"
"strings"
"sync"

"github.com/go-acme/lego/v4/challenge/dns01"
"github.com/sacloud/libsacloud/api"
"github.com/sacloud/libsacloud/sacloud"
"github.com/sacloud/iaas-api-go"
"github.com/sacloud/iaas-api-go/search"
)

const sacloudAPILockKey = "lego/dns/sacloud"
// This mutex is required for concurrent updates.
// see: https://github.com/go-acme/lego/pull/850
var mu sync.Mutex

func (d *DNSProvider) addTXTRecord(fqdn, domain, value string, ttl int) error {
sacloud.LockByKey(sacloudAPILockKey)
defer sacloud.UnlockByKey(sacloudAPILockKey)
mu.Lock()
defer mu.Unlock()

zone, err := d.getHostedZone(domain)
if err != nil {
return fmt.Errorf("sakuracloud: %w", err)
return fmt.Errorf("%w", err)
}

name := extractRecordName(fqdn, zone.Name)

zone.AddRecord(zone.CreateNewRecord(name, "TXT", value, ttl))
_, err = d.client.Update(zone.ID, zone)
records := append(zone.Records, &iaas.DNSRecord{
Name: name,
Type: "TXT",
RData: value,
TTL: ttl,
})
_, err = d.client.UpdateSettings(context.Background(), zone.ID, &iaas.DNSUpdateSettingsRequest{
Records: records,
SettingsHash: zone.SettingsHash,
})
if err != nil {
return fmt.Errorf("sakuracloud: API call failed: %w", err)
return fmt.Errorf("API call failed: %w", err)
}

return nil
}

func (d *DNSProvider) cleanupTXTRecord(fqdn, domain string) error {
sacloud.LockByKey(sacloudAPILockKey)
defer sacloud.UnlockByKey(sacloudAPILockKey)
func (d *DNSProvider) cleanupTXTRecord(fqdn, domain, value string) error {
mu.Lock()
defer mu.Unlock()

zone, err := d.getHostedZone(domain)
if err != nil {
return fmt.Errorf("sakuracloud: %w", err)
return err
}

records := findTxtRecords(fqdn, zone)
recordName := extractRecordName(fqdn, zone.Name)

for _, record := range records {
var updRecords []sacloud.DNSRecordSet
for _, r := range zone.Settings.DNS.ResourceRecordSets {
if !(r.Name == record.Name && r.Type == record.Type && r.RData == record.RData) {
updRecords = append(updRecords, r)
}
var updRecords iaas.DNSRecords
for _, r := range zone.Records {
if !(r.Name == recordName && r.Type == "TXT" && r.RData == value) {
updRecords = append(updRecords, r)
}
zone.Settings.DNS.ResourceRecordSets = updRecords
}

_, err = d.client.Update(zone.ID, zone)
settings := &iaas.DNSUpdateSettingsRequest{
Records: updRecords,
SettingsHash: zone.SettingsHash,
}
_, err = d.client.UpdateSettings(context.Background(), zone.ID, settings)
if err != nil {
return fmt.Errorf("sakuracloud: API call failed: %w", err)
return fmt.Errorf("API call failed: %w", err)
}

return nil
}

func (d *DNSProvider) getHostedZone(domain string) (*sacloud.DNS, error) {
func (d *DNSProvider) getHostedZone(domain string) (*iaas.DNS, error) {
authZone, err := dns01.FindZoneByFqdn(dns01.ToFqdn(domain))
if err != nil {
return nil, err
}

zoneName := dns01.UnFqdn(authZone)

res, err := d.client.Reset().WithNameLike(zoneName).Find()
conditions := &iaas.FindCondition{
Filter: search.Filter{
search.Key("Name"): search.ExactMatch(zoneName),
},
}

res, err := d.client.Find(context.Background(), conditions)
if err != nil {
var notFound api.Error
if errors.As(err, &notFound) && notFound.ResponseCode() == http.StatusNotFound {
if iaas.IsNotFoundError(err) {
return nil, fmt.Errorf("zone %s not found on SakuraCloud DNS: %w", zoneName, err)
}

return nil, fmt.Errorf("API call failed: %w", err)
}

for _, zone := range res.CommonServiceDNSItems {
for _, zone := range res.DNS {
if zone.Name == zoneName {
return &zone, nil
return zone, nil
}
}

return nil, fmt.Errorf("zone %s not found", zoneName)
}

func findTxtRecords(fqdn string, zone *sacloud.DNS) []sacloud.DNSRecordSet {
recordName := extractRecordName(fqdn, zone.Name)

var res []sacloud.DNSRecordSet
for _, record := range zone.Settings.DNS.ResourceRecordSets {
if record.Name == recordName && record.Type == "TXT" {
res = append(res, record)
}
}
return res
}

func extractRecordName(fqdn, zone string) string {
name := dns01.UnFqdn(fqdn)
if idx := strings.Index(name, "."+zone); idx != -1 {
Expand Down
Loading

0 comments on commit d344d8b

Please sign in to comment.