Class: Rubix::Template

Inherits:
Model
  • Object
show all
Includes:
Associations::HasManyHostGroups, Associations::HasManyHosts
Defined in:
lib/rubix/models/template.rb

Instance Attribute Summary collapse

Attributes inherited from Model

#id, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Associations::HasManyHostGroups

#host_group_ids, #host_group_ids=, #host_group_params, #host_groups, #host_groups=

Methods included from Associations::HasManyHosts

#host_ids, #host_ids=, #hosts, #hosts=

Methods inherited from Model

all, all_params, all_request, #before_destroy, #before_update, #create, #create_request, #destroy, #destroy_request, each, find, find_or_create, find_request, id_field, #id_field, #new_record?, request, #request, #resource_name, resource_name, #save, #update, #update_request, zabbix_name

Methods included from Logs

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

Constructor Details

#initialize(properties = {}) ⇒ Template

Returns a new instance of Template.



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

def initialize properties={}
  super(properties)
  @name     = properties[:name]
  
  self.host_ids = properties[:host_ids]
  self.hosts    = properties[:hosts]

  self.host_group_ids = properties[:host_group_ids]
  self.host_groups    = properties[:host_groups]
end

Instance Attribute Details

#nameObject

Properties & Finding ==



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

def name
  @name
end

Class Method Details

.build(template) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/rubix/models/template.rb', line 62

def self.build template
  new({
        :id       => (template[id_field] || template['hostid']).to_i,
        :name     => template['host'],
        :host_ids => template['hosts'].map { |host_info| host_info['hostid'].to_i },
        :host_group_ids => template['groups'].map { |group| group['groupid'].to_i }
      })
end

.find_params(options = {}) ⇒ Object



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

def self.find_params options={}
  get_params.merge(:filter => {:host => options[:name], :hostid => options[:id]})
end

.get_paramsObject



54
55
56
# File 'lib/rubix/models/template.rb', line 54

def self.get_params
  super().merge(:select_groups => :refer, :select_hosts => :refer)
end

Instance Method Details

#create_paramsObject

CRUD ==



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

def create_params
  {:host => name, :groups => host_group_params}
end

#destroy_paramsObject



50
51
52
# File 'lib/rubix/models/template.rb', line 50

def destroy_params
  [{id_field => id}]
end

#update_paramsObject



46
47
48
# File 'lib/rubix/models/template.rb', line 46

def update_params
  [create_params.merge(id_field => id)]
end

#validateObject

Validation ==

Raises:



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

def validate
  raise ValidationError.new("A template must have at least one host group.") if host_group_ids.nil? || host_group_ids.empty?
  true
end