netbox-client-ruby 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae1d31ceba9d0b8990392ec90a28fa14e1ccad11
4
- data.tar.gz: 242bbeccaba7d294b425f7dca4dda68cec7ef7ad
3
+ metadata.gz: 06b586e73594396dd7fdcffce33dde0a8a7c5ae6
4
+ data.tar.gz: 6c10c27dc81db8f26a3f5a5c319ef06b7d2b8688
5
5
  SHA512:
6
- metadata.gz: 16e34743cc6866cdd3a3916ddc1706af081214416b592316a06bb22db034e8f728c6bd74373d69f5f0add5ce639aed31d5860d824ba0c7ad9e163888d40d7564
7
- data.tar.gz: f906ac593eac5c959de0ad5efb8561dced8546e76408240d46b0ef3545f3ff69ca679b00189d83397fcf17396896cdb52de809682899da445116826c924a70b0
6
+ metadata.gz: 792d23f9938ea1e8a974db2a53fd67d6de86899f2a1f4cce6e5778e16d7ea1f3a72f97cfff2c738d03b59fe62a33e8d0faa4c8c1ff6e2942b660d4ed1d07db0d
7
+ data.tar.gz: 7ee301dccf03b6c31691871416d65cdb8dded5a4c46f4a26cd313abe2b23855e9c6112289fea316055c6f70dc436184d43182b585db1eca8d5e0399293255f03
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- netbox-client-ruby (0.1.2)
4
+ netbox-client-ruby (0.1.3)
5
5
  dry-configurable (~> 0.1)
6
6
  faraday (>= 0.11.0)
7
7
  faraday-detailed_logger (~> 2.1)
@@ -18,7 +18,7 @@ GEM
18
18
  diff-lcs (1.3)
19
19
  dry-configurable (0.7.0)
20
20
  concurrent-ruby (~> 1.0)
21
- faraday (0.12.2)
21
+ faraday (0.13.1)
22
22
  multipart-post (>= 1.2, < 3)
23
23
  faraday-detailed_logger (2.1.2)
24
24
  faraday (~> 0.8)
data/README.md CHANGED
@@ -76,6 +76,32 @@ puts "The first site is called #{first_site.name}."
76
76
  devices_of_site = NetboxClientRuby.dcim.devices.filter(site: first_site.slug)
77
77
  puts "#{devices_of_site.total} devices belong to the site. #{devices_of_site}.length devices have been fetched."
78
78
 
79
+ # get a site by id
80
+ s = NetboxClientRuby.dcim.site(1)
81
+
82
+ # update a site
83
+ s.update(name: 'Zurich', slug: 'zrh')
84
+
85
+ # update a site (alternative)
86
+ s.name = 'Amsterdam'
87
+ s.slug = 'ams'
88
+ s.save
89
+
90
+ # create a site
91
+ new_s = NetboxClientRuby::DCIM::Site.new
92
+ new_s.name = 'Berlin'
93
+ new_s.slug = 'ber'
94
+ new_s.save
95
+
96
+ # create a site (alternative)
97
+ new_s = NetboxClientRuby::DCIM::Site
98
+ .new(name: 'Berlin', slug: 'ber')
99
+ .save
100
+
101
+ # delete a site
102
+ s = NetboxClientRuby.dcim.site(1)
103
+ s.delete
104
+
79
105
  # working with secrets
80
106
  secrets = NetboxClientRuby.secrets.secrets
81
107
  puts "#{secrets.total} secrets are in your Netbox."
@@ -106,6 +132,8 @@ Not all objects which the Netbox API exposes are currently implemented. Implemen
106
132
  * Interfaces
107
133
  * Manufacturers
108
134
  * Platforms
135
+ * Power Outlets
136
+ * Power Ports
109
137
  * Racks
110
138
  * Regions
111
139
  * Sites
@@ -126,7 +154,7 @@ Not all objects which the Netbox API exposes are currently implemented. Implemen
126
154
  * Tenancy:
127
155
  * Tenant
128
156
  * Tenant Groups
129
-
157
+
130
158
  If you can't find the object you need, also check
