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

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, #new_record?, #request, request, resource_name, #save, #update, #update_params, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ UserMacro

Returns a new instance of UserMacro.



11
12
13
14
15
16
17
18
# File 'lib/rubix/models/user_macro.rb', line 11

def initialize properties={}
  super(properties)
  self.name  = properties[:name] || self.class.unmacro_name(properties[:macro])
  @value = properties[:value]

  self.host    = properties[:host]
  self.host_id = properties[:host_id]
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



20
21
22
# File 'lib/rubix/models/user_macro.rb', line 20

def name
  @name
end

#valueObject

Properties & Finding ==



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

def value
  @value
end

Class Method Details

.build(macro) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/rubix/models/user_macro.rb', line 91

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



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

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

.id_fieldObject



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

def self.id_field
  'hostmacroid'
end

.macro_name(name) ⇒ Object



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

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

.unmacro_name(name) ⇒ Object



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

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

Instance Method Details

#create_requestObject



70
71
72
# File 'lib/rubix/models/user_macro.rb', line 70

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

#destroy_requestObject



78
79
80
# File 'lib/rubix/models/user_macro.rb', line 78

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

#macro_nameObject



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

def macro_name
  self.class.macro_name(name)
end

#mass_add_paramsObject

Requests ==



66
67
68
# File 'lib/rubix/models/user_macro.rb', line 66

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

#resource_nameObject



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

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

#update_requestObject



74
75
76
# File 'lib/rubix/models/user_macro.rb', line 74

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

#validateObject

Validation ==

Raises:



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

def validate
  raise ValidationError.new("A user macro must have both a 'name' and a 'value'") if name.nil? || name.strip.empty? || value.nil? || value.strip.empty?
  true
end