File: zones_records.go

package info (click to toggle)
golang-github-dnsimple-dnsimple-go 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,428 kB
  • sloc: makefile: 3
file content (158 lines) | stat: -rw-r--r-- 5,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package dnsimple

import (
	"context"
	"fmt"
)

// ZoneRecord represents a zone record in DNSimple.
type ZoneRecord struct {
	ID           int64    `json:"id,omitempty"`
	ZoneID       string   `json:"zone_id,omitempty"`
	ParentID     int64    `json:"parent_id,omitempty"`
	Type         string   `json:"type,omitempty"`
	Name         string   `json:"name"`
	Content      string   `json:"content,omitempty"`
	TTL          int      `json:"ttl,omitempty"`
	Priority     int      `json:"priority,omitempty"`
	SystemRecord bool     `json:"system_record,omitempty"`
	Regions      []string `json:"regions,omitempty"`
	CreatedAt    string   `json:"created_at,omitempty"`
	UpdatedAt    string   `json:"updated_at,omitempty"`
}

// ZoneRecordAttributes represents the attributes you can send to create/update a zone record.
//
// Compared to most other calls in this library, you should not use ZoneRecord as payload for record calls.
// This is because it can lead to side effects due to the inability of go to distinguish between a non-present string
// and an empty string. Name can be both, therefore a specific struct is required.
type ZoneRecordAttributes struct {
	ZoneID   string   `json:"zone_id,omitempty"`
	Type     string   `json:"type,omitempty"`
	Name     *string  `json:"name,omitempty"`
	Content  string   `json:"content,omitempty"`
	TTL      int      `json:"ttl,omitempty"`
	Priority int      `json:"priority,omitempty"`
	Regions  []string `json:"regions,omitempty"`
}

func zoneRecordPath(accountID string, zoneName string, recordID int64) (path string) {
	path = fmt.Sprintf("/%v/zones/%v/records", accountID, zoneName)
	if recordID != 0 {
		path += fmt.Sprintf("/%v", recordID)
	}
	return
}

// ZoneRecordResponse represents a response from an API method that returns a ZoneRecord struct.
type ZoneRecordResponse struct {
	Response
	Data *ZoneRecord `json:"data"`
}

// ZoneRecordsResponse represents a response from an API method that returns a collection of ZoneRecord struct.
type ZoneRecordsResponse struct {
	Response
	Data []ZoneRecord `json:"data"`
}

// ZoneRecordListOptions specifies the optional parameters you can provide
// to customize the ZonesService.ListZoneRecords method.
type ZoneRecordListOptions struct {
	// Select records where the name matches given string.
	Name *string `url:"name,omitempty"`

	// Select records where the name contains given string.
	NameLike *string `url:"name_like,omitempty"`

	// Select records of given type.
	// Eg. TXT, A, NS.
	Type *string `url:"type,omitempty"`

	ListOptions
}

// ListRecords lists the zone records for a zone.
//
// See https://developer.dnsimple.com/v2/zones/records/#listZoneRecords
func (s *ZonesService) ListRecords(ctx context.Context, accountID string, zoneName string, options *ZoneRecordListOptions) (*ZoneRecordsResponse, error) {
	path := versioned(zoneRecordPath(accountID, zoneName, 0))
	recordsResponse := &ZoneRecordsResponse{}

	path, err := addURLQueryOptions(path, options)
	if err != nil {
		return nil, err
	}

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

	recordsResponse.HTTPResponse = resp
	return recordsResponse, nil
}

// CreateRecord creates a zone record.
//
// See https://developer.dnsimple.com/v2/zones/records/#createZoneRecord
func (s *ZonesService) CreateRecord(ctx context.Context, accountID string, zoneName string, recordAttributes ZoneRecordAttributes) (*ZoneRecordResponse, error) {
	path := versioned(zoneRecordPath(accountID, zoneName, 0))
	recordResponse := &ZoneRecordResponse{}

	resp, err := s.client.post(ctx, path, recordAttributes, recordResponse)
	if err != nil {
		return nil, err
	}

	recordResponse.HTTPResponse = resp
	return recordResponse, nil
}

// GetRecord fetches a zone record.
//
// See https://developer.dnsimple.com/v2/zones/records/#getZoneRecord
func (s *ZonesService) GetRecord(ctx context.Context, accountID string, zoneName string, recordID int64) (*ZoneRecordResponse, error) {
	path := versioned(zoneRecordPath(accountID, zoneName, recordID))
	recordResponse := &ZoneRecordResponse{}

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

	recordResponse.HTTPResponse = resp
	return recordResponse, nil
}

// UpdateRecord updates a zone record.
//
// See https://developer.dnsimple.com/v2/zones/records/#updateZoneRecord
func (s *ZonesService) UpdateRecord(ctx context.Context, accountID string, zoneName string, recordID int64, recordAttributes ZoneRecordAttributes) (*ZoneRecordResponse, error) {
	path := versioned(zoneRecordPath(accountID, zoneName, recordID))
	recordResponse := &ZoneRecordResponse{}
	resp, err := s.client.patch(ctx, path, recordAttributes, recordResponse)

	if err != nil {
		return nil, err
	}

	recordResponse.HTTPResponse = resp
	return recordResponse, nil
}

// DeleteRecord PERMANENTLY deletes a zone record from the zone.
//
// See https://developer.dnsimple.com/v2/zones/records/#deleteZoneRecord
func (s *ZonesService) DeleteRecord(ctx context.Context, accountID string, zoneName string, recordID int64) (*ZoneRecordResponse, error) {
	path := versioned(zoneRecordPath(accountID, zoneName, recordID))
	recordResponse := &ZoneRecordResponse{}

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

	recordResponse.HTTPResponse = resp
	return recordResponse, nil
}