Skip to content
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
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.13.0
10 changes: 10 additions & 0 deletions lib/netbox_client_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ module NetboxClientRuby
setting :ssl_options, default: { verify: true }
end

def self.load_collection(collection, method_name, class_name)
collection.define_method(method_name) { class_name.new }
collection.__send__(:module_function, method_name)
end

def self.load_entity(entity, method_name, class_name)
entity.define_method(method_name) { |id| class_name.new id }
entity.__send__(:module_function, method_name)
end

def self.circuits
NetboxClientRuby::Circuits
end
Expand Down
6 changes: 2 additions & 4 deletions lib/netbox_client_ruby/api/circuits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ module Circuits
circuit_types: CircuitTypes,
circuit_terminations: CircuitTerminations,
}.each_pair do |method_name, class_name|
define_method(method_name) { class_name.new }
module_function(method_name)
NetboxClientRuby.load_collection(self, method_name, class_name)
end

{
Expand All @@ -18,8 +17,7 @@ module Circuits
circuit_type: CircuitType,
circuit_termination: CircuitTermination,
}.each_pair do |method_name, class_name|
define_method(method_name) { |id| class_name.new id }
module_function(method_name)
NetboxClientRuby.load_entity(self, method_name, class_name)
end
end
end
6 changes: 2 additions & 4 deletions lib/netbox_client_ruby/api/dcim.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ module DCIM
sites: Sites,
virtual_chassis_list: VirtualChassisList,
}.each_pair do |method_name, class_name|
define_method(method_name) { class_name.new }
module_function(method_name)
NetboxClientRuby.load_collection(self, method_name, class_name)
end

{
Expand All @@ -54,8 +53,7 @@ module DCIM
site: Site,
virtual_chassis: VirtualChassis,
}.each_pair do |method_name, class_name|
define_method(method_name) { |id| class_name.new id }
module_function(method_name)
NetboxClientRuby.load_entity(self, method_name, class_name)
end
end
end
6 changes: 2 additions & 4 deletions lib/netbox_client_ruby/api/extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ module Extras
journal_entries: JournalEntries,
tags: Tags,
}.each_pair do |method_name, class_name|
define_method(method_name) { class_name.new }
module_function(method_name)
NetboxClientRuby.load_collection(self, method_name, class_name)
end

{
config_context: ConfigContext,
journal_entry: JournalEntry,
tag: Tag,
}.each_pair do |method_name, class_name|
define_method(method_name) { |id| class_name.new id }
module_function(method_name)
NetboxClientRuby.load_entity(self, method_name, class_name)
end
end
end
6 changes: 2 additions & 4 deletions lib/netbox_client_ruby/api/ipam.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ module IPAM
vlan_groups: VlanGroups,
vrfs: Vrfs,
}.each_pair do |method_name, class_name|
define_method(method_name) { class_name.new }
module_function(method_name)
NetboxClientRuby.load_collection(self, method_name, class_name)
end

{
Expand All @@ -30,8 +29,7 @@ module IPAM
vlan_group: VlanGroup,
vrf: Vrf,
}.each_pair do |method_name, class_name|
define_method(method_name) { |id| class_name.new id }
module_function(method_name)
NetboxClientRuby.load_entity(self, method_name, class_name)
end
end
end
6 changes: 2 additions & 4 deletions lib/netbox_client_ruby/api/secrets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ module Secrets
generate_rsa_key_pair: RSAKeyPair,
get_session_key: SessionKey,
}.each_pair do |method_name, class_name|
define_method(method_name) { class_name.new }
module_function(method_name)
NetboxClientRuby.load_collection(self, method_name, class_name)
end

{
secret_role: SecretRole,
secret: Secret,
}.each_pair do |method_name, class_name|
define_method(method_name) { |id| class_name.new id }
module_function(method_name)
NetboxClientRuby.load_entity(self, method_name, class_name)
end

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

