Class: Rubix::UserMacro

Inherits:
Model
  • Object
show all
Includes:
Associations::BelongsToHost
Defined in:
lib/rubix/models/user_macro.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#id, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations::BelongsToHost

#host, #host=, #host_id, #host_id=

Methods inherited from Model

#after_create, all, all_params, all_request, #before_destroy, #before_update, #create, #create_params, #destroy, #destroy_params, each, find, find_or_create, find_request, get_params, #id_field, list, #new_record?, properties, request, #request, resource_name, #save, #to_hash, #update, #update_params, #validate, web_request, zabbix_attr, zabbix_define, zabbix_name

Methods included from Logs

#debug, #error, #fatal, #info, #warn

Constructor Details

#initialize(properties = {}) ⇒ UserMacro

Returns a new instance of UserMacro


18
19
20
21
22
23
24
# File 'lib/rubix/models/user_macro.rb', line 18

def initialize properties={}
  super(properties)
  set_name(properties[:name]  || self.class.unmacro_name(properties[:macro]), false)
  
  self.host    = properties[:host]
  self.host_id = properties[:host_id]
end

Instance Attribute Details

#nameObject (readonly)

Properties & Finding ==


9
10
11
# File 'lib/rubix/models/user_macro.rb', line 9

def name
  @name
end

Class Method Details

.build(macro) ⇒ Object


81
82
83
84
85
86
87
88
# File 'lib/rubix/models/user_macro.rb', line 81

def self.build macro
  new({
        :id      => macro[id_field].to_i,
        :name    => unmacro_name(macro['macro']),
        :value   => macro['value'],
        :host_id => macro['hostid']
      })
end

.find_params(options = {}) ⇒ Object


72
73
74
75
76
77
78
79
# File 'lib/rubix/models/user_macro.rb', line 72

def self.find_params options={}
  super().merge({
                  :hostids => [options[:host_id]],
                  :filter => {
                    :macro => macro_name(options[:name])
                  }
                })
end

.id_fieldObject


38
39
40
# File 'lib/rubix/models/user_macro.rb', line 38

def self.id_field
  'hostmacroid'
end

.macro_name(name) ⇒ Object


30
31
32
# File 'lib/rubix/models/user_macro.rb', line 30

def self.macro_name name
  "{$#{name.upcase}}"
end

.unmacro_name(name) ⇒ Object


26
27
28
# File 'lib/rubix/models/user_macro.rb', line 26

def self.unmacro_name name
  (name || '').gsub(/^\{\$/, '').gsub(/\}$/, '').upcase
end

Instance Method Details

#create_requestObject


60
61
62
# File 'lib/rubix/models/user_macro.rb', line 60

def create_request
  request('usermacro.massAdd', mass_add_params)
end

#destroy_requestObject


68
69
70
# File 'lib/rubix/models/user_macro.rb', line 68

def destroy_request
  request('usermacro.massRemove', :hostids => [host_id], :macros => [macro_name])
end

#macro_nameObject


34
35
36
# File 'lib/rubix/models/user_macro.rb', line 34

def macro_name
  self.class.macro_name(name)
end

#mass_add_paramsObject

Requests ==


56
57
58
# File 'lib/rubix/models/user_macro.rb', line 56

def mass_add_params
  { :macros => [{:macro => macro_name, :value => value}], :hosts => [{:hostid => host_id}] }
end

#resource_nameObject


42
43
44
# File 'lib/rubix/models/user_macro.rb', line 42

def resource_name
  "#{self.class.resource_name} #{self.name || self.id}"
end

#set_name(n, validate = true) ⇒ Object

Raises:


10
11
12
13
14
# File 'lib/rubix/models/user_macro.rb', line 10

def set_name n, validate=true
  return if n.nil? || n.empty?
  raise ValidationError.new("Cannot change the name of a UserMacro once it's created.") if validate && @name && (!new_record?)
  @name = n
end

#update_requestObject


64
65
66
# File 'lib/rubix/models/user_macro.rb', line 64

def update_request
  request('usermacro.massUpdate', mass_add_params)
end