class Hosts

Public Instance Methods

create(options = {}) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 15
def create(options = {})
  client.host_create(options) unless exists?(options['host'])
end
create_or_update(options = {}) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 19
def create_or_update(options = {})
  raise EmptyHostname, 'Host name cannot be empty !' if options['host'].nil?
  if exists?(options['host'])
    id = get_id(options['host'])
    options['hostid'] = id
    client.host_update(options)
  else
    create(options)
  end
end
delete(name) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 62
def delete(name)
  if exists?(name)
    client.host_delete([get_id(name)])
  else
    raise NonExistingHost, "Host #{name} cannot be deleted because it does not exist !"
  end
end
exists?(name) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 44
def exists?(name)
  result = client.host_get('filter' => { 'host' => name })
  if result.nil? || result.empty?
    false
  else
    true
  end
end
get_all() click to toggle source
# File lib/zapix/proxies/hosts.rb, line 53
def get_all
  host_names_and_ids = client.host_get('output' => ['name'])

  # the fucking api ALWAYS returns the ids and that's
  # why we need to extract the names separately

  extract_host_names(host_names_and_ids)
end
get_hosts_for_hostgroup(group_id) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 11
def get_hosts_for_hostgroup(group_id)
  client.host_get('groupids' => [group_id])
end
get_id(name) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 3
def get_id(name)
  if exists?(name)
    client.host_get('filter' => { 'host' => name }).first['hostid']
  else
    raise NonExistingHost, "Host #{name} does not exist !"
  end
end
update_macros(options = {}) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 40
def update_macros(options = {})
  client.host_update('hostid' => options['host_id'], 'macros' => options['macros'])
end
update_templates(options = {}) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 35
def update_templates(options = {})
  template_ids = options['template_ids'].map { |id| { 'templateid' => id } }
  client.host_update('hostid' => options['host_id'], 'templates' => template_ids)
end

Private Instance Methods

extract_host_names(hosts_and_ids) click to toggle source
# File lib/zapix/proxies/hosts.rb, line 75
def extract_host_names(hosts_and_ids)
  hosts_and_ids.map do |current_host|
    current_host['name']
  end
end