/
zones.ex
286 lines (204 loc) · 9.94 KB
/
zones.ex
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
defmodule Dnsimple.Zones do
@moduledoc """
Provides functions to interact with the
[zone endpoints](https://developer.dnsimple.com/v2/zones/).
See:
- https://developer.dnsimple.com/v2/zones/
- https://developer.dnsimple.com/v2/zones/records/
"""
@moduledoc section: :api
alias Dnsimple.Client
alias Dnsimple.Listing
alias Dnsimple.Response
alias Dnsimple.Zone
alias Dnsimple.ZoneFile
alias Dnsimple.ZoneDistribution
alias Dnsimple.ZoneRecord
@doc """
Returns the zones in the account.
See:
- https://developer.dnsimple.com/v2/zones/#list
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.list_zones(client, account_id = 1010)
{:ok, response} = Dnsimple.Zones.list_zones(client, account_id = 1010, page: 2, per_page: 10)
{:ok, response} = Dnsimple.Zones.list_zones(client, account_id = 1010, sort: "name:desc")
{:ok, response} = Dnsimple.Zones.list_zones(client, account_id = 1010, filter: [name_like: ".es"])
"""
@spec list_zones(Client.t, String.t | integer, Keyword.t) :: {:ok|:error, Response.t}
def list_zones(client, account_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones")
Listing.get(client, url, options)
|> Response.parse(%{"data" => [%Zone{}], "pagination" => %Response.Pagination{}})
end
@doc """
Returns a zone.
See:
- https://developer.dnsimple.com/v2/zones/#get
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.get_zone(client, account_id = 1010, zone_id = 12)
{:ok, response} = Dnsimple.Zones.get_zone(client, account_id = 1010, zone_id = "example.com")
"""
@spec get_zone(Client.t, String.t | integer, String.t | integer, Keyword.t) :: {:ok|:error, Response.t}
def get_zone(client, account_id, zone_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}")
Client.get(client, url, options)
|> Response.parse(%{"data" => %Zone{}})
end
@doc """
Returns the zone file of the zone.
See:
- https://developer.dnsimple.com/v2/zones/#file
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.get_zone_file(client, account_id = 1010, zone_id = 12)
{:ok, response} = Dnsimple.Zones.get_zone_file(client, account_id = 1010, zone_id = "example.com")
"""
@spec get_zone_file(Client.t, String.t | integer, String.t | integer, Keyword.t) :: {:ok|:error, Response.t}
def get_zone_file(client, account_id, zone_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/file")
Client.get(client, url, options)
|> Response.parse(%{"data" => %ZoneFile{}})
end
@doc """
Returns the distribution status of a zone.
See:
- https://developer.dnsimple.com/v2/zones/#checkZoneDistribution
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.check_zone_distribution(client, account_id = 1010, zone_id = 12)
{:ok, response} = Dnsimple.Zones.check_zone_distribution(client, account_id = 1010, zone_id = "example.com")
"""
@spec check_zone_distribution(Client.t, String.t | integer, String.t | integer, Keyword.t) :: {:ok|:error, Response.t}
def check_zone_distribution(client, account_id, zone_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/distribution")
Client.get(client, url, options)
|> Response.parse(%{"data" => %ZoneDistribution{}})
end
@doc """
Returns the distribution status of a zone record.
See:
- https://developer.dnsimple.com/v2/zones/#checkZoneRecordDistribution
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.check_zone_record_distribution(client, account_id = 1010, zone_id = 12, record_id = 999)
{:ok, response} = Dnsimple.Zones.check_zone_record_distribution(client, account_id = 1010, zone_id = "example.com", record_id = 999)
"""
@spec check_zone_record_distribution(Client.t, String.t | integer, String.t | integer, integer, Keyword.t) :: {:ok|:error, Response.t}
def check_zone_record_distribution(client, account_id, zone_id, record_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/records/#{record_id}/distribution")
Client.get(client, url, options)
|> Response.parse(%{"data" => %ZoneDistribution{}})
end
@doc """
Returns the records in the zone.
See:
- https://developer.dnsimple.com/v2/zones/records/#listZoneRecords
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.list_zone_records(client, account_id = 1010, zone_id = "example.com")
{:ok, response} = Dnsimple.Zones.list_zone_records(client, account_id = 1010, zone_id = 12, page: 2, per_page: 10)
{:ok, response} = Dnsimple.Zones.list_zone_records(client, account_id = 1010, zone_id = "example.com", sort: "type:asc")
{:ok, response} = Dnsimple.Zones.list_zone_records(client, account_id = 1010, zone_id = 12, filter: [type: "A", name: ""])
"""
@spec list_zone_records(Client.t, String.t | integer, String.t | integer, Keyword.t) :: {:ok|:error, Response.t}
def list_zone_records(client, account_id, zone_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/records")
Listing.get(client, url, options)
|> Response.parse(%{"data" => [%ZoneRecord{}], "pagination" => %Response.Pagination{}})
end
@doc """
Returns a zone record.
See:
- https://developer.dnsimple.com/v2/zones/records/#getZoneRecord
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.get_zone_record(client, account_id = 1010, zone_id = 12, record_id = 345)
{:ok, response} = Dnsimple.Zones.get_zone_record(client, account_id = 1010, zone_id = "example.com", record_id = 123)
"""
@spec get_zone_record(Client.t, String.t | integer, String.t | integer, integer, Keyword.t) :: {:ok|:error, Response.t}
def get_zone_record(client, account_id, zone_id, record_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/records/#{record_id}")
Client.get(client, url, options)
|> Response.parse(%{"data" => %ZoneRecord{}})
end
@doc """
Creates a record in the zone.
See:
- https://developer.dnsimple.com/v2/zones/records/#createZoneRecord
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.create_zone_record(client, account_id = 1010, zone_id = "example.com", %{
name: "www",
type: "CNAME",
content: "example.com",
ttl: 3600,
})
"""
@spec create_zone_record(Client.t, String.t | integer, String.t | integer, Keyword.t, Keyword.t) :: {:ok|:error, Response.t}
def create_zone_record(client, account_id, zone_id, attributes, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/records")
Client.post(client, url, attributes, options)
|> Response.parse(%{"data" => %ZoneRecord{}})
end
@doc """
Updates a zone record.
- https://developer.dnsimple.com/v2/zones/records/#updateZoneRecord
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.update_zone_record(client, account_id = 1010, zone_id = "example.com", record_id = 1, %{
ttl: 600
})
"""
@spec update_zone_record(Client.t, String.t | integer, String.t | integer, integer, Keyword.t, Keyword.t) :: {:ok|:error, Response.t}
def update_zone_record(client, account_id, zone_id, record_id, attributes, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/records/#{record_id}")
Client.patch(client, url, attributes, options)
|> Response.parse(%{"data" => %ZoneRecord{}})
end
@doc """
Deletes a record from the zone.
**Warning**: this is a destructive operation.
See:
- https://developer.dnsimple.com/v2/zones/records/#deleteZoneRecord
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.delete_zone_record(client, account_id = 1010, zone_id = 12, record_id = 1)
{:ok, response} = Dnsimple.Zones.delete_zone_record(client, account_id = 1010, zone_id = "example.com", record_id = 1)
"""
@spec delete_zone_record(Client.t, String.t, String.t, integer, Keyword.t) :: {:ok|:error, Response.t}
def delete_zone_record(client, account_id, zone_id, record_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/records/#{record_id}")
Client.delete(client, url, options)
|> Response.parse(nil)
end
@doc """
Activate DNS resolution for the zone in the account.
See:
- https://developer.dnsimple.com/v2/zones/#activateZoneService
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.activate_dns(client, account_id = 1010, zone_id = "example.com")
"""
@spec activate_dns(Client.t, integer | String.t, integer | String.t, Keyword.t) :: {:ok|:error, Response.t}
def activate_dns(client, account_id, zone_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/activation")
Client.put(client, url, Client.empty_body(), options)
|> Response.parse(%{"data" => %Zone{}})
end
@doc """
Deactivate DNS resolution for the zone in the account.
See:
- https://developer.dnsimple.com/v2/zones/#deactivateZoneService
## Examples:
client = %Dnsimple.Client{access_token: "a1b2c3d4"}
{:ok, response} = Dnsimple.Zones.deactivate_dns(client, account_id = 1010, zone_id = "example.com")
"""
@spec deactivate_dns(Client.t, integer | String.t, integer | String.t, Keyword.t) :: {:ok|:error, Response.t}
def deactivate_dns(client, account_id, zone_id, options \\ []) do
url = Client.versioned("/#{account_id}/zones/#{zone_id}/activation")
Client.delete(client, url, options)
|> Response.parse(%{"data" => %Zone{}})
end
end