Skip to content

Commit e406e6e

Browse files
author
Kaka Ruto
committed
Add an endpoint to update all the values of a DNS record
1 parent 1c36e09 commit e406e6e

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ Cloudflare.connect(key: key, email: email) do |connection|
4949
# Add a DNS record. Here we add an A record for `batman.example.com`:
5050
zone = zones.find_by_name("example.com")
5151
zone.dns_records.create('A', 'batman', '1.2.3.4', proxied: false)
52+
53+
# Update a DNS record. Here we update the A record above to be a CNAME record to 'nairobi.kanairo.com'
54+
record = zone.dns_records.find_by_name("example.com") }
55+
record.update(type: "CNAME", name: "nairobi", content: "kanairo.com", proxied: true)
56+
5257

5358
# Get firewall rules:
5459
all_rules = zone.firewall_rules

lib/cloudflare/dns.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ def update_content(content, **options)
4545
@value = response.result
4646
end
4747

48+
def update(type: nil, name: nil, content: nil, **options)
49+
response = put(
50+
type: type || @record[:type],
51+
name: name || @record[:name],
52+
content: content || @record[:content],
53+
**options
54+
)
55+
56+
@value = response.result
57+
end
58+
4859
def type
4960
value[:type]
5061
end

spec/cloudflare/dns_spec.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
end
1313
end
1414

15-
context "new record" do
15+
describe "#create" do
1616
it "can create dns record" do
1717
@record = zone.dns_records.create("A", subdomain, "1.2.3.4")
1818
expect(@record.type).to be == "A"
@@ -29,8 +29,9 @@
2929
end
3030
end
3131

32-
context "with existing record" do
32+
describe "#update_content" do
3333
let(:record) {@record = zone.dns_records.create("A", subdomain, "1.2.3.4")}
34+
3435
it "can update dns content" do
3536
record.update_content("4.3.2.1")
3637
expect(record.content).to be == "4.3.2.1"
@@ -47,4 +48,26 @@
4748
expect(fetched_record.proxied).to be_truthy
4849
end
4950
end
51+
52+
describe "#update" do
53+
let(:subject) { record.update(**new_params)}
54+
55+
let(:record) { @record = zone.dns_records.create("A", "old", "1.2.3.4", proxied: false) }
56+
57+
let(:new_params) do
58+
{
59+
type: "CNAME",
60+
name: "new",
61+
content: "example.com",
62+
proxied: true
63+
}
64+
end
65+
66+
it "can update dns record" do
67+
expect { subject }.to change { record.name }.to("#{new_params[:name]}.#{zone.name}")
68+
.and change { record.type }.to(new_params[:type])
69+
.and change { record.content }.to(new_params[:content])
70+
.and change { record.proxied }.to(new_params[:proxied])
71+
end
72+
end
5073
end

0 commit comments

Comments
 (0)