Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(provider): add support for Spaceship #907

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/spaceship.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Spaceship

## Configuration

Docs can be found for the [spaceship API](https://docs.spaceship.dev/).

### Example
amroessam marked this conversation as resolved.
Show resolved Hide resolved

```json
{
"settings": [
{
"provider": "spaceship",
"domain": "example.com",
"host": "subdomain",
"api_key": "YOUR_API_KEY",
"api_secret": "YOUR_API_SECRET",
"ip_version": "ipv4"
}
]
}
```

### Compulsory parameters

- `"domain"` is the domain to update. It can be a root domain (i.e. `example.com`) or a subdomain (i.e. `subdomain.example.com`), or a wildcard (i.e. `*.example.com`). In case of a wildcard, it only works if there is no existing wildcard records of any record type.
- `"api_key"` is your API key which can be obtained from [API Manager](https://www.spaceship.com/application/api-manager/).
- `"api_secret"` is your API secret which is provided along with your API key in the API Manager.

### Optional parameters

- `"ip_version"` can be `ipv4` (A records), or `ipv6` (AAAA records) or `ipv4 or ipv6` (update one of the two, depending on the public ip found). It defaults to `ipv4 or ipv6`.
- `"ipv6_suffix"` is the IPv6 interface identifier suffix to use. It can be for example `0:0:0:0:72ad:8fbb:a54e:bedd/64`. If left empty, it defaults to no suffix and the raw public IPv6 address obtained is used in the record updating.
- `"ttl"` is the record TTL which defaults to 3600 seconds.
2 changes: 2 additions & 0 deletions internal/provider/constants/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const (
Route53 models.Provider = "route53"
SelfhostDe models.Provider = "selfhost.de"
Servercow models.Provider = "servercow"
Spaceship models.Provider = "spaceship"
Spdyn models.Provider = "spdyn"
Strato models.Provider = "strato"
Variomedia models.Provider = "variomedia"
Expand Down Expand Up @@ -104,6 +105,7 @@ func ProviderChoices() []models.Provider {
Porkbun,
Route53,
SelfhostDe,
Spaceship,
Spdyn,
Strato,
Variomedia,
Expand Down
1 change: 1 addition & 0 deletions internal/provider/errors/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var (
ErrIPReceivedMismatch = errors.New("mismatching IP address received")
ErrIPSentMalformed = errors.New("malformed IP address sent")
ErrNoService = errors.New("no service")
ErrRateLimit = errors.New("rate limit exceeded")
ErrPrivateIPSent = errors.New("private IP cannot be routed")
ErrReceivedNoIP = errors.New("received no IP address in response")
ErrReceivedNoResult = errors.New("received no result in response")
Expand Down
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import (
"github.com/qdm12/ddns-updater/internal/provider/providers/route53"
"github.com/qdm12/ddns-updater/internal/provider/providers/selfhostde"
"github.com/qdm12/ddns-updater/internal/provider/providers/servercow"
"github.com/qdm12/ddns-updater/internal/provider/providers/spaceship"
"github.com/qdm12/ddns-updater/internal/provider/providers/spdyn"
"github.com/qdm12/ddns-updater/internal/provider/providers/strato"
"github.com/qdm12/ddns-updater/internal/provider/providers/variomedia"
Expand Down Expand Up @@ -178,6 +179,8 @@ func New(providerName models.Provider, data json.RawMessage, domain, owner strin
return selfhostde.New(data, domain, owner, ipVersion, ipv6Suffix)
case constants.Servercow:
return servercow.New(data, domain, owner, ipVersion, ipv6Suffix)
case constants.Spaceship:
return spaceship.New(data, domain, owner, ipVersion, ipv6Suffix)
case constants.Spdyn:
return spdyn.New(data, domain, owner, ipVersion, ipv6Suffix)
case constants.Strato:
Expand Down
55 changes: 55 additions & 0 deletions internal/provider/providers/spaceship/createrecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package spaceship

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)

func (p *Provider) createRecord(ctx context.Context, client *http.Client, recordType, address string) error {
const defaultTTL = 3600

u := url.URL{
Scheme: "https",
Host: "spaceship.dev",
Path: "/api/v1/dns/records/" + p.domain,
}

createData := struct {
Force bool `json:"force"`
Items []Record `json:"items"`
}{
Force: true,
Items: []Record{{
Type: recordType,
Name: p.owner,
Address: address,
TTL: defaultTTL,
}},
}

requestBody := bytes.NewBuffer(nil)
if err := json.NewEncoder(requestBody).Encode(createData); err != nil {
return fmt.Errorf("encoding request body: %w", err)
}
request, err := http.NewRequestWithContext(ctx, http.MethodPut, u.String(), requestBody)
if err != nil {
return fmt.Errorf("creating request: %w", err)
}
p.setHeaders(request)

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent {
return p.handleAPIError(response)
}

return nil
}
43 changes: 43 additions & 0 deletions internal/provider/providers/spaceship/deleterecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package spaceship

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)

func (p *Provider) deleteRecord(ctx context.Context, client *http.Client, record Record) error {
u := url.URL{
Scheme: "https",
Host: "spaceship.dev",
Path: "/api/v1/dns/records/" + p.domain,
}

deleteData := []Record{record}

var requestBody bytes.Buffer
if err := json.NewEncoder(&requestBody).Encode(deleteData); err != nil {
return fmt.Errorf("encoding request body: %w", err)
}

request, err := http.NewRequestWithContext(ctx, http.MethodDelete, u.String(), &requestBody)
if err != nil {
return fmt.Errorf("creating request: %w", err)
}
p.setHeaders(request)

response, err := client.Do(request)
if err != nil {
return err
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent {
return p.handleAPIError(response)
}

return nil
}
80 changes: 80 additions & 0 deletions internal/provider/providers/spaceship/getrecord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package spaceship

import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"

"github.com/qdm12/ddns-updater/internal/provider/errors"
)

func (p *Provider) getRecords(ctx context.Context, client *http.Client) (records []Record, err error) {
u := url.URL{
Scheme: "https",
Host: "spaceship.dev",
Path: "/api/v1/dns/records/" + p.domain,
}

values := url.Values{}
// pagination values, mandatory for the API
values.Set("take", "100")
values.Set("skip", "0")
amroessam marked this conversation as resolved.
Show resolved Hide resolved
u.RawQuery = values.Encode()

request, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("creating http request: %w", err)
}
p.setHeaders(request)

response, err := client.Do(request)
if err != nil {
return nil, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
var apiError APIError
if err := json.NewDecoder(response.Body).Decode(&apiError); err != nil {
return nil, fmt.Errorf("%w: %d", errors.ErrHTTPStatusNotValid, response.StatusCode)
}

switch response.StatusCode {
case http.StatusUnauthorized:
return nil, fmt.Errorf("%w: invalid API credentials", errors.ErrAuth)
case http.StatusNotFound:
if apiError.Detail == "SOA record for domain "+p.domain+" not found." {
return nil, fmt.Errorf("%w: domain %s must be configured in Spaceship first",
errors.ErrDomainNotFound, p.domain)
}
return nil, fmt.Errorf("%w: %s", errors.ErrRecordResourceSetNotFound, apiError.Detail)
case http.StatusBadRequest:
var details string
for _, d := range apiError.Data {
if d.Field != "" {
details += fmt.Sprintf(" %s: %s;", d.Field, d.Details)
} else {
details += fmt.Sprintf(" %s;", d.Details)
}
}
return nil, fmt.Errorf("%w:%s", errors.ErrBadRequest, details)
case http.StatusTooManyRequests:
return nil, fmt.Errorf("%w: rate limit exceeded", errors.ErrRateLimit)
default:
return nil, fmt.Errorf("%w: %d: %s",
errors.ErrHTTPStatusNotValid, response.StatusCode, apiError.Detail)
}
}

var recordsResponse struct {
Items []Record `json:"items"`
}

if err := json.NewDecoder(response.Body).Decode(&recordsResponse); err != nil {
return nil, fmt.Errorf("decoding response: %w", err)
}
amroessam marked this conversation as resolved.
Show resolved Hide resolved

return recordsResponse.Items, nil
}
Loading
Loading