-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Evesy/master
Add support for custom_hostnames
- Loading branch information
Showing
10 changed files
with
224 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, :custom_metadata | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
spec/cloudflair/fixtures/zone/custom_hostname_deleted.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters