Skip to content

Commit

Permalink
✅ Adds support for DeviceType
Browse files Browse the repository at this point in the history
  • Loading branch information
cimnine committed May 4, 2017
1 parent 650b15c commit c326572
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 18 deletions.
8 changes: 6 additions & 2 deletions lib/netbox_client_ruby/api/dcim.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'netbox_client_ruby/api/dcim/device_type'
require 'netbox_client_ruby/api/dcim/device_types'
require 'netbox_client_ruby/api/dcim/manufacturer'
require 'netbox_client_ruby/api/dcim/manufacturers'
require 'netbox_client_ruby/api/dcim/region'
Expand All @@ -11,7 +13,8 @@ class DCIM
{
sites: Sites,
regions: Regions,
manufacturers: Manufacturers
manufacturers: Manufacturers,
device_types: DeviceTypes
}.each_pair do |method_name, class_name|
define_method(method_name) do
class_name.new
Expand All @@ -21,7 +24,8 @@ class DCIM
{
site: Site,
region: Region,
manufacturer: Manufacturer
manufacturer: Manufacturer,
device_type: DeviceType
}.each_pair do |method_name, class_name|
define_method(method_name) do |id|
class_name.new id
Expand Down
22 changes: 10 additions & 12 deletions lib/netbox_client_ruby/api/dcim/device_type.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
require 'netbox_client_ruby/entity'
require 'netbox_client_ruby/api/dcim/manufacturer'
require 'netbox_client_ruby/api/tenancy/tenant'

module NetboxClientRuby
class InterfaceOrdering
attr_reader :value, :label

def initialize(raw_data)
@value = raw_data['value']
@label = raw_data['label']
end
end

class DeviceType
include NetboxClientRuby::Entity

Expand All @@ -12,18 +20,8 @@ class DeviceType
creation_path 'dcim/device-types/'
object_fields(
manufacturer: proc { |raw_data| NetboxClientRuby::Manufacturer.new raw_data['id'] },
interface_ordering: NetboxClientRuby::DeviceTypeInterfaceOrdering
interface_ordering: NetboxClientRuby::InterfaceOrdering
)
readonly_fields :display_name

end

class IpAddressStatus
attr_reader :value, :label

def initialize(raw_data)
@value = raw_data['value']
@label = raw_data['label']
end
end
end
2 changes: 1 addition & 1 deletion lib/netbox_client_ruby/api/dcim/device_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module NetboxClientRuby
class DeviceTypes
include NetboxClientRuby::Entities

path 'ipam/ip-addresses.json'
path 'dcim/device-types.json'
data_key 'results'
count_key 'count'
entity_creator :entity_creator
Expand Down
152 changes: 152 additions & 0 deletions spec/netbox_client_ruby/api/dcim/device_type_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
require 'spec_helper'

describe NetboxClientRuby::DeviceType, faraday_stub: true do
let(:entity_id) { 1 }
let(:expected_model) { 'devicetype1' }
let(:sut) { NetboxClientRuby::DeviceType }
let(:base_url) { '/api/dcim/device-types/' }

let(:request_url) { "#{base_url}#{entity_id}.json" }
let(:response) { File.read("spec/fixtures/dcim/device-type_#{entity_id}.json") }

subject { sut.new entity_id }

describe '#id' do
it 'shall be the expected id' do
expect(subject.id).to eq(entity_id)
end
end

describe '#model' do
it 'should fetch the data' do
expect(faraday).to receive(:get).and_call_original

expect(subject.model).to_not be_nil
end

it 'shall be the expected model' do
expect(subject.model).to eq(expected_model)
end
end

describe '#manufacturer' do
it 'should return a Manufacturer' do
expect(subject.manufacturer).to be_a(NetboxClientRuby::Manufacturer)
end

it 'should be the expected manufacturer' do
expect(subject.manufacturer.id).to be(1)
end
end

describe '#interface_ordering' do
it 'should return a InterfaceOrdering' do
expect(subject.interface_ordering).to be_a(NetboxClientRuby::InterfaceOrdering)
end
end

describe '.delete' do
let(:request_method) { :delete }
let(:response_status) { 204 }
let(:response) { nil }

it 'should delete the object' do
expect(faraday).to receive(request_method).and_call_original
subject.delete
end
end

describe '.update' do
let(:request_method) { :patch }
let(:request_params) { { 'model' => 'noob' } }

it 'should update the object' do
expect(faraday).to receive(request_method).and_call_original
expect(subject.update(model: 'noob').model).to eq(expected_model)
end
end

