Skip to content

Commit

Permalink
Merge 5ff1889 into e6800fd
Browse files Browse the repository at this point in the history
  • Loading branch information
rdubya committed Mar 12, 2019
2 parents e6800fd + 5ff1889 commit da2e4b1
Show file tree
Hide file tree
Showing 15 changed files with 635 additions and 97 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ matrix:

env:
global:
CLOUDFLARE_TEST_ZONE_MANAGEMENT: 'true'
secure: c5yG7N1r9nYuw47Y90jGeoHNvkL59AAC55dtPAhKP+gjXWW8hKbntj3oj+lVkxEqzRpzEQgYZzUElrP+6mJnb20ldclZg03L56243tMtVEcpGOx/MFhnIBkx3kKu1H6ydKKMxieHxjsLQ3vnpcIZ3p0skTQjYbjdu607tjbyg7s=
21 changes: 7 additions & 14 deletions lib/cloudflare/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,17 @@
module Cloudflare
class Account < Representation
end

class Accounts < Representation
include Paginate

def represent(metadata, attributes)
resource = @resource.with(path: attributes[:id])

return Account.new(resource, metadata: metadata, value: attributes)

def representation
Account
end

def create(name)
response = self.post(name: name)

return represent(response.headers, response.read)
end

def find_by_id(id)
Zone.new(@resource.with(path: id))
represent_message(self.post(name: name))
end

end
end
68 changes: 68 additions & 0 deletions lib/cloudflare/custom_hostname/ssl_attribute.rb
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
76 changes: 76 additions & 0 deletions lib/cloudflare/custom_hostname/ssl_attribute/settings.rb
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
84 changes: 84 additions & 0 deletions lib/cloudflare/custom_hostnames.rb
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
27 changes: 9 additions & 18 deletions lib/cloudflare/dns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,52 +40,43 @@ def update_content(content)
name: @record[:name],
content: content
)

@value = response.result
end

def type
value[:type]
end

def name
value[:name]
end

def content
value[:content]
end

def to_s
"#{@record[:name]} #{@record[:type]} #{@record[:content]}"
end
end

class Records < Representation
include Paginate

def representation
Record
end

TTL_AUTO = 1

def create(type, name, content, **options)
message = self.post(type: type, name: name, content: content, **options)

id = message.result[:id]
resource = @resource.with(path: id)

return representation.new(resource, metadata: message.headers, value: message.result)
represent_message(self.post(type: type, name: name, content: content, **options))
end

def find_by_name(name)
each(name: name).first
end

def find_by_id(id)
Record.new(@resource.with(path: id))
end
end
end
end
25 changes: 9 additions & 16 deletions lib/cloudflare/firewall.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ class Rule < Representation
def mode
value[:mode]
end

def notes
value[:notes]
end

def configuration
value[:configuration]
end

def to_s
"#{configuration[:value]} - #{mode} - #{notes}"
end
end

class Rules < Representation
include Paginate

def representation
Rule
end

def set(mode, value, notes: nil, target: 'ip')
notes ||= "cloudflare gem [#{mode}] #{Time.now.strftime('%m/%d/%y')}"

message = self.post({
mode: mode.to_s,
notes: notes,
Expand All @@ -61,17 +61,10 @@ def set(mode, value, notes: nil, target: 'ip')
value: value.to_s,
}
})

id = message.result[:id]
resource = @resource.with(path: id)

return representation.new(resource, metadata: message.headers, value: message.result)
end

def find_by_id(id)
Rule.new(@resource.with(path: id))

represent_message(message)
end

def each_by_value(value, &block)
each(configuration_value: value, &block)
end
Expand Down

0 comments on commit da2e4b1

Please sign in to comment.