Skip to content

Commit

Permalink
Remove replace, fix Update, delete dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
ejstreet committed Feb 22, 2023
1 parent 6b5da84 commit 909568c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 48 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ This project is a work-in-progress, see the following table for supported featur
|DNS|ListRecords|GET|✔️|
||FilterRecord*|GET|✔️|
||Create|POST|✔️|
||Update|PATCH|⚠️|
||Replace*|DELETE/POST|✔️|
||Update|PATCH|✔️|
||Delete|DELETE|✔️|
|PURL|List|GET|✔️|
||Retrieve|GET|✔️|
Expand Down
12 changes: 3 additions & 9 deletions omglol/common_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package omglol

import (
"math/rand"
"fmt"
"os"
"time"
)
Expand All @@ -11,7 +11,7 @@ var testKey = os.Getenv("OMGLOL_API_KEY")
var testName = os.Getenv("OMGLOL_USERNAME")
var testOwnedDomain = os.Getenv("OMGLOL_TEST_OWNED_DOMAIN") // some tests will only work if you own the domain you are testing against

func setHostURL () string {
func setHostURL() string {
if hostURL, exists := os.LookupEnv("OMGLOL_API_HOST"); exists {
return hostURL
} else {
Expand All @@ -25,13 +25,7 @@ var testHostURL = setHostURL()
func generateRunUID() string {
RunUID := os.Getenv("GITHUB_RUN_ID") + os.Getenv("GITHUB_RUN_ATTEMPT")
if RunUID == "" {
rand.Seed(time.Now().UnixNano())

charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
length := 10
for i := 0; i < length; i++ {
RunUID += string(charset[rand.Intn(len(charset))])
}
RunUID = fmt.Sprintf("ts%d", time.Now().Unix())
}
return RunUID
}
Expand Down
55 changes: 22 additions & 33 deletions omglol/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (c *Client) ListDNSRecords(address string) (*[]DNSRecord, error) {
type recordsResponse struct {
Request request `json:"request"`
Response struct {
Message string `json:"message"`
DNS []DNSRecord `json:"dns"`
Message string `json:"message"`
DNS []DNSRecord `json:"dns"`
} `json:"response"`
}

Expand Down Expand Up @@ -163,19 +163,6 @@ func NewDNSEntry(Type, Name, Data string, TTL int64, Priority ...int64) *DNSEntr
}
}

func newDNSRecord(id, ttl int64, typ, name, data, createdAt, updatedAt string, priority *int64) *DNSRecord {
return &DNSRecord{
ID: &id,
Type: &typ,
Name: &name,
Data: &data,
Priority: priority,
TTL: &ttl,
CreatedAt: &createdAt,
UpdatedAt: &updatedAt,
}
}

// This function resolves an inconsistency in the current version of the API response. Hopefully it won't be required for too long.
func convertRecordResponse(r dnsRecordContent) *DNSRecord {
var d DNSRecord
Expand Down Expand Up @@ -227,9 +214,26 @@ func (c *Client) CreateDNSRecord(domain string, record DNSEntry) (*DNSRecord, er
}

// Update an existing DNS record. See https://api.omg.lol/#token-patch-dns-edit-an-existing-dns-record
// Note this method does not work at time of writing due to an API bug, see https://github.com/neatnik/omg.lol/issues/584
// Suggested workaround is to use the Replace function instead, which uses the same interface.
func (c *Client) UpdateDNSRecord(domain string, record DNSEntry, record_id int64) (*DNSRecord, error) {
func (c *Client) UpdateDNSRecord(domain string, entry DNSEntry, record_id int64) (*DNSRecord, error) {
// The following struct wrangling is a workaround, see https://github.com/neatnik/omg.lol/issues/584
type updateRecord struct {
Type *string `json:"type"`
Name *string `json:"name"`
Data *string `json:"data"`
Priority *int64 `json:"priority"`
TTL *int64 `json:"ttl"`
ID int64 `json:"id"`
}

record := updateRecord{
Type: entry.Type,
Name: entry.Name,
Data: entry.Data,
Priority: entry.Priority,
TTL: entry.TTL,
ID: record_id,
}

jsonData, err := json.Marshal(record)

req, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/address/%s/dns/%d", c.HostURL, domain, record_id), bytes.NewBuffer([]byte(jsonData)))
Expand Down Expand Up @@ -273,18 +277,3 @@ func (c *Client) DeleteDNSRecord(domain string, record_id int64) error {

return nil
}

// Delete a record with a given ID, then Create a new one using the provided values.
func (c *Client) ReplaceDNSRecord(domain string, record DNSEntry, record_id int64) (*DNSRecord, error) {
err := c.DeleteDNSRecord(domain, record_id)
if err != nil {
return nil, err
}

r, err := c.CreateDNSRecord(domain, record)
if err != nil {
return nil, err
}

return r, nil
}
7 changes: 3 additions & 4 deletions omglol/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestFilterDNSRecords(t *testing.T) {
t.Logf(d2.ToString())
}

// There is currently no test for the Update method, as it does not work at time of writing, see https://github.com/neatnik/omg.lol/issues/584
func TestCreateAndDeleteDNSRecord(t *testing.T) {
c, err := NewClient(testEmail, testKey, testHostURL)

Expand All @@ -78,7 +77,7 @@ func TestCreateAndDeleteDNSRecord(t *testing.T) {
}
}

func TestCreateReplaceDeleteDNSRecord(t *testing.T) {
func TestCreateUpdateDeleteDNSRecord(t *testing.T) {
c, err := NewClient(testEmail, testKey, testHostURL)

if err != nil {
Expand All @@ -87,7 +86,7 @@ func TestCreateReplaceDeleteDNSRecord(t *testing.T) {

record1 := NewDNSEntry("TXT", "testcreate"+RunUID, "test=true", 300)

record2 := NewDNSEntry("TXT", "testreplace"+RunUID, "test=true", 300)
record2 := NewDNSEntry("TXT", "testupdate"+RunUID, "test=true", 300)

create, err := c.CreateDNSRecord(testOwnedDomain, *record1)
if err != nil {
Expand All @@ -96,7 +95,7 @@ func TestCreateReplaceDeleteDNSRecord(t *testing.T) {

t.Logf(create.ToString())

replace, err := c.ReplaceDNSRecord(testOwnedDomain, *record2, *create.ID)
replace, err := c.UpdateDNSRecord(testOwnedDomain, *record2, *create.ID)
if err != nil {
t.Errorf(err.Error())
}
Expand Down

0 comments on commit 909568c

Please sign in to comment.