{
Expand All @@ -18,8 +17,7 @@ module Tenancy
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)
NetboxClientRuby.load_entity(self, method_name, class_name)
end
end
end
6 changes: 2 additions & 4 deletions lib/netbox_client_ruby/api/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ module Virtualization
virtual_machines: VirtualMachines,
interfaces: Interfaces,
}.each_pair do |method_name, class_name|
define_method(method_name) { class_name.new }
module_function(method_name)
NetboxClientRuby.load_collection(self, method_name, class_name)
end

{
Expand All @@ -20,8 +19,7 @@ module Virtualization
virtual_machine: VirtualMachine,
interface: Interface,
}.each_pair do |method_name, class_name|
define_method(method_name) { |id| class_name.new id }
module_function(method_name)
NetboxClientRuby.load_entity(self, method_name, class_name)
end
end
end
38 changes: 23 additions & 15 deletions lib/netbox_client_ruby/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,12 @@ module ClassMethods
# id 'an_id_field' => 'id_field_in_data'
# id an_id_field: 'id_field_in_data', :another_id_field: 'id_field2_in_data'
#
def id(*fields) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
def id(*fields)
return @id_fields if @id_fields

raise ArgumentError, "No 'id' was defined, but one is expected." if fields.empty?

@id_fields = {}
if fields.first.is_a?(Hash)
fields.first.each { |key, value| @id_fields[key.to_s] = value.to_s }
else
fields.map(&:to_s).each do |field|
field_as_string = field.to_s
@id_fields[field_as_string] = field_as_string
end
end

@id_fields.each_key do |field|
define_method(field) { instance_variable_get :"@#{field}" }
end

@id_fields = load_attributes(fields)
@id_fields
end

Expand Down Expand Up @@ -88,6 +75,8 @@ def object_fields(*fields_to_class_map)
end
end

private

def sanitize_mapping(fields_to_class_map) # rubocop:disable Metrics/MethodLength
fields_map = {}
fields_to_class_map.each do |field_definition|
Expand All @@ -101,6 +90,25 @@ def sanitize_mapping(fields_to_class_map) # rubocop:disable Metrics/MethodLength
end
fields_map
end

def load_attributes(fields) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
id_fields = {}

if fields.first.is_a?(Hash)
fields.first.each { |key, value| id_fields[key.to_s] = value.to_s }
else
fields.map(&:to_s).each do |field|
field_as_string = field.to_s
id_fields[field_as_string] = field_as_string
end
end

id_fields.each_key do |field|
define_method(field) { instance_variable_get :"@#{field}" }
end

id_fields
end
end

def initialize(given_values = nil) # rubocop:disable Metrics/MethodLength
Expand Down
11 changes: 11 additions & 0 deletions lib/netbox_client_ruby/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module NetboxClientRuby
def self.gem_version
Gem::Version.new VERSION::STRING
end

module VERSION
STRING = File.read(File.expand_path('../../VERSION', __dir__)).strip
end
end
8 changes: 4 additions & 4 deletions netbox-client-ruby.gemspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

require_relative 'lib/netbox_client_ruby/version'

Gem::Specification.new do |spec|
spec.name = 'netbox-client-ruby'
spec.version = `git describe --tags --match="v[0-9]*" --abbrev=0`.strip.delete_prefix("v")
spec.version = NetboxClientRuby::VERSION::STRING

spec.summary = 'A read/write client for Netbox v2.'
spec.homepage = 'https://github.com/ninech/netbox-client-ruby'
Expand All @@ -19,9 +21,7 @@ Gem::Specification.new do |spec|
spec.metadata['bug_tracker_uri'] = 'https://github.com/ninech/netbox-client-ruby/issues'
end

spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.files = Dir['README.md', 'LICENSE.txt', 'VERSION', 'lib/**/*.rb']

spec.required_ruby_version = '>= 2.7.0'

Expand Down