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

Support created and updated for domain records #600

Merged
Merged
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
58 changes: 32 additions & 26 deletions domain_records.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package linodego

import (
"context"
"encoding/json"
"time"

"github.com/linode/linodego/internal/parseabletime"
)

// DomainRecord represents a DomainRecord object
Expand All @@ -17,6 +21,8 @@ type DomainRecord struct {
Protocol *string `json:"protocol"`
TTLSec int `json:"ttl_sec"`
Tag *string `json:"tag"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}

// DomainRecordCreateOptions fields are those accepted by CreateDomainRecord
Expand Down Expand Up @@ -63,6 +69,27 @@ const (
RecordTypeCAA DomainRecordType = "CAA"
)

// UnmarshalJSON for DomainRecord responses
func (d *DomainRecord) UnmarshalJSON(b []byte) error {
type Mask DomainRecord

p := struct {
*Mask
Created *parseabletime.ParseableTime `json:"created"`
Updated *parseabletime.ParseableTime `json:"updated"`
}{
Mask: (*Mask)(d),
}

if err := json.Unmarshal(b, &p); err != nil {
return err
}

d.Created = (*time.Time)(p.Created)
d.Updated = (*time.Time)(p.Updated)
return nil
}

// GetUpdateOptions converts a DomainRecord to DomainRecordUpdateOptions for use in UpdateDomainRecord
func (d DomainRecord) GetUpdateOptions() (du DomainRecordUpdateOptions) {
du.Type = d.Type
Expand All @@ -81,50 +108,29 @@ func (d DomainRecord) GetUpdateOptions() (du DomainRecordUpdateOptions) {

// ListDomainRecords lists DomainRecords
func (c *Client) ListDomainRecords(ctx context.Context, domainID int, opts *ListOptions) ([]DomainRecord, error) {
response, err := getPaginatedResults[DomainRecord](ctx, c, formatAPIPath("domains/%d/records", domainID), opts)
if err != nil {
return nil, err
}

return response, nil
return getPaginatedResults[DomainRecord](ctx, c, formatAPIPath("domains/%d/records", domainID), opts)
}

// GetDomainRecord gets the domainrecord with the provided ID
func (c *Client) GetDomainRecord(ctx context.Context, domainID int, recordID int) (*DomainRecord, error) {
e := formatAPIPath("domains/%d/records/%d", domainID, recordID)
response, err := doGETRequest[DomainRecord](ctx, c, e)
if err != nil {
return nil, err
}

return response, nil
return doGETRequest[DomainRecord](ctx, c, e)
}

// CreateDomainRecord creates a DomainRecord
func (c *Client) CreateDomainRecord(ctx context.Context, domainID int, opts DomainRecordCreateOptions) (*DomainRecord, error) {
e := formatAPIPath("domains/%d/records", domainID)
response, err := doPOSTRequest[DomainRecord](ctx, c, e, opts)
if err != nil {
return nil, err
}

return response, nil
return doPOSTRequest[DomainRecord](ctx, c, e, opts)
}

// UpdateDomainRecord updates the DomainRecord with the specified id
func (c *Client) UpdateDomainRecord(ctx context.Context, domainID int, recordID int, opts DomainRecordUpdateOptions) (*DomainRecord, error) {
e := formatAPIPath("domains/%d/records/%d", domainID, recordID)
response, err := doPUTRequest[DomainRecord](ctx, c, e, opts)
if err != nil {
return nil, err
}

return response, nil
return doPUTRequest[DomainRecord](ctx, c, e, opts)
}

// DeleteDomainRecord deletes the DomainRecord with the specified id
func (c *Client) DeleteDomainRecord(ctx context.Context, domainID int, recordID int) error {
e := formatAPIPath("domains/%d/records/%d", domainID, recordID)
err := doDELETERequest(ctx, c, e)
return err
return doDELETERequest(ctx, c, e)
}
6 changes: 6 additions & 0 deletions test/integration/domain_records_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ func TestDomainRecord_Create_smoke(t *testing.T) {
t.Errorf("Error creating domain record, got error %v", err)
}

assertDateSet(t, record.Created)
assertDateSet(t, record.Updated)

expected := testDomainRecordCreateOpts

// cant compare Target, fixture IPs are sanitized
Expand All @@ -37,6 +40,9 @@ func TestDomainRecord_Update(t *testing.T) {
t.Error(err)
}

assertDateSet(t, record.Created)
assertDateSet(t, record.Updated)

updateOpts := linodego.DomainRecordUpdateOptions{
Name: "renamed",
}
Expand Down
46 changes: 17 additions & 29 deletions test/integration/fixtures/TestDomainRecord_Create.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interactions:
url: https://api.linode.com/v4beta/domains
method: POST
response:
body: '{"id": 3041953, "type": "master", "domain": "linodego-blue-test.com", "tags":
body: '{"id": 3153528, "type": "master", "domain": "linodego-blue-test.com", "tags":
[], "group": "", "status": "active", "errors": "", "description": "", "soa_email":
"example@example.com", "retry_sec": 0, "master_ips": [], "axfr_ips": [], "expire_sec":
0, "refresh_sec": 0, "ttl_sec": 0, "created": "2018-01-02T03:04:05", "updated":
Expand Down Expand Up @@ -43,7 +43,7 @@ interactions:
Content-Type:
- application/json
Expires:
- Thu, 25 Jul 2024 17:44:22 GMT
- Thu, 31 Oct 2024 20:38:28 GMT
Pragma:
- no-cache
Strict-Transport-Security:
Expand All @@ -58,12 +58,9 @@ interactions:
- DENY
- DENY
X-Oauth-Scopes:
- account:read_write databases:read_write domains:read_write events:read_write
firewall:read_write images:read_write ips:read_write linodes:read_write lke:read_write
longview:read_write nodebalancers:read_write object_storage:read_write stackscripts:read_write
volumes:read_write vpc:read_write
- '*'
X-Ratelimit-Limit:
- "400"
- "800"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
Expand All @@ -79,10 +76,10 @@ interactions:
- application/json
User-Agent:
- linodego/dev https://github.com/linode/linodego
url: https://api.linode.com/v4beta/domains/3041953/records
url: https://api.linode.com/v4beta/domains/3153528/records
method: POST
response:
body: '{"id": 35694150, "type": "A", "name": "a", "target": "127.0.0.1", "priority":
body: '{"id": 36666864, "type": "A", "name": "a", "target": "127.0.0.1", "priority":
0, "weight": 0, "port": 0, "service": null, "protocol": null, "ttl_sec": 0,
"tag": null, "created": "2018-01-02T03:04:05", "updated": "2018-01-02T03:04:05"}'
headers:
Expand All @@ -109,7 +106,7 @@ interactions:
Content-Type:
- application/json
Expires:
- Thu, 25 Jul 2024 17:44:22 GMT
- Thu, 31 Oct 2024 20:38:28 GMT
Pragma:
- no-cache
Strict-Transport-Security:
Expand All @@ -124,12 +121,9 @@ interactions:
- DENY
- DENY
X-Oauth-Scopes:
- account:read_write databases:read_write domains:read_write events:read_write
firewall:read_write images:read_write ips:read_write linodes:read_write lke:read_write
longview:read_write nodebalancers:read_write object_storage:read_write stackscripts:read_write
volumes:read_write vpc:read_write
- '*'
X-Ratelimit-Limit:
- "400"
- "800"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
Expand All @@ -145,7 +139,7 @@ interactions:
- application/json
User-Agent:
- linodego/dev https://github.com/linode/linodego
url: https://api.linode.com/v4beta/domains/3041953/records/35694150
url: https://api.linode.com/v4beta/domains/3153528/records/36666864
method: DELETE
response:
body: '{}'
Expand Down Expand Up @@ -173,7 +167,7 @@ interactions:
Content-Type:
- application/json
Expires:
- Thu, 25 Jul 2024 17:44:22 GMT
- Thu, 31 Oct 2024 20:38:29 GMT
Pragma:
- no-cache
Strict-Transport-Security:
Expand All @@ -188,12 +182,9 @@ interactions:
- DENY
- DENY
X-Oauth-Scopes:
- account:read_write databases:read_write domains:read_write events:read_write
firewall:read_write images:read_write ips:read_write linodes:read_write lke:read_write
longview:read_write nodebalancers:read_write object_storage:read_write stackscripts:read_write
volumes:read_write vpc:read_write
- '*'
X-Ratelimit-Limit:
- "400"
- "800"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
Expand All @@ -209,7 +200,7 @@ interactions:
- application/json
User-Agent:
- linodego/dev https://github.com/linode/linodego
url: https://api.linode.com/v4beta/domains/3041953
url: https://api.linode.com/v4beta/domains/3153528
method: DELETE
response:
body: '{}'
Expand Down Expand Up @@ -237,7 +228,7 @@ interactions:
Content-Type:
- application/json
Expires:
- Thu, 25 Jul 2024 17:44:22 GMT
- Thu, 31 Oct 2024 20:38:29 GMT
Pragma:
- no-cache
Strict-Transport-Security:
Expand All @@ -252,12 +243,9 @@ interactions:
- DENY
- DENY
X-Oauth-Scopes:
- account:read_write databases:read_write domains:read_write events:read_write
firewall:read_write images:read_write ips:read_write linodes:read_write lke:read_write
longview:read_write nodebalancers:read_write object_storage:read_write stackscripts:read_write
volumes:read_write vpc:read_write
- '*'
X-Ratelimit-Limit:
- "400"
- "800"
X-Xss-Protection:
- 1; mode=block
status: 200 OK
Expand Down
Loading