From a43cdfab69806f0ccc4739677ee129652c8ba252 Mon Sep 17 00:00:00 2001 From: Brian French Date: Thu, 17 Apr 2025 20:37:20 -0400 Subject: [PATCH 1/2] Passing tests. --- .../api/dcim/mac_address.rb | 14 ++ .../api/dcim/mac_addresses.rb | 20 +++ spec/fixtures/dcim/mac-address_1-update.json | 34 ++++ spec/fixtures/dcim/mac-address_1.json | 34 ++++ spec/fixtures/dcim/mac-addresses.json | 40 +++++ .../api/dcim/mac_address_spec.rb | 149 ++++++++++++++++++ .../api/dcim/mac_addresses_spec.rb | 59 +++++++ 7 files changed, 350 insertions(+) create mode 100644 lib/netbox_client_ruby/api/dcim/mac_address.rb create mode 100644 lib/netbox_client_ruby/api/dcim/mac_addresses.rb create mode 100644 spec/fixtures/dcim/mac-address_1-update.json create mode 100644 spec/fixtures/dcim/mac-address_1.json create mode 100644 spec/fixtures/dcim/mac-addresses.json create mode 100644 spec/netbox_client_ruby/api/dcim/mac_address_spec.rb create mode 100644 spec/netbox_client_ruby/api/dcim/mac_addresses_spec.rb diff --git a/lib/netbox_client_ruby/api/dcim/mac_address.rb b/lib/netbox_client_ruby/api/dcim/mac_address.rb new file mode 100644 index 0000000..cf052ba --- /dev/null +++ b/lib/netbox_client_ruby/api/dcim/mac_address.rb @@ -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 diff --git a/lib/netbox_client_ruby/api/dcim/mac_addresses.rb b/lib/netbox_client_ruby/api/dcim/mac_addresses.rb new file mode 100644 index 0000000..ca68f0b --- /dev/null +++ b/lib/netbox_client_ruby/api/dcim/mac_addresses.rb @@ -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 diff --git a/spec/fixtures/dcim/mac-address_1-update.json b/spec/fixtures/dcim/mac-address_1-update.json new file mode 100644 index 0000000..3b1cc0a --- /dev/null +++ b/spec/fixtures/dcim/mac-address_1-update.json @@ -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": {} +} \ No newline at end of file diff --git a/spec/fixtures/dcim/mac-address_1.json b/spec/fixtures/dcim/mac-address_1.json new file mode 100644 index 0000000..15e635b --- /dev/null +++ b/spec/fixtures/dcim/mac-address_1.json @@ -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": {} +} \ No newline at end of file diff --git a/spec/fixtures/dcim/mac-addresses.json b/spec/fixtures/dcim/mac-addresses.json new file mode 100644 index 0000000..e4e0c8c --- /dev/null +++ b/spec/fixtures/dcim/mac-addresses.json @@ -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": {} + } + ] +} diff --git a/spec/netbox_client_ruby/api/dcim/mac_address_spec.rb b/spec/netbox_client_ruby/api/dcim/mac_address_spec.rb new file mode 100644 index 0000000..8dfca66 --- /dev/null +++ b/spec/netbox_client_ruby/api/dcim/mac_address_spec.rb @@ -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 diff --git a/spec/netbox_client_ruby/api/dcim/mac_addresses_spec.rb b/spec/netbox_client_ruby/api/dcim/mac_addresses_spec.rb new file mode 100644 index 0000000..c63d044 --- /dev/null +++ b/spec/netbox_client_ruby/api/dcim/mac_addresses_spec.rb @@ -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 From 931bcd4b0d54130ed46974c3cba92f8570830ceb Mon Sep 17 00:00:00 2001 From: Brian French Date: Thu, 17 Apr 2025 21:25:46 -0400 Subject: [PATCH 2/2] Forgot to add them to the Module --- lib/netbox_client_ruby/api/dcim.rb | 2 ++ spec/netbox_client_ruby/api/dcim_spec.rb | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/netbox_client_ruby/api/dcim.rb b/lib/netbox_client_ruby/api/dcim.rb index f019407..e5660a4 100644 --- a/lib/netbox_client_ruby/api/dcim.rb +++ b/lib/netbox_client_ruby/api/dcim.rb @@ -12,6 +12,7 @@ module DCIM interfaces: Interfaces, interface_connections: InterfaceConnections, inventory_items: InventoryItems, + mac_addresses: MacAddresses, manufacturers: Manufacturers, platforms: Platforms, power_connections: PowerConnections, @@ -39,6 +40,7 @@ module DCIM interface: Interface, interface_connection: InterfaceConnection, inventory_item: InventoryItem, + mac_address: MacAddress, manufacturer: Manufacturer, platform: Platform, power_connection: PowerConnection, diff --git a/spec/netbox_client_ruby/api/dcim_spec.rb b/spec/netbox_client_ruby/api/dcim_spec.rb index 6c9b92b..fa2fdd7 100644 --- a/spec/netbox_client_ruby/api/dcim_spec.rb +++ b/spec/netbox_client_ruby/api/dcim_spec.rb @@ -15,6 +15,7 @@ module DCIM interfaces: Interfaces, interface_connections: InterfaceConnections, inventory_items: InventoryItems, + mac_addresses: MacAddresses, manufacturers: Manufacturers, platforms: Platforms, power_connections: PowerConnections, @@ -58,6 +59,7 @@ module DCIM interface: Interface, interface_connection: InterfaceConnection, inventory_item: InventoryItem, + mac_address: MacAddress, manufacturer: Manufacturer, platform: Platform, power_connection: PowerConnection,