describe '.reload' do
it 'should reload the object' do
expect(faraday).to receive(request_method).twice.and_call_original

subject.reload
subject.reload
end
end

describe '.save' do
let(:model) { 'foobar' }
let(:slug) { model }
let(:request_params) { { 'model' => model, 'slug' => slug } }

context 'update' do
let(:request_method) { :patch }

subject do
region = sut.new entity_id
region.model = model
region.slug = slug
region
end

it 'does not call PATCH until save is called' do
expect(faraday).to_not receive(request_method)
expect(faraday).to_not receive(:get)

expect(subject.model).to eq(model)
expect(subject.slug).to eq(slug)
end

it 'calls PATCH when save is called' do
expect(faraday).to receive(request_method).and_call_original

expect(subject.save).to be(subject)
end

it 'Reads the anwer from the PATCH answer' do
expect(faraday).to receive(request_method).and_call_original

subject.save
expect(subject.model).to eq(expected_model)
expect(subject.slug).to eq(expected_model)
end
end

context 'create' do
let(:request_method) { :post }
let(:request_url) { base_url }

subject do
region = sut.new
region.model = model
region.slug = slug
region
end

it 'does not POST until save is called' do
expect(faraday).to_not receive(request_method)
expect(faraday).to_not receive(:get)

expect(subject.model).to eq(model)
expect(subject.slug).to eq(slug)
end

it 'POSTs the data upon a call of save' do
expect(faraday).to receive(request_method).and_call_original

expect(subject.save).to be(subject)
end

it 'Reads the answer from the POST' do
expect(faraday).to receive(request_method).and_call_original

subject.save

expect(subject.id).to be(1)
expect(subject.model).to eq(expected_model)
expect(subject.slug).to eq(expected_model)
end
end
end
end
57 changes: 57 additions & 0 deletions spec/netbox_client_ruby/api/dcim/device_types_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
require 'spec_helper'

describe NetboxClientRuby::DeviceTypes, faraday_stub: true do
let(:expected_length) { 1 }
let(:singular_type) { NetboxClientRuby::DeviceType }

let(:response) { File.read('spec/fixtures/dcim/device-types.json') }
let(:request_url) { '/api/dcim/device-types.json' }
let(:request_url_params) do
{ limit: NetboxClientRuby.config.netbox.pagination.default_limit }
end

context 'unpaged fetch' do
describe '#length' do
it 'shall be the expected length' do
expect(subject.length).to be expected_length
end
end

describe '#total' do
it 'shall be the expected total' do
expect(subject.total).to be expected_length
end
end
end

describe '#reload' do
it 'fetches the correct data' do
expect(faraday).to receive(:get).and_call_original
subject.reload
end

it 'caches the data' do
expect(faraday).to receive(:get).and_call_original
subject.total
subject.total
end

it 'reloads the data' do
expect(faraday).to receive(:get).twice.and_call_original
subject.reload
subject.reload
end
end

describe '#as_array' do
it 'return the correct amount' do
expect(subject.as_array.length).to be expected_length
end

it 'returns Site instances' do
subject.as_array.each do |element|
expect(element).to be_a singular_type
end
end
end
end
2 changes: 1 addition & 1 deletion spec/netbox_client_ruby/api/dcim/site_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe NetboxClientRuby::Site, faraday_stub: true do
let(:site_id) { 1 }
let(:response) { File.read('spec/fixtures/dcim/site.json') }
let(:response) { File.read("spec/fixtures/dcim/site_#{site_id}.json") }
let(:request_url) { "/api/dcim/sites/#{site_id}.json" }

subject { NetboxClientRuby::Site.new site_id }
Expand Down
6 changes: 4 additions & 2 deletions spec/netbox_client_ruby/api/dcim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
{
sites: NetboxClientRuby::Sites,
regions: NetboxClientRuby::Regions,
manufacturers: NetboxClientRuby::Manufacturers
manufacturers: NetboxClientRuby::Manufacturers,
device_types: NetboxClientRuby::DeviceTypes
}.each do |method, expected_class|
describe ".#{method}" do
subject { NetboxClientRuby::DCIM.new.public_send(method) }
Expand All @@ -29,7 +30,8 @@
{
site: NetboxClientRuby::Site,
region: NetboxClientRuby::Region,
manufacturer: NetboxClientRuby::Manufacturer
manufacturer: NetboxClientRuby::Manufacturer,
device_type: NetboxClientRuby::DeviceType
}.each do |method, expected_class|
describe ".#{method}" do
let(:id) { 1 }
Expand Down

0 comments on commit c326572

Please sign in to comment.