class HostGroups
Public Instance Methods
any_hosts?(hostgroup)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 52 def any_hosts?(hostgroup) raise NonExistingHostgroup, "Hostgroup #{hostgroup} does not exist !" unless exists?(hostgroup) result = client.hostgroup_get('filter' => { 'name' => [hostgroup] }, 'selectHosts' => 'count').first['hosts'].to_i # result = client.hostgroup_get('countOutput' => 'true', 'filter' => {'name' => [hostgroup]}, 'selectHosts' => 'count').to_i result >= 1 ? true : false end
create(name)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 10 def create(name) client.hostgroup_create('name' => name) unless exists?(name) end
create_or_update(name)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 14 def create_or_update(name) if exists?(name) id = get_id(name) client.hostgroup_update('groupid' => id, 'name' => name) else create(name) end end
delete(name)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 59 def delete(name) if exists?(name) # host cannot exist without a hostgroup, so we need to delete # the attached hosts also if any_hosts?(name) # delete all hosts attached to a hostgroup host_ids = get_host_ids_of(name) host_ids.each do |id| client.host_delete([id]) end # now it is ok to delete the group client.hostgroup_delete([get_id(name)]) else client.hostgroup_delete([get_id(name)]) end else raise NonExistingHostgroup, "Hostgroup #{name} does not exist !" end end
exists?(name)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 23 def exists?(name) result = client.hostgroup_get('filter' => { 'name' => [name] }) if result.nil? || result.empty? return false else return true end end
extract_host_groups(group_names_and_ids)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 90 def extract_host_groups(group_names_and_ids) group_names_and_ids.map do |hostgroup| hostgroup['name'] end end
extract_host_ids(query_result)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 86 def extract_host_ids(query_result) query_result.first['hosts'].map { |host| host['hostid'] } end
get_all()
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 79 def get_all # the fucking API also returns the ids and that's # why we need to extract the names host_groups_with_ids = client.hostgroup_get('output' => ['name']) extract_host_groups(host_groups_with_ids) end
get_host_ids_of(hostgroup)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 47 def get_host_ids_of(hostgroup) result = client.hostgroup_get('filter' => { 'name' => [hostgroup] }, 'selectHosts' => 'refer') extract_host_ids(result) end
get_id(name)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 32 def get_id(name) if exists?(name) result = client.hostgroup_get('filter' => { 'name' => [name] }) result[0]['groupid'] else raise NonExistingHostgroup, "Hostgroup #{name} does not exist !" end end
mass_create(*names)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 4 def mass_create(*names) names.each do |group_name| create(group_name) end end
mass_delete(*names)
click to toggle source
# File lib/zapix/proxies/hostgroups.rb, line 41 def mass_delete(*names) names.each do |group_name| delete(group_name) end end