Class: Rubix::Model
- Inherits:
-
Object
- Object
- Rubix::Model
- Extended by:
- Logs
- Includes:
- Logs
- Defined in:
- lib/rubix/models/model.rb
Overview
A base class for all Zabbix models to subclass.
It might be worth using ActiveModel – but maybe not. The goal is to keep dependencies low while still retaining expressiveness.
Direct Known Subclasses
Action, Application, Condition, Host, HostGroup, Item, MediaType, Medium, Operation, Script, Template, TimeSeries, Trigger, User, UserGroup, UserMacro
Instance Attribute Summary collapse
-
#id ⇒ Fixnum?
The ID of this model.
-
#properties ⇒ Hash
] the properties this model was initialized with.
Class Method Summary collapse
-
.all(options = {}) ⇒ Array<Rubix::Model>
List all objects of this resource’s type.
-
.all_params(options = {}) ⇒ Hash
Parameters to list all the objects of this resource’s type.
-
.all_request(options = {}) ⇒ Rubix::Response
Send a request to list all objects of this resource’s type.
-
.each(options = {}, &block) ⇒ Array<Rubix::Model>
Execute block once for each element of the result set.
-
.find(options = {}) ⇒ Rubix::Model?
Find a resource using the given
options
or returnnil
if none is found. -
.find_or_create(options = {}) ⇒ Rubix::Model, false
Find a resource using the given
options
or create one if none can be found. -
.find_params(options = {}) ⇒ Hash
Parameters for finding a specific resource.
-
.find_request(options = {}) ⇒ Rubix::Response
Send a find request for a specific resource.
-
.get_params ⇒ Hash
Parameters for ‘get’-type requests for this resource’s type.
-
.id_field ⇒ String
This is the name of the id field returned in Zabbix responses –
hostid
,groupid
,hostmacroid
, &c. -
.list(ids) ⇒ Object
List ==.
-
.properties ⇒ Object
Helpers ==.
-
.request(method, params) ⇒ Rubix::Response
Send a request to the Zabbix API.
-
.resource_name ⇒ String
This is the name of the resource as used inside Rubix – Host, HostGroup, UserMacro, &c.
-
.web_request(verb, path, data = {}) ⇒ Object
Send a web request to the Zabbix web application.
- .zabbix_attr(name, options = {}) ⇒ Object
- .zabbix_define(defname, hash) ⇒ Object
-
.zabbix_name ⇒ String
This is the name of the resource as used by Zabbix – host, hostgroup, usermacro, &c.
Instance Method Summary collapse
-
#after_create ⇒ Object
Run this hook after creating a new resource.
-
#before_destroy ⇒ true, false
A hook that will be run before this resource is destroyed.
-
#before_update ⇒ true, false
A hook that will be run before this resource is updated.
-
#create ⇒ true, false
Create this resource.
-
#create_params ⇒ Hash
Parameters for creating a new resource of this type.
-
#create_request ⇒ Rubix::Response
Send a request to create this resource.
-
#destroy ⇒ true, false
Destroy this resource.
-
#destroy_params ⇒ Array<Fixnum>
Parameters for destroying this resource.
-
#destroy_request ⇒ Rubix::Response
Send a request to destroy this resource.
-
#id_field ⇒ String
This is the name of the id field returned in Zabbix responses –
hostid
,groupid
,hostmacroid
, &c. -
#initialize(properties = {}) ⇒ Model
constructor
Create a new model instance.
-
#new_record? ⇒ true, false
Is this a new record? We can tell because the ID must be blank.
-
#request(method, params) ⇒ Rubix::Response
Send a request to the Zabbix API.
-
#resource_name ⇒ String
This is the name of this resource instance, using this object’s ‘name’ property if possible.
-
#save ⇒ true, false
Save this record.
-
#to_hash ⇒ Hash
Return this object as a Hash.
-
#update ⇒ true, false
Update this resource.
-
#update_params ⇒ Hash
Parameters for updating a resource of this type.
-
#update_request ⇒ Rubix::Response
Send a request to update this resource.
-
#validate ⇒ true, false
Validate this record.
Methods included from Logs
debug, error, fatal, info, warn
Constructor Details
#initialize(properties = {}) ⇒ Model
Create a new model instance. This may represent a new or existing Zabbix resource.
71 72 73 74 75 76 77 |
# File 'lib/rubix/models/model.rb', line 71 def initialize properties={} @properties = properties self.class.properties.keys.each do |property| self.send("#{property}=", properties[property]) end @id = properties[:id] end |
Instance Attribute Details
#id ⇒ Fixnum?
Returns the ID of this model.
13 14 15 |
# File 'lib/rubix/models/model.rb', line 13 def id @id end |
#properties ⇒ Hash
Returns ] the properties this model was initialized with.
10 11 12 |
# File 'lib/rubix/models/model.rb', line 10 def properties @properties end |
Class Method Details
.all(options = {}) ⇒ Array<Rubix::Model>
List all objects of this resource’s type.
317 318 319 320 321 322 323 324 325 |
# File 'lib/rubix/models/model.rb', line 317 def self.all ={} response = all_request() if response.has_data? response.result.map { |obj_data| build(obj_data) } else error("Error listing all Zabbix #{resource_name}s: #{response.}") unless response.success? [] end end |
.all_params(options = {}) ⇒ Hash
Parameters to list all the objects of this resource’s type.
301 302 303 |
# File 'lib/rubix/models/model.rb', line 301 def self.all_params ={} get_params.merge() end |
.all_request(options = {}) ⇒ Rubix::Response
Send a request to list all objects of this resource’s type.
309 310 311 |
# File 'lib/rubix/models/model.rb', line 309 def self.all_request ={} request("#{zabbix_name}.get", all_params()) end |
.each(options = {}, &block) ⇒ Array<Rubix::Model>
Execute block once for each element of the result set.
331 332 333 |
# File 'lib/rubix/models/model.rb', line 331 def self.each ={}, &block all().each(&block) end |
.find(options = {}) ⇒ Rubix::Model?
Find a resource using the given options
or return nil
if none is found.
360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/rubix/models/model.rb', line 360 def self.find ={} response = find_request() case when response.has_data? build(response.result.first) when response.success? # a successful but empty response means it wasn't found else error("Error finding Zabbix #{resource_name} using #{.inspect}: #{response.}") nil end end |
.find_or_create(options = {}) ⇒ Rubix::Model, false
Find a resource using the given options
or create one if none can be found. Will return false
if the object cannot be found and cannot be created.
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/rubix/models/model.rb', line 379 def self.find_or_create ={} response = find_request() case when response.has_data? build(response.result.first) when response.success? # doesn't exist obj = new() if obj.save obj else false end else error("Error creating Zabbix #{resource_name} using #{.inspect}: #{response.}") false end end |
.find_params(options = {}) ⇒ Hash
Parameters for finding a specific resource.
343 344 345 |
# File 'lib/rubix/models/model.rb', line 343 def self.find_params ={} get_params.merge() end |
.find_request(options = {}) ⇒ Rubix::Response
Send a find request for a specific resource.
351 352 353 |
# File 'lib/rubix/models/model.rb', line 351 def self.find_request ={} request("#{zabbix_name}.get", find_params()) end |
.get_params ⇒ Hash
Parameters for ‘get’-type requests for this resource’s type.
293 294 295 |
# File 'lib/rubix/models/model.rb', line 293 def self.get_params { :output => :extend } end |
.id_field ⇒ String
This is the name of the id field returned in Zabbix responses – hostid
, groupid
, hostmacroid
, &c.
50 51 52 |
# File 'lib/rubix/models/model.rb', line 50 def self.id_field "#{zabbix_name}id" end |
.list(ids) ⇒ Object
List ==
402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/rubix/models/model.rb', line 402 def self.list ids return [] if ids.nil? || ids.empty? response = request("#{zabbix_name}.get", get_params.merge((id_field + 's') => ids)) case when response.has_data? response.result.map do |obj| build(obj) end when response.success? [] else error("Error listing Zabbix #{resource_name}s: #{response.}") end end |
.properties ⇒ Object
Helpers ==
421 422 423 |
# File 'lib/rubix/models/model.rb', line 421 def self.properties @properties ||= {} end |
.request(method, params) ⇒ Rubix::Response
Send a request to the Zabbix API. This is just a convenience method for Rubix::Connection#request
.
95 96 97 |
# File 'lib/rubix/models/model.rb', line 95 def self.request method, params Rubix.connection && Rubix.connection.request(method, params) end |
.resource_name ⇒ String
This is the name of the resource as used inside Rubix – Host, HostGroup, UserMacro, &c.
26 27 28 |
# File 'lib/rubix/models/model.rb', line 26 def self.resource_name self.to_s.split('::').last end |
.web_request(verb, path, data = {}) ⇒ Object
Send a web request to the Zabbix web application. This is just a convenience method for Rubix::Connection#web_request
.
105 106 107 |
# File 'lib/rubix/models/model.rb', line 105 def self.web_request verb, path, data={} Rubix.connection && Rubix.connection.web_request(verb, path, data) end |
.zabbix_attr(name, options = {}) ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'lib/rubix/models/model.rb', line 425 def self.zabbix_attr name, ={} name = name.to_s.to_sym @properties ||= {} @properties[name] = if [:default].nil? attr_accessor name else attr_writer name define_method name do current_value = instance_variable_get("@#{name}") return current_value unless current_value.nil? instance_variable_set("@#{name}", [:default]) end end end |
.zabbix_define(defname, hash) ⇒ Object
442 443 444 445 446 447 448 449 450 451 |
# File 'lib/rubix/models/model.rb', line 442 def self.zabbix_define defname, hash codes = hash names = hash.invert.freeze codes.keys.each do |key| codes[key.to_s] = codes[key] end codes.freeze const_set "#{defname}_CODES", codes const_set "#{defname}_NAMES", names end |
.zabbix_name ⇒ String
This is the name of the resource as used by Zabbix – host, hostgroup, usermacro, &c.
42 43 44 |
# File 'lib/rubix/models/model.rb', line 42 def self.zabbix_name resource_name.downcase end |
Instance Method Details
#after_create ⇒ Object
Run this hook after creating a new resource.
181 182 183 |
# File 'lib/rubix/models/model.rb', line 181 def after_create true end |
#before_destroy ⇒ true, false
A hook that will be run before this resource is destroyed.
Override this in a subclass to implement any desired before-destroy functionality. Must return true
or false
.
282 283 284 |
# File 'lib/rubix/models/model.rb', line 282 def before_destroy true end |
#before_update ⇒ true, false
A hook that will be run before this resource is updated.
Override this in a subclass to implement any desired before-update functionality. Must return true
or false
.
234 235 236 |
# File 'lib/rubix/models/model.rb', line 234 def before_update true end |
#create ⇒ true, false
Create this resource.
166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/rubix/models/model.rb', line 166 def create return false unless validate response = create_request if response.has_data? @id = response.result[id_field + 's'].first.to_i info("Created Zabbix #{resource_name}") true else error("Error creating Zabbix #{resource_name}: #{response.}") return false end after_create end |
#create_params ⇒ Hash
Parameters for creating a new resource of this type.
152 153 154 |
# File 'lib/rubix/models/model.rb', line 152 def create_params {} end |
#create_request ⇒ Rubix::Response
Send a request to create this resource.
159 160 161 |
# File 'lib/rubix/models/model.rb', line 159 def create_request request("#{self.class.zabbix_name}.create", create_params) end |
#destroy ⇒ true, false
Destroy this resource.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/rubix/models/model.rb', line 259 def destroy return true if new_record? return false unless before_destroy response = destroy_request case when response.has_data? && response.result.values.first.first.to_i == id info("Destroyed Zabbix #{resource_name}") true when response.zabbix_error? && response. =~ /does not exist/i # was never there true else error("Could not destroy Zabbix #{resource_name}: #{response.}") false end end |
#destroy_params ⇒ Array<Fixnum>
Parameters for destroying this resource.
245 246 247 |
# File 'lib/rubix/models/model.rb', line 245 def destroy_params [id] end |
#destroy_request ⇒ Rubix::Response
Send a request to destroy this resource.
252 253 254 |
# File 'lib/rubix/models/model.rb', line 252 def destroy_request request("#{self.class.zabbix_name}.delete", destroy_params) end |
#id_field ⇒ String
This is the name of the id field returned in Zabbix responses – hostid
, groupid
, hostmacroid
, &c.
58 59 60 |
# File 'lib/rubix/models/model.rb', line 58 def id_field self.class.id_field end |
#new_record? ⇒ true, false
Is this a new record? We can tell because the ID must be blank.
112 113 114 |
# File 'lib/rubix/models/model.rb', line 112 def new_record? @id.nil? end |
#request(method, params) ⇒ Rubix::Response
Send a request to the Zabbix API. This is just a convenience method for Rubix::Connection#request
.
85 86 87 |
# File 'lib/rubix/models/model.rb', line 85 def request method, params self.class.request(method, params) end |
#resource_name ⇒ String
This is the name of this resource instance, using this object’s ‘name’ property if possible.
34 35 36 |
# File 'lib/rubix/models/model.rb', line 34 def resource_name "#{self.class.resource_name} #{respond_to?(:name) ? self.name : self.id}" end |
#save ⇒ true, false
Save this record.
Will create new records and update old ones.
121 122 123 |
# File 'lib/rubix/models/model.rb', line 121 def save new_record? ? create : update end |
#to_hash ⇒ Hash
Return this object as a Hash.
141 142 143 |
# File 'lib/rubix/models/model.rb', line 141 def to_hash update_params end |
#update ⇒ true, false
Update this resource.
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/rubix/models/model.rb', line 210 def update return false unless validate return create if new_record? return false unless before_update response = update_request case when response.has_data? && (response.result.values.first == true || response.result.values.first.map(&:to_i).include?(id)) info("Updated Zabbix #{resource_name}") true when response.has_data? error("No error, but failed to update Zabbix #{resource_name}") false else error("Error updating Zabbix #{resource_name}: #{response.}") false end end |
#update_params ⇒ Hash
Parameters for updating a resource of this type.
192 193 194 195 196 197 198 |
# File 'lib/rubix/models/model.rb', line 192 def update_params if id create_params.merge({id_field => id}) else create_params end end |
#update_request ⇒ Rubix::Response
Send a request to update this resource.
203 204 205 |
# File 'lib/rubix/models/model.rb', line 203 def update_request request("#{self.class.zabbix_name}.update", update_params) end |
#validate ⇒ true, false
Validate this record.
Override this method in a subclass and have it raise a Rubix::ValidationError
if validation fails.
131 132 133 134 135 136 |
# File 'lib/rubix/models/model.rb', line 131 def validate self.class.properties.each_pair do |property, | raise ValidationError.new("A #{self.class.resource_name} must have a #{property}") if [:required] && (self.send(property).nil? || self.send(property).empty?) end true end |