lib/zabbixapi/templates.rb in zabbixapi-0.5.0b3 vs lib/zabbixapi/templates.rb in zabbixapi-0.5.0b4
- old
+ new
@@ -1,33 +1,73 @@
class ZabbixApi
- class Templates < Basic
+ class Templates
- def api_method_name
- "template"
+ def initialize(client)
+ @client = client
end
- def api_identify
- "name"
+ # Create template
+ #
+ # * *Args* :
+ # - +data+ -> Hash with :host => "Template_Name" and :groups => array with hostgroup ids
+ # * *Returns* :
+ # - Nil or Integer
+ def create(data)
+ result = @client.api_request(:method => "template.create", :params => [data])
+ result.empty? ? nil : result['templateids'][0].to_i
end
- def create(data)
- create_array(data)
+ # Add template
+ # Synonym create
+ def add(data)
+ create(data)
end
+ # Delete template
+ #
+ # * *Args* :
+ # - +data+ -> Hash with :host => "Template_Name"
+ # * *Returns* :
+ # - Nil or Integer
def delete(data)
- delete_array_sym(data)
+ result = @client.api_request(:method => "template.delete", :params => [:templateid => data])
+ result.empty? ? nil : result['templateids'][0].to_i
end
+ # Destroy template
+ # Synonym delete
+ def destroy(data)
+ delete(data)
+ end
+
+ # Return templateids linked with host
+ #
+ # * *Args* :
+ # - +data+ -> Hash with :hostids => [hostid]
+ # * *Returns* :
+ # - Array with templateids
def get_ids_by_host(data)
result = []
@client.api_request(:method => "template.get", :params => data).each do |tmpl|
result << tmpl['templateid']
end
result
end
-
+ # Return templateid
+ #
+ # * *Args* :
+ # - +data+ -> Hash with :host => "Template_Name" and :groups => array with hostgroup ids
+ # * *Returns* :
+ # - Integer
+ def get_or_create(data)
+ unless templateid = get_id(:host => data[:host])
+ templateid = create(data)
+ end
+ templateid
+ end
+
# Analog Zabbix api call massUpdate
#
# * *Args* :
# - +data+ -> Hash with :hosts_id => [hostid1, hostid2 ...], and :templates_id => [templateid1, templateid2 ...]
# * *Returns* :
@@ -111,9 +151,21 @@
result = @client.api_request(:method => "template.get", :params => {:filter => data, :output => "extend"})
result.empty? ? [] : result.values
else
@client.api_request(:method => "template.get", :params => {:filter => data, :output => "extend"})
end
+ end
+
+ # Return info about template
+ #
+ # * *Args* :
+ # - +data+ -> Hash with :host => "Template name"
+ # * *Returns* :
+ # - Nil or Integer
+ def get_id(data)
+ templateid = nil
+ get_full_data(data).each { |template| templateid = template['templateid'].to_i if template['host'] == data[:host] }
+ templateid
end
end
end