Skip to content

Commit

Permalink
fix: string or int as ID (becasue PHP) and fix missing elements
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 8, 2024
1 parent 5268b60 commit f86c003
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
12 changes: 9 additions & 3 deletions providers/dns/shellrent/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c Client) ListServices(ctx context.Context) ([]int, error) {
return nil, err
}

result := Response[[]int]{}
result := Response[[]IntOrString]{}

err = c.do(req, &result)
if err != nil {
Expand All @@ -61,7 +61,13 @@ func (c Client) ListServices(ctx context.Context) ([]int, error) {
return nil, result.Base
}

return result.Data, nil
var ids []int

for _, datum := range result.Data {
ids = append(ids, datum.Value())
}

return ids, nil
}

// GetServiceDetails gets service details.
Expand Down Expand Up @@ -131,7 +137,7 @@ func (c Client) CreateRecord(ctx context.Context, domainID int, record Record) (
if result.Code != 0 {
return 0, result.Base
}
return result.Data.ID, nil
return result.Data.ID.Value(), nil
}

// DeleteRecord deletes a record.
Expand Down
4 changes: 2 additions & 2 deletions providers/dns/shellrent/internal/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestClient_CreateRecord(t *testing.T) {
services, err := client.CreateRecord(context.Background(), 123, Record{})
require.NoError(t, err)

expected := 150
expected := 2255674

assert.Equal(t, expected, services)
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func TestClient_DeleteRecord_error_status(t *testing.T) {
require.EqualError(t, err, "code 2: Token di autorizzazione non valido")
}

func TestName(t *testing.T) {
func TestTTLRounder(t *testing.T) {
testCases := []struct {
desc string
value int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"error": 0,
"message": "",
"title": "",
"message": "Record DNS aggiunto con successo",
"data": {
"id": 150
"id": "2255674"
}
}
48 changes: 43 additions & 5 deletions providers/dns/shellrent/internal/types.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package internal

import "fmt"
import (
"fmt"
"strconv"
)

type Response[T any] struct {
Base
Expand Down Expand Up @@ -29,8 +32,43 @@ type DomainDetails struct {
}

type Record struct {
ID int `json:"id,omitempty"`
Kind string `json:"kind,omitempty"`
Host string `json:"host,omitempty"`
TTL int `json:"ttl,omitempty"` // It can be set to the following values (number of seconds): 3600, 14400, 28800, 57600, 86400
ID IntOrString `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Host string `json:"host,omitempty"`
TTL int `json:"ttl,omitempty"` // It can be set to the following values (number of seconds): 3600, 14400, 28800, 57600, 86400
Destination string `json:"destination,omitempty"`
}

type IntOrString int

func (m *IntOrString) Value() int {
if m == nil {
return 0
}

return int(*m)
}

func (m *IntOrString) UnmarshalJSON(data []byte) error {
if len(data) == 0 {
return nil
}

raw := string(data)
if data[0] == '"' {
var err error
raw, err = strconv.Unquote(string(data))
if err != nil {
return err
}
}

v, err := strconv.Atoi(raw)
if err != nil {
return err
}

*m = IntOrString(v)

return nil
}
11 changes: 7 additions & 4 deletions providers/dns/shellrent/shellrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ func (d *DNSProvider) Present(domain, token, keyAuth string) error {
}

record := internal.Record{
Kind: "TXT",
Host: subDomain,
TTL: internal.TTLRounder(d.config.TTL),
Type: "TXT",
Host: subDomain,
TTL: internal.TTLRounder(d.config.TTL),
Destination: info.Value,
}

recordID, err := d.client.CreateRecord(ctx, zone.ID, record)
Expand Down Expand Up @@ -186,13 +187,15 @@ func (d *DNSProvider) findZone(ctx context.Context, domain string) (*internal.Do
return nil, fmt.Errorf("get domain details: %w", err)
}

domain := domain

for {
i := strings.Index(domain, ".")
if i == -1 {
break
}

if domainDetails.DomainName == domain {
if strings.EqualFold(domainDetails.DomainName, domain) {
return domainDetails, nil
}

Expand Down

0 comments on commit f86c003

Please sign in to comment.