Skip to content

Commit 329239e

Browse files
authored
feat: add NetboxClientRuby.dcim.locations (#116)
1 parent e59d97a commit 329239e

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ Not all objects which the Netbox API exposes are currently implemented. Implemen
158158
* Front Ports: `NetboxClientRuby.dcim.front_ports`
159159
* Interfaces: `NetboxClientRuby.dcim.interfaces`
160160
* Interface Connections: `NetboxClientRuby.dcim.interface_connections`
161+
* Locations: `NetboxClientRuby.dcim.locations`
161162
* Manufacturers: `NetboxClientRuby.dcim.manufacturers`
162163
* Platforms: `NetboxClientRuby.dcim.platforms`
163164
* Power Connections: `NetboxClientRuby.dcim.power_connections`

lib/netbox_client_ruby/api/dcim.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module DCIM
1313
interfaces: Interfaces,
1414
interface_connections: InterfaceConnections,
1515
inventory_items: InventoryItems,
16+
locations: Locations,
1617
mac_addresses: MacAddresses,
1718
manufacturers: Manufacturers,
1819
platforms: Platforms,
@@ -42,6 +43,7 @@ module DCIM
4243
interface: Interface,
4344
interface_connection: InterfaceConnection,
4445
inventory_item: InventoryItem,
46+
location: Location,
4547
mac_address: MacAddress,
4648
manufacturer: Manufacturer,
4749
platform: Platform,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# frozen_string_literal: true
2+
3+
module NetboxClientRuby
4+
module DCIM
5+
class Location
6+
include Entity
7+
8+
id id: :id
9+
deletable true
10+
path 'dcim/locations/:id/'
11+
creation_path 'dcim/locations/'
12+
end
13+
end
14+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module NetboxClientRuby
4+
module DCIM
5+
class Locations
6+
include Entities
7+
8+
path 'dcim/locations/'
9+
data_key 'results'
10+
count_key 'count'
11+
entity_creator :entity_creator
12+
13+
private
14+
15+
def entity_creator(raw_entity)
16+
Location.new raw_entity['id']
17+
end
18+
end
19+
end
20+
end

spec/fixtures/dcim/location_1.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"id": 1,
3+
"name": "1",
4+
"slug": "1",
5+
"description": "The location of my office"
6+
}

