-
-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
635 additions
and
97 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,68 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative './ssl_attribute/settings' | ||
|
||
module Cloudflare | ||
|
||
class CustomHostname < Representation | ||
|
||
class SSLAttribute | ||
|
||
def initialize(params) | ||
@params = params | ||
end | ||
|
||
def active? | ||
status == 'active' | ||
end | ||
|
||
def cname | ||
@params[:cname] | ||
end | ||
|
||
def cname_target | ||
@params[:cname_target] | ||
end | ||
|
||
def http_body | ||
@params[:http_body] | ||
end | ||
|
||
def http_url | ||
@params[:http_url] | ||
end | ||
|
||
def method | ||
@params[:method] | ||
end | ||
|
||
def pending_validation? | ||
status == 'pending_validation' | ||
end | ||
|
||
# Wraps the settings hash if it exists or initializes the settings hash and then wraps it | ||
def settings | ||
@settings ||= Settings.new(@params[:settings] ||= {}) | ||
end | ||
|
||
def status | ||
@params[:status] | ||
end | ||
|
||
def to_h | ||
@params | ||
end | ||
|
||
def type | ||
@params[:type] | ||
end | ||
|
||
def validation_errors | ||
@params[:validation_errors] | ||
end | ||
|
||
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,76 @@ | ||
# frozen_string_literal: true | ||
|
||
module Cloudflare | ||
|
||
class CustomHostname < Representation | ||
|
||
class SSLAttribute | ||
|
||
class Settings | ||
|
||
def initialize(settings) | ||
@settings = settings | ||
end | ||
|
||
def ciphers | ||
@settings[:ciphers] | ||
end | ||
|
||
def ciphers=(value) | ||
@settings[:ciphers] = value | ||
end | ||
|
||
# This will return the raw value, it is needed because | ||
# if a value is nil we can't assume that it means it is off | ||
def http2 | ||
@settings[:http2] | ||
end | ||
|
||
# Always coerce into a boolean, if the key is not | ||
# provided, this value may not be accurate | ||
def http2? | ||
http2 == 'on' | ||
end | ||
|
||
def http2=(value) | ||
process_boolean(:http2, value) | ||
end | ||
|
||
def min_tls_version | ||
@settings[:min_tls_version] | ||
end | ||
|
||
def min_tls_version=(value) | ||
@settings[:min_tls_version] = value | ||
end | ||
|
||
# This will return the raw value, it is needed because | ||
# if a value is nil we can't assume that it means it is off | ||
def tls_1_3 | ||
@settings[:tls_1_3] | ||
end | ||
|
||
# Always coerce into a boolean, if the key is not | ||
# provided, this value may not be accurate | ||
def tls_1_3? | ||
tls_1_3 == 'on' | ||
end | ||
|
||
def tls_1_3=(value) | ||
process_boolean(:tls_1_3, value) | ||
end | ||
|
||
private | ||
|
||
def process_boolean(key, value) | ||
if value.nil? | ||
@settings.delete(key) | ||
else | ||
@settings[key] = !value || value == 'off' ? 'off' : 'on' | ||
end | ||
end | ||
|
||
end | ||
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,84 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'custom_hostname/ssl_attribute' | ||
require_relative 'paginate' | ||
require_relative 'representation' | ||
|
||
module Cloudflare | ||
|
||
class CustomHostname < Representation | ||
|
||
# Only available if enabled for your zone | ||
def custom_origin | ||
value[:custom_origin_server] | ||
end | ||
|
||
# Only available if enabled for your zone | ||
def custom_metadata | ||
value[:custom_metadata] | ||
end | ||
|
||
def hostname | ||
value[:hostname] | ||
end | ||
|
||
def id | ||
value[:id] | ||
end | ||
|
||
def ssl | ||
@ssl ||= SSLAttribute.new(value[:ssl]) | ||
end | ||
|
||
# Check if the cert has been validated | ||
# passing true will send a request to Cloudflare to try to validate the cert | ||
def ssl_active?(force_update = false) | ||
send_patch(ssl: { method: ssl.method, type: ssl.type }) if force_update && ssl.pending_validation? | ||
ssl.active? | ||
end | ||
|
||
def update_settings(metadata: nil, origin: nil, ssl: nil) | ||
attrs = {} | ||
attrs[:custom_metadata] = metadata if metadata | ||
attrs[:custom_origin_server] = origin if origin | ||
attrs[:ssl] = ssl if ssl | ||
|
||
send_patch(attrs) | ||
end | ||
|
||
alias :to_s :hostname | ||
|
||
private | ||
|
||
def send_patch(data) | ||
response = patch(data) | ||
|
||
@ssl = nil # Kill off our cached version of the ssl object so it will be regenerated from the response | ||
@value = response.result | ||
end | ||
|
||
end | ||
|
||
class CustomHostnames < Representation | ||
include Paginate | ||
|
||
def representation | ||
CustomHostname | ||
end | ||
|
||
# initializes a custom hostname object and yields it for customization before saving | ||
def create(hostname, metadata: nil, origin: nil, ssl: {}, &block) | ||
attrs = { hostname: hostname, ssl: { method: 'http', type: 'dv' }.merge(ssl) } | ||
attrs[:custom_metadata] = metadata if metadata | ||
attrs[:custom_origin_server] = origin if origin | ||
|
||
represent_message(self.post(attrs)) | ||
end | ||
|
||
def find_by_hostname(hostname) | ||
each(hostname: hostname).first | ||
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
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
Oops, something went wrong.