netbox-client-ruby 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/.rspec +2 -0
  4. data/.ruby-version +1 -0
  5. data/.travis.yml +8 -0
  6. data/Dockerfile +12 -0
  7. data/Gemfile +4 -0
  8. data/Gemfile.lock +75 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +138 -0
  11. data/Rakefile +6 -0
  12. data/VERSION +1 -0
  13. data/bin/console +14 -0
  14. data/bin/setup +8 -0
  15. data/docker-compose.test.yml +5 -0
  16. data/docker-compose.yml +37 -0
  17. data/docker/start.sh +3 -0
  18. data/docker/start.test.sh +3 -0
  19. data/lib/netbox-client-ruby.rb +1 -0
  20. data/lib/netbox_client_ruby.rb +25 -0
  21. data/lib/netbox_client_ruby/api.rb +18 -0
  22. data/lib/netbox_client_ruby/api/dcim.rb +55 -0
  23. data/lib/netbox_client_ruby/api/dcim/device.rb +31 -0
  24. data/lib/netbox_client_ruby/api/dcim/device_role.rb +12 -0
  25. data/lib/netbox_client_ruby/api/dcim/device_roles.rb +19 -0
  26. data/lib/netbox_client_ruby/api/dcim/device_type.rb +26 -0
  27. data/lib/netbox_client_ruby/api/dcim/device_types.rb +19 -0
  28. data/lib/netbox_client_ruby/api/dcim/devices.rb +19 -0
  29. data/lib/netbox_client_ruby/api/dcim/interface.rb +14 -0
  30. data/lib/netbox_client_ruby/api/dcim/interfaces.rb +19 -0
  31. data/lib/netbox_client_ruby/api/dcim/manufacturer.rb +12 -0
  32. data/lib/netbox_client_ruby/api/dcim/manufacturers.rb +19 -0
  33. data/lib/netbox_client_ruby/api/dcim/platform.rb +12 -0
  34. data/lib/netbox_client_ruby/api/dcim/platforms.rb +19 -0
  35. data/lib/netbox_client_ruby/api/dcim/rack.rb +12 -0
  36. data/lib/netbox_client_ruby/api/dcim/racks.rb +19 -0
  37. data/lib/netbox_client_ruby/api/dcim/region.rb +13 -0
  38. data/lib/netbox_client_ruby/api/dcim/regions.rb +19 -0
  39. data/lib/netbox_client_ruby/api/dcim/site.rb +15 -0
  40. data/lib/netbox_client_ruby/api/dcim/sites.rb +19 -0
  41. data/lib/netbox_client_ruby/api/ipam.rb +53 -0
  42. data/lib/netbox_client_ruby/api/ipam/aggregate.rb +14 -0
  43. data/lib/netbox_client_ruby/api/ipam/aggregates.rb +19 -0
  44. data/lib/netbox_client_ruby/api/ipam/ip_address.rb +42 -0
  45. data/lib/netbox_client_ruby/api/ipam/ip_addresses.rb +19 -0
  46. data/lib/netbox_client_ruby/api/ipam/prefix.rb +47 -0
  47. data/lib/netbox_client_ruby/api/ipam/prefixes.rb +19 -0
  48. data/lib/netbox_client_ruby/api/ipam/rir.rb +12 -0
  49. data/lib/netbox_client_ruby/api/ipam/rirs.rb +19 -0
  50. data/lib/netbox_client_ruby/api/ipam/role.rb +12 -0
  51. data/lib/netbox_client_ruby/api/ipam/roles.rb +19 -0
  52. data/lib/netbox_client_ruby/api/ipam/vlan.rb +40 -0
  53. data/lib/netbox_client_ruby/api/ipam/vlan_group.rb +14 -0
  54. data/lib/netbox_client_ruby/api/ipam/vlan_groups.rb +19 -0
  55. data/lib/netbox_client_ruby/api/ipam/vlans.rb +19 -0
  56. data/lib/netbox_client_ruby/api/ipam/vrf.rb +14 -0
  57. data/lib/netbox_client_ruby/api/ipam/vrfs.rb +19 -0
  58. data/lib/netbox_client_ruby/api/tenancy.rb +25 -0
  59. data/lib/netbox_client_ruby/api/tenancy/tenant.rb +14 -0
  60. data/lib/netbox_client_ruby/api/tenancy/tenant_group.rb +12 -0
  61. data/lib/netbox_client_ruby/api/tenancy/tenant_groups.rb +19 -0
  62. data/lib/netbox_client_ruby/api/tenancy/tenants.rb +19 -0
  63. data/lib/netbox_client_ruby/communication.rb +74 -0
  64. data/lib/netbox_client_ruby/connection.rb +33 -0
  65. data/lib/netbox_client_ruby/entities.rb +200 -0
  66. data/lib/netbox_client_ruby/entity.rb +345 -0
  67. data/lib/netbox_client_ruby/error/client_error.rb +4 -0
  68. data/lib/netbox_client_ruby/error/local_error.rb +4 -0
  69. data/lib/netbox_client_ruby/error/remote_error.rb +4 -0
  70. data/netbox-client-ruby.gemspec +43 -0
  71. data/netbox.env +17 -0
  72. metadata +255 -0
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/ipam/ip_address'
3
+
4
+ module NetboxClientRuby
5
+ class IpAddresses
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'ipam/ip-addresses.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::IpAddress.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,47 @@
1
+ require 'netbox_client_ruby/entity'
2
+ require 'netbox_client_ruby/api/ipam/role'
3
+ require 'netbox_client_ruby/api/dcim/site'
4
+ require 'netbox_client_ruby/api/ipam/vrf'
5
+ require 'netbox_client_ruby/api/ipam/vlan'
6
+ require 'netbox_client_ruby/api/ipam/vlan_group'
7
+ require 'netbox_client_ruby/api/tenancy/tenant'
8
+
9
+ module NetboxClientRuby
10
+ class Prefix
11
+ include NetboxClientRuby::Entity
12
+
13
+ id id: :id
14
+ deletable true
15
+ path 'ipam/prefixes/:id.json'
16
+ creation_path 'ipam/prefixes/'
17
+ object_fields(
18
+ site: proc { |raw_data| NetboxClientRuby::Site.new raw_data['id'] },
19
+ vrf: proc { |raw_data| NetboxClientRuby::Vrf.new raw_data['id'] },
20
+ tenant: proc { |raw_data| NetboxClientRuby::Tenant.new raw_data['id'] },
21
+ vlan: proc { |raw_data| NetboxClientRuby::Vlan.new raw_data['id'] },
22
+ status: proc { |raw_data| NetboxClientRuby::PrefixStatus.new raw_data['value'] },
23
+ role: proc { |raw_data| NetboxClientRuby::Role.new raw_data['id'] }
24
+ )
25
+ readonly_fields :display_name
26
+ end
27
+
28
+ class PrefixStatus
29
+ attr_reader :value, :label
30
+
31
+ def initialize(status_value)
32
+ @value = status_value
33
+ @label = case status_value
34
+ when 0 then
35
+ 'Container'.freeze
36
+ when 1 then
37
+ 'Active'.freeze
38
+ when 2 then
39
+ 'Reserved'.freeze
40
+ when 3 then
41
+ 'Deprecated'.freeze
42
+ else
43
+ 'UNDEFINED'.freeze
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/ipam/prefix'
3
+
4
+ module NetboxClientRuby
5
+ class Prefixes
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'ipam/prefixes.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::Prefix.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require 'netbox_client_ruby/entity'
2
+
3
+ module NetboxClientRuby
4
+ class Rir
5
+ include NetboxClientRuby::Entity
6
+
7
+ id id: :id
8
+ deletable true
9
+ path 'ipam/rirs/:id.json'
10
+ creation_path 'ipam/rirs/'
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/ipam/rir'
3
+
4
+ module NetboxClientRuby
5
+ class Rirs
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'ipam/rirs.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::Rir.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require 'netbox_client_ruby/entity'
2
+
3
+ module NetboxClientRuby
4
+ class Role
5
+ include NetboxClientRuby::Entity
6
+
7
+ id id: :id
8
+ deletable true
9
+ path 'ipam/roles/:id.json'
10
+ creation_path 'ipam/roles/'
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/ipam/role'
3
+
4
+ module NetboxClientRuby
5
+ class Roles
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'ipam/roles.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::Role.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ require 'netbox_client_ruby/entity'
2
+ require 'netbox_client_ruby/api/tenancy/tenant'
3
+ require 'netbox_client_ruby/api/ipam/role'
4
+ require 'netbox_client_ruby/api/ipam/vlan_group'
5
+
6
+ module NetboxClientRuby
7
+ class Vlan
8
+ include NetboxClientRuby::Entity
9
+
10
+ id id: :id
11
+ deletable true
12
+ path 'ipam/vlans/:id.json'
13
+ creation_path 'ipam/vlans/'
14
+ object_fields(
15
+ tenant: proc { |raw_data| NetboxClientRuby::Tenant.new raw_data['id'] },
16
+ role: proc { |raw_data| NetboxClientRuby::Role.new raw_data['id'] },
17
+ status: proc { |raw_data| NetboxClientRuby::VlanStatus.new raw_data['value'] },
18
+ group: proc { |raw_data| NetboxClientRuby::VlanGroup.new raw_data['id'] }
19
+ )
20
+ readonly_fields :display_name
21
+ end
22
+
23
+ class VlanStatus
24
+ attr_reader :value, :label
25
+
26
+ def initialize(status_value)
27
+ @value = status_value
28
+ @label = case status_value
29
+ when 1 then
30
+ 'Active'.freeze
31
+ when 2 then
32
+ 'Reserved'.freeze
33
+ when 3 then
34
+ 'Deprecated'.freeze
35
+ else
36
+ 'UNDEFINED'.freeze
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ require 'netbox_client_ruby/entity'
2
+ require 'netbox_client_ruby/api/dcim/site'
3
+
4
+ module NetboxClientRuby
5
+ class VlanGroup
6
+ include NetboxClientRuby::Entity
7
+
8
+ id id: :id
9
+ deletable true
10
+ path 'ipam/vlan-groups/:id.json'
11
+ creation_path 'ipam/vlan-groups/'
12
+ object_fields site: proc { |raw_data| NetboxClientRuby::Site.new raw_data['id'] }
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/ipam/vlan_group'
3
+
4
+ module NetboxClientRuby
5
+ class VlanGroups
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'ipam/vlan-groups.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::VlanGroup.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/ipam/vlan'
3
+
4
+ module NetboxClientRuby
5
+ class Vlans
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'ipam/vlans.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::Vlan.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ require 'netbox_client_ruby/entity'
2
+ require 'netbox_client_ruby/api/tenancy/tenant'
3
+
4
+ module NetboxClientRuby
5
+ class Vrf
6
+ include NetboxClientRuby::Entity
7
+
8
+ id id: :id
9
+ deletable true
10
+ path 'ipam/vrfs/:id.json'
11
+ creation_path 'ipam/vrfs/'
12
+ object_fields tenant: proc { |raw_data| NetboxClientRuby::Tenant.new raw_data['id'] }
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/ipam/vrf'
3
+
4
+ module NetboxClientRuby
5
+ class Vrfs
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'ipam/vrfs.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::Vrf.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,25 @@
1
+ require 'netbox_client_ruby/api/tenancy/tenant'
2
+ require 'netbox_client_ruby/api/tenancy/tenants'
3
+ require 'netbox_client_ruby/api/tenancy/tenant_group'
4
+ require 'netbox_client_ruby/api/tenancy/tenant_groups'
5
+ require 'netbox_client_ruby/communication'
6
+
7
+ module NetboxClientRuby
8
+ class Tenancy
9
+ def tenants
10
+ Tenants.new
11
+ end
12
+
13
+ def tenant(id)
14
+ Tenant.new id
15
+ end
16
+
17
+ def tenant_groups
18
+ TenantGroups.new
19
+ end
20
+
21
+ def tenant_group
22
+ TenantGroup.new
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,14 @@
1
+ require 'netbox_client_ruby/entity'
2
+ require 'netbox_client_ruby/api/tenancy/tenant_group'
3
+
4
+ module NetboxClientRuby
5
+ class Tenant
6
+ include NetboxClientRuby::Entity
7
+
8
+ id id: :id
9
+ deletable true
10
+ path 'tenancy/tenants/:id.json'
11
+ creation_path 'tenancy/tenants/'
12
+ object_fields group: proc { |raw_data| NetboxClientRuby::TenantGroup.new raw_data['id'] }
13
+ end
14
+ end
@@ -0,0 +1,12 @@
1
+ require 'netbox_client_ruby/entity'
2
+
3
+ module NetboxClientRuby
4
+ class TenantGroup
5
+ include NetboxClientRuby::Entity
6
+
7
+ id id: :id
8
+ deletable true
9
+ path 'tenancy/tenant-groups/:id.json'
10
+ creation_path 'tenancy/tenant-groups/'
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/tenancy/tenant_group'
3
+
4
+ module NetboxClientRuby
5
+ class TenantGroups
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'tenancy/tenant-groups.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::TenantGroup.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/tenancy/tenant'
3
+
4
+ module NetboxClientRuby
5
+ class Tenants
6
+ include NetboxClientRuby::Entities
7
+
8
+ path 'tenancy/tenants.json'
9
+ data_key 'results'
10
+ count_key 'count'
11
+ entity_creator :entity_creator
12
+
13
+ private
14
+
15
+ def entity_creator(raw_entity)
16
+ NetboxClientRuby::Tenant.new raw_entity['id']
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,74 @@
1
+ require 'netbox_client_ruby/error/client_error'
2
+ require 'netbox_client_ruby/error/remote_error'
3
+ require 'netbox_client_ruby/connection'
4
+
5
+ module NetboxClientRuby
6
+ module Communication
7
+ def response(response)
8
+ return nil if response.status == 304
9
+ return {} if response.status == 204
10
+
11
+ raise_on_http_error response.status
12
+
13
+ read response
14
+ end
15
+
16
+ def connection
17
+ NetboxClientRuby::Connection.new
18
+ end
19
+
20
+ def hash_to_object(hash)
21
+ objectified_class = Class.new
22
+ objectified_instance = objectified_class.new
23
+ hash.each do |k, v|
24
+ variable_name = sanitize_variable_name(k)
25
+ variable_name = "_#{variable_name}" if objectified_instance.methods.map(&:to_s).include?(variable_name)
26
+
27
+ objectified_instance.instance_variable_set("@#{variable_name}", v)
28
+ objectified_class.send(:define_method, variable_name, proc { instance_variable_get("@#{variable_name}") })
29
+ end
30
+ objectified_instance
31
+ end
32
+
33
+ private
34
+
35
+ def read(response)
36
+ response.body
37
+ end
38
+
39
+ def raise_on_http_error(status)
40
+ case status
41
+ when 200..399 then
42
+ when 400..499 then
43
+ raise_on_http_client_error status
44
+ when 500..599 then
45
+ raise NetboxClientRuby::RemoteError, "#{status} Remote Error"
46
+ else
47
+ raise NetboxClientRuby::RemoteError, "#{status} Unknown Error Code"
48
+ end
49
+ end
50
+
51
+ def raise_on_http_client_error(status)
52
+ case status
53
+ when 400 then
54
+ raise NetboxClientRuby::ClientError, '400 Bad Request'
55
+ when 401 then
56
+ raise NetboxClientRuby::ClientError, '401 Unauthorized'
57
+ when 403 then
58
+ raise NetboxClientRuby::ClientError, '403 Forbidden'
59
+ when 405 then
60
+ raise NetboxClientRuby::ClientError, '405 Method Not Allowed'
61
+ when 415 then
62
+ raise NetboxClientRuby::ClientError, '415 Unsupported Media Type'
63
+ when 429 then
64
+ raise NetboxClientRuby::ClientError, '429 Too Many Requests'
65
+ else
66
+ raise NetboxClientRuby::ClientError, "#{status} Request Error"
67
+ end
68
+ end
69
+
70
+ def sanitize_variable_name(raw_name)
71
+ raw_name.gsub(/[^a-zA-Z0-9_]/, '_')
72
+ end
73
+ end
74
+ end