/
templates_records.rb
135 lines (126 loc) · 5.68 KB
/
templates_records.rb
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
module Dnsimple
class Client
module TemplatesRecords
# Lists the records in the template.
#
# @see https://developer.dnsimple.com/v2/templates/records/#list
# @see #all_records
#
# @example List the first page of records for the template "alpha"
# client.templates.records(1010, "alpha")
#
# @example List records for the template "alpha", providing a specific page
# client.templates.records(1010, "alpha", page: 2)
#
# @example List records for the template "alpha", providing sorting policy
# client.templates.records(1010, "alpha", sort: "type:asc")
#
# @param [Fixnum] account_id the account ID
# @param [String] template_id the template name
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::PaginatedResponse<Dnsimple::Struct::TemplateRecord>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def records(account_id, template_id, options = {})
endpoint = Client.versioned("/%s/templates/%s/records" % [account_id, template_id])
response = client.get(endpoint, Options::ListOptions.new(options))
Dnsimple::PaginatedResponse.new(response, response["data"].map { |r| Struct::TemplateRecord.new(r) })
end
alias list_records records
# Lists ALL the records in the template.
#
# This method is similar to {#records}, but instead of returning the results of a specific page
# it iterates all the pages and returns the entire collection.
#
# Please use this method carefully, as fetching the entire collection will increase the number of requests
# you send to the API server and you may eventually risk to hit the throttle limit.
#
# @see https://developer.dnsimple.com/v2/templates/records/#list
# @see #all_records
#
# @example List all the records for "alpha template
# client.templates.all_records(1010, "alpha")
#
# @param [Fixnum] account_id the account ID
# @param [String] template_id the template name
# @param [Hash] options the filtering and sorting options
# @option options [Integer] :page current page (pagination)
# @option options [Integer] :per_page number of entries to return (pagination)
# @option options [String] :sort sorting policy
# @return [Dnsimple::CollectionResponse<Dnsimple::Struct::TemplateRecord>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def all_records(account_id, template_id, options = {})
paginate(:records, account_id, template_id, options)
end
# Creates a record in the template.
#
# @see https://developer.dnsimple.com/v2/templates/records/#create
#
# @example Create an A record for "alpha" template
# client.templates.create_record(1010, "alpha", name: "", type: "A", content: "192.168.1.1", ttl: 600)
#
# @param [Fixnum] account_id the account ID
# @param [String] template_id the template name
# @param [Hash] attributes
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::TemplateRecord>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def create_record(account_id, template_id, attributes, options = {})
Extra.validate_mandatory_attributes(attributes, [:type, :name, :content])
endpoint = Client.versioned("/%s/templates/%s/records" % [account_id, template_id])
response = client.post(endpoint, attributes, options)
Dnsimple::Response.new(response, Struct::TemplateRecord.new(response["data"]))
end
# Gets a record from the template.
#
# @see https://developer.dnsimple.com/v2/templates/records/#get
#
# @example Get record 123 in "alpha template
# client.templates.record(1010, "alpha", 123)
#
# @param [Fixnum] account_id the account ID
# @param [String] template_id the template name
# @param [Fixnum] record_id the record ID
# @param [Hash] options
# @return [Dnsimple::Response<Dnsimple::Struct::TemplateRecord>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def record(account_id, template_id, record_id, options = {})
endpoint = Client.versioned("/%s/templates/%s/records/%s" % [account_id, template_id, record_id])
response = client.get(endpoint, options)
Dnsimple::Response.new(response, Struct::TemplateRecord.new(response["data"]))
end
# Deletes a record from the template.
#
# WARNING: this cannot be undone.
#
# @see https://developer.dnsimple.com/v2/templates/records/#delete
#
# @example Delete record 123 in "alpha template
# client.templates.delete_record(1010, "alpha", 123)
#
# @param [Fixnum] account_id the account ID
# @param [String] template_id the template name
# @param [Fixnum] record_id the record ID
# @param [Hash] options
# @return [Dnsimple::Response<nil>]
#
# @raise [Dnsimple::NotFoundError]
# @raise [Dnsimple::RequestError]
def delete_record(account_id, template_id, record_id, options = {})
endpoint = Client.versioned("/%s/templates/%s/records/%s" % [account_id, template_id, record_id])
response = client.delete(endpoint, options)
Dnsimple::Response.new(response, nil)
end
end
end
end