Skip to content

Commit

Permalink
Feat/update dns sdk (#163)
Browse files Browse the repository at this point in the history
* Fix: UpdateRecord method only accept mx payload

* fix: Remove unsupport record

* feat: List record in GetZone method

* feat: Update test dns

* feat: wrap put record payload & add ID field

---------

Co-authored-by: Huy Dương <[email protected]>
  • Loading branch information
huyduong2792 and Huy Dương authored Aug 3, 2023
1 parent 53bd14c commit eadeeb3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 95 deletions.
2 changes: 1 addition & 1 deletion dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DNSService interface {
DeleteZone(ctx context.Context, zoneID string) error
CreateRecord(ctx context.Context, zoneID string, crpl interface{}) (*Record, error)
GetRecord(ctx context.Context, recordID string) (*Record, error)
UpdateRecord(ctx context.Context, recordID string, urpl *UpdateRecordPayload) (*ExtendedRecord, error)
UpdateRecord(ctx context.Context, recordID string, urpl interface{}) (*Record, error)
DeleteRecord(ctx context.Context, recordID string) error
}

Expand Down
129 changes: 52 additions & 77 deletions dns_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,20 @@ type WrappedRecordPayload struct {
Record interface{} `json:"record"`
}

// Addrs - contains the list of addresses in regions.
type Addrs struct {
HN []string `json:"HN"`
HCM []string `json:"HCM"`
SG []string `json:"SG"`
USA []string `json:"USA"`
}

// RoutingData - contains the routing data for version 4 and 6 addresses.
type RoutingData struct {
AddrsV4 Addrs `json:"addrs_v4,omitempty"`
AddrsV6 Addrs `json:"addrs_v6,omitempty"`
}

// RoutingPolicyData - contains the routing policy data for version 4 and 6 addresses and the health check information.
type RoutingPolicyData struct {
RoutingData RoutingData `json:"routing_data,omitempty"`
HealthCheck HealthCheck `json:"healthcheck,omitempty"`
}

// HealthCheck - contains the health check information.
type HealthCheck struct {
TCPConnect TCPHealthCheck `json:"tcp_connect,omitempty"`
HTTPStatus HTTPHealthCheck `json:"http_status,omitempty"`
}

// TCPHealthCheck - contains the TCP health check information.
type TCPHealthCheck struct {
TCPPort int `json:"tcp_port,omitempty"`
// MXData - contains the data for an MX record.
type MXData struct {
Value string `json:"value"`
Priority int `json:"priority"`
}

// HTTPHealthCheck - contains the HTTP health check information.
type HTTPHealthCheck struct {
HTTPPort int `json:"http_port,omitempty"`
URLPath string `json:"url_path,omitempty"`
VHost string `json:"vhost,omitempty"`
OkCodes []int `json:"ok_codes,omitempty"`
Interval int `json:"interval,omitempty"`
// SRVData - contains the data for an SRV record.
type SRVData struct {
Port int `json:"port"`
Priority int `json:"priority"`
Protocol string `json:"protocol"`
Service string `json:"service"`
Target string `json:"target"`
Weight int `json:"weight"`
}

// BaseCreateRecordPayload - contains the base payload for creating a record.
Expand All @@ -65,58 +41,54 @@ type CreateNormalRecordPayload struct {
Data []string `json:"data"`
}

// CreatePolicyRecordPayload - contains the payload for creating a policy record.
type CreatePolicyRecordPayload struct {
BaseCreateRecordPayload
RoutingPolicyData RoutingPolicyData `json:"routing_policy_data"`
}

// CreateMXRecordPayload - contains the payload for creating an MX record.
type CreateMXRecordPayload struct {
BaseCreateRecordPayload
Data []MXData `json:"data"`
}

// MXData - contains the data for an MX record.
type MXData struct {
Value string `json:"value"`
Priority int `json:"priority"`
// CreateSRVRecordPayload - contains the payload for creating an SRV record.
type CreateSRVRecordPayload struct {
BaseCreateRecordPayload
Data []SRVData `json:"data"`
}

// RecordData - contains the data for a record.
type RecordData struct {
Value string `json:"value"`
Priority int `json:"priority"`
// BaseUpdateRecordPayload - contains the payload for updating a record.
type BaseUpdateRecordPayload struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
TTL int `json:"ttl,omitempty"`
}
type UpdateNormalRecordPayload struct {
BaseUpdateRecordPayload
Data []string `json:"data"`
}

// UpdateRecordPayload - contains the payload for updating a record.
type UpdateRecordPayload struct {
Name string `json:"name,omitempty"`
Type string `json:"type,omitempty"`
TTL int `json:"ttl,omitempty"`
Data []MXData `json:"data,omitempty"`
RoutingPolicyData RoutingPolicyData `json:"routing_policy_data,omitempty"`
// CreateMXRecordPayload - contains the payload for creating an MX record.
type UpdateMXRecordPayload struct {
BaseUpdateRecordPayload
Data []MXData `json:"data"`
}

// Record - contains the information of a record.
type Record struct {
ID string `json:"id"`
Name string `json:"name"`
Delete int `json:"deleted"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
TenantID string `json:"tenant_id"`
ZoneID string `json:"zone_id"`
Type string `json:"type"`
TTL int `json:"ttl"`
Data []interface{} `json:"data"`
RoutingPolicyData RoutingPolicyData `json:"routing_policy_data"`
// UpdateSRVRecordPayload - contains the payload for creating an SRV record.
type UpdateSRVRecordPayload struct {
BaseUpdateRecordPayload
Data []SRVData `json:"data"`
}

// ExtendedRecord - contains the extended information of a record.
type ExtendedRecord struct {
Record
Data []RecordData `json:"data"`
// Record - contains the information of a record.
type Record struct {
ID string `json:"id"`
Name string `json:"name"`
Delete int `json:"deleted"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
TenantID string `json:"tenant_id"`
ZoneID string `json:"zone_id"`
Type string `json:"type"`
TTL int `json:"ttl"`
Data []interface{} `json:"data"`
}

// Records - contains the list of records.
Expand Down Expand Up @@ -168,8 +140,11 @@ func (d *dnsService) GetRecord(ctx context.Context, recordID string) (*Record, e
}

// UpdateRecord - Update a DNS record
func (d *dnsService) UpdateRecord(ctx context.Context, recordID string, urpl *UpdateRecordPayload) (*ExtendedRecord, error) {
req, err := d.client.NewRequest(ctx, http.MethodPut, dnsName, d.recordItemPath(recordID), urpl)
func (d *dnsService) UpdateRecord(ctx context.Context, recordID string, urpl interface{}) (*Record, error) {
payload := WrappedRecordPayload{
Record: urpl,
}
req, err := d.client.NewRequest(ctx, http.MethodPut, dnsName, d.recordItemPath(recordID), &payload)
if err != nil {
return nil, err
}
Expand All @@ -178,7 +153,7 @@ func (d *dnsService) UpdateRecord(ctx context.Context, recordID string, urpl *Up
return nil, err
}
defer resp.Body.Close()
var data *ExtendedRecord
var data *Record
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
Expand Down
17 changes: 9 additions & 8 deletions dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func TestCreateZone(t *testing.T) {
"id": "47b9147c-7332-4dd8-b56e-56c3a30d6bd5",
"name": "ddddafs.com",
"type": "NS",
"ttl": "3600",
"ttl": 3600,
"data": [
"ns4.bizflycloud.vn.",
"ns5.bizflycloud.vn.",
Expand All @@ -93,7 +93,7 @@ func TestCreateZone(t *testing.T) {
"id": "30ac3108-ac5e-43e8-9eea-1743d6770aa5",
"name": "ddddafs.com",
"type": "SOA",
"ttl": "3600",
"ttl": 3600,
"data": [
"ns4.bizflycloud.vn."
],
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestUpdateRecord(t *testing.T) {
var d dnsService
mux.HandleFunc(testlib.DNSURL(d.recordItemPath("0ed9f98b-7991-4d49-929f-801f246d21f3")), func(writer http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method)
var payload *UpdateRecordPayload
var payload *UpdateMXRecordPayload
require.NoError(t, json.NewDecoder(r.Body).Decode(&payload))
resp := `{
"id": "0ed9f98b-7991-4d49-929f-801f246d21f3",
Expand All @@ -275,10 +275,12 @@ func TestUpdateRecord(t *testing.T) {
}`
_, _ = fmt.Fprint(writer, resp)
})
payload := UpdateRecordPayload{
Name: "mx",
Type: "MX",
TTL: 300,
payload := UpdateMXRecordPayload{
BaseUpdateRecordPayload: BaseUpdateRecordPayload{
Name: "mx",
Type: "MX",
TTL: 300,
},
Data: []MXData{
MXData{
Value: "imap1.vccloud.vn",
Expand All @@ -289,7 +291,6 @@ func TestUpdateRecord(t *testing.T) {
Priority: 2,
},
},
RoutingPolicyData: RoutingPolicyData{},
}
zone, err := client.DNS.UpdateRecord(ctx, "0ed9f98b-7991-4d49-929f-801f246d21f3", &payload)
require.NoError(t, err)
Expand Down
10 changes: 1 addition & 9 deletions dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ type Meta struct {
Page int `json:"page"`
}

// RecordSet - contains the information of a record set
type RecordSet struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
TTL string `json:"ttl"`
}

// Zone - contains the information of a zone
type Zone struct {
ID string `json:"id"`
Expand All @@ -42,7 +34,7 @@ type WrappedZonePayload struct {
// ExtendedZone - contains the information of a zone and embedded record sets
type ExtendedZone struct {
Zone
RecordsSet []RecordSet `json:"record_set"`
RecordsSet []Record `json:"record_set"`
}

// ListZoneResp - contains the response of list zones and metadata
Expand Down

0 comments on commit eadeeb3

Please sign in to comment.