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



12
13
14
# File 'lib/zapix/proxies/hosts.rb', line 12

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

#create_or_update(options = {}) ⇒ Object

Raises:



16
17
18
19
20
21
22
23
24
25
# File 'lib/zapix/proxies/hosts.rb', line 16

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.merge!('hostid' => id)
    client.host_update(options)
  else
    create(options)
  end
end

#delete(name) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/zapix/proxies/hosts.rb', line 54

def delete(name)
  if exists?(name)
    client.host_delete(['hostid' => get_id(name)])
  else
    raise NonExistingHost, "Host #{name} cannot be deleted because it does not exist !"
  end
end

#exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/zapix/proxies/hosts.rb', line 41

def exists?(name)
  client.host_exists({'host' => name})
end

#get_allObject



45
46
47
48
49
50
51
52
# File 'lib/zapix/proxies/hosts.rb', line 45

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_id(name) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/zapix/proxies/hosts.rb', line 4

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


27
28
29
30
# File 'lib/zapix/proxies/hosts.rb', line 27

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



37
38
39
# File 'lib/zapix/proxies/hosts.rb', line 37

def update_macros(options={})
  client.host_update('hostid' => options['host_id'], 'macros' => options['macros'])
end

#update_templates(options = {}) ⇒ Object



32
33
34
35
# File 'lib/zapix/proxies/hosts.rb', line 32

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