Class: CloudflareClient::Zone::CustomHostname

Inherits:
Base show all
Defined in:
lib/cloudflare_client/zone/custom_hostname.rb

Overview

Direct Known Subclasses

CustomHostnameV2

Constant Summary collapse

VALID_METHODS =
%w[http email cname].freeze
VALID_TYPES =
['read only', 'dv'].freeze
VALID_ORDERS =
%w[ssl ssl_status].freeze
DEFAULT_SSL_PROPERTIES =
{ method: 'http', type: 'dv' }.freeze

Constants inherited from CloudflareClient::Zone

VALID_ZONE_STATUSES

Constants inherited from CloudflareClient

API_BASE, POSSIBLE_API_SETTINGS, VALID_BUNDLE_METHODS, VALID_DIRECTIONS, VALID_MATCHES, VERSION

Instance Attribute Summary

Attributes inherited from Base

#zone_id

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods inherited from CloudflareClient::Zone

#create_zone, #delete_zone, #edit_zone, #purge_zone_cache, #update_zone_settings, #zone, #zone_activation_check, #zone_setting, #zone_settings, #zones

Methods inherited from CloudflareClient

#initialize

Constructor Details

This class inherits a constructor from CloudflareClient::Zone::Base

Instance Method Details

#create(hostname:, ssl: DEFAULT_SSL_PROPERTIES, custom_metadata: {}, custom_origin_server: nil) ⇒ Object

create custom_hostname

  • :custom_metadata may only work for enterprise or better customers

  • :ssl has undocumented properties: ‘custom_certificate’ and ‘custom_key’, or can be nulled



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cloudflare_client/zone/custom_hostname.rb', line 12

def create(hostname:, ssl: DEFAULT_SSL_PROPERTIES, custom_metadata: {}, custom_origin_server: nil)
  #FIXME: implement checks for the custom_metedata/find out of it's going to be exposed to anyone else
  #"custom_metadata":{"hsts_enabled":"true"}
  #"custom_metadata":{"hsts_enabled":"true","custom_maxage":value}
  id_check('hostname', hostname)

  if ssl && ssl[:method] && ssl[:type]
    valid_value_check(:method, ssl[:method], VALID_METHODS)
    valid_value_check(:type,   ssl[:type],   VALID_TYPES)
  end

  data                   = { hostname: hostname, ssl: ssl }
  data[:custom_origin_server] = custom_origin_server unless custom_origin_server.nil?
  data[:custom_metadata] =  unless .empty?

  cf_post(path: "/zones/#{zone_id}/custom_hostnames", data: data)
end

#delete(id:) ⇒ Object

delete a custom hostname and ssl certs



84
85
86
87
88
# File 'lib/cloudflare_client/zone/custom_hostname.rb', line 84

def delete(id:)
  id_check('id', id)

  cf_delete(path: "/zones/#{zone_id}/custom_hostnames/#{id}")
end

#list(hostname: nil, id: nil, page: 1, per_page: 50, order: 'ssl', direction: 'desc', ssl: 0) ⇒ Object

list custom_hostnames



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cloudflare_client/zone/custom_hostname.rb', line 32

def list(hostname: nil, id: nil, page: 1, per_page: 50, order: 'ssl', direction: 'desc', ssl: 0)
  raise 'cannot use both hostname and id' if hostname && id
  valid_value_check(:order, order, VALID_ORDERS)
  valid_value_check(:direction, direction, VALID_DIRECTIONS)
  valid_value_check(:ssl, ssl, [0, 1])

  params            = {page: page, per_page: per_page, order: order, direction: direction, ssl: ssl}
  params[:hostname] = hostname if hostname
  params[:id]       = id if id

  cf_get(path: "/zones/#{zone_id}/custom_hostnames", params: params)
end

#show(id:) ⇒ Object

details of a custom hostname



47
48
49
50
51
# File 'lib/cloudflare_client/zone/custom_hostname.rb', line 47

def show(id:)
  id_check('id', id)

  cf_get(path: "/zones/#{zone_id}/custom_hostnames/#{id}")
end

#update(id:, ssl: {}, custom_metadata: nil, custom_origin_server: nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/cloudflare_client/zone/custom_hostname.rb', line 56

def update(id:, ssl: {}, custom_metadata: nil, custom_origin_server: nil)
  id_check('id', id)

  data = {}

  if ssl && ssl[:method] && ssl[:type]
    valid_value_check(:method, ssl[:method], VALID_METHODS)
    valid_value_check(:type,   ssl[:type],   VALID_TYPES)
  end

  # Setting this to "null" requests removal of the attached certificate. We're
  # using {} as the default value to denote "don't alter the SSL".
  data[:ssl] = ssl unless ssl == {}

  unless .nil?
    raise 'custom_metadata must be an object' unless .is_a?(Hash)
    data[:custom_metadata] = 
  end

  unless custom_origin_server.nil?
    data[:custom_origin_server] = custom_origin_server
  end

  cf_patch(path: "/zones/#{zone_id}/custom_hostnames/#{id}", data: data)
end