spec/fixtures/dcim/locations.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"count": 1,
3+
"next": null,
4+
"previous": null,
5+
"results": [
6+
{
7+
"id": 1,
8+
"name": "1",
9+
"slug": "1",
10+
"description": "The location of my office"
11+
}
12+
]
13+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe NetboxClientRuby::DCIM::Location, faraday_stub: true do
6+
subject { sut.new entity_id }
7+
8+
let(:entity_id) { 1 }
9+
let(:expected_name) { '1' }
10+
let(:sut) { described_class }
11+
let(:base_url) { '/api/dcim/locations/' }
12+
13+
let(:request_url) { "#{base_url}#{entity_id}/" }
14+
let(:response) { File.read("spec/fixtures/dcim/location_#{entity_id}.json") }
15+
16+
describe '#id' do
17+
it 'shall be the expected id' do
18+
expect(subject.id).to eq(entity_id)
19+
end
20+
end
21+
22+
describe '#name' do
23+
it 'fetches the data' do
24+
expect(faraday).to receive(:get).and_call_original
25+
26+
expect(subject.name).to_not be_nil
27+
end
28+
29+
it 'shall be the expected name' do
30+
expect(subject.name).to eq(expected_name)
31+
end
32+
end
33+
34+
describe '.delete' do
35+
let(:request_method) { :delete }
36+
let(:response_status) { 204 }
37+
let(:response) { nil }
38+
39+
it 'deletes the object' do
40+
expect(faraday).to receive(request_method).and_call_original
41+
subject.delete
42+
end
43+
end
44+
45+
describe '.update' do
46+
let(:request_method) { :patch }
47+
let(:request_params) { { 'name' => 'noob' } }
48+
49+
it 'updates the object' do
50+
expect(faraday).to receive(request_method).and_call_original
51+
expect(subject.update(name: 'noob').name).to eq(expected_name)
52+
end
53+
end
54+
55+
describe '.reload' do
56+
it 'reloads the object' do
57+
expect(faraday).to receive(request_method).twice.and_call_original
58+
59+
subject.reload
60+
subject.reload
61+
end
62+
end
63+
64+
describe '.save' do
65+
let(:name) { 'foobar' }
66+
let(:slug) { name }
67+
let(:request_params) { { 'name' => name, 'slug' => slug } }
68+
69+
context 'update' do
70+
subject do
71+
region = sut.new entity_id
72+
region.name = name
73+
region.slug = slug
74+
region
75+
end
76+
77+
let(:request_method) { :patch }
78+
79+
it 'does not call PATCH until save is called' do
80+
expect(faraday).to_not receive(request_method)
81+
expect(faraday).to_not receive(:get)
82+
83+
expect(subject.name).to eq(name)
84+
expect(subject.slug).to eq(slug)
85+
end
86+
87+
it 'calls PATCH when save is called' do
88+
expect(faraday).to receive(request_method).and_call_original
89+
90+
expect(subject.save).to be(subject)
91+
end
92+
93+
it 'Reads the answer from the PATCH answer' do
94+
expect(faraday).to receive(request_method).and_call_original
95+
96+
subject.save
97+
expect(subject.name).to eq(expected_name)
98+
expect(subject.slug).to eq(expected_name)
99+
end
100+
end
101+
102+
context 'create' do
103+
subject do
104+
region = sut.new
105+
region.name = name
106+
region.slug = slug
107+
region
108+
end
109+
110+
let(:request_method) { :post }
111+
let(:request_url) { base_url }
112+
113+
it 'does not POST until save is called' do
114+
expect(faraday).to_not receive(request_method)
115+
expect(faraday).to_not receive(:get)
116+
117+
expect(subject.name).to eq(name)
118+
expect(subject.slug).to eq(slug)
119+
end
120+
121+
it 'POSTs the data upon a call of save' do
122+
expect(faraday).to receive(request_method).and_call_original
123+
124+
expect(subject.save).to be(subject)
125+
end
126+
127+
it 'Reads the answer from the POST' do
128+
expect(faraday).to receive(request_method).and_call_original
129+
130+
subject.save
131+
132+
expect(subject.id).to be(1)
133+
expect(subject.name).to eq(expected_name)
134+
expect(subject.slug).to eq(expected_name)
135+
end
136+
end
137+
end
138+
end
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe NetboxClientRuby::DCIM::Locations, faraday_stub: true do
6+
let(:expected_length) { 1 }
7+
let(:singular_type) { NetboxClientRuby::DCIM::Location }
8+
9+
let(:response) { File.read('spec/fixtures/dcim/locations.json') }
10+
let(:request_url) { '/api/dcim/locations/' }
11+
let(:request_url_params) do
12+
{ limit: NetboxClientRuby.config.netbox.pagination.default_limit }
13+
end
14+
15+
context 'unpaged fetch' do
16+
describe '#length' do
17+
it 'shall be the expected length' do
18+
expect(subject.length).to be expected_length
19+
end
20+
end
21+
22+
describe '#total' do
23+
it 'shall be the expected total' do
24+
expect(subject.total).to be expected_length
25+
end
26+
end
27+
end
28+
29+
describe '#reload' do
30+
it 'fetches the correct data' do
31+
expect(faraday).to receive(:get).and_call_original
32+
subject.reload
33+
end
34+
35+
it 'caches the data' do
36+
expect(faraday).to receive(:get).and_call_original
37+
subject.total
38+
subject.total
39+
end
40+
41+
it 'reloads the data' do
42+
expect(faraday).to receive(:get).twice.and_call_original
43+
subject.reload
44+
subject.reload
45+
end
46+
end
47+
48+
describe '#as_array' do
49+
it 'return the correct amount' do
50+
expect(subject.to_a.length).to be expected_length
51+
end
52+
53+
it 'returns Location instances' do
54+
subject.to_a.each do |element|
55+
expect(element).to be_a singular_type
56+
end
57+
end
58+
end
59+
end

0 commit comments

Comments
 (0)