Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Not all objects which the Netbox API exposes are currently implemented. Implemen
* Devices: `NetboxClientRuby.dcim.devices`
* Device Roles: `NetboxClientRuby.dcim.device_roles`
* Device Types: `NetboxClientRuby.dcim.device_types`
* Front Ports: `NetboxClientRuby.dcim.front_ports`
* Interfaces: `NetboxClientRuby.dcim.interfaces`
* Interface Connections: `NetboxClientRuby.dcim.interface_connections`
* Manufacturers: `NetboxClientRuby.dcim.manufacturers`
Expand All @@ -166,6 +167,7 @@ Not all objects which the Netbox API exposes are currently implemented. Implemen
* Rack Groups: `NetboxClientRuby.dcim.rack_groups`
* Rack Roles: `NetboxClientRuby.dcim.rack_roles`
* Rack Reservations: `NetboxClientRuby.dcim.rack_reservations`
* Rear Ports: `NetboxClientRuby.dcim.rear_ports`
* Regions: `NetboxClientRuby.dcim.regions`
* Sites: `NetboxClientRuby.dcim.sites`
* Virtual Chassis: `NetboxClientRuby.dcim.virtual_chassis_list`
Expand Down
4 changes: 4 additions & 0 deletions lib/netbox_client_ruby/api/dcim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module DCIM
devices: Devices,
device_roles: DeviceRoles,
device_types: DeviceTypes,
front_ports: FrontPorts,
interfaces: Interfaces,
interface_connections: InterfaceConnections,
inventory_items: InventoryItems,
Expand All @@ -22,6 +23,7 @@ module DCIM
rack_groups: RackGroups,
rack_reservations: RackReservations,
rack_roles: RackRoles,
rear_ports: RearPorts,
regions: Regions,
sites: Sites,
virtual_chassis_list: VirtualChassisList,
Expand All @@ -36,6 +38,7 @@ module DCIM
device: Device,
device_role: DeviceRole,
device_type: DeviceType,
front_port: FrontPort,
interface: Interface,
interface_connection: InterfaceConnection,
inventory_item: InventoryItem,
Expand All @@ -49,6 +52,7 @@ module DCIM
rack_group: RackGroup,
rack_reservation: RackReservation,
rack_role: RackRole,
rear_port: RearPort,
region: Region,
site: Site,
virtual_chassis: VirtualChassis,
Expand Down
14 changes: 14 additions & 0 deletions lib/netbox_client_ruby/api/dcim/front_port.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module NetboxClientRuby
module DCIM
class FrontPort
include Entity

id id: :id
deletable true
path 'dcim/front-ports/:id/'
creation_path 'dcim/front-ports/'
end
end
end
20 changes: 20 additions & 0 deletions lib/netbox_client_ruby/api/dcim/front_ports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module NetboxClientRuby
module DCIM
class FrontPorts
include Entities

path 'dcim/front-ports/'
data_key 'results'
count_key 'count'
entity_creator :entity_creator

private

def entity_creator(raw_entity)
FrontPort.new raw_entity['id']
end
end
end
end
14 changes: 14 additions & 0 deletions lib/netbox_client_ruby/api/dcim/rear_port.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module NetboxClientRuby
module DCIM
class RearPort
include Entity

id id: :id
deletable true
path 'dcim/rear-ports/:id/'
creation_path 'dcim/rear-ports/'
end
end
end
20 changes: 20 additions & 0 deletions lib/netbox_client_ruby/api/dcim/rear_ports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module NetboxClientRuby
module DCIM
class RearPorts
include Entities

path 'dcim/rear-ports/'
data_key 'results'
count_key 'count'
entity_creator :entity_creator

private

def entity_creator(raw_entity)
RearPort.new raw_entity['id']
end
end
end
end
10 changes: 10 additions & 0 deletions spec/fixtures/dcim/front-port_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": 1,
"name": "1",
"slug": "1",
"type": {
"value": "8p8c",
"label": "8P8C"
},
"description": "The first front port"
}
17 changes: 17 additions & 0 deletions spec/fixtures/dcim/front-ports.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"name": "1",
"slug": "1",
"type": {
"value": "8p8c",
"label": "8P8C"
},
"description": "The first front port"
}
]
}
10 changes: 10 additions & 0 deletions spec/fixtures/dcim/rear-port_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"id": 1,
"name": "1",
"slug": "1",
"type": {
"value": "8p8c",
"label": "8P8C"
},
"description": "The first rear port"
}
17 changes: 17 additions & 0 deletions spec/fixtures/dcim/rear-ports.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"name": "1",
"slug": "1",
"type": {
"value": "8p8c",
"label": "8P8C"
},
"description": "The first rear port"
}
]
}
138 changes: 138 additions & 0 deletions spec/netbox_client_ruby/api/dcim/front_port_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe NetboxClientRuby::DCIM::FrontPort, faraday_stub: true do
subject { sut.new entity_id }

let(:entity_id) { 1 }
let(:expected_name) { '1' }
let(:sut) { described_class }
let(:base_url) { '/api/dcim/front-ports/' }

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

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

describe '#name' do
it 'fetches the data' do
expect(faraday).to receive(:get).and_call_original

expect(subject.name).to_not be_nil
end

it 'shall be the expected name' do
expect(subject.name).to eq(expected_name)
end
end

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

it 'deletes 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) { { 'name' => 'noob' } }

it 'updates the object' do
expect(faraday).to receive(request_method).and_call_original
expect(subject.update(name: 'noob').name).to eq(expected_name)
end
end

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

subject.reload
subject.reload
end
end

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

context 'update' do
subject do
region = sut.new entity_id
region.name = name
region.slug = slug
region
end

let(:request_method) { :patch }

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.name).to eq(name)
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 answer from the PATCH answer' do
expect(faraday).to receive(request_method).and_call_original

subject.save
expect(subject.name).to eq(expected_name)
expect(subject.slug).to eq(expected_name)
end
end

context 'create' do
subject do
region = sut.new
region.name = name
region.slug = slug
region
end

let(:request_method) { :post }
let(:request_url) { base_url }

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

expect(subject.name).to eq(name)
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.name).to eq(expected_name)
expect(subject.slug).to eq(expected_name)
end
end
end
end
59 changes: 59 additions & 0 deletions spec/netbox_client_ruby/api/dcim/front_ports_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe NetboxClientRuby::DCIM::FrontPorts, faraday_stub: true do
let(:expected_length) { 1 }
let(:singular_type) { NetboxClientRuby::DCIM::FrontPort }

let(:response) { File.read('spec/fixtures/dcim/front-ports.json') }
let(:request_url) { '/api/dcim/front-ports/' }
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.to_a.length).to be expected_length
end

it 'returns FrontPort instances' do
subject.to_a.each do |element|
expect(element).to be_a singular_type
end
end
end
end
Loading