cloudflare_client_rb 4.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cloudflare_client.rb +211 -0
  3. data/lib/cloudflare_client/certificate.rb +40 -0
  4. data/lib/cloudflare_client/organization.rb +26 -0
  5. data/lib/cloudflare_client/organization/access_rule.rb +76 -0
  6. data/lib/cloudflare_client/organization/invite.rb +53 -0
  7. data/lib/cloudflare_client/organization/member.rb +37 -0
  8. data/lib/cloudflare_client/organization/railgun.rb +79 -0
  9. data/lib/cloudflare_client/organization/role.rb +19 -0
  10. data/lib/cloudflare_client/railgun.rb +63 -0
  11. data/lib/cloudflare_client/version.rb +3 -0
  12. data/lib/cloudflare_client/virtual_dns_cluster.rb +133 -0
  13. data/lib/cloudflare_client/virtual_dns_cluster/analytic.rb +38 -0
  14. data/lib/cloudflare_client/zone.rb +129 -0
  15. data/lib/cloudflare_client/zone/analytics.rb +56 -0
  16. data/lib/cloudflare_client/zone/base.rb +9 -0
  17. data/lib/cloudflare_client/zone/custom_hostname.rb +86 -0
  18. data/lib/cloudflare_client/zone/custom_page.rb +28 -0
  19. data/lib/cloudflare_client/zone/custom_ssl.rb +62 -0
  20. data/lib/cloudflare_client/zone/dns.rb +66 -0
  21. data/lib/cloudflare_client/zone/firewall.rb +3 -0
  22. data/lib/cloudflare_client/zone/firewall/access_rule.rb +87 -0
  23. data/lib/cloudflare_client/zone/firewall/waf_package.rb +46 -0
  24. data/lib/cloudflare_client/zone/firewall/waf_package/base.rb +9 -0
  25. data/lib/cloudflare_client/zone/firewall/waf_package/rule.rb +46 -0
  26. data/lib/cloudflare_client/zone/firewall/waf_package/rule_group.rb +42 -0
  27. data/lib/cloudflare_client/zone/keyless_ssl.rb +56 -0
  28. data/lib/cloudflare_client/zone/log.rb +51 -0
  29. data/lib/cloudflare_client/zone/page_rule.rb +64 -0
  30. data/lib/cloudflare_client/zone/railgun_connections.rb +43 -0
  31. data/lib/cloudflare_client/zone/rate_limit.rb +73 -0
  32. data/lib/cloudflare_client/zone/ssl.rb +28 -0
  33. data/lib/cloudflare_client/zone/ssl/certificate_pack.rb +32 -0
  34. data/lib/cloudflare_client/zone/subscription.rb +55 -0
  35. metadata +76 -0
