glare 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d34218aa3655c0e7e2ab1605e3ee6ad339d9850a
4
+ data.tar.gz: 9bd27535c1e035d47310393ab00ef2bf779525a2
5
+ SHA512:
6
+ metadata.gz: 4672e678f5f30e202b77aa962459b2c8a7fd2161a82368e878d0abbf9a992bbfefb64a3e3be7f06fa7bcfda8f964b0c666498e6cc9b2002a3a84daec4a6950a3
7
+ data.tar.gz: 7233ef28109e26dd0ed213249b2b6998fa6baf91f64bd6695f7d1946e4de3381886622a27def3a1fcc18c2d6d337f962a80a34e50bea3388e174c5f88d0da1b5
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .envrc
11
+ /spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format doc
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cf.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'coveralls', require: false
8
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Flywire
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # Glare
2
+
3
+ Ruby gem to interact with CloudFlare API v4
4
+
5
+ ## Build Status
6
+
7
+ [![Build Status](https://travis-ci.org/peertransfer/glare.svg?branch=master)](https://travis-ci.org/peertransfer/glare)
8
+ [![Coverage Status](https://coveralls.io/repos/github/peertransfer/glare/badge.svg?branch=master)](https://coveralls.io/github/peertransfer/glare?branch=master)
9
+ [![Code Climate](https://codeclimate.com/github/peertransfer/glare/badges/gpa.svg)](https://codeclimate.com/github/peertransfer/glare)
10
+ [![Dependency Status](https://gemnasium.com/peertransfer/glare.svg)](https://gemnasium.com/peertransfer/glare)
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'glare'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install glare
27
+
28
+ ## Usage
29
+
30
+ ### Create/update DNS record
31
+
32
+ ### Delete DNS record
33
+
34
+ ## Development
35
+
36
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
37
+
38
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
39
+
40
+ ## Contributing
41
+
42
+ 1. Fork it ( https://github.com/peertransfer/glare/fork )
43
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
44
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
45
+ 4. Push to the branch (`git push origin my-new-feature`)
46
+ 5. Create a new Pull Request
data/glare.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'glare/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'glare'
8
+ spec.version = Glare::VERSION
9
+ spec.authors = ['Jose Luis Salas', 'Omar Lopez']
10
+ spec.email = ['josacar@users.noreply.github.com', 'olopez@users.noreply.github.com']
11
+
12
+ spec.summary = 'API client for CloudFlare v4 API'
13
+ spec.homepage = 'https://github.com/peertransfer/cf'
14
+ spec.license = 'MIT'
15
+
16
+ files = Dir['lib/**/*.rb']
17
+ rootfiles = ['Gemfile', 'glare.gemspec', 'README.md', 'LICENSE']
18
+ dotfiles = ['.gitignore', '.rspec']
19
+
20
+ spec.files = files + rootfiles + dotfiles
21
+ spec.test_files = Dir['spec/**/*.{rb,json}']
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.9'
25
+ spec.add_development_dependency 'rake', '~> 11.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.4'
27
+
28
+ spec.add_dependency 'public_suffix'
29
+ spec.add_dependency 'httpclient'
30
+ end
data/lib/glare.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'public_suffix'
2
+ require 'glare/version'
3
+ require 'glare/credentials'
4
+ require 'glare/client'
5
+ require 'glare/domain'
6
+ require 'glare/result'
7
+ require 'glare/dns_record'
8
+ require 'glare/dns_records'
9
+
10
+ module Glare
11
+ class << self
12
+ def register(fqdn, destination, type)
13
+ client = build_client
14
+ Domain.new(client).register(fqdn, destination, type)
15
+ end
16
+
17
+ def resolve(fqdn, type)
18
+ client = build_client
19
+ Domain.new(client).resolve(fqdn, type)
20
+ end
21
+
22
+ def deregister(fqdn, type)
23
+ client = build_client
24
+ Domain.new(client).deregister(fqdn, type)
25
+ end
26
+
27
+ private
28
+
29
+ CF_EMAIL = 'CF_EMAIL'.freeze
30
+ CF_AUTH_KEY = 'CF_AUTH_KEY'.freeze
31
+
32
+ def client(credentials)
33
+ Glare::Client.new(credentials.email, credentials.auth_key)
34
+ end
35
+
36
+ def default_credentials
37
+ email = ENV[CF_EMAIL]
38
+ auth_key = ENV[CF_AUTH_KEY]
39
+ Credentials.new(email, auth_key)
40
+ end
41
+
42
+ def build_client
43
+ credentials = default_credentials
44
+ client(credentials)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,33 @@
1
+ require 'jsonclient'
2
+
3
+ module Glare
4
+ class Client
5
+ BASE_URL = 'https://api.cloudflare.com/client/v4'.freeze
6
+
7
+ def initialize(email, auth_key)
8
+ @headers = {
9
+ 'Content-Type' => 'application/json',
10
+ 'X-Auth-Email' => email,
11
+ 'X-Auth-Key' => auth_key
12
+ }
13
+ @http = JSONClient.new
14
+ @http.debug_dev = STDERR if ENV['CF_DEBUG']
15
+ end
16
+
17
+ def get(query, params)
18
+ @http.get(BASE_URL + query, params, @headers)
19
+ end
20
+
21
+ def post(query, data)
22
+ @http.post(BASE_URL + query, data, @headers)
23
+ end
24
+
25
+ def put(query, data)
26
+ @http.put(BASE_URL + query, data, @headers)
27
+ end
28
+
29
+ def delete(query, params=nil)
30
+ @http.delete(BASE_URL + query, params, @headers)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,10 @@
1
+ module Glare
2
+ class Credentials
3
+ def initialize(email, auth_key)
4
+ @email = email
5
+ @auth_key = auth_key
6
+ end
7
+
8
+ attr_reader :email, :auth_key
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module Glare
2
+ class DnsRecord
3
+ def initialize(name:, type:, content:)
4
+ @name = name
5
+ @type = type
6
+ @content = content
7
+ end
8
+
9
+ def to_h
10
+ {
11
+ type: @type,
12
+ name: @name,
13
+ content: @content
14
+ }
15
+ end
16
+ attr_reader :content, :type
17
+ end
18
+ private_constant :DnsRecord
19
+ end
@@ -0,0 +1,56 @@
1
+ module Glare
2
+ class CfDnsRecord
3
+ def initialize(id:, name:, type:, content:)
4
+ @id = id
5
+ @name = name
6
+ @type = type
7
+ @content = content
8
+ end
9
+ attr_reader :id, :name, :type, :content
10
+ end
11
+
12
+ class DnsRecords < Result
13
+ def initialize(result)
14
+ super(result)
15
+ @records = records
16
+ end
17
+
18
+ def records_to_update(desired_records)
19
+ @records.reject do |record|
20
+ desired_records.any? { |r| r.content == record.content }
21
+ end
22
+ end
23
+
24
+ def count
25
+ @records.count
26
+ end
27
+
28
+ def each
29
+ @records.each { |record| yield(record) }
30
+ end
31
+
32
+ def records_to_delete(targer_number)
33
+ records_to_delete = count - targer_number
34
+ return [] if records_to_delete < 0
35
+
36
+ @records.pop(records_to_delete)
37
+ end
38
+
39
+ def records_to_create(desired_records)
40
+ desired_records.drop(count)
41
+ end
42
+
43
+ private
44
+
45
+ def records
46
+ result['result'].map do |item|
47
+ CfDnsRecord.new(
48
+ id: item['id'],
49
+ name: item['name'],
50
+ type: item['type'],
51
+ content: item['content']
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,115 @@
1
+ module Glare
2
+ class Domain
3
+ class Zone
4
+ def initialize(client, fqdn)
5
+ @client = client
6
+ @fqdn = fqdn
7
+ end
8
+
9
+ def records(type)
10
+ records = record_search(type)
11
+ DnsRecords.new(records)
12
+ end
13
+
14
+ def id
15
+ return @id if @id
16
+ zone_search = @client.get('/zones', name: registered_domain)
17
+ @id = Result.new(zone_search).first_result_id
18
+ end
19
+
20
+ private
21
+
22
+ def registered_domain
23
+ PublicSuffix.parse(@fqdn).domain
24
+ end
25
+
26
+ def record_search(type)
27
+ @client.get("/zones/#{id}/dns_records", name: @fqdn, type: type)
28
+ end
29
+ end
30
+
31
+ class Record
32
+ class << self
33
+ def register(client, zone, dns_records)
34
+ @client = client
35
+ existing_records = zone.records(dns_records.first.type)
36
+ zone_id = zone.id
37
+
38
+ update(zone_id, dns_records, existing_records)
39
+ end
40
+
41
+ def deregister(client, zone, dns_records)
42
+ @client = client
43
+ zone_id = zone.id
44
+
45
+ delete(zone_id, dns_records)
46
+ end
47
+
48
+ private
49
+
50
+ def delete(zone_id, dns_records)
51
+ dns_records.each do |record|
52
+ @client.delete("/zones/#{zone_id}/dns_records/#{record.id}")
53
+ end
54
+ end
55
+
56
+ def update(zone_id, dns_records, existing_records)
57
+ update_current_records(zone_id, dns_records, existing_records)
58
+ delete_uneeded_records(zone_id, dns_records, existing_records)
59
+ create_new_records(zone_id, dns_records, existing_records)
60
+ end
61
+
62
+ def update_current_records(zone_id, dns_records, existing_records)
63
+ records_to_update = existing_records.records_to_update(dns_records)
64
+ updates = records_to_update.zip(dns_records)
65
+ updates.each do |existing_record, dns_record|
66
+ @client.put("/zones/#{zone_id}/dns_records/#{existing_record.id}", dns_record.to_h)
67
+ end
68
+ end
69
+
70
+ def delete_uneeded_records(zone_id, dns_records, existing_records)
71
+ records_to_delete = existing_records.records_to_delete(dns_records.count)
72
+ records_to_delete.each do |record|
73
+ @client.delete("/zones/#{zone_id}/dns_records/#{record.id}")
74
+ end
75
+ end
76
+
77
+ def create_new_records(zone_id, dns_records, existing_records)
78
+ records_to_create = existing_records.records_to_create(dns_records)
79
+ create(zone_id, records_to_create)
80
+ end
81
+
82
+ def create(zone_id, dns_records)
83
+ dns_records.each do |dns_record|
84
+ @client.post("/zones/#{zone_id}/dns_records", dns_record.to_h)
85
+ end
86
+ end
87
+ end
88
+ end
89
+
90
+ def initialize(client)
91
+ @client = client
92
+ end
93
+
94
+ def register(fqdn, destinations, type)
95
+ dns_records = Array(destinations).map do |destination|
96
+ DnsRecord.new(type: type, name: fqdn, content: destination)
97
+ end
98
+
99
+ zone = Zone.new(@client, fqdn)
100
+ Record.register(@client, zone, dns_records)
101
+ end
102
+
103
+ def resolve(fqdn, type)
104
+ zone = Zone.new(@client, fqdn)
105
+ result = zone.records(type)
106
+ result.contents
107
+ end
108
+
109
+ def deregister(fqdn, type)
110
+ zone = Zone.new(@client, fqdn)
111
+ dns_records = zone.records(type)
112
+ Record.deregister(@client, zone, dns_records)
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,26 @@
1
+ module Glare
2
+ class Result
3
+ def initialize(result)
4
+ @result = result
5
+ end
6
+
7
+ def ocurrences
8
+ result['result_info']['count'].to_i
9
+ end
10
+
11
+ def first_result_id
12
+ result['result'].first['id']
13
+ end
14
+
15
+ def contents
16
+ Array(result['result']).map { |item| item['content'] }
17
+ end
18
+
19
+ private
20
+
21
+ def result
22
+ @result.content
23
+ end
24
+ end
25
+ private_constant :Result
26
+ end
@@ -0,0 +1,3 @@
1
+ module Glare
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'glare'
2
+
3
+ RSpec.describe 'delete domain' do
4
+ context 'when a domain is registered' do
5
+ let(:domain) { 'a.flywire.cc' }
6
+ let(:type) { 'A' }
7
+ let(:destination) { ['1.2.3.5', '6.7.8.9'] }
8
+ before do
9
+ register_domain(domain, destination)
10
+ end
11
+
12
+ it 'deletes all records with given type' do
13
+ expect(resolve(domain)).to eq(destination)
14
+ delete(domain)
15
+ expect(resolve(domain)).to eq([])
16
+ end
17
+ end
18
+
19
+ def delete(domain)
20
+ Glare.deregister(domain, type)
21
+ end
22
+
23
+ def resolve(domain)
24
+ Glare.resolve(domain, type)
25
+ end
26
+
27
+ def register_domain(domain, destination)
28
+ Glare.register(domain, destination, type)
29
+ end
30
+ end
@@ -0,0 +1,13 @@
1
+ {
2
+ "errors": [],
3
+ "messages": [],
4
+ "result": [],
5
+ "result_info": {
6
+ "count": 0,
7
+ "page": 1,
8
+ "per_page": 20,
9
+ "total_count": 0,
10
+ "total_pages": 0
11
+ },
12
+ "success": true
13
+ }
@@ -0,0 +1,82 @@
1
+ {
2
+ "errors": [],
3
+ "messages": [],
4
+ "result": [
5
+ {
6
+ "checked_on": "2016-05-12T10:20:14.209689Z",
7
+ "created_on": "2016-05-12T10:15:06.531372Z",
8
+ "development_mode": 0,
9
+ "id": "9de4eb694c380d79845d35cd939cc7a7",
10
+ "meta": {
11
+ "custom_certificate_quota": 0,
12
+ "multiple_railguns_allowed": false,
13
+ "page_rule_quota": 3,
14
+ "phishing_detected": false,
15
+ "step": 4,
16
+ "wildcard_proxiable": false
17
+ },
18
+ "modified_on": "2016-05-12T10:19:14.185994Z",
19
+ "name": "example.com",
20
+ "name_servers": [
21
+ "coco.ns.cloudflare.com",
22
+ "jeff.ns.cloudflare.com"
23
+ ],
24
+ "original_dnshost": null,
25
+ "original_name_servers": [
26
+ "ns-11.awsdns-15.com",
27
+ "ns-288.awsdns-33.org",
28
+ "ns-32.awsdns-62.co.uk",
29
+ "ns-74.awsdns-27.net"
30
+ ],
31
+ "original_registrar": null,
32
+ "owner": {
33
+ "email": "user@example.com",
34
+ "id": "dbaf2b4d4317b92cbc3b1820a5eaf6d3",
35
+ "type": "user"
36
+ },
37
+ "paused": false,
38
+ "permissions": [
39
+ "#analytics:read",
40
+ "#billing:edit",
41
+ "#billing:read",
42
+ "#cache_purge:edit",
43
+ "#dns_records:edit",
44
+ "#dns_records:read",
45
+ "#lb:edit",
46
+ "#lb:read",
47
+ "#logs:read",
48
+ "#organization:edit",
49
+ "#organization:read",
50
+ "#ssl:edit",
51
+ "#ssl:read",
52
+ "#waf:edit",
53
+ "#waf:read",
54
+ "#zone:edit",
55
+ "#zone:read",
56
+ "#zone_settings:edit",
57
+ "#zone_settings:read"
58
+ ],
59
+ "plan": {
60
+ "can_subscribe": true,
61
+ "currency": "USD",
62
+ "externally_managed": false,
63
+ "frequency": "",
64
+ "id": "0feeeeeeeeeeeeeeeeeeeeeeeeeeeeee",
65
+ "is_subscribed": true,
66
+ "legacy_id": "free",
67
+ "name": "Free Website",
68
+ "price": 0
69
+ },
70
+ "status": "pending",
71
+ "type": "full"
72
+ }
73
+ ],
74
+ "result_info": {
75
+ "count": 1,
76
+ "page": 1,
77
+ "per_page": 20,
78
+ "total_count": 1,
79
+ "total_pages": 1
80
+ },
81
+ "success": true
82
+ }
@@ -0,0 +1,48 @@
1
+ {
2
+ "errors": [],
3
+ "messages": [],
4
+ "result": [
5
+ {
6
+ "content": "destination.com",
7
+ "created_on": "2016-05-12T11:53:49.233342Z",
8
+ "id": "a1f984afe5544840505494298f54c33e",
9
+ "locked": false,
10
+ "meta": {
11
+ "auto_added": false
12
+ },
13
+ "modified_on": "2016-05-12T11:53:49.233342Z",
14
+ "name": "wadus.example.com",
15
+ "proxiable": true,
16
+ "proxied": false,
17
+ "ttl": 1,
18
+ "type": "CNAME",
19
+ "zone_id": "9de4eb694c380d79845d35cd939cc7a7",
20
+ "zone_name": "example.com"
21
+ },
22
+ {
23
+ "content": "another_destination.com",
24
+ "created_on": "2016-05-12T11:53:49.233342Z",
25
+ "id": "b3142498230989gsd0f88h80998908fc",
26
+ "locked": false,
27
+ "meta": {
28
+ "auto_added": false
29
+ },
30
+ "modified_on": "2016-05-12T11:53:49.233342Z",
31
+ "name": "wadus.example.com",
32
+ "proxiable": true,
33
+ "proxied": false,
34
+ "ttl": 1,
35
+ "type": "CNAME",
36
+ "zone_id": "9de4eb694c380d79845d35cd939cc7a7",
37
+ "zone_name": "example.com"
38
+ }
39
+ ],
40
+ "result_info": {
41
+ "count": 2,
42
+ "page": 1,
43
+ "per_page": 20,
44
+ "total_count": 1,
45
+ "total_pages": 1
46
+ },
47
+ "success": true
48
+ }
@@ -0,0 +1,56 @@
1
+ require 'glare'
2
+
3
+ RSpec.describe 'Resolve domain' do
4
+ context 'when a domain is registered' do
5
+ let(:domain) { 'cname.flywire.cc' }
6
+ let(:destination) { ['peertransfer.me'] }
7
+ let(:type) { 'CNAME' }
8
+ before do
9
+ register_domain(domain, destination)
10
+ end
11
+
12
+ it 'resolves to right destination' do
13
+ expect(resolve(domain)).to eq(destination)
14
+ end
15
+ end
16
+
17
+ context 'when a domain contains more than one destination' do
18
+ let(:domain) { 'a.flywire.cc' }
19
+ let(:type) { 'A' }
20
+ before do
21
+ register_domain(domain, destination)
22
+ end
23
+
24
+ context 'two new records' do
25
+ let(:destination) { ['1.2.3.4', '5.6.7.8'] }
26
+
27
+ it 'resolves to right destination' do
28
+ expect(resolve(domain)).to eq(destination)
29
+ end
30
+ end
31
+
32
+ context 'deletes one record' do
33
+ let(:destination) { ['1.2.3.9'] }
34
+
35
+ it 'resolves to right destination' do
36
+ expect(resolve(domain)).to eq(destination)
37
+ end
38
+ end
39
+
40
+ context 'adds one record' do
41
+ let(:destination) { ['1.2.3.5', '6.7.8.9'] }
42
+
43
+ it 'resolves to right destination' do
44
+ expect(resolve(domain)).to eq(destination)
45
+ end
46
+ end
47
+ end
48
+
49
+ def register_domain(domain, destination)
50
+ Glare.register(domain, destination, type)
51
+ end
52
+
53
+ def resolve(domain)
54
+ Glare.resolve(domain, type)
55
+ end
56
+ end
@@ -0,0 +1,97 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
7
+ # this file to always be loaded, without a need to explicitly require it in any
8
+ # files.
9
+ #
10
+ # Given that it is always loaded, you are encouraged to keep this file as
11
+ # light-weight as possible. Requiring heavyweight dependencies from this file
12
+ # will add to the boot time of your test suite on EVERY test run, even for an
13
+ # individual file that may not need all of that loaded. Instead, consider making
14
+ # a separate helper file that requires the additional dependencies and performs
15
+ # the additional setup, and require it from the spec files that actually need
16
+ # it.
17
+ #
18
+ # The `.rspec` file also contains a few flags that are not defaults but that
19
+ # users commonly want.
20
+ #
21
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
22
+ RSpec.configure do |config|
23
+ # rspec-expectations config goes here. You can use an alternate
24
+ # assertion/expectation library such as wrong or the stdlib/minitest
25
+ # assertions if you prefer.
26
+ config.expect_with :rspec do |expectations|
27
+ # This option will default to `true` in RSpec 4. It makes the `description`
28
+ # and `failure_message` of custom matchers include text for helper methods
29
+ # defined using `chain`, e.g.:
30
+ # be_bigger_than(2).and_smaller_than(4).description
31
+ # # => "be bigger than 2 and smaller than 4"
32
+ # ...rather than:
33
+ # # => "be bigger than 2"
34
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
+ end
36
+
37
+ # rspec-mocks config goes here. You can use an alternate test double
38
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
39
+ config.mock_with :rspec do |mocks|
40
+ # Prevents you from mocking or stubbing a method that does not exist on
41
+ # a real object. This is generally recommended, and will default to
42
+ # `true` in RSpec 4.
43
+ mocks.verify_partial_doubles = true
44
+ end
45
+
46
+ # The settings below are suggested to provide a good initial experience
47
+ # with RSpec, but feel free to customize to your heart's content.
48
+ # These two settings work together to allow you to limit a spec run
49
+ # to individual examples or groups you care about by tagging them with
50
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
51
+ # get run.
52
+ config.filter_run :focus
53
+ config.run_all_when_everything_filtered = true
54
+
55
+ # Allows RSpec to persist some state between runs in order to support
56
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
57
+ # you configure your source control system to ignore this file.
58
+ config.example_status_persistence_file_path = 'spec/examples.txt'
59
+
60
+ # Limits the available syntax to the non-monkey patched syntax that is
61
+ # recommended. For more details, see:
62
+ # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
63
+ # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
64
+ # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
65
+ config.disable_monkey_patching!
66
+
67
+ # This setting enables warnings. It's recommended, but in some cases may
68
+ # be too noisy due to issues in dependencies.
69
+ # config.warnings = true
70
+
71
+ # Many RSpec users commonly either run the entire suite or an individual
72
+ # file, and it's useful to allow more verbose output when running an
73
+ # individual spec file.
74
+ if config.files_to_run.one?
75
+ # Use the documentation formatter for detailed output,
76
+ # unless a formatter has already been configured
77
+ # (e.g. via a command-line flag).
78
+ config.default_formatter = 'doc'
79
+ end
80
+
81
+ # Print the 10 slowest examples and example groups at the
82
+ # end of the spec run, to help surface which specs are running
83
+ # particularly slow.
84
+ config.profile_examples = 10
85
+
86
+ # Run specs in random order to surface order dependencies. If you find an
87
+ # order dependency and want to debug it, you can fix the order by providing
88
+ # the seed, which is printed after each run.
89
+ # --seed 1234
90
+ config.order = :random
91
+
92
+ # Seed global randomization in this process using the `--seed` CLI option.
93
+ # Setting this allows you to use `--seed` to deterministically reproduce
94
+ # test failures related to randomization by passing the same `--seed` value
95
+ # as the one that triggered the failure.
96
+ Kernel.srand config.seed
97
+ end
@@ -0,0 +1,229 @@
1
+ require 'glare'
2
+
3
+ RSpec.describe Glare do
4
+ before do
5
+ allow(ENV).to receive(:[]).with('CF_EMAIL').and_return('an_email')
6
+ allow(ENV).to receive(:[]).with('CF_AUTH_KEY').and_return('an_auth_key')
7
+
8
+ allow(Glare::Client).to receive(:new).and_return(client)
9
+ end
10
+ let(:client) { spy(Glare::Client) }
11
+ let(:zone_list) { load_fixture('list_zone') }
12
+ let(:empty_result) { load_fixture('empty_result') }
13
+ let(:wadus_records) { load_fixture('wadus_records') }
14
+
15
+ describe '.resolve' do
16
+ it 'resolves a fqdn' do
17
+ allow(client).to receive(:get).
18
+ with('/zones', name: 'example.com').
19
+ and_return(zone_list)
20
+
21
+ allow(client).to receive(:get).with(
22
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
23
+ name: 'wadus.example.com', type: 'CNAME'
24
+ ).and_return(wadus_records)
25
+
26
+ destination = Glare.resolve('wadus.example.com', 'CNAME')
27
+ expect(destination).to eq(['destination.com', 'another_destination.com'])
28
+ end
29
+ end
30
+
31
+ describe '.register' do
32
+ before do
33
+ allow(client).to receive(:get).
34
+ with('/zones', name: 'example.com').
35
+ and_return(zone_list)
36
+
37
+ allow(client).to receive(:get).with(
38
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
39
+ name: 'example.com', type: 'CNAME'
40
+ ).and_return(empty_result)
41
+ end
42
+
43
+ it 'uses default credentials' do
44
+ Glare.register('example.com', :a_destination, 'CNAME')
45
+
46
+ expect(Glare::Client).to have_received(:new).with('an_email', 'an_auth_key')
47
+ end
48
+
49
+ it 'uses the registration endpoint' do
50
+ Glare.register('example.com', :a_destination, 'CNAME')
51
+
52
+ expect(client).to have_received(:post) do |*args|
53
+ expect(args.first).to match(%r{/zones/.*/dns_records})
54
+ end
55
+ end
56
+
57
+ it 'retrieves zone id for a given domain name' do
58
+ Glare.register('example.com', :a_destination, 'CNAME')
59
+
60
+ expect(client).to have_received(:get).
61
+ with('/zones', name: 'example.com')
62
+
63
+ expect(client).to have_received(:post) do |*args|
64
+ expect(args.first).to eq('/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records')
65
+ end
66
+ end
67
+
68
+ it 'retrieves record to check if exists' do
69
+ Glare.register('example.com', :a_destination, 'CNAME')
70
+
71
+ expect(client).to have_received(:get).with(
72
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
73
+ name: 'example.com', type: 'CNAME'
74
+ )
75
+ end
76
+
77
+ it 'sends registration data to creation endpoint when record does not exist' do
78
+ allow(client).to receive(:get).with(
79
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
80
+ name: 'not-exist.example.com', type: 'CNAME'
81
+ ).and_return(empty_result)
82
+
83
+ Glare.register('not-exist.example.com', :a_destination, 'CNAME')
84
+
85
+ expect(client).not_to have_received(:put).
86
+ with('/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records', any_args)
87
+
88
+ expect(client).to have_received(:post).with(
89
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
90
+ type: 'CNAME', name: 'not-exist.example.com', content: :a_destination
91
+ )
92
+ end
93
+
94
+ it 'sends registration data to creation endpoint when record does not exist with multiple records' do
95
+ allow(client).to receive(:get).with(
96
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
97
+ name: 'not-exist.example.com', type: 'CNAME'
98
+ ).and_return(empty_result)
99
+
100
+ Glare.register('not-exist.example.com', [:a_destination, :another_destination], 'CNAME')
101
+
102
+ expect(client).not_to have_received(:put).
103
+ with('/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records', any_args)
104
+
105
+ expect(client).to have_received(:post).with(
106
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
107
+ type: 'CNAME', name: 'not-exist.example.com', content: :a_destination
108
+ )
109
+
110
+ expect(client).to have_received(:post).with(
111
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
112
+ type: 'CNAME', name: 'not-exist.example.com', content: :another_destination
113
+ )
114
+ end
115
+
116
+ context 'when records exist' do
117
+ before do
118
+ allow(client).to receive(:get).with(
119
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
120
+ name: 'wadus.example.com', type: 'CNAME'
121
+ ).and_return(wadus_records)
122
+ end
123
+
124
+ context 'same number of records to update' do
125
+ context 'records contents are different' do
126
+ it 'sends registration data to update endpoint' do
127
+ Glare.register('wadus.example.com', ['a_destination.com', 'yet_another_destination.com'], 'CNAME')
128
+
129
+ expect(client).not_to have_received(:post).
130
+ with('/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records', any_args)
131
+
132
+ expect(client).to have_received(:put).with(
133
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/a1f984afe5544840505494298f54c33e',
134
+ type: 'CNAME', name: 'wadus.example.com', content: 'a_destination.com'
135
+ )
136
+
137
+ expect(client).to have_received(:put).with(
138
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/b3142498230989gsd0f88h80998908fc',
139
+ type: 'CNAME', name: 'wadus.example.com', content: 'yet_another_destination.com'
140
+ )
141
+ end
142
+ end
143
+
144
+ context 'records contents are the same' do
145
+ it 'sends registration data to update endpoint' do
146
+ Glare.register('wadus.example.com', ['destination.com', 'another_destination.com'], 'CNAME')
147
+
148
+ expect(client).not_to have_received(:post).
149
+ with('/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records', any_args)
150
+
151
+ expect(client).not_to have_received(:put).with(
152
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/a1f984afe5544840505494298f54c33e',
153
+ any_args
154
+ )
155
+
156
+ expect(client).not_to have_received(:put).with(
157
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/b3142498230989gsd0f88h80998908fc',
158
+ any_args
159
+ )
160
+ end
161
+ end
162
+ end
163
+
164
+ it 'updates different records and deletes extra ones' do
165
+ Glare.register('wadus.example.com', ['a_destination.com'], 'CNAME')
166
+
167
+ expect(client).not_to have_received(:post).
168
+ with('/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records', any_args)
169
+
170
+ expect(client).to have_received(:put).with(
171
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/a1f984afe5544840505494298f54c33e',
172
+ any_args
173
+ )
174
+
175
+ expect(client).to have_received(:delete).with(
176
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/b3142498230989gsd0f88h80998908fc'
177
+ )
178
+ end
179
+
180
+ it 'updates different records and creates new ones' do
181
+ Glare.register('wadus.example.com', ['destination.com', 'another_destination.com', 'a_third_destination.com'], 'CNAME')
182
+
183
+ expect(client).not_to have_received(:put).with(
184
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/a1f984afe5544840505494298f54c33e',
185
+ any_args
186
+ )
187
+
188
+ expect(client).not_to have_received(:put).with(
189
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/b3142498230989gsd0f88h80998908fc',
190
+ any_args
191
+ )
192
+
193
+ expect(client).to have_received(:post).with(
194
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
195
+ type: 'CNAME', name: 'wadus.example.com', content: 'a_third_destination.com'
196
+ )
197
+ end
198
+ end
199
+ end
200
+
201
+ describe '.delete' do
202
+ it 'deletes all records for a fqdn' do
203
+ allow(client).to receive(:get).
204
+ with('/zones', name: 'example.com').
205
+ and_return(zone_list)
206
+
207
+ allow(client).to receive(:get).with(
208
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records',
209
+ name: 'wadus.example.com', type: 'CNAME'
210
+ ).and_return(wadus_records)
211
+
212
+ Glare.deregister('wadus.example.com', 'CNAME')
213
+
214
+ expect(client).to have_received(:delete).with(
215
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/a1f984afe5544840505494298f54c33e'
216
+ )
217
+
218
+ expect(client).to have_received(:delete).with(
219
+ '/zones/9de4eb694c380d79845d35cd939cc7a7/dns_records/b3142498230989gsd0f88h80998908fc'
220
+ )
221
+ end
222
+ end
223
+
224
+ def load_fixture(fixture)
225
+ fixture_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures'))
226
+ json = IO.read(File.join(fixture_dir, "#{fixture}.json"))
227
+ ::HTTP::Message.new_response(JSON.parse(json))
228
+ end
229
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: glare
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jose Luis Salas
8
+ - Omar Lopez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2016-06-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.9'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.9'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '11.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '11.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.4'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.4'
56
+ - !ruby/object:Gem::Dependency
57
+ name: public_suffix
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: httpclient
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :runtime
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description:
85
+ email:
86
+ - josacar@users.noreply.github.com
87
+ - olopez@users.noreply.github.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - LICENSE
96
+ - README.md
97
+ - glare.gemspec
98
+ - lib/glare.rb
99
+ - lib/glare/client.rb
100
+ - lib/glare/credentials.rb
101
+ - lib/glare/dns_record.rb
102
+ - lib/glare/dns_records.rb
103
+ - lib/glare/domain.rb
104
+ - lib/glare/result.rb
105
+ - lib/glare/version.rb
106
+ - spec/delete_domain_spec.rb
107
+ - spec/fixtures/empty_result.json
108
+ - spec/fixtures/list_zone.json
109
+ - spec/fixtures/wadus_records.json
110
+ - spec/resolve_domain_spec.rb
111
+ - spec/spec_helper.rb
112
+ - spec/units/cf_spec.rb
113
+ homepage: https://github.com/peertransfer/cf
114
+ licenses:
115
+ - MIT
116
+ metadata: {}
117
+ post_install_message:
118
+ rdoc_options: []
119
+ require_paths:
120
+ - lib
121
+ required_ruby_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ requirements: []
132
+ rubyforge_project:
133
+ rubygems_version: 2.4.5
134
+ signing_key:
135
+ specification_version: 4
136
+ summary: API client for CloudFlare v4 API
137
+ test_files:
138
+ - spec/delete_domain_spec.rb
139
+ - spec/resolve_domain_spec.rb
140
+ - spec/spec_helper.rb
141
+ - spec/units/cf_spec.rb
142
+ - spec/fixtures/empty_result.json
143
+ - spec/fixtures/list_zone.json
144
+ - spec/fixtures/wadus_records.json