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

#before_update, #create, #destroy, find, find_or_create, #new_record?, #request, request, #resource_name, resource_name, #save, #update

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)
  @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

Properties & Finding ==



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

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



24
25
26
27
28
29
30
31
# File 'lib/rubix/models/user_macro.rb', line 24

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

.find_request(options = {}) ⇒ Object



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

def self.find_request options={}
  request('usermacro.get', 'hostids' => [options[:host_id]], 'filter' => {'macro' => macro_name(options[:name])}, "output" => "extend")
end

.id_fieldObject



45
46
47
# File 'lib/rubix/models/user_macro.rb', line 45

def self.id_field
  'hostmacroid'
end

.macro_name(name) ⇒ Object



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

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

.unmacro_name(name) ⇒ Object



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

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

Instance Method Details

#create_requestObject

CRUD ==



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

def create_request
  request('usermacro.massAdd', 'macros' => [{'macro' => macro_name, 'value' => value}], 'hosts' => [{'hostid' => host_id}])
end

#destroy_requestObject



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

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

#macro_nameObject



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

def macro_name
  self.class.macro_name(name)
end

#update_requestObject



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

def update_request
  request('usermacro.massUpdate', 'macros' => [{'macro' => macro_name, 'value' => value}], 'hosts' => [{'hostid' => host_id}])
end

#validateObject

Validation ==

Raises:



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

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