Skip to content

Commit

Permalink
Add config contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
btriller authored and thde committed Nov 29, 2021
1 parent 3e824f5 commit 954669a
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ Not all objects which the Netbox API exposes are currently implemented. Implemen
* Virtual Chassis: `NetboxClientRuby.dcim.virtual_chassis_list`
(⚠️ Exception: The access is different and the class is called `VirtualChassisList` because the plural and singular
names are the same and this poses a conflict.)
* Extras:
* Config Contexts: `NetboxClientRuby.extras.config_contexts`
* Journal Entries: `NetboxClientRuby.extras.journal_entries`
* Tags: `NetboxClientRuby.extras.tags`
* IPAM:
* Aggregates: `NetboxClientRuby.ipam.aggregates`
* IP Addresses: `NetboxClientRuby.ipam.ip_addresses`
Expand Down
4 changes: 4 additions & 0 deletions lib/netbox_client_ruby/api/extras.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'netbox_client_ruby/api/extras/config_context'
require 'netbox_client_ruby/api/extras/config_contexts'
require 'netbox_client_ruby/api/extras/journal_entry'
require 'netbox_client_ruby/api/extras/journal_entries'
require 'netbox_client_ruby/api/extras/tag'
Expand All @@ -6,6 +8,7 @@
module NetboxClientRuby
module Extras
{
config_contexts: ConfigContexts,
journal_entries: JournalEntries,
tags: Tags
}.each_pair do |method_name, class_name|
Expand All @@ -14,6 +17,7 @@ module Extras
end

{
config_context: ConfigContext,
journal_entry: JournalEntry,
tag: Tag
}.each_pair do |method_name, class_name|
Expand Down
25 changes: 25 additions & 0 deletions lib/netbox_client_ruby/api/extras/config_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'netbox_client_ruby/entity'

module NetboxClientRuby
module Extras
class ConfigContext
include Entity

id id: :id
deletable true
path 'extras/config-contexts/:id.json'
creation_path 'extras/config-contexts/'
object_fields(
regions: proc { |raw_data| DCIM::Region.new raw_data['id'] },
sites: proc { |raw_data| DCIM::Site.new raw_data['id'] },
roles: proc { |raw_data| DCIM::DeviceRole.new raw_data['id'] },
platforms: proc { |raw_data| DCIM::Platform.new raw_data['id'] },
cluster_groups: proc { |raw_data| Virtualization::ClusterGroup.new raw_data['id'] },
clusters: proc { |raw_data| Virtualization::Cluster.new raw_data['id'] },
tenant_groups: proc { |raw_data| Tenancy::TenantGroup.new raw_data['id'] },
tenants: proc { |raw_data| Tenancy::Tenant.new raw_data['id'] },
tags: proc { |raw_data| Tag.new raw_data['id'] }
)
end
end
end
21 changes: 21 additions & 0 deletions lib/netbox_client_ruby/api/extras/config_contexts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'netbox_client_ruby/entities'
require 'netbox_client_ruby/api/extras/config_context'

module NetboxClientRuby
module Extras
class ConfigContexts
include Entities

path 'extras/config-contexts.json'
data_key 'results'
count_key 'count'
entity_creator :entity_creator

private

def entity_creator(raw_entity)
ConfigContext.new raw_entity['id']
end
end
end
end
18 changes: 18 additions & 0 deletions spec/fixtures/extras/config_context_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"id": 1,
"url": "http://netbox/api/extras/config-contexts/1/",
"name": "Production",
"weight": 1000,
"description": "",
"is_active": true,
"regions": [],
"sites": [],
"roles": [],
"platforms": [],
"cluster_groups": [],
"clusters": [],
"tenant_groups": [],
"tenants": [],
"tags": [],
"data": {}
}
25 changes: 25 additions & 0 deletions spec/fixtures/extras/config_contexts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"count": 1,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"url": "http://netbox/api/extras/config-contexts/1/",
"name": "Production",
"weight": 1000,
"description": "",
"is_active": true,
"regions": [],
"sites": [],
"roles": [],
"platforms": [],
"cluster_groups": [],
"clusters": [],
"tenant_groups": [],
"tenants": [],
"tags": [],
"data": {}
}
]
}
137 changes: 137 additions & 0 deletions spec/netbox_client_ruby/api/extras/config_context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
require 'spec_helper'

module NetboxClientRuby
module Extras
describe ConfigContext, faraday_stub: true do
let(:entity_id) { 1 }
let(:base_url) { '/api/extras/config-contexts/' }
let(:request_url) { "#{base_url}#{entity_id}.json" }
let(:response) { File.read("spec/fixtures/extras/config_context_#{entity_id}.json") }

subject { ConfigContext.new entity_id }

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

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

subject.name
end

it 'shall be the expected name' do
expect(subject.name).to eq('Production')
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) { { 'name' => 'noob' } }

it 'should update the object' do
expect(faraday).to receive(request_method).and_call_original
expect(subject.update(name: 'noob').name).to eq('Production')
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(:name) { 'foobar' }
let(:weight) { 1000 }
let(:request_params) { { 'name' => name, 'weight' => weight } }

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

subject do
entity = ConfigContext.new entity_id
entity.name = name
entity.weight = weight
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.name).to eq(name)
expect(subject.weight).to eq(weight)
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('Production')
expect(subject.weight).to eq(1000)
end
end

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

subject do
entity = ConfigContext.new
entity.name = name
entity.weight = weight
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.name).to eq(name)
expect(subject.weight).to eq(weight)
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('Production')
expect(subject.weight).to eq(1000)
end
end
end
end
end
end
61 changes: 61 additions & 0 deletions spec/netbox_client_ruby/api/extras/config_contexts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
require 'spec_helper'

module NetboxClientRuby
module Extras
describe ConfigContexts, faraday_stub: true do
let(:expected_number_of_items) { 1 }
let(:expected_singular_type) { ConfigContext }

let(:response) { File.read('spec/fixtures/extras/config_contexts.json') }
let(:request_url) { '/api/extras/config-contexts.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_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
end
end

0 comments on commit 954669a

Please sign in to comment.