lib/cloudflare/zone.rb in cloudflare-3.0.0 vs lib/cloudflare/zone.rb in cloudflare-3.1.0

- old
+ new

@@ -1,7 +1,8 @@ # Copyright, 2012, by Marcin Prokop. # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com> +# Copyright, 2017, by David Rosenbloom. <http://artifactory.com> # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -32,11 +33,16 @@ def initialize(url, record = nil, **options) super(url, **options) @record = record || self.get.result end - + + def update_content(content) + response = self.put({type: @record[:type], name: @record[:name], content: content}.to_json, content_type: 'application/json') + response.successful? + end + attr :record def to_s "#{@record[:name]} #{@record[:type]} #{@record[:content]}" end @@ -50,11 +56,12 @@ end attr :zone def all - self.get.results.map{|record| DNSRecord.new(concat_urls(url, record[:id]), record, **options)} + results = paginate(DNSRecords, url) + results.map{|record| DNSRecord.new(concat_urls(url, record[:id]), record, **options)} end def find_by_name(name) response = self.get(params: {name: name}) @@ -68,23 +75,95 @@ def find_by_id(id) DNSRecord.new(concat_urls(url, id), **options) end end - class Zone < Resource + class FirewallRule < Resource def initialize(url, record = nil, **options) super(url, **options) + + @record = record || self.get.result + end + + attr :record + + def to_s + "#{@record[:configuration][:value]} - #{@record[:mode]} - #{@record[:notes]}" + end + end + + class FirewallRules < Resource + def initialize(url, zone, **options) + super(url, **options) + + @zone = zone + end + + attr :zone + + def all(mode = nil, ip = nil, notes = nil) + url_args = "" + url_args.concat("&mode=#{mode}") if mode + url_args.concat("&configuration_value=#{ip}") if ip + url_args.concat("&notes=#{notes}") if notes + + results = paginate(FirewallRules, url, url_args) + results.map{|record| FirewallRule.new(concat_urls(url, record[:id]), record, **options)} + end + + def firewalled_ips(rules) + rules.collect {|r| r.record[:configuration][:value]} + end + + def blocked_ips + firewalled_ips(all("block")) + end + + def set(mode, ip, note) + data = { + mode: mode.to_s, + configuration: { + target: "ip", + value: ip.to_s, + notes: "cloudflare gem firewall_rules [#{mode}] #{note} #{Time.now.strftime("%m/%d/%y")}" + } + } + post(data.to_json, content_type: 'application/json') + end + + def unset(mode, value) + rule = send("find_by_#{mode}", value) + rule.delete + end + + def find_by_id(id) + FirewallRule.new(concat_urls(url, id), **options) + end + + def find_by_ip(ip) + rule = FirewallRule.new(concat_urls(url, "?configuration_value=#{ip}"), **options) + FirewallRule.new(concat_urls(url, rule.record.first[:id]), **options) + end + end + + class Zone < Resource + def initialize(url, record = nil, **options) + super(url, **options) @record = record || self.get.result end attr :record def dns_records @dns_records ||= DNSRecords.new(concat_urls(url, 'dns_records'), self, **options) end + def firewall_rules + @firewall_rules ||= FirewallRules.new(concat_urls(url, "firewall/access_rules/rules"), self, **options) + end + def to_s @record[:name] end end @@ -92,10 +171,10 @@ def all self.get.results.map{|record| Zone.new(concat_urls(url, record[:id]), record, **options)} end def find_by_name(name) - record = self.get(params: {name: name}).result + response = self.get(params: {name: name}) unless response.empty? record = response.results.first Zone.new(concat_urls(url, record[:id]), record, **options)