build ip updating logic, own dyn dns

This commit is contained in:
Torsten 2022-03-31 20:48:14 +03:00
parent 58fe0ef5fb
commit 8e88fc7ac6
2 changed files with 45 additions and 5 deletions

View File

@ -1,11 +1,26 @@
require "dnsimple"
class DnsUpdateJob < ApplicationJob
def self.get_client
Dnsimple::Client.new(access_token: Rails.application.credentials.simple_token)
end
queue_as :default
def client
@client ||= Dnsimple::Client.new(access_token: Rails.application.credentials.simple_token)
end
def simple_id
Rails.application.credentials.simple_id
end
def my_ip
HTTParty.get("http://icanhazip.com").strip
end
def find_record(name , host)
zones = client.zones.list_zone_records(simple_id, host, filter: { type: 'A' , name: name })
zones.data.first
end
def update_ip
record = find_record("gateway" , "hubfeenix.fi")
client.zones.update_zone_record(simple_id,"hubfeenix.fi", record.id , content: my_ip)
end
def perform(*args)
# Do something later
end
end

View File

@ -1,5 +1,30 @@
require 'rails_helper'
RSpec.describe DnsUpdateJob, type: :job do
pending "add some examples to (or delete) #{__FILE__}"
ActiveJob::Base.queue_adapter = :test
let(:my_ip){"85.76.133.88"}
it "should get ip" do
res = DnsUpdateJob.new.my_ip
expect(res).to eq my_ip
end
it "should init client" do
client = DnsUpdateJob.new.client
expect(client.class).to be Dnsimple::Client
end
it "should find domain" do
zone = DnsUpdateJob.new.find_record("gateway","hubfeenix.fi")
expect(zone.class).to eq Dnsimple::Struct::ZoneRecord
expect(zone.name).to eq "gateway"
expect(zone.content).to eq my_ip
end
it "update ip" do
updater = DnsUpdateJob.new
updater.update_ip
zone = updater.find_record("gateway","hubfeenix.fi")
expect(zone.content).to eq my_ip
end
end