Class: HostGroups

Inherits:
Base
  • Object
show all
Defined in:
lib/zapix/proxies/hostgroups.rb

Defined Under Namespace

Classes: NonExistingHostgroup

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

#any_hosts?(hostgroup) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



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



10
11
12
# File 'lib/zapix/proxies/hostgroups.rb', line 10

def create(name)
  client.hostgroup_create('name' => name) unless exists?(name)
end

#create_or_update(name) ⇒ Object



14
15
16
17
18
19
20
21
# 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) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
# 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) ⇒ Object



90
91
92
93
94
# 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) ⇒ Object



86
87
88
# File 'lib/zapix/proxies/hostgroups.rb', line 86

def extract_host_ids(query_result)
  query_result.first['hosts'].map { |host| host['hostid'] }
end

#get_allObject



79
80
81
82
83
84
# 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) ⇒ Object



47
48
49
50
# 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) ⇒ Object



32
33
34
35
36
37
38
39
# 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) ⇒ Object



4
5
6
7
8
# 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) ⇒ Object



41
42
43
44
45
# File 'lib/zapix/proxies/hostgroups.rb', line 41

def mass_delete(*names)
  names.each do |group_name|
    delete(group_name)
  end
end