Skip to content

Commit

Permalink
Merge pull request #7 from aerialls/api_v2beta1
Browse files Browse the repository at this point in the history
feat(scaleway): update API to v2beta1
  • Loading branch information
aerialls committed Dec 19, 2020
2 parents b57515f + f51965c commit 81b66d6
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 61 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/golangci-lint.yml
@@ -0,0 +1,21 @@
name: Lint
on:
push:
tags:
- v*
branches:
- master
pull_request:

jobs:
golangci:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.33
19 changes: 0 additions & 19 deletions .github/workflows/linter.yml

This file was deleted.

5 changes: 4 additions & 1 deletion .github/workflows/release.yml
Expand Up @@ -2,7 +2,7 @@ name: Release
on:
push:
tags:
- '*'
- v*

jobs:
goreleaser:
Expand All @@ -13,15 +13,18 @@ jobs:
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: 1.15

- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: aerialls
password: ${{ secrets.DOCKER_HUB_TOKEN }}

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Expand Up @@ -13,9 +13,11 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- name: Test
run: go test ./...
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -40,7 +40,7 @@ docker run --rm -d --name scaleway-ddns -v `pwd`:/config aerialls/scaleway-ddns

```yaml
scaleway:
organization_id: __ORGANIZATION_ID__
project_id: __PROJECT_ID__
access_key: __ACCESS_KEY__
secret_key: __SECRET_KEY__

Expand Down
7 changes: 5 additions & 2 deletions cmd/scaleway-ddns/main.go
Expand Up @@ -33,7 +33,7 @@ var rootCmd = &cobra.Command{

dns, err := scaleway.NewDNS(
logger,
cfg.ScalewayConfig.OrganizationID,
cfg.ScalewayConfig.ProjectID,
cfg.ScalewayConfig.AccessKey,
cfg.ScalewayConfig.SecretKey,
)
Expand Down Expand Up @@ -68,7 +68,10 @@ func init() {
rootCmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging")
rootCmd.Flags().BoolVarP(&dryRun, "dry-run", "d", false, "don't update DNS records")

rootCmd.MarkFlagRequired("config")
err := rootCmd.MarkFlagRequired("config")
if err != nil {
logger.Fatal(err)
}
}

func initConfig() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/scaleway-ddns/version.go
Expand Up @@ -16,7 +16,7 @@ var versionCmd = &cobra.Command{
Use: "version",
Short: "Prints the version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(fmt.Sprintf("scaleway-ddns %s (commit %s, built at %s)", version, commit, date))
fmt.Printf("scaleway-ddns %s (commit %s, built at %s)\n", version, commit, date)
},
}

Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Expand Up @@ -40,9 +40,9 @@ func (c *Config) validate() error {
}

scwCfg := c.ScalewayConfig
if scwCfg.AccessKey == "" || scwCfg.SecretKey == "" || scwCfg.OrganizationID == "" {
if scwCfg.AccessKey == "" || scwCfg.SecretKey == "" || scwCfg.ProjectID == "" {
return fmt.Errorf(
"scaleway parameters (access_key, secret_key, organization_id) cannot be empty",
"scaleway parameters (access_key, secret_key, project_id) cannot be empty",
)
}

Expand Down
6 changes: 3 additions & 3 deletions config/data.go
Expand Up @@ -23,9 +23,9 @@ type IPConfig struct {

// ScalewayConfig struct for the required configuration to use the Scaleway API
type ScalewayConfig struct {
OrganizationID string `yaml:"organization_id"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
ProjectID string `yaml:"project_id"`
AccessKey string `yaml:"access_key"`
SecretKey string `yaml:"secret_key"`
}

