cloudflare 3.1.0 → 3.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/.rubocop.yml +19 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -3
- data/README.md +8 -0
- data/Rakefile +15 -13
- data/cloudflare.gemspec +12 -10
- data/lib/cloudflare.rb +5 -3
- data/lib/cloudflare/connection.rb +45 -41
- data/lib/cloudflare/response.rb +42 -42
- data/lib/cloudflare/rspec/connection.rb +14 -12
- data/lib/cloudflare/user.rb +9 -7
- data/lib/cloudflare/version.rb +3 -1
- data/lib/cloudflare/zone.rb +176 -163
- data/spec/cloudflare/zone_spec.rb +125 -91
- data/spec/fake_cloudflare/cloudflare.rb +17 -0
- data/spec/spec_helper.rb +171 -24
- metadata +24 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9e424c7c834e532b5a8783095b16ebde0f449ba26c39d33b3cf08042af234e6d
|
4
|
+
data.tar.gz: 48d3e57b0ffaafc85633b7ee7d3ac3d65046bcb4d6d563f005f6ae364204275d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cf74a6f05ab0c6188efadce6a8744b4f46dbec3d8d386c7287274b84e46eb5e9aaa3c40bd51c1e26084d9e86b831d76cdf6471e9c550baf8d30ee04804e04671
|
7
|
+
data.tar.gz: f2825bb5f23eb32b12141cf58f05405e38a41bde64a6a3b92ff58934a3f5b36594428caf4d14b69434dbbedf71f8e565bfc3fd01a59fe4af87b86584bf1e0070
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Naming/UncommunicativeMethodParamName:
|
2
|
+
AllowedNames:
|
3
|
+
- ip
|
4
|
+
- id
|
5
|
+
|
6
|
+
Style/Documentation:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Metrics/LineLength:
|
10
|
+
Max: 120
|
11
|
+
|
12
|
+
Metrics/MethodLength:
|
13
|
+
Max: 20
|
14
|
+
|
15
|
+
Lint/Debugger:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Gemspec/RequiredRubyVersion:
|
19
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.5.0
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -89,6 +89,14 @@ data = {"mode":"block","configuration":{"target":"ip","value":"#{ip}"},"notes":"
|
|
89
89
|
response = zones.first.firewall_rules.post(data.to_json, content_type: 'application/json')
|
90
90
|
```
|
91
91
|
|
92
|
+
Add a DNS record dynamically. Here we add an A record for `batman.example.com`:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
client = Cloudflare.connect(key: CF_KEY, email: CF_EMAIL)
|
96
|
+
zone = client.zones.find_by_name("example.com")
|
97
|
+
zone.dns_records.post({"type":"A","name":"batman","content":"127.0.0.1","proxied":false}.to_json, :content_type => "application/json")
|
98
|
+
```
|
99
|
+
|
92
100
|
## Contributing
|
93
101
|
|
94
102
|
1. Fork it
|
data/Rakefile
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
require 'rspec/core/rake_task'
|
3
5
|
|
4
6
|
RSpec::Core::RakeTask.new(:test)
|
5
7
|
|
6
|
-
task :
|
8
|
+
task default: :test
|
7
9
|
|
8
10
|
task :coverage do
|
9
|
-
|
11
|
+
ENV['COVERAGE'] = 'y'
|
10
12
|
end
|
11
13
|
|
12
14
|
task :console do
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
15
|
+
require 'cloudflare'
|
16
|
+
require 'pry'
|
17
|
+
|
18
|
+
email = ENV['CLOUDFLARE_EMAIL']
|
19
|
+
key = ENV['CLOUDFLARE_KEY']
|
20
|
+
|
21
|
+
connection = Cloudflare::Connection.new(key: key, email: email)
|
22
|
+
|
23
|
+
binding.pry
|
22
24
|
end
|
data/cloudflare.gemspec
CHANGED
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
require_relative 'lib/cloudflare/version'
|
3
5
|
|
4
6
|
Gem::Specification.new do |spec|
|
@@ -12,16 +14,16 @@ Gem::Specification.new do |spec|
|
|
12
14
|
spec.homepage = 'https://github.com/b4k3r/cloudflare'
|
13
15
|
spec.licenses = ['MIT']
|
14
16
|
|
15
|
-
spec.files = `git ls-files`.split(
|
17
|
+
spec.files = `git ls-files`.split("\n")
|
16
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
-
spec.require_paths = [
|
19
|
-
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
20
22
|
spec.required_ruby_version = '>= 2.0.0'
|
21
|
-
|
22
|
-
spec.add_dependency 'rest-client'
|
23
|
-
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
26
|
-
spec.add_development_dependency
|
23
|
+
|
24
|
+
spec.add_dependency 'rest-client', '~> 2.0.2'
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
27
|
+
spec.add_development_dependency 'rake'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.6'
|
27
29
|
end
|
data/lib/cloudflare.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright, 2012, by Marcin Prokop.
|
2
4
|
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
3
5
|
#
|
@@ -24,7 +26,7 @@ require_relative 'cloudflare/zone'
|
|
24
26
|
require_relative 'cloudflare/user'
|
25
27
|
|
26
28
|
module Cloudflare
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
def self.connect(**options)
|
30
|
+
Connection.new(**options)
|
31
|
+
end
|
30
32
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright, 2012, by Marcin Prokop.
|
2
4
|
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
3
5
|
#
|
@@ -23,51 +25,53 @@ require 'net/http'
|
|
23
25
|
require 'json'
|
24
26
|
|
25
27
|
require 'rest-client'
|
28
|
+
require 'uri'
|
26
29
|
|
27
30
|
require_relative 'response'
|
28
31
|
|
29
32
|
module Cloudflare
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
33
|
+
DEFAULT_URL = 'https://api.cloudflare.com/client/v4/'
|
34
|
+
TIMEOUT = 10 # Default is 5 seconds
|
35
|
+
DEFAULT_HEADERS = { 'Content-Type' => 'application/json' }.freeze
|
36
|
+
|
37
|
+
class Resource < RestClient::Resource
|
38
|
+
include Enumerable
|
39
|
+
|
40
|
+
# @param api_key [String] `X-Auth-Key` or `X-Auth-User-Service-Key` if no email provided.
|
41
|
+
# @param email [String] `X-Auth-Email`, your email address for the account.
|
42
|
+
def initialize(url = DEFAULT_URL, key: nil, email: nil, **options)
|
43
|
+
headers = options[:headers] || DEFAULT_HEADERS.dup
|
44
|
+
|
45
|
+
if email.nil?
|
46
|
+
headers['X-Auth-User-Service-Key'] = key
|
47
|
+
else
|
48
|
+
headers['X-Auth-Key'] = key
|
49
|
+
headers['X-Auth-Email'] = email
|
50
|
+
end
|
51
|
+
|
52
|
+
# Convert HTTP API responses to our own internal response class:
|
53
|
+
super(url, headers: headers, accept: 'application/json', **options) do |response|
|
54
|
+
Response.new(response.request.url, response.body)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def paginate(obj, url, url_args = '')
|
59
|
+
page = 1
|
60
|
+
page_size = 50
|
61
|
+
results = []
|
53
62
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
63
|
+
# fetch and aggregate all pages
|
64
|
+
loop do
|
65
|
+
query = URI.encode_www_form scope_type: :organization, per_page: page_size, page: page
|
66
|
+
rules = obj.new(concat_urls(url, "?#{query}#{url_args}"), self, **options)
|
67
|
+
results += rules.get.results
|
68
|
+
break if results.empty? || results.size % page_size != 0
|
69
|
+
page += 1
|
70
|
+
end
|
71
|
+
results
|
72
|
+
end
|
73
|
+
end
|
58
74
|
|
59
|
-
|
60
|
-
|
61
|
-
rules = obj.new(concat_urls(url, "?scope_type=organization#{url_args}&per_page=#{page_size}&page=#{page}"), self, **options)
|
62
|
-
results += rules.get.results
|
63
|
-
break if results.size % page_size != 0
|
64
|
-
page += 1
|
65
|
-
end
|
66
|
-
|
67
|
-
return results
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
class Connection < Resource
|
72
|
-
end
|
75
|
+
class Connection < Resource
|
76
|
+
end
|
73
77
|
end
|
data/lib/cloudflare/response.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright, 2012, by Marcin Prokop.
|
2
4
|
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
3
5
|
#
|
@@ -22,52 +24,50 @@
|
|
22
24
|
require 'json'
|
23
25
|
|
24
26
|
module Cloudflare
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
27
|
+
class RequestError < StandardError
|
28
|
+
def initialize(what, response)
|
29
|
+
super("#{what}: #{response.errors.join(', ')}")
|
30
|
+
|
31
|
+
@response = response
|
32
|
+
end
|
33
|
+
|
34
|
+
attr_reader :response
|
35
|
+
end
|
36
|
+
|
37
|
+
class Response
|
38
|
+
def initialize(what, content)
|
39
|
+
@what = what
|
40
|
+
|
41
|
+
@body = JSON.parse(content, symbolize_names: true)
|
42
|
+
end
|
43
|
+
|
44
|
+
attr_reader :body
|
45
|
+
|
46
|
+
def result
|
47
|
+
raise RequestError.new(@what, self) unless successful?
|
43
48
|
|
44
|
-
|
45
|
-
|
46
|
-
raise RequestError.new(@what, self)
|
47
|
-
end
|
48
|
-
|
49
|
-
body[:result]
|
50
|
-
end
|
49
|
+
body[:result]
|
50
|
+
end
|
51
51
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
52
|
+
# Treat result as an array (often it is).
|
53
|
+
def results
|
54
|
+
Array(result)
|
55
|
+
end
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
def empty?
|
58
|
+
result.empty?
|
59
|
+
end
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
61
|
+
def successful?
|
62
|
+
body[:success]
|
63
|
+
end
|
64
64
|
|
65
|
-
|
66
|
-
|
67
|
-
|
65
|
+
def errors
|
66
|
+
body[:errors]
|
67
|
+
end
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
69
|
+
def messages
|
70
|
+
body[:messages]
|
71
|
+
end
|
72
|
+
end
|
73
73
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
4
|
#
|
3
5
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
@@ -21,16 +23,16 @@
|
|
21
23
|
require_relative '../../cloudflare'
|
22
24
|
|
23
25
|
module Cloudflare
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
26
|
+
module RSpec
|
27
|
+
module Connection
|
28
|
+
end
|
29
|
+
|
30
|
+
RSpec.shared_context Connection do
|
31
|
+
# You must specify these in order for the tests to run.
|
32
|
+
let(:email) { 'jake@example.net' }
|
33
|
+
let(:key) { '5up3rS3cr3tAuthK3y' }
|
34
|
+
|
35
|
+
let(:connection) { Cloudflare.connect(key: key, email: email) }
|
36
|
+
end
|
37
|
+
end
|
36
38
|
end
|
data/lib/cloudflare/user.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright, 2012, by Marcin Prokop.
|
2
4
|
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
3
5
|
#
|
@@ -22,12 +24,12 @@
|
|
22
24
|
require_relative 'connection'
|
23
25
|
|
24
26
|
module Cloudflare
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
class Connection < Resource
|
28
|
+
def user
|
29
|
+
@user ||= User.new(concat_urls(url, 'user'), options)
|
30
|
+
end
|
31
|
+
end
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
+
class User < Resource
|
34
|
+
end
|
33
35
|
end
|
data/lib/cloudflare/version.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright, 2012, by Marcin Prokop.
|
2
4
|
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
3
5
|
#
|
@@ -20,5 +22,5 @@
|
|
20
22
|
# THE SOFTWARE.
|
21
23
|
|
22
24
|
module Cloudflare
|
23
|
-
|
25
|
+
VERSION = '3.2.0'
|
24
26
|
end
|
data/lib/cloudflare/zone.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Copyright, 2012, by Marcin Prokop.
|
2
4
|
# Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
3
5
|
# Copyright, 2017, by David Rosenbloom. <http://artifactory.com>
|
@@ -23,167 +25,178 @@
|
|
23
25
|
require_relative 'connection'
|
24
26
|
|
25
27
|
module Cloudflare
|
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
|
-
|
28
|
+
class Connection < Resource
|
29
|
+
def zones
|
30
|
+
@zones ||= Zones.new(concat_urls(url, 'zones'), options)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class DNSRecord < Resource
|
35
|
+
def initialize(url, record = nil, **options)
|
36
|
+
super(url, **options)
|
37
|
+
|
38
|
+
@record = record || get.result
|
39
|
+
end
|
40
|
+
|
41
|
+
def update_content(content)
|
42
|
+
response = put({ type: @record[:type],
|
43
|
+
name: @record[:name],
|
44
|
+
content: content }.to_json,
|
45
|
+
content_type: 'application/json')
|
46
|
+
response.successful?
|
47
|
+
end
|
48
|
+
|
49
|
+
attr_reader :record
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
"#{@record[:name]} #{@record[:type]} #{@record[:content]}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
class DNSRecords < Resource
|
57
|
+
def initialize(url, zone, **options)
|
58
|
+
super(url, **options)
|
59
|
+
|
60
|
+
@zone = zone
|
61
|
+
end
|
62
|
+
|
63
|
+
attr_reader :zone
|
64
|
+
|
65
|
+
def all
|
66
|
+
results = paginate(DNSRecords, url)
|
67
|
+
results.map { |record| DNSRecord.new(concat_urls(url, record[:id]), record, **options) }
|
68
|
+
end
|
69
|
+
|
70
|
+
def find_by_name(name)
|
71
|
+
response = get(params: { name: name })
|
72
|
+
|
73
|
+
return if response.empty?
|
74
|
+
record = response.results.first
|
75
|
+
|
76
|
+
DNSRecord.new(concat_urls(url, record[:id]), record, **options)
|
77
|
+
end
|
78
|
+
|
79
|
+
def find_by_id(id)
|
80
|
+
DNSRecord.new(concat_urls(url, id), **options)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
class FirewallRule < Resource
|
85
|
+
def initialize(url, record = nil, **options)
|
86
|
+
super(url, **options)
|
87
|
+
|
88
|
+
@record = record || get.result
|
89
|
+
end
|
90
|
+
|
91
|
+
attr_reader :record
|
92
|
+
|
93
|
+
def to_s
|
94
|
+
"#{@record[:configuration][:value]} - #{@record[:mode]} - #{@record[:notes]}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class FirewallRules < Resource
|
99
|
+
def initialize(url, zone, **options)
|
100
|
+
super(url, **options)
|
101
|
+
|
102
|
+
@zone = zone
|
103
|
+
end
|
104
|
+
|
105
|
+
attr_reader :zone
|
106
|
+
|
107
|
+
def all(mode = nil, ip = nil, notes = nil)
|
108
|
+
url_args = ''
|
109
|
+
url_args.concat("&mode=#{mode}") if mode
|
110
|
+
url_args.concat("&configuration_value=#{ip}") if ip
|
111
|
+
url_args.concat("¬es=#{notes}") if notes
|
112
|
+
|
113
|
+
results = paginate(FirewallRules, url, url_args)
|
114
|
+
results.map { |record| FirewallRule.new(concat_urls(url, record[:id]), record, **options) }
|
115
|
+
end
|
116
|
+
|
117
|
+
def firewalled_ips(rules)
|
118
|
+
rules.collect { |r| r.record[:configuration][:value] }
|
119
|
+
end
|
120
|
+
|
121
|
+
def blocked_ips
|
122
|
+
firewalled_ips(all('block'))
|
123
|
+
end
|
124
|
+
|
125
|
+
def set(mode, ip, note)
|
126
|
+
data = {
|
127
|
+
mode: mode.to_s,
|
128
|
+
configuration: {
|
129
|
+
target: 'ip',
|
130
|
+
value: ip.to_s,
|
131
|
+
notes: "cloudflare gem firewall_rules [#{mode}] #{note} #{Time.now.strftime('%m/%d/%y')}"
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
post(data.to_json, content_type: 'application/json')
|
136
|
+
end
|
137
|
+
|
138
|
+
def unset(mode, value)
|
139
|
+
rule = send("find_by_#{mode}", value)
|
140
|
+
rule.delete
|
141
|
+
end
|
142
|
+
|
143
|
+
def find_by_id(id)
|
144
|
+
FirewallRule.new(concat_urls(url, id), **options)
|
145
|
+
end
|
146
|
+
|
147
|
+
def find_by_ip(ip)
|
148
|
+
rule = FirewallRule.new(concat_urls(url, "?configuration_value=#{ip}"), **options)
|
149
|
+
FirewallRule.new(concat_urls(url, rule.record.first[:id]), **options)
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
class Zone < Resource
|
154
|
+
DEFAULT_PURGE_CACHE_PARAMS = {
|
155
|
+
purge_everything: true
|
156
|
+
}.freeze
|
157
|
+
|
158
|
+
def initialize(url, record = nil, preload = false, **options)
|
159
|
+
super(url, **options)
|
160
|
+
@record = record || get.result if preload
|
161
|
+
end
|
162
|
+
|
163
|
+
attr_reader :record
|
164
|
+
|
165
|
+
def dns_records
|
166
|
+
@dns_records ||= DNSRecords.new(concat_urls(url, 'dns_records'), self, **options)
|
167
|
+
end
|
168
|
+
|
169
|
+
def firewall_rules
|
170
|
+
@firewall_rules ||= FirewallRules.new(concat_urls(url, 'firewall/access_rules/rules'), self, **options)
|
171
|
+
end
|
172
|
+
|
173
|
+
def purge_cache(params = DEFAULT_PURGE_CACHE_PARAMS)
|
174
|
+
response = self['purge_cache'].post(params&.to_json || {})
|
175
|
+
response.successful?
|
176
|
+
end
|
177
|
+
|
178
|
+
def to_s
|
179
|
+
@record[:name]
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
class Zones < Resource
|
184
|
+
def all
|
185
|
+
results = paginate(Zone, url)
|
186
|
+
results.map { |record| Zone.new(concat_urls(url, record[:id]), record, **options) }
|
187
|
+
end
|
188
|
+
|
189
|
+
def find_by_name(name)
|
190
|
+
response = get(params: { name: name })
|
191
|
+
|
192
|
+
return if response.empty?
|
193
|
+
record = response.results.first
|
194
|
+
|
195
|
+
Zone.new(concat_urls(url, record[:id]), record, true, **options)
|
196
|
+
end
|
197
|
+
|
198
|
+
def find_by_id(id)
|
199
|
+
Zone.new(concat_urls(url, id), **options)
|
200
|
+
end
|
201
|
+
end
|
189
202
|
end
|