Skip to content

Commit

Permalink
Add support to activate and deactivate DNS services for a zone (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilson Lin authored Aug 11, 2023
1 parent 3fe0d6c commit 1aa4e81
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 12 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# CHANGELOG

## main

## 1.2.1 (Unreleased)

FEATURES:

- NEW: Added `ActivateZoneDns` to activate DNS services (resolution) for a zone. (dnsimple/dnsimple-go#145)
- NEW: Added `DeactivateZoneDns` to deactivate DNS services (resolution) for a zone. (dnsimple/dnsimple-go#145)

IMPROVEMENTS:

- `EmailForward` `From` is deprecated. Please use `AliasName` instead for creating email forwards, and `AliasEmail` when retrieving email forwards. (dnsimple/dnsimple-go#145)
- `EmailForward` `To` is deprecated. Please use `DestinationEmail` instead for creating email forwards. (dnsimple/dnsimple-go#145)

## 1.2.0

- NEW: Support `GetDomainRegistration` and `GetDomainRenewal` APIs (dnsimple/dnsimple-go#132)
Expand Down
19 changes: 13 additions & 6 deletions dnsimple/domains_email_forwards.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import (

// EmailForward represents an email forward in DNSimple.
type EmailForward struct {
ID int64 `json:"id,omitempty"`
DomainID int64 `json:"domain_id,omitempty"`
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
ID int64 `json:"id,omitempty"`
DomainID int64 `json:"domain_id,omitempty"`
// Deprecated: for requests, please use `AliasName` instead; for responses, please use `AliasEmail` instead.
From string `json:"from,omitempty"`
// WARNING: This is not set in responses, please use `AliasEmail` instead.
AliasName string `json:"alias_name,omitempty"`
// WARNING: This is not used by requests, please use `AliasName` instead.
AliasEmail string `json:"alias_email,omitempty"`
// Deprecated: please use `DestinationEmail` instead.
To string `json:"to,omitempty"`
DestinationEmail string `json:"destination_email,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}

func emailForwardPath(accountID string, domainIdentifier string, forwardID int64) (path string) {
Expand Down
15 changes: 9 additions & 6 deletions dnsimple/domains_email_forwards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,15 @@ func TestDomainsService_GetEmailForward(t *testing.T) {
assert.NoError(t, err)
forward := forwardResponse.Data
wantSingle := &EmailForward{
ID: 41872,
DomainID: 235146,
From: "[email protected]",
To: "[email protected]",
CreatedAt: "2021-01-25T13:54:40Z",
UpdatedAt: "2021-01-25T13:54:40Z"}
ID: 41872,
DomainID: 235146,
From: "[email protected]",
AliasName: "",
AliasEmail: "[email protected]",
To: "[email protected]",
DestinationEmail: "[email protected]",
CreatedAt: "2021-01-25T13:54:40Z",
UpdatedAt: "2021-01-25T13:54:40Z"}
assert.Equal(t, wantSingle, forward)
}

Expand Down
32 changes: 32 additions & 0 deletions dnsimple/zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,35 @@ func (s *ZonesService) GetZoneFile(ctx context.Context, accountID string, zoneNa
zoneFileResponse.HTTPResponse = resp
return zoneFileResponse, nil
}

// ActivateZoneDns activates DNS services for a zone.
//
// See https://developer.dnsimple.com/v2/zones/#activateZoneService
func (s *ZonesService) ActivateZoneDns(ctx context.Context, accountID string, zoneName string) (*ZoneResponse, error) {
path := versioned(fmt.Sprintf("/%v/zones/%v/activation", accountID, zoneName))
zoneResponse := &ZoneResponse{}

resp, err := s.client.put(ctx, path, nil, zoneResponse)
if err != nil {
return nil, err
}

zoneResponse.HTTPResponse = resp
return zoneResponse, nil
}

// DeactivateZoneDns deactivates DNS services for a zone.
//
// See https://developer.dnsimple.com/v2/zones/#deactivateZoneService
func (s *ZonesService) DeactivateZoneDns(ctx context.Context, accountID string, zoneName string) (*ZoneResponse, error) {
path := versioned(fmt.Sprintf("/%v/zones/%v/activation", accountID, zoneName))
zoneResponse := &ZoneResponse{}

resp, err := s.client.delete(ctx, path, nil, zoneResponse)
if err != nil {
return nil, err
}

zoneResponse.HTTPResponse = resp
return zoneResponse, nil
}
64 changes: 64 additions & 0 deletions dnsimple/zones_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,67 @@ func TestZonesService_GetZoneFile(t *testing.T) {
}
assert.Equal(t, wantSingle, zoneFile)
}

func TestZonesService_ActivateZoneDns(t *testing.T) {
setupMockServer()
defer teardownMockServer()

mux.HandleFunc("/v2/1010/zones/example.com/activation", func(w http.ResponseWriter, r *http.Request) {
httpResponse := httpResponseFixture(t, "/api/activateZoneService/success.http")

testMethod(t, r, "PUT")
testHeaders(t, r)

w.WriteHeader(httpResponse.StatusCode)
_, _ = io.Copy(w, httpResponse.Body)
})

accountID := "1010"
zoneName := "example.com"

zoneResponse, err := client.Zones.ActivateZoneDns(context.Background(), accountID, zoneName)

assert.NoError(t, err)
zone := zoneResponse.Data
wantSingle := &Zone{
ID: 1,
AccountID: 1010,
Name: "example.com",
Reverse: false,
CreatedAt: "2015-04-23T07:40:03Z",
UpdatedAt: "2015-04-23T07:40:03Z",
}
assert.Equal(t, wantSingle, zone)
}

func TestZonesService_DeactivateZoneDns(t *testing.T) {
setupMockServer()
defer teardownMockServer()

mux.HandleFunc("/v2/1010/zones/example.com/activation", func(w http.ResponseWriter, r *http.Request) {
httpResponse := httpResponseFixture(t, "/api/deactivateZoneService/success.http")

testMethod(t, r, "DELETE")
testHeaders(t, r)

w.WriteHeader(httpResponse.StatusCode)
_, _ = io.Copy(w, httpResponse.Body)
})

accountID := "1010"
zoneName := "example.com"

zoneResponse, err := client.Zones.DeactivateZoneDns(context.Background(), accountID, zoneName)

assert.NoError(t, err)
zone := zoneResponse.Data
wantSingle := &Zone{
ID: 1,
AccountID: 1010,
Name: "example.com",
Reverse: false,
CreatedAt: "2015-04-23T07:40:03Z",
UpdatedAt: "2015-04-23T07:40:03Z",
}
assert.Equal(t, wantSingle, zone)
}
16 changes: 16 additions & 0 deletions fixtures.http/api/activateZoneService/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 08 Aug 2023 04:19:23 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
X-RateLimit-Limit: 2400
X-RateLimit-Remaining: 2399
X-RateLimit-Reset: 1691471963
X-WORK-WITH-US: Love automation? So do we! https://dnsimple.com/jobs
ETag: W/"fe6afd982459be33146933235343d51d"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 8e8ac535-9f46-4304-8440-8c68c30427c3
X-Runtime: 0.176579
Strict-Transport-Security: max-age=63072000

{"data":{"id":1,"account_id":1010,"name":"example.com","reverse":false,"secondary":false,"last_transferred_at":null,"active":true,"created_at":"2015-04-23T07:40:03Z","updated_at":"2015-04-23T07:40:03Z"}}
16 changes: 16 additions & 0 deletions fixtures.http/api/deactivateZoneService/success.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 08 Aug 2023 04:19:52 GMT
Content-Type: application/json; charset=utf-8
Connection: keep-alive
X-RateLimit-Limit: 2400
X-RateLimit-Remaining: 2398
X-RateLimit-Reset: 1691471962
X-WORK-WITH-US: Love automation? So do we! https://dnsimple.com/jobs
ETag: W/"5f30a37d01b99bb9e620ef1bbce9a014"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: d2f7bba4-4c81-4818-81d2-c9bbe95f104e
X-Runtime: 0.133278
Strict-Transport-Security: max-age=63072000

{"data":{"id":1,"account_id":1010,"name":"example.com","reverse":false,"secondary":false,"last_transferred_at":null,"active":false,"created_at":"2015-04-23T07:40:03Z","updated_at":"2015-04-23T07:40:03Z"}}

0 comments on commit 1aa4e81

Please sign in to comment.