Skip to content

Adds Tenancy/Contacts and Contact Groups #94

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

Closed
wants to merge 15 commits into from
Closed
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ Not all objects which the Netbox API exposes are currently implemented. Implemen
* Tenancy:
* Tenant: `NetboxClientRuby.tenancy.tenants`
* Tenant Groups: `NetboxClientRuby.tenancy.tenant_groups`
* Contact: `NetboxClientRuby.tenancy.contacts`
* Contact Groups: `NetboxClientRuby.tenancy.contact_groups`
* Virtualization:
* Cluster Types: `NetboxClientRuby.virtualization.cluster_types`
* Cluster Groups: `NetboxClientRuby.virtualization.cluster_groups`
Expand Down Expand Up @@ -243,7 +245,7 @@ Before opening a PR, please
* extend the existing specs
* run rspec
* run rubocop and fix your warnings
* check if this README.md file needs adjustments
* check if this `README.md` file needs adjustments

## License

Expand Down
8 changes: 6 additions & 2 deletions lib/netbox_client_ruby/api/tenancy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ module NetboxClientRuby
module Tenancy
{
tenants: Tenants,
tenant_groups: TenantGroups
tenant_groups: TenantGroups,
contacts: Contacts,
contact_groups: ContactGroups
}.each_pair do |method_name, class_name|
define_method(method_name) { class_name.new }
module_function(method_name)
end

{
tenant: Tenant,
tenant_group: TenantGroup
tenant_group: TenantGroup,
contact: Contact,
contact_group: ContactGroup
}.each_pair do |method_name, class_name|
define_method(method_name) { |id| class_name.new id }
module_function(method_name)
Expand Down
15 changes: 15 additions & 0 deletions lib/netbox_client_ruby/api/tenancy/contact.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module NetboxClientRuby
module Tenancy
class Contact
include Entity

id id: :id
deletable true
path 'tenancy/contacts/:id/'
creation_path 'tenancy/contacts/'
object_fields group: proc { |raw_data| ContactGroup.new raw_data['id'] }
end
end
end
15 changes: 15 additions & 0 deletions lib/netbox_client_ruby/api/tenancy/contact_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module NetboxClientRuby
module Tenancy
class ContactGroup
include Entity

id id: :id
deletable true
path 'tenancy/contact-groups/:id/'
creation_path 'tenancy/contact-groups/'
object_fields parent: proc { |raw_data| ContactGroup.new raw_data['id'] }
end
end
end
20 changes: 20 additions & 0 deletions lib/netbox_client_ruby/api/tenancy/contact_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module NetboxClientRuby
module Tenancy
class ContactGroups
include Entities

path 'tenancy/contact-groups/'
data_key 'results'
count_key 'count'
entity_creator :entity_creator

private

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

module NetboxClientRuby
module Tenancy
class Contacts
include Entities

path 'tenancy/contacts/'
data_key 'results'
count_key 'count'
entity_creator :entity_creator

private

def entity_creator(raw_entity)
Contact.new raw_entity['id']
end
end
end
end
7 changes: 7 additions & 0 deletions spec/fixtures/tenancy/contact-group_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"id": 1,
"display": "Parent Customers",
"name": "Parent Customers",
"slug": "parent-customers",
"description": "Parent Contact contact group"
}
15 changes: 15 additions & 0 deletions spec/fixtures/tenancy/contact-group_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": 2,
"display": "Child Customers",
"name": "Child Customers",
"slug": "child-customers",
"description": "Child contact group",
"parent": {
"id": 1,
"url": "http://localhost/api/tenancy/contact_groups/1/",
"display": "Parent Customers",
"name": "Parent Customers",
"slug": "parent-customers",
"description": "Parent Contact contact group"
}
}
30 changes: 30 additions & 0 deletions spec/fixtures/tenancy/contact-groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"count": 2,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"display": "Parent Customers",
"name": "Parent Customers",
"slug": "parent-customers",
"description": "Parent Contact contact group",
"parent": null
},
{
"id": 2,
"display": "Child Customers",
"name": "Child Customers",
"slug": "child-customers",
"description": "Child contact group",
"parent": {
"id": 1,
"url": "http://localhost/api/tenancy/contact_groups/1/",
"display": "Parent Customers",
"name": "Parent Customers",
"slug": "parent-customers",
"description": "Parent Contact contact group"
}
}
]
}
9 changes: 9 additions & 0 deletions spec/fixtures/tenancy/contact_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 1,
"name": "contact1",
"email": "[email protected]",
"group": null,
"description": "",
"comments": "",
"custom_fields": {}
}
9 changes: 9 additions & 0 deletions spec/fixtures/tenancy/contact_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 2,
"name": "contact2",
"email": "[email protected]",
"group": null,
"description": "",
"comments": "",
"custom_fields": {}
}
16 changes: 16 additions & 0 deletions spec/fixtures/tenancy/contact_3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": 3,
"name": "tenant3",
"email": "[email protected]",
"group": {
"id": 2,
"url": "http://localhost/api/tenancy/contact_groups/2/",
"display": "Child Customers",
"name": "Child Customers",
"slug": "child-customers",
"description": "Child contact group"
},
"description": "",
"comments": "",
"custom_fields": {}
}
41 changes: 41 additions & 0 deletions spec/fixtures/tenancy/contacts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"count": 3,
"next": null,
"previous": null,
"results": [
{
"id": 3,
"name": "contact3",
"email": "[email protected]",
"group": {
"id": 2,
"url": "http://localhost/api/tenancy/contact_groups/2/",
"display": "Child Customers",
"name": "Child Customers",
"slug": "child-customers",
"description": "Child contact group"
},
"description": "",
"comments": "",
"custom_fields": {}
},
{
"id": 1,
"name": "contact1",
"email": "[email protected]",
"group": null,
"description": "",
"comments": "",
"custom_fields": {}
},
{
"id": 2,
"name": "contact2",
"email": "[email protected]",
"group": null,
"description": "",
"comments": "",
"custom_fields": {}
}
]
}
151 changes: 151 additions & 0 deletions spec/netbox_client_ruby/api/tenancy/contact_group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe NetboxClientRuby::Tenancy::ContactGroup, faraday_stub: true do
let(:contact_group_id) { 1 }
let(:base_url) { '/api/tenancy/contact-groups/' }
let(:request_url) { "#{base_url}#{contact_group_id}/" }
let(:response) { File.read("spec/fixtures/tenancy/contact-group_#{contact_group_id}.json") }

subject { NetboxClientRuby::Tenancy::ContactGroup.new contact_group_id }

describe '#id' do
it 'shall be the expected id' do
expect(subject.id).to eq(contact_group_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('Parent Customers')
end
end

describe '.parent' do
it 'should be nil' do
expect(subject.parent).to be_nil
end

context 'Child Contact Group' do
let(:contact_group_id) { 2 }

it 'should be a Contact Group object' do
parent_contact_group = subject.parent
expect(parent_contact_group).to be_a NetboxClientRuby::Tenancy::ContactGroup
expect(parent_contact_group.id).to eq(1)
end
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('Parent Customers')
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(:slug) { name }
let(:request_params) { { 'name' => name, 'slug' => slug } }

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

subject do
entity = NetboxClientRuby::Tenancy::ContactGroup.new contact_group_id
entity.name = name
entity.slug = slug
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.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('Parent Customers')
expect(subject.slug).to eq('parent-customers')
end
end

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

subject do
entity = NetboxClientRuby::Tenancy::ContactGroup.new
entity.name = name
entity.slug = slug
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.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('Parent Customers')
expect(subject.slug).to eq('parent-customers')
end
end
end
end
Loading