Skip to content
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
50 changes: 50 additions & 0 deletions dns_zones.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

const zoneAvailableNameserversURL = "/dns/available-name-servers.json"
const zoneListURL = "/dns/list-zones.json"
const zoneCreateUrl = "/dns/register.json"
const zoneGetURL = "/dns/get-zone-info.json"
const zoneTriggerUpdateURL = "/dns/update-zone.json"
const zoneUpdateStatusURL = "/dns/update-status.json"
Expand Down Expand Up @@ -78,6 +79,47 @@ type ZoneUpdateStatus struct {
IsUpdated APIBool `json:"updated"`
}

type CreateZone struct {
Name string `json:"name"`
Type ZoneType `json:"type"`
Ns []string `json:"ns"`
MasterIp string `json:"master_ip"`
}

// AsParams returns the HTTP parameters for a zone to use within the create zone API method
func (zone CreateZone) AsParams() HTTPParams {
params := HTTPParams{
"domain-name": zone.Name,
"ns": zone.Ns,
}
switch zone.Type {
case ZoneTypeMaster:
params["zone-type"] = "master"
case ZoneTypeGeoDNS:
params["zone-type"] = "geo-dns"
case ZoneTypeParked:
params["zone-type"] = "parked"
case ZoneTypeSlave:
params["zone-type"] = "slave"
params["master-ip"] = zone.MasterIp
case ZoneTypeUnknown:
params["zone-type"] = "unknown"
}

return params
}

// NewZone instantiates a new CreateZone which can be used within ClouDNS API methods. It does -not- add this zone
// automatically.
func NewZone(name string, zoneType ZoneType, ns []string, masterIp string) CreateZone {
return CreateZone{
Name: name,
Type: zoneType,
Ns: ns,
MasterIp: masterIp,
}
}

// List returns all zones
// Official Docs: https://www.cloudns.net/wiki/article/50/
func (svc *ZoneService) List(ctx context.Context) ([]Zone, error) {
Expand Down Expand Up @@ -181,6 +223,14 @@ func (svc *ZoneService) GetUsage(ctx context.Context) (result ZoneUsage, err err
return
}

// Create a new zone
func (svc *ZoneService) Create(ctx context.Context, zone CreateZone) (result StatusResult, err error) {
params := zone.AsParams()

err = svc.api.request(ctx, "POST", zoneCreateUrl, params, nil, &result)
return
}

// UnmarshalJSON converts the ClouDNS zone type into the correct ZoneType enumeration value
func (zt *ZoneType) UnmarshalJSON(data []byte) error {
switch strings.Trim(string(data), `"`) {
Expand Down
14 changes: 13 additions & 1 deletion dns_zones_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cloudns

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestZoneService_AvailableNameservers(t *testing.T) {
Expand Down Expand Up @@ -90,3 +91,14 @@ func TestZoneService_GetUsage(t *testing.T) {
_, err := client.Zones.GetUsage(ctx)
assert.NoError(t, err, "should not fail")
}

func TestZoneService_Create(t *testing.T) {
teardown := setup(t)
defer teardown()

zone := NewZone("sampion.io", ZoneTypeMaster, []string{}, "")
_, err := client.Zones.Create(ctx, zone)
if err != nil {
t.Fatalf("Records.Create() returned error: %v", err)
}
}
105 changes: 105 additions & 0 deletions fixtures/TestZoneService_Create.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
version: 2
interactions:
- id: 0
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.cloudns.net
remote_addr: ""
request_uri: ""
body: '{"domain-name":"sampion.io","ns":[],"zone-type":"master"}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- cloudns-go/test
url: https://api.cloudns.net/dns/add-record.json
method: POST
response:
proto: HTTP/2.0
proto_major: 2
proto_minor: 0
transfer_encoding: []
trailer: {}
content_length: -1
uncompressed: true
body: '{"status":"Failed","statusDescription":"Invalid authentication, incorrect user ID or password."}'
headers:
Content-Type:
- application/json
Date:
- Sat, 04 Mar 2023 08:51:41 GMT
Server:
- nginx
Strict-Transport-Security:
- max-age=31536000; includeSubdomains; preload
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: 605.6862ms
- id: 1
request:
proto: HTTP/1.1
proto_major: 1
proto_minor: 1
content_length: 0
transfer_encoding: []
trailer: {}
host: api.cloudns.net
remote_addr: ""
request_uri: ""
body: '{"domain-name":"sampion.io","ns":[],"zone-type":"master"}'
form: {}
headers:
Accept:
- application/json
Content-Type:
- application/json
User-Agent:
- cloudns-go/test
url: https://api.cloudns.net/dns/register.json
method: POST
response:
proto: HTTP/2.0
proto_major: 2
proto_minor: 0
transfer_encoding: []
trailer: {}
content_length: -1
uncompressed: true
body: '{"status":"Failed","statusDescription":"Invalid authentication, incorrect user ID or password."}'
headers:
Content-Type:
- application/json
Date:
- Sat, 04 Mar 2023 09:26:16 GMT
Server:
- nginx
Strict-Transport-Security:
- max-age=31536000; includeSubdomains; preload
Vary:
- Accept-Encoding
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- SAMEORIGIN
X-Xss-Protection:
- 1; mode=block
status: 200 OK
code: 200
duration: 734.0522ms
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/ppmathis/cloudns-go
module github.com/inamvar/cloudns-go

go 1.16

Expand Down