Class: Hosts

Inherits:
Base show all
Defined in:
lib/zapix/proxies/hosts.rb

Defined Under Namespace

Classes: EmptyHostname, NonExistingHost

Instance Attribute Summary

Attributes inherited from Base

#client

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Base

Instance Method Details

#create(options = {}) ⇒ Object



15
16
17
# File 'lib/zapix/proxies/hosts.rb', line 15

def create(options = {})
  client.host_create(options) unless exists?(options['host'])
end

#create_or_update(options = {}) ⇒ Object

Raises:



19
20
21
22
23
24
25
26
27
28
# 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) ⇒ Object



62
63
64
65
66
67
68
# 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) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
# 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_allObject



53
54
55
56
57
58
59
60
# 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) ⇒ Object



11
12
13
# 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) ⇒ Object



3
4
5
6
7
8
9
# 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


30
31
32
33
# File 'lib/zapix/proxies/hosts.rb', line 30

def unlink_and_clear_templates(options = {})
  template_ids = options['template_ids'].map { |id| { 'templateid' => id } }
  client.host_update('hostid' => options['host_id'], 'templates_clear' => template_ids)
end

#update_macros(options = {}) ⇒ Object



40
41
42
# 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 = {}) ⇒ Object



35
36
37
38
# 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