Skip to content

Adds MAC Addresses. #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2025
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 lib/netbox_client_ruby/api/dcim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module DCIM
interfaces: Interfaces,
interface_connections: InterfaceConnections,
inventory_items: InventoryItems,
mac_addresses: MacAddresses,
manufacturers: Manufacturers,
platforms: Platforms,
power_connections: PowerConnections,
Expand Down Expand Up @@ -39,6 +40,7 @@ module DCIM
interface: Interface,
interface_connection: InterfaceConnection,
inventory_item: InventoryItem,
mac_address: MacAddress,
manufacturer: Manufacturer,
platform: Platform,
power_connection: PowerConnection,
Expand Down
14 changes: 14 additions & 0 deletions lib/netbox_client_ruby/api/dcim/mac_address.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module NetboxClientRuby
module DCIM
class MacAddress
include Entity

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

module NetboxClientRuby
module DCIM
class MacAddresses
include Entities

path 'dcim/mac-addresses/'
data_key 'results'
count_key 'count'
entity_creator :entity_creator

private

def entity_creator(raw_entity)
MacAddress.new raw_entity['id']
end
end
end
end
34 changes: 34 additions & 0 deletions spec/fixtures/dcim/mac-address_1-update.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

{
"id": 1,
"url": "http://localhost/api/dcim/mac-addresses/1/",
"display_url": "http://localhost/dcim/mac-addresses/1/",
"display": "23:2f:bd:ad:aa:d2",
"mac_address": "23:2f:bd:ad:aa:d2",
"assigned_object_type": "dcim.interface",
"assigned_object_id": 2,
"assigned_object": {
"id": 2,
"device": {
"id": 1,
"url": "http://localhost/api/dcim/devices/1/",
"name": "device1",
"display_name": "device1"
},
"name": "e2",
"form_factor": {
"value": 1000,
"label": "1000BASE-T (1GE)"
},
"lag": null,
"mac_address": null,
"mgmt_only": false,
"description": "",
"connection": null,
"connected_interface": null
},
"description": "",
"comments": "",
"tags": [],
"custom_fields": {}
}
34 changes: 34 additions & 0 deletions spec/fixtures/dcim/mac-address_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

{
"id": 1,
"url": "http://localhost/api/dcim/mac-addresses/1/",
"display_url": "http://localhost/dcim/mac-addresses/1/",
"display": "23:2f:bd:ad:aa:d1",
"mac_address": "23:2f:bd:ad:aa:d1",
"assigned_object_type": "dcim.interface",
"assigned_object_id": 1,
"assigned_object": {
"id": 1,
"device": {
"id": 1,
"url": "http://localhost/api/dcim/devices/1/",
"name": "device1",
"display_name": "device1"
},
"name": "e1",
"form_factor": {
"value": 1000,
"label": "1000BASE-T (1GE)"
},
"lag": null,
"mac_address": null,
"mgmt_only": false,
"description": "",
"connection": null,
"connected_interface": null
},
"description": "",
"comments": "",
"tags": [],
"custom_fields": {}
}
40 changes: 40 additions & 0 deletions spec/fixtures/dcim/mac-addresses.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"url": "http://localhost/api/dcim/mac-addresses/1/",
"display_url": "http://localhost/dcim/mac-addresses/1/",
"display": "23:2f:bd:ad:aa:d1",
"mac_address": "23:2f:bd:ad:aa:d1",
"assigned_object_type": "dcim.interface",
"assigned_object_id": 1,
"assigned_object": {
"id": 1,
"device": {
"id": 1,
"url": "http://localhost/api/dcim/devices/1/",
"name": "device1",
"display_name": "device1"
},
"name": "e1",
"form_factor": {
"value": 1000,
"label": "1000BASE-T (1GE)"
},
"lag": null,
"mac_address": null,
"mgmt_only": false,
"description": "",
"connection": null,
"connected_interface": null
},
"description": "",
"comments": "",
"tags": [],
"custom_fields": {}
}
]
}
149 changes: 149 additions & 0 deletions spec/netbox_client_ruby/api/dcim/mac_address_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe NetboxClientRuby::DCIM::MacAddress, faraday_stub: true do
let(:entity_id) { 1 }
let(:base_url) { '/api/dcim/mac-addresses/' }
let(:request_url) { "#{base_url}#{entity_id}/" }
let(:response) { File.read("spec/fixtures/dcim/mac-address_#{entity_id}.json") }