131
159
  [the source code](https://github.com/ninech/netbox-client-ruby/tree/master/lib/netbox_client_ruby/api)
132
160
  if it was added in the meantime without the list above having been updated.
@@ -159,6 +187,6 @@ The gem is available as open source under the terms of the [MIT License](http://
159
187
 
160
188
  ## About
161
189
 
162
- This gem is currently maintained and funded by [nine.ch](https://nine.ch).
190
+ This gem is currently maintained and funded by [nine](https://nine.ch).
163
191
 
164
- [![nine.ch logo](https://blog.nine.ch/assets/logo.png)](https://nine.ch)
192
+ [![logo of the company 'nine'](https://logo.apps.at-nine.ch/Dmqied_eSaoBMQwk3vVgn4UIgDo=/trim/500x0/logo_claim.png)](https://www.nine.ch)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/docker-compose.yml CHANGED
@@ -1,25 +1,5 @@
1
1
  version: '2'
2
2
  services:
3
- app:
4
- build:
5
- context: .
6
- args:
7
- RUBY_VERSION: 2.3.4
8
- image: netbox-client-ruby:2.3.4
9
- depends_on:
10
- - nginx
11
- volumes:
12
- - .:/app
13
- app24:
14
- build:
15
- context: .
16
- args:
17
- RUBY_VERSION: 2.4.1
18
- image: netbox-client-ruby:2.4.1
19
- depends_on:
20
- - nginx
21
- volumes:
22
- - .:/app
23
3
  nginx:
24
4
  image: nginx:1.11-alpine
25
5
  command: nginx -g 'daemon off;' -c /etc/netbox-nginx/nginx.conf
@@ -12,6 +12,10 @@ require 'netbox_client_ruby/api/dcim/manufacturer'
12
12
  require 'netbox_client_ruby/api/dcim/manufacturers'
13
13
  require 'netbox_client_ruby/api/dcim/platform'
14
14
  require 'netbox_client_ruby/api/dcim/platforms'
15
+ require 'netbox_client_ruby/api/dcim/power_outlet'
16
+ require 'netbox_client_ruby/api/dcim/power_outlets'
17
+ require 'netbox_client_ruby/api/dcim/power_port'
18
+ require 'netbox_client_ruby/api/dcim/power_ports'
15
19
  require 'netbox_client_ruby/api/dcim/rack'
16
20
  require 'netbox_client_ruby/api/dcim/racks'
17
21
  require 'netbox_client_ruby/api/dcim/region'
@@ -30,6 +34,8 @@ module NetboxClientRuby
30
34
  inventory_items: InventoryItems,
31
35
  manufacturers: Manufacturers,
32
36
  platforms: Platforms,
37
+ power_outlets: PowerOutlets,
38
+ power_ports: PowerPorts,
33
39
  racks: Racks,
34
40
  regions: Regions,
35
41
  sites: Sites
@@ -46,6 +52,8 @@ module NetboxClientRuby
46
52
  inventory_item: InventoryItem,
47
53
  manufacturer: Manufacturer,
48
54
  platform: Platform,
55
+ power_outlet: PowerOutlet,
56
+ power_port: PowerPort,
49
57
  rack: Rack,
50
58
  region: Region,
51
59
  site: Site
@@ -0,0 +1,18 @@
1
+ require 'netbox_client_ruby/entity'
2
+ require 'netbox_client_ruby/api/dcim/device'
3
+ require 'netbox_client_ruby/api/dcim/power_port'
4
+
5
+ module NetboxClientRuby
6
+ module DCIM
7
+ class PowerOutlet
8
+ include Entity
9
+
10
+ id id: :id
11
+ deletable true
12
+ path 'dcim/power-outlets/:id.json'
13
+ creation_path 'dcim/power-outlets/'
14
+ object_fields device: proc { |raw_data| Device.new raw_data['id'] }
15
+ object_fields connected_port: proc { |raw_data| PowerPort.new raw_data }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/dcim/interface'
3
+
4
+ module NetboxClientRuby
5
+ module DCIM
6
+ class PowerOutlets
7
+ include Entities
8
+
9
+ path 'dcim/power-outlets.json'
10
+ data_key 'results'
11
+ count_key 'count'
12
+ entity_creator :entity_creator
13
+
14
+ private
15
+
16
+ def entity_creator(raw_entity)
17
+ PowerOutlet.new raw_entity['id']
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ require 'netbox_client_ruby/entity'
2
+ require 'netbox_client_ruby/api/dcim/device'
3
+ require 'netbox_client_ruby/api/dcim/power_outlet'
4
+
5
+ module NetboxClientRuby
6
+ module DCIM
7
+ class PowerPort
8
+ include Entity
9
+
10
+ id id: :id
11
+ deletable true
12
+ path 'dcim/power-ports/:id.json'
13
+ creation_path 'dcim/power-ports/'
14
+ object_fields device: proc { |raw_data| Device.new raw_data['id'] }
15
+ object_fields power_outlet: proc { |raw_data| PowerOutlet.new raw_data['id'] }
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'netbox_client_ruby/entities'
2
+ require 'netbox_client_ruby/api/dcim/interface'
3
+
4
+ module NetboxClientRuby
5
+ module DCIM
6
+ class PowerPorts
7
+ include Entities
8
+
9
+ path 'dcim/power-ports.json'
10
+ data_key 'results'
11
+ count_key 'count'
12
+ entity_creator :entity_creator
13
+
14
+ private
15
+
16
+ def entity_creator(raw_entity)
17
+ PowerPort.new raw_entity['id']
18
+ end
19
+ end
20
+ end
21
+ end
@@ -8,7 +8,7 @@ module NetboxClientRuby
8
8
  return nil if response.status == 304
9
9
  return {} if response.status == 204
10
10
 
11
- raise_on_http_error response.status
11
+ raise_on_http_error response
12
12
 
13
13
  read response
14
14
  end
@@ -32,43 +32,60 @@ module NetboxClientRuby
32
32
 
33
33
  private
34
34
 
35
+ def sanitize_variable_name(raw_name)
36
+ raw_name.gsub(/[^a-zA-Z0-9_]/, '_')
37
+ end
38
+
35
39
  def read(response)
36
40
  response.body
37
41
  end
38
42
 
39
- def raise_on_http_error(status)
43
+ def raise_on_http_error(response)
44
+ status = response.status
45
+ body = response.body
46
+
40
47
  case status
41
48
  when 200..399 then
42
49
  when 400..499 then
43
- raise_on_http_client_error status
50
+ raise_on_http_client_error response
44
51
  when 500..599 then
45
- raise NetboxClientRuby::RemoteError, "#{status} Remote Error"
52
+ raise NetboxClientRuby::RemoteError, "#{status} Remote Error#{formatted_body(body)}"
46
53
  else
47
- raise NetboxClientRuby::RemoteError, "#{status} Unknown Error Code"
54
+ raise NetboxClientRuby::RemoteError, "#{status} Unknown Error Code#{formatted_body(body)}"
48
55
  end
49
56
  end
50
57
 
51
- def raise_on_http_client_error(status)
58
+ def raise_on_http_client_error(response)
59
+ status = response.status
60
+ body = response.body
61
+
52
62
  case status
53
63
  when 400 then
54
- raise NetboxClientRuby::ClientError, '400 Bad Request'
64
+ raise_client_error '400 Bad Request', body
55
65
  when 401 then
56
- raise NetboxClientRuby::ClientError, '401 Unauthorized'
66
+ raise_client_error '401 Unauthorized', body
57
67
  when 403 then
58
- raise NetboxClientRuby::ClientError, '403 Forbidden'
68
+ raise_client_error '403 Forbidden', body
59
69
  when 405 then
60
- raise NetboxClientRuby::ClientError, '405 Method Not Allowed'
70
+ raise_client_error '405 Method Not Allowed', body
61
71
  when 415 then
62
- raise NetboxClientRuby::ClientError, '415 Unsupported Media Type'
72
+ raise_client_error '415 Unsupported Media Type', body
63
73
  when 429 then
64
- raise NetboxClientRuby::ClientError, '429 Too Many Requests'
74
+ raise_client_error '429 Too Many Requests', body
65
75
  else
66
- raise NetboxClientRuby::ClientError, "#{status} Request Error"
76
+ raise_client_error "#{status} Request Error", body
67
77
  end
68
78
  end
69
79
 
70
- def sanitize_variable_name(raw_name)
71
- raw_name.gsub(/[^a-zA-Z0-9_]/, '_')
80
+ def raise_client_error(message, body = nil)
81
+ raise NetboxClientRuby::ClientError, "#{message}#{formatted_body(body)}"
82
+ end
83
+
84
+ def formatted_body(body)
85
+ return '' if body.nil? || body.empty?
86
+ shortened = body.to_s[0, 50]
87
+ one_line = shortened.gsub(/\n/, '\n')
88
+ " (#{one_line})"
72
89
  end
73
90
  end
74
91
  end
@@ -104,17 +104,22 @@ module NetboxClientRuby
104
104
  end
105
105
  end
106
106
 
107
- def initialize(given_ids = nil)
108
- return self if given_ids.nil?
107
+ def initialize(given_values = nil)
108
+ return self if given_values.nil?
109
109
 
110
- if id_fields.count == 1 && !given_ids.is_a?(Hash)
111
- instance_variable_set("@#{id_fields.keys.first}", given_ids)
110
+ if id_fields.count == 1 && !given_values.is_a?(Hash)
111
+ instance_variable_set("@#{id_fields.keys.first}", given_values)
112
112
  return self
113
113
  end
114
114
 
115
- check_given_ids(given_ids)
116
-
117
- given_ids.each { |id_field, id_value| instance_variable_set "@#{id_field}", id_value }
115
+ given_values.each do |field, value|
116
+ if id_fields.key? field.to_s
117
+ instance_variable_set "@#{field}", value
118
+ else
119
+ # via method_missing, because it checks for readonly fields, etc.
120
+ method_missing("#{field}=", value)
121
+ end
122
+ end
118
123
 
119
124
  self
120
125
  end
@@ -189,12 +194,10 @@ module NetboxClientRuby
189
194
  is_instance_variable = instance_variables.include?("@#{name[0..-2]}".to_sym)
190
195
  not_this_classes_business = is_readonly_field || is_instance_variable
191
196
 
192
- if not_this_classes_business
193
- super
194
- else
195
- dirty_data[name[0..-2]] = args[0]
196
- return args[0]
197
- end
197
+ return super if not_this_classes_business
198
+
199
+ dirty_data[name[0..-2]] = args[0]
200
+ return args[0]
198
201
  end
199
202
 
200
203
  return dirty_data[name] if dirty_data.key? name
@@ -247,21 +250,6 @@ module NetboxClientRuby
247
250
  self
248
251
  end
249
252
 
250
- def check_given_ids(given_ids)
251
- if !given_ids.is_a? Hash
252
- raise ArgumentError, "'#{self.class}.new' expects the argument to be a Hash when multiple ids are defined"
253
- elsif given_ids.empty?
254
- raise ArgumentError, "'#{self.class}.new' expects a the argument to not be empty."
255
- end
256
-
257
- missing_ids = id_fields.keys - given_ids.keys.map(&:to_s)
258
-
259
- unless missing_ids.empty?
260
- raise ArgumentError,
261
- "'#{self.class}.new' expects a value for all defined IDs. The following values are missing: '#{missing_ids.join(', ')}'"
262
- end
263
- end
264
-
265
253
  def data
266
254
  @data ||= get
267
255
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: netbox-client-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christian Mäder
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-06 00:00:00.000000000 Z
11
+ date: 2017-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -222,6 +222,10 @@ files:
222
222
  - lib/netbox_client_ruby/api/dcim/manufacturers.rb
223
223
  - lib/netbox_client_ruby/api/dcim/platform.rb
224
224
  - lib/netbox_client_ruby/api/dcim/platforms.rb
225
+ - lib/netbox_client_ruby/api/dcim/power_outlet.rb
226
+ - lib/netbox_client_ruby/api/dcim/power_outlets.rb
227
+ - lib/netbox_client_ruby/api/dcim/power_port.rb
228
+ - lib/netbox_client_ruby/api/dcim/power_ports.rb
225
229
  - lib/netbox_client_ruby/api/dcim/rack.rb
226
230
  - lib/netbox_client_ruby/api/dcim/racks.rb
227
231
  - lib/netbox_client_ruby/api/dcim/region.rb