// DomainConfig struct for the domain parameters
Expand Down
39 changes: 24 additions & 15 deletions ddns/ddns.go
Expand Up @@ -32,11 +32,8 @@ func (d *DynamicDNSUpdater) Start() {
d.doStart()

ticker := time.NewTicker(time.Duration(cfg.Interval) * time.Second)
for {
select {
case <-ticker.C:
d.doStart()
}
for range ticker.C {
d.doStart()
}
}

Expand Down Expand Up @@ -87,13 +84,14 @@ func (d *DynamicDNSUpdater) UpdateRecord(
return nil
}

scalewayIP, err := dns.GetRecord(domain.Name, domain.Record, recordType)
scalewayRecord, err := dns.GetRecord(domain.Name, domain.Record, recordType)
if err != nil {
return err
}

if scalewayIP == "" {
scalewayIP = "(empty)"
scalewayIP := "(empty)"
if scalewayRecord != nil {
scalewayIP = scalewayRecord.Data
}

currentIP, err := ip.GetPublicIP(cfg.URL)
Expand Down Expand Up @@ -124,13 +122,24 @@ func (d *DynamicDNSUpdater) UpdateRecord(
return nil
}

err = dns.UpdateRecord(
domain.Name,
domain.Record,
domain.TTL,
currentIP,
recordType,
)
if scalewayRecord != nil {
err = dns.UpdateRecord(
domain.Name,
scalewayRecord.ID,
domain.Record,
domain.TTL,
currentIP,
recordType,
)
} else {
err = dns.AddRecord(
domain.Name,
domain.Record,
domain.TTL,
currentIP,
recordType,
)
}

if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20201215142451-63cc4dfb5426
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.6.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -145,8 +145,8 @@ github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6So
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7 h1:Do8ksLD4Nr3pA0x0hnLOLftZgkiTDvwPDShRTUxtXpE=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20201215142451-63cc4dfb5426 h1:GX1FokVmvFfyPZA9nTuyohcE9iLOto3qw17BOdiHbRE=
github.com/scaleway/scaleway-sdk-go v1.0.0-beta.7.0.20201215142451-63cc4dfb5426/go.mod h1:CJJ5VAbozOl0yEw7nHB9+7BXTJbIn6h7W+f6Gau5IP8=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down
2 changes: 1 addition & 1 deletion scaleway-ddns.yml
@@ -1,5 +1,5 @@
scaleway:
organization_id: __ORGANIZATION_ID__
project_id: __PROJECT_ID__
access_key: __ACCESS_KEY__
secret_key: __SECRET_KEY__

Expand Down
54 changes: 41 additions & 13 deletions scaleway/dns.go
@@ -1,7 +1,7 @@
package scaleway

import (
domainAPI "github.com/scaleway/scaleway-sdk-go/api/domain/v2alpha2"
domainAPI "github.com/scaleway/scaleway-sdk-go/api/domain/v2beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
log "github.com/sirupsen/logrus"
)
Expand All @@ -15,12 +15,12 @@ type DNS struct {
// NewDNS returns a new DNS instance
func NewDNS(
logger *log.Logger,
organizationID string,
projectID string,
accessKey string,
secretKey string,
) (*DNS, error) {
client, err := scw.NewClient(
scw.WithDefaultOrganizationID(organizationID),
scw.WithDefaultProjectID(projectID),
scw.WithAuth(accessKey, secretKey),
)

Expand All @@ -34,16 +34,44 @@ func NewDNS(
}, nil
}

// UpdateRecord updates a DNS record from a specific domain
func (d *DNS) UpdateRecord(domain string, name string, ttl uint32, data string, recordType string) error {
// AddRecord adds a new DNS record
func (d *DNS) AddRecord(domain string, name string, ttl uint32, data string, recordType string) error {
api := domainAPI.NewAPI(d.client)
_, err := api.UpdateDNSZoneRecords(&domainAPI.UpdateDNSZoneRecordsRequest{
DNSZone: domain,
Changes: []*domainAPI.RecordChange{
{
Add: &domainAPI.RecordChangeAdd{
Records: []*domainAPI.Record{
{
Name: name,
Data: data,
Type: d.getRecordTypeFromString(recordType),
TTL: ttl,
},
},
},
},
},
ReturnAllRecords: scw.BoolPtr(false),
})

if err != nil {
return err
}

return nil
}

// UpdateRecord updates an existing DNS record
func (d *DNS) UpdateRecord(domain string, id string, name string, ttl uint32, data string, recordType string) error {
api := domainAPI.NewAPI(d.client)
_, err := api.UpdateDNSZoneRecords(&domainAPI.UpdateDNSZoneRecordsRequest{
DNSZone: domain,
Changes: []*domainAPI.RecordChange{
{
Set: &domainAPI.RecordChangeSet{
Name: name,
Type: d.getRecordTypeFromString(recordType),
ID: &id,
Records: []*domainAPI.Record{
{
Name: name,
Expand All @@ -65,8 +93,8 @@ func (d *DNS) UpdateRecord(domain string, name string, ttl uint32, data string,
return nil
}

// GetRecord returns the value from a DNS record
func (d *DNS) GetRecord(domain string, name string, recordType string) (string, error) {
// GetRecord returns a DNS record
func (d *DNS) GetRecord(domain string, name string, recordType string) (*domainAPI.Record, error) {
api := domainAPI.NewAPI(d.client)
records, err := api.ListDNSZoneRecords(&domainAPI.ListDNSZoneRecordsRequest{
DNSZone: domain,
Expand All @@ -75,14 +103,14 @@ func (d *DNS) GetRecord(domain string, name string, recordType string) (string,
})

if err != nil {
return "", err
return nil, err
}

if records.TotalCount != 1 {
return "", nil
if records.TotalCount == 0 {
return nil, nil
}

return records.Records[0].Data, nil
return records.Records[0], nil
}

func (d *DNS) getRecordTypeFromString(recordType string) domainAPI.RecordType {
Expand Down

0 comments on commit 81b66d6

Please sign in to comment.