Skip to content
This repository has been archived by the owner on Jan 24, 2020. It is now read-only.

Commit

Permalink
Add server_puppetdb_data class
Browse files Browse the repository at this point in the history
  Fixes #60
  • Loading branch information
tallenaz committed Jul 25, 2017
1 parent 81c67ab commit 6ae2a4e
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 29 deletions.
5 changes: 5 additions & 0 deletions app/models/puppetdb_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ def ipaddress(fqdn)
response = client.request('facts', ['and', ['=', 'certname', fqdn], ['=', 'name', 'ipaddress']])
response.data.first['value']
end

def all_fqdns
response = client.request('facts', ['=', 'name', 'hostname'])
response.data.collect { |key| key['certname'] }
end
end
20 changes: 0 additions & 20 deletions app/models/server_puppet_data.rb

This file was deleted.

23 changes: 23 additions & 0 deletions app/models/server_puppetdb_data.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

class ServerPuppetdbData
attr_reader :client

def initialize
@client = PuppetdbClient.new
end

def self.run
new.load_data
end

def load_data
client.all_fqdns.each do |fqdn|
ip = client.ipaddress(fqdn)
hostname = fqdn.split('.').first
Server.find_or_create_by(fqdn: fqdn).tap do |server|
server.update(ip: ip, hostname: hostname)
end
end
end
end
2 changes: 1 addition & 1 deletion db/migrate/20170607163021_create_servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def change
create_table :servers do |t|
t.string :hostname
t.string :fqdn
t.inet :ip
t.string :ip
t.string :dev_team
t.boolean :pupgraded

Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
create_table "servers", force: :cascade do |t|
t.string "hostname"
t.string "fqdn"
t.inet "ip"
t.string "ip"
t.string "dev_team"
t.boolean "pupgraded"
t.datetime "created_at", null: false
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/load_data.rake
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# frozen_string_literal: true

namespace :load_data do
desc 'loads server data from puppet and puppet\'s hiera files'
desc 'loads server data from puppetdb'
task servers: :environment do
ServerPuppetData.run
ServerPuppetdbData.run
end

desc 'loads repo data for the sul-dlss org'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
"value": "123.45.67.890",
"name": "ipaddress",
"certname": "some-server-stage.stanford.edu"
},
{
"value": "123.45.67.891",
"name": "ipaddress",
"certname": "some-server-prod.stanford.edu"
}
]
16 changes: 11 additions & 5 deletions spec/models/puppetdb_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
end
end

describe '#ipaddress' do
before do
response = file_fixture('puppetdb_fact').read
stub_request(:any, /puppetdb.example.com/).to_return(status: 200, body: response, headers: { 'Content-Type' => 'application/json' })
end
before do
response = file_fixture('puppetdb_facts').read
stub_request(:any, /puppetdb.example.com/).to_return(status: 200, body: response, headers: { 'Content-Type' => 'application/json' })
end

describe '#ipaddress' do
it 'returns the ipaddress of a server' do
expect(client.ipaddress('some-server-stage.stanford.edu')).to eq '123.45.67.890'
end
end

describe '#all_fqdns' do
it 'returns all fqdns from the response' do
expect(client.all_fqdns).to eq ['some-server-stage.stanford.edu', 'some-server-prod.stanford.edu']
end
end
end
31 changes: 31 additions & 0 deletions spec/models/server_puppetdb_data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'rails_helper'
require 'ipaddr'

RSpec.describe ServerPuppetdbData do
subject(:server_puppetdb_data) { described_class.new }

describe '#initialize' do
it 'creates an PuppetdbClient instance' do
expect(server_puppetdb_data.client).to be_an PuppetdbClient
end
end

describe '#load_data' do
describe 'with valid response' do
before do
facts = file_fixture('puppetdb_facts').read
stub_request(:any, /puppetdb.example.com/).to_return(status: 200, body: facts, headers: { 'Content-Type' => 'application/json' })
end

it 'creates objects from api response' do
server_puppetdb_data.load_data
server = Server.first
expect(server.fqdn).to eq 'some-server-stage.stanford.edu'
expect(server.hostname).to eq 'some-server-stage'
expect(server.ip).to eq '123.45.67.890'
end
end
end
end

0 comments on commit 6ae2a4e

Please sign in to comment.