@@ -0,0 +1,79 @@
1
+ class CloudflareClient::Organization::Railgun < CloudflareClient::Organization
2
+ ##
3
+ # org railgun
4
+
5
+ ##
6
+ # list railguns
7
+ def create(name:)
8
+ id_check('name', name)
9
+ data = {name: name}
10
+ cf_post(path: "/organizations/#{org_id}/railguns", data: data)
11
+ end
12
+
13
+ ##
14
+ # list railguns
15
+ def list(page: 1, per_page: 50, direction: 'desc')
16
+ params = {}
17
+
18
+ unless direction.nil?
19
+ valid_value_check(:direction, direction, VALID_DIRECTIONS)
20
+ params[:direction] = direction
21
+ end
22
+
23
+ unless page.nil?
24
+ range_check(:page, page, 1)
25
+ params[:page] = page
26
+ end
27
+
28
+ unless per_page.nil?
29
+ range_check(:per_page, per_page, 5, 50)
30
+ params[:per_page] = per_page
31
+ end
32
+
33
+ cf_get(path: "/organizations/#{org_id}/railguns", params: params)
34
+ end
35
+
36
+ ##
37
+ # show railgun details
38
+ def show(id:)
39
+ id_check(:id, id)
40
+
41
+ cf_get(path: "/organizations/#{org_id}/railguns/#{id}")
42
+ end
43
+
44
+ ##
45
+ # get zones connected to a given railgun
46
+ def zones(id:)
47
+ id_check(:id, id)
48
+
49
+ cf_get(path: "/organizations/#{org_id}/railguns/#{id}/zones")
50
+ end
51
+
52
+ ##
53
+ # enable a railgun
54
+ def enable(id:)
55
+ update_enabled(id: id, enabled: true)
56
+ end
57
+
58
+ ##
59
+ # disable a railgun
60
+ def disable(id:)
61
+ update_enabled(id: id, enabled: false)
62
+ end
63
+
64
+ ##
65
+ # delete an org railgun
66
+ def delete(id:)
67
+ id_check(:id, id)
68
+
69
+ cf_delete(path: "/organizations/#{org_id}/railguns/#{id}")
70
+ end
71
+
72
+ private
73
+
74
+ def update_enabled(id:, enabled:)
75
+ id_check(:id, id)
76
+
77
+ cf_patch(path: "/organizations/#{org_id}/railguns/#{id}", data: {enabled: enabled})
78
+ end
79
+ end
@@ -0,0 +1,19 @@
1
+ class CloudflareClient::Organization::Role < CloudflareClient::Organization
2
+ ##
3
+ # org roles
4
+ #
5
+
6
+ ##
7
+ # list all organization roles
8
+ def list
9
+ cf_get(path: "/organizations/#{org_id}/roles")
10
+ end
11
+
12
+ ##
13
+ # get details of an organization role
14
+ def show(id:)
15
+ id_check(:id, id)
16
+
17
+ cf_get(path: "/organizations/#{org_id}/roles/#{id}")
18
+ end
19
+ end
@@ -0,0 +1,63 @@
1
+ class CloudflareClient::Railgun < CloudflareClient
2
+ ##
3
+ # Railgun methods
4
+ def initialize(*args)
5
+ super
6
+ end
7
+
8
+ ##
9
+ # create(name: 'name of railgun')
10
+ def create(name:)
11
+ raise 'Railgun name cannot be nil' if name.nil?
12
+ data = {name: name}
13
+ cf_post(path: '/railguns', data: data)
14
+ end
15
+
16
+ ##
17
+ # Get all the railguns
18
+ def list(page: 1, per_page: 50, direction: 'desc')
19
+ raise 'direction must be either desc | asc' unless direction == 'desc' || direction == 'asc'
20
+ params = {page: page, per_page: per_page, direction: direction}
21
+ cf_get(path: '/railguns', params: params)
22
+ end
23
+
24
+ ##
25
+ # Get a single railgun
26
+ def show(id:)
27
+ raise 'must provide the id of the railgun' if id.nil?
28
+ cf_get(path: "/railguns/#{id}")
29
+ end
30
+
31
+ ##
32
+ # Get CF zones associated with a railgun
33
+ def zones(id:)
34
+ raise 'must provide the id of the railgun' if id.nil?
35
+ cf_get(path: "/railguns/#{id}/zones")
36
+ end
37
+
38
+ ##
39
+ # Enable a railgun
40
+ def enable(id:)
41
+ update_enabled(id: id, enabled: true)
42
+ end
43
+
44
+ ##
45
+ # Disable a railgun
46
+ def disable(id:)
47
+ update_enabled(id: id, enabled: false)
48
+ end
49
+
50
+ ##
51
+ # delete a railgun
52
+ def delete(id:)
53
+ raise 'must provide the id of the railgun' if id.nil?
54
+ cf_delete(path: "/railguns/#{id}")
55
+ end
56
+
57
+ private
58
+
59
+ def update_enabled(id:, enabled:)
60
+ raise 'must provide the id of the railgun' if id.nil?
61
+ cf_patch(path: "/railguns/#{id}", data: {enabled: enabled})
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ class CloudflareClient
2
+ VERSION = '4.0.0'
3
+ end
@@ -0,0 +1,133 @@
1
+ class CloudflareClient::VirtualDnsCluster < CloudflareClient
2
+ require_relative '../cloudflare_client/virtual_dns_cluster/analytic.rb'
3
+
4
+ VALID_SCOPES = %i[user organization].freeze
5
+
6
+ attr_reader :uri_prefix
7
+
8
+ ##
9
+ # virtual DNS
10
+ # using scope to determine if this is for users or for orgs
11
+ def initialize(args)
12
+ scope = args.delete(:scope)&.to_sym
13
+ org_id = args.delete(:org_id)
14
+
15
+ valid_value_check(:scope, scope, VALID_SCOPES)
16
+
17
+ if scope == :user
18
+ @uri_prefix = '/user'
19
+ else
20
+ id_check(:org_id, org_id)
21
+
22
+ @uri_prefix = "/organizations/#{org_id}"
23
+ end
24
+
25
+ super
26
+ end
27
+
28
+ ##
29
+ # list virutal dns clsuters for a user or an org
30
+ def list
31
+ cf_get(path: "#{uri_prefix}/virtual_dns")
32
+ end
33
+
34
+ ##
35
+ # create a virtual dns cluster
36
+ def create(name:,
37
+ origin_ips:,
38
+ minimum_cache_ttl: nil,
39
+ maximum_cache_ttl: nil,
40
+ deprecate_any_request: nil,
41
+ ratelimit: nil)
42
+ id_check(:name, name)
43
+ max_length_check(:name, name, 160)
44
+ non_empty_array_check(:origin_ips, origin_ips)
45
+
46
+ data = {name: name, origin_ips: origin_ips}
47
+
48
+ unless minimum_cache_ttl.nil?
49
+ range_check(:minimum_cache_ttl, minimum_cache_ttl, 30, 36000)
50
+ data[:minimum_cache_ttl] = minimum_cache_ttl
51
+ end
52
+
53
+ unless maximum_cache_ttl.nil?
54
+ range_check(:maximum_cache_ttl, maximum_cache_ttl, 30, 36000)
55
+ data[:maximum_cache_ttl] = maximum_cache_ttl
56
+ end
57
+
58
+ unless deprecate_any_request.nil?
59
+ valid_value_check(:deprecate_any_request, deprecate_any_request, [true, false])
60
+ data[:deprecate_any_request] = deprecate_any_request
61
+ end
62
+
63
+ unless ratelimit.nil?
64
+ range_check(:ratelimit, ratelimit, 0, 100000000)
65
+ data[:ratelimit] = ratelimit
66
+ end
67
+
68
+ cf_post(path: "#{uri_prefix}/virtual_dns", data: data)
69
+ end
70
+
71
+ ##
72
+ # details of a cluster
73
+ def show(id:)
74
+ id_check(:id, id)
75
+
76
+ cf_get(path: "#{uri_prefix}/virtual_dns/#{id}")
77
+ end
78
+
79
+ ##
80
+ # delete a dns cluster (user)
81
+ def delete(id:)
82
+ id_check(:id, id)
83
+
84
+ cf_delete(path: "#{uri_prefix}/virtual_dns/#{id}")
85
+ end
86
+
87
+ ##
88
+ # updates a dns cluster (user)
89
+ def update(id:,
90
+ name: nil,
91
+ origin_ips: nil,
92
+ minimum_cache_ttl: nil,
93
+ maximum_cache_ttl: nil,
94
+ deprecate_any_request: nil,
95
+ ratelimit: nil)
96
+ id_check(:id, id)
97
+
98
+ data = {}
99
+
100
+ unless name.nil?
101
+ id_check(:name, name)
102
+ max_length_check(:name, name, 160)
103
+ data[:name] = name
104
+ end
105
+
106
+ unless origin_ips.nil?
107
+ non_empty_array_check(:origin_ips, origin_ips)
108
+ data[:origin_ips] = origin_ips
109
+ end
110
+
111
+ unless minimum_cache_ttl.nil?
112
+ range_check(:minimum_cache_ttl, minimum_cache_ttl, 30, 36000)
113
+ data[:minimum_cache_ttl] = minimum_cache_ttl
114
+ end
115
+
116
+ unless maximum_cache_ttl.nil?
117
+ range_check(:maximum_cache_ttl, maximum_cache_ttl, 30, 36000)
118
+ data[:maximum_cache_ttl] = maximum_cache_ttl
119
+ end
120
+
121
+ unless deprecate_any_request.nil?
122
+ valid_value_check(:deprecate_any_request, deprecate_any_request, [true, false])
123
+ data[:deprecate_any_request] = deprecate_any_request
124
+ end
125
+
126
+ unless ratelimit.nil?
127
+ range_check(:ratelimit, ratelimit, 0, 100000000)
128
+ data[:ratelimit] = ratelimit
129
+ end
130
+
131
+ cf_patch(path: "#{uri_prefix}/virtual_dns/#{id}", data: data)
132
+ end
133
+ end
@@ -0,0 +1,38 @@
1
+ class CloudflareClient::VirtualDnsCluster::Analytic < CloudflareClient::VirtualDnsCluster
2
+ attr_reader :virtual_dns_id
3
+
4
+ ##
5
+ # virtual DNS Analytics (users and orgs)
6
+ #
7
+ def initialize(args)
8
+ @virtual_dns_id = args.delete(:virtual_dns_id)
9
+ id_check(:virtual_dns_id, virtual_dns_id)
10
+ super
11
+ end
12
+
13
+ def report(dimensions:, metrics:, since_ts:, until_ts:, filters: nil, sort: nil, limit: nil)
14
+ non_empty_array_check(:dimensions, dimensions)
15
+ non_empty_array_check(:metrics, metrics)
16
+ iso8601_check(:since_ts, since_ts)
17
+ iso8601_check(:until_ts, until_ts)
18
+
19
+ params = {dimensions: dimensions, metrics: metrics, since: since_ts, until: until_ts}
20
+
21
+ unless sort.nil?
22
+ non_empty_array_check(:sort, sort)
23
+ params[:sort] = sort
24
+ end
25
+
26
+ unless filters.nil?
27
+ basic_type_check(:filters, filters, String)
28
+ params[:filters] = filters
29
+ end
30
+
31
+ unless limit.nil?
32
+ basic_type_check(:limit, limit, Integer)
33
+ params[:limit] = limit
34
+ end
35
+
36
+ cf_get(path: "#{uri_prefix}/virtual_dns/#{virtual_dns_id}/dns_analytics/report", params: params)
37
+ end
38
+ end
@@ -0,0 +1,129 @@
1
+ class CloudflareClient
2
+ class Zone < CloudflareClient
3
+ require_relative './zone/base.rb'
4
+ Dir[File.expand_path('../zone/*.rb', __FILE__)].each { |f| require f }
5
+
6
+ VALID_ZONE_STATUSES = %w[active pending initializing moved deleted deactivated].freeze
7
+
8
+ ##
9
+ # Zone based operations
10
+ def initialize(*args)
11
+ super
12
+ end
13
+
14
+ ##
15
+ # list_zones will either list all zones or search for zones based on params
16
+ # results are paginated!
17
+ # list_zones(name: name_of_zone, status: active|pending, page: page_no)
18
+ def zones(name: nil, status: nil, per_page: 50, page: 1)
19
+ params = {}
20
+ params[:per_page] = per_page
21
+ params[:page] = page
22
+ params[:name] = name unless name.nil?
23
+
24
+ unless status.nil?
25
+ raise "status must be one of #{VALID_ZONE_STATUSES.flatten}" unless VALID_ZONE_STATUSES.include?(status)
26
+ params[:status] = status
27
+ end
28
+
29
+ cf_get(path: '/zones', params: params)
30
+ end
31
+
32
+ ##
33
+ # create's a zone with a given name
34
+ # create_zone(name: name_of_zone, jump_start: true|false (default true),
35
+ # organization: {id: org_id, name: org_name})
36
+ def create_zone(name:, jump_start: true, organization: {id: nil, name: nil})
37
+ raise('Zone name required') if name.nil?
38
+ unless organization[:id].nil? && organization[:name].nil
39
+ org_data = organization.merge(status: 'active', permissions: ['#zones:read'])
40
+ end
41
+ data = {name: name, jump_start: jump_start, organization: org_data}
42
+ cf_post(path: '/zones', data: data)
43
+ end
44
+
45
+ ##
46
+ # request another zone activation (ssl) check
47
+ # zone_activation_check(zone_id:)
48
+ def zone_activation_check(zone_id:)
49
+ raise('zone_id required') if zone_id.nil?
50
+ cf_put(path: "/zones/#{zone_id}/activation_check")
51
+ end
52
+
53
+ ##
54
+ # return all the details for a given zone_id
55
+ # zone_details(zone_id: id_of_my_zone
56
+ def zone(zone_id:)
57
+ raise('zone_id required') if zone_id.nil?
58
+ cf_get(path: "/zones/#{zone_id}")
59
+ end
60
+
61
+ ##
62
+ # edit the properties of a zone
63
+ # NOTE: some of these options require an enterprise account
64
+ # edit_zone(zone_id: id_of_zone, paused: true|false,
65
+ # vanity_name_servers: ['ns1.foo.bar', 'ns2.foo.bar'], plan: {id: plan_id})
66
+ def edit_zone(zone_id:, paused: nil, vanity_name_servers: [], plan: {id: nil})
67
+ raise('zone_id required') if zone_id.nil?
68
+ data = {}
69
+ data[:paused] = paused unless paused.nil?
70
+ data[:vanity_name_servers] = vanity_name_servers unless vanity_name_servers.empty?
71
+ data[:plan] = plan unless plan[:id].nil?
72
+ cf_patch(path: "/zones/#{zone_id}", data: data)
73
+ end
74
+
75
+ ##
76
+ # various zone caching controlls.
77
+ # supploy an array of tags, or files, or the purge_everything bool
78
+ def purge_zone_cache(zone_id:, tags: [], files: [], purge_everything: nil)
79
+ raise('zone_id required') if zone_id.nil?
80
+ if purge_everything.nil? && (tags.empty? && files.empty?)
81
+ raise('specify a combination tags[], files[] or purge_everything')
82
+ end
83
+ data = {}
84
+ data[:purge_everything] = purge_everything unless purge_everything.nil?
85
+ data[:tags] = tags unless tags.empty?
86
+ data[:files] = files unless files.empty?
87
+ cf_delete(path: "/zones/#{zone_id}/purge_cache", data: data)
88
+ end
89
+
90
+ ##
91
+ # delete a given zone
92
+ # delete_zone(zone_id: id_of_zone
93
+ def delete_zone(zone_id:)
94
+ raise('zone_id required') if zone_id.nil?
95
+ cf_delete(path: "/zones/#{zone_id}")
96
+ end
97
+
98
+ ##
99
+ # return all settings for a given zone
100
+ def zone_settings(zone_id:)
101
+ raise('zone_id required') if zone_id.nil?
102
+ cf_get(path: "/zones/#{zone_id}/settings")
103
+ end
104
+
105
+ ##
106
+ # there are a lot of settings that can be returned.
107
+ def zone_setting(zone_id:, name:)
108
+ raise('zone_id required') if zone_id.nil?
109
+ raise('setting_name not valid') if name.nil? || !valid_setting?(name)
110
+ cf_get(path: "/zones/#{zone_id}/settings/#{name}")
111
+ end
112
+
113
+ ##
114
+ # update 1 or more settings in a zone
115
+ # settings: [{name: value: true},{name: 'value'}...]
116
+ # https://api.cloudflare.com/#zone-settings-properties
117
+ def update_zone_settings(zone_id:, settings: [])
118
+ raise('zone_id required') if zone_id.nil?
119
+ data = settings.map do |setting|
120
+ raise("setting_name \"#{setting[:name]}\" not valid") unless valid_setting?(setting[:name])
121
+ {id: setting[:name], value: setting[:value]}
122
+ end
123
+ data = {items: data}
124
+ cf_patch(path: "/zones/#{zone_id}/settings", data: data)
125
+ end
126
+
127
+ #TODO: zone_rate_plans
128
+ end
129
+ end