subject { NetboxClientRuby::DCIM::MacAddress.new entity_id }

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

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

subject.mac_address
end

it 'shall be the expected mac_address' do
expect(subject.mac_address).to eq('23:2f:bd:ad:aa:d1')
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) { { 'mac_address' => '23:2f:bd:ad:aa:d2' } }
let(:response) { File.read("spec/fixtures/dcim/mac-address_#{entity_id}-update.json") }

it 'should update the object' do
expect(faraday).to receive(request_method).and_call_original
expect(subject.update(mac_address: '23:2f:bd:ad:aa:d2').mac_address).to eq('23:2f:bd:ad:aa:d2')
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(:mac_address) { '23:2f:bd:ad:aa:d2' }
let(:assigned_object_type) { 'dcim.interface' }
let(:assigned_object_id) { 2 }
let(:request_params) do
{
'mac_address' => mac_address,
'assigned_object_type' => assigned_object_type,
'assigned_object_id' => assigned_object_id
}
end

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

subject do
entity = NetboxClientRuby::DCIM::MacAddress.new entity_id
entity.mac_address = mac_address
entity.assigned_object_type = assigned_object_type
entity.assigned_object_id = assigned_object_id
entity
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.mac_address).to eq(mac_address)
expect(subject.assigned_object_type).to eq(assigned_object_type)
expect(subject.assigned_object_id).to eq(assigned_object_id)
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.mac_address).to eq('23:2f:bd:ad:aa:d1')
expect(subject.assigned_object_type).to eq('dcim.interface')
expect(subject.assigned_object_id).to eq(1)
end
end

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

subject do
entity = NetboxClientRuby::DCIM::MacAddress.new
entity.mac_address = mac_address
entity.assigned_object_type = assigned_object_type
entity.assigned_object_id = assigned_object_id
entity
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.mac_address).to eq(mac_address)
expect(subject.assigned_object_type).to eq(assigned_object_type)
expect(subject.assigned_object_id).to eq(assigned_object_id)
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.mac_address).to eq('23:2f:bd:ad:aa:d1')
expect(subject.assigned_object_type).to eq('dcim.interface')
expect(subject.assigned_object_id).to eq(1)
end
end
end
end
59 changes: 59 additions & 0 deletions spec/netbox_client_ruby/api/dcim/mac_addresses_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::MacAddresses, faraday_stub: true do
let(:expected_number_of_items) { 1 }
let(:expected_singular_type) { NetboxClientRuby::DCIM::MacAddress }

let(:response) { File.read('spec/fixtures/dcim/mac-addresses.json') }
let(:request_url) { '/api/dcim/mac-addresses/' }
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_number_of_items
end
end

describe '#total' do
it 'shall be the expected total' do
expect(subject.total).to be expected_number_of_items
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_number_of_items
end

it 'returns single instances' do
subject.to_a.each do |element|
expect(element).to be_a expected_singular_type
end
end
end
end
2 changes: 2 additions & 0 deletions spec/netbox_client_ruby/api/dcim_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module DCIM
interfaces: Interfaces,
interface_connections: InterfaceConnections,
inventory_items: InventoryItems,
mac_addresses: MacAddresses,
manufacturers: Manufacturers,
platforms: Platforms,
power_connections: PowerConnections,
Expand Down Expand Up @@ -58,6 +59,7 @@ module DCIM
interface: Interface,
interface_connection: InterfaceConnection,
inventory_item: InventoryItem,
mac_address: MacAddress,
manufacturer: Manufacturer,
platform: Platform,
power_connection: PowerConnection,
Expand Down