From e7229c593f7a4111ef965f548e6d091fcb5e5bea Mon Sep 17 00:00:00 2001 From: Mike Eves Date: Tue, 7 May 2019 14:18:59 +0100 Subject: [PATCH 1/2] Add support for custom_hostnames --- README.md | 1 + lib/cloudflair/api/zone.rb | 1 + lib/cloudflair/api/zone/custom_hostname.rb | 17 ++++ lib/cloudflair/api/zone__custom_hostnames.rb | 29 +++++++ .../api/zone/custom_hostname_spec.rb | 46 +++++++++++ .../api/zone__custom_hostnames_spec.rb | 78 +++++++++++++++++++ .../fixtures/zone/custom_hostname.json | 16 ++++ .../zone/custom_hostname_deleted.json | 10 +++ .../fixtures/zone/custom_hostnames.json | 25 ++++++ spec/spec_helper.rb | 1 + 10 files changed, 224 insertions(+) create mode 100644 lib/cloudflair/api/zone/custom_hostname.rb create mode 100644 lib/cloudflair/api/zone__custom_hostnames.rb create mode 100644 spec/cloudflair/api/zone/custom_hostname_spec.rb create mode 100644 spec/cloudflair/api/zone__custom_hostnames_spec.rb create mode 100644 spec/cloudflair/fixtures/zone/custom_hostname.json create mode 100644 spec/cloudflair/fixtures/zone/custom_hostname_deleted.json create mode 100644 spec/cloudflair/fixtures/zone/custom_hostnames.json diff --git a/README.md b/README.md index d0d080c..7d1a412 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ A good reference on how to use this wrapper are also the Rspecs. * `/zones/:zone_id/available_plans` GET * `/zones/:zone_id/available_plans/:plan_id` GET * `/zones/:zone_id/available_rate_plans` +* `/zones/:zone_id/custom_hostnames` GET, POST, PATCH * `/zones/:zone_id/dns_records` GET, POST * `/zones/:zone_id/dns_records/:record_id` GET, DELETE (PUT not implemented) * `/zones/:zone_id/purge_cache` POST diff --git a/lib/cloudflair/api/zone.rb b/lib/cloudflair/api/zone.rb index 22bb227..f335337 100644 --- a/lib/cloudflair/api/zone.rb +++ b/lib/cloudflair/api/zone.rb @@ -12,6 +12,7 @@ class Zone require 'cloudflair/api/zone__dns_records' require 'cloudflair/api/zone__available_plans' require 'cloudflair/api/zone__railguns' + require 'cloudflair/api/zone__custom_hostnames' attr_reader :zone_id diff --git a/lib/cloudflair/api/zone/custom_hostname.rb b/lib/cloudflair/api/zone/custom_hostname.rb new file mode 100644 index 0000000..d1888e5 --- /dev/null +++ b/lib/cloudflair/api/zone/custom_hostname.rb @@ -0,0 +1,17 @@ +require 'cloudflair/entity' + +module Cloudflair + class CustomHostname + include Cloudflair::Entity + + attr_reader :zone_id, :custom_hostname_id + deletable true + patchable_fields :ssl, :custom_origin_server + path 'zones/:zone_id/custom_hostnames/:custom_hostname_id' + + def initialize(zone_id, custom_hostname_id) + @zone_id = zone_id + @custom_hostname_id = custom_hostname_id + end + end +end diff --git a/lib/cloudflair/api/zone__custom_hostnames.rb b/lib/cloudflair/api/zone__custom_hostnames.rb new file mode 100644 index 0000000..08f1a29 --- /dev/null +++ b/lib/cloudflair/api/zone__custom_hostnames.rb @@ -0,0 +1,29 @@ +require 'cloudflair/api/zone/custom_hostname' + +module Cloudflair + class Zone + def custom_hostnames(filter = {}) + raw_hostnames = response connection.get("#{path}/custom_hostnames", filter) + + raw_hostnames.map { |raw_hostname| build_custom_hostname(raw_hostname) } + end + + def custom_hostname(custom_hostname_id) + Cloudflair::CustomHostname.new zone_id, custom_hostname_id + end + + def new_custom_hostname(hostname_data) + raw_hostname = response connection.post("#{path}/custom_hostnames", hostname_data) + + build_custom_hostname raw_hostname + end + + private + + def build_custom_hostname(raw_hostname) + hostname = custom_hostname raw_hostname['id'] + hostname.data = raw_hostname + hostname + end + end +end diff --git a/spec/cloudflair/api/zone/custom_hostname_spec.rb b/spec/cloudflair/api/zone/custom_hostname_spec.rb new file mode 100644 index 0000000..0d9b98e --- /dev/null +++ b/spec/cloudflair/api/zone/custom_hostname_spec.rb @@ -0,0 +1,46 @@ +require 'spec_helper' + +describe Cloudflair::CustomHostname do + let(:zone_identifier) { '023e105f4ecef8ad9ca31a8372d0c353' } + let(:hostname_identifier) { '0d89c70d-ad9f-4843-b99f-6cc0252067e9' } + let(:response_json) { File.read('spec/cloudflair/fixtures/zone/custom_hostname.json') } + let(:url) { "/client/v4/zones/#{zone_identifier}/custom_hostnames/#{hostname_identifier}" } + subject { Cloudflair.zone(zone_identifier).custom_hostname(hostname_identifier) } + + before do + faraday_stubs.get(url) do |_env| + [200, { content_type: 'application/json' }, response_json] + end + allow(Faraday).to receive(:new).and_return faraday + end + + it 'loads the data on demand and caches' do + expect(faraday).to receive(:get).once.and_call_original + + expect(subject.id).to eq hostname_identifier + expect(subject.hostname).to eq 'app.example.com' + end + + it 'reloads the AvailablePlan on request' do + expect(faraday).to receive(:get).twice.and_call_original + + expect(subject.reload).to be subject + expect(subject.reload).to be subject + end + + describe '#delete' do + let(:response_json) { File.read('spec/cloudflair/fixtures/zone/custom_hostname_deleted.json') } + + before do + faraday_stubs.delete(url) do |_env| + [200, { content_type: 'application/json' }, response_json] + end + end + + it 'performs the delete' do + expect(faraday).to receive(:delete).once.and_call_original + + subject.delete + end + end +end diff --git a/spec/cloudflair/api/zone__custom_hostnames_spec.rb b/spec/cloudflair/api/zone__custom_hostnames_spec.rb new file mode 100644 index 0000000..ef15cc6 --- /dev/null +++ b/spec/cloudflair/api/zone__custom_hostnames_spec.rb @@ -0,0 +1,78 @@ +require 'spec_helper' + +describe Cloudflair::Zone, 'custom_hostname things' do + before do + allow(Faraday).to receive(:new).and_return faraday + end + + let(:zone_identifier) { '023e105f4ecef8ad9ca31a8372d0c353' } + let(:url) { "/client/v4/zones/#{zone_identifier}/custom_hostnames" } + subject(:zone) { Cloudflair.zone zone_identifier } + + describe '#custom_hostname' do + it 'returns a Custom Hostname instance' do + expect(subject.custom_hostname('abcdef')).to be_a Cloudflair::CustomHostname + end + end + + describe '#new_custom_hostname=' do + let(:new_custom_hostname_data) { { hostname: 'app.example.com', ssl: {method: 'http', type: 'dv', settings: {http2: 'on', min_tls_version: '1.2', tls_1_3: 'on', ciphers: ['ECDHE-RSA-AES128-GCM-SHA256', 'AES128-SHA']}}} } + let(:response_json) { File.read('spec/cloudflair/fixtures/zone/custom_hostname.json') } + let(:the_new_custom_hostname) { subject.new_custom_hostname(new_custom_hostname_data) } + + before do + faraday_stubs.post(url, new_custom_hostname_data) do |_env| + [200, { content_type: 'application/json' }, response_json] + end + end + + it 'returns a new CustomHostname instance' do + expect(faraday).to receive(:post).and_call_original + + expect(the_new_custom_hostname).to be_a Cloudflair::CustomHostname + end + + it 'prepopulates the returned CustomHostname instance' do + expect(faraday).to_not receive(:get) + + expect(the_new_custom_hostname.id).to eq('0d89c70d-ad9f-4843-b99f-6cc0252067e9') + expect(the_new_custom_hostname.hostname).to eq('app.example.com') + expect(the_new_custom_hostname.ssl['type']).to eq('dv') + end + end + + describe '#custom_hostnames' do + let(:response_json) { File.read('spec/cloudflair/fixtures/zone/custom_hostnames.json') } + subject { zone.custom_hostnames } + + before do + faraday_stubs.get(url) do |_env| + [200, { content_type: 'application/json' }, response_json] + end + end + + it 'calls the other url' do + expect(faraday).to receive(:get).once.and_call_original + + subject + end + + it 'returns the correct types' do + expect(subject).to be_a Array + subject.each do |plan| + expect(plan).to be_a Cloudflair::CustomHostname + end + end + + it 'returns the correct amount' do + expect(subject.length).to be 1 + end + + it 'returns the correct values' do + custom_hostname = subject[0] + expect(custom_hostname.id).to eq '0d89c70d-ad9f-4843-b99f-6cc0252067e9' + expect(custom_hostname.hostname).to eq 'app.example.com' + expect(custom_hostname.ssl['type']).to eq 'dv' + end + end +end diff --git a/spec/cloudflair/fixtures/zone/custom_hostname.json b/spec/cloudflair/fixtures/zone/custom_hostname.json new file mode 100644 index 0000000..6f6446e --- /dev/null +++ b/spec/cloudflair/fixtures/zone/custom_hostname.json @@ -0,0 +1,16 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": { + "id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9", + "hostname": "app.example.com", + "ssl": { + "status": "pending_validation", + "method": "http", + "type": "dv", + "cname_target": "dcv.digicert.com", + "cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com" + } + } +} diff --git a/spec/cloudflair/fixtures/zone/custom_hostname_deleted.json b/spec/cloudflair/fixtures/zone/custom_hostname_deleted.json new file mode 100644 index 0000000..3827358 --- /dev/null +++ b/spec/cloudflair/fixtures/zone/custom_hostname_deleted.json @@ -0,0 +1,10 @@ +{ + "result": { + "id": "8a5577f0-ed62-4716-8c1f-1ae34a7f7cdb", + "ssl": null, + "created_at": "2019-05-03T16:51:01.146758Z" + }, + "success": true, + "errors": [], + "messages": [] +} diff --git a/spec/cloudflair/fixtures/zone/custom_hostnames.json b/spec/cloudflair/fixtures/zone/custom_hostnames.json new file mode 100644 index 0000000..ad65570 --- /dev/null +++ b/spec/cloudflair/fixtures/zone/custom_hostnames.json @@ -0,0 +1,25 @@ +{ + "success": true, + "errors": [], + "messages": [], + "result": [ + { + "id": "0d89c70d-ad9f-4843-b99f-6cc0252067e9", + "hostname": "app.example.com", + "ssl": { + "status": "pending_validation", + "method": "http", + "type": "dv", + "cname_target": "dcv.digicert.com", + "cname": "810b7d5f01154524b961ba0cd578acc2.app.example.com" + } + } + ], + "result_info": { + "page": 1, + "per_page": 20, + "count": 1, + "total_count": 2000 + } +} + diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ae90092..6090925 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -22,6 +22,7 @@ require 'cloudflair/api/zone/analytics' require 'cloudflair/api/zone/available_plan' require 'cloudflair/api/zone/available_rate_plan' +require 'cloudflair/api/zone/custom_hostname' require 'cloudflair/api/zone/dns_record' require 'cloudflair/api/zone/purge_cache' require 'cloudflair/api/zone/railgun' From bc3abc363813433db504ea4b5ddfebc716232950 Mon Sep 17 00:00:00 2001 From: Mike Eves Date: Fri, 17 May 2019 15:23:34 +0100 Subject: [PATCH 2/2] Add custom_metadata as patchable field on custom hostname endpoint --- lib/cloudflair/api/zone/custom_hostname.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cloudflair/api/zone/custom_hostname.rb b/lib/cloudflair/api/zone/custom_hostname.rb index d1888e5..d554de8 100644 --- a/lib/cloudflair/api/zone/custom_hostname.rb +++ b/lib/cloudflair/api/zone/custom_hostname.rb @@ -6,7 +6,7 @@ class CustomHostname attr_reader :zone_id, :custom_hostname_id deletable true - patchable_fields :ssl, :custom_origin_server + patchable_fields :ssl, :custom_origin_server, :custom_metadata path 'zones/:zone_id/custom_hostnames/:custom_hostname_id' def initialize(zone_id, custom_hostname_id)