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:



48
49
50
51
52
# File 'lib/zapix/proxies/hostgroups.rb', line 48

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 >= 1 ? true : false
end

#create(name) ⇒ Object



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

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

#create_or_update(name) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/zapix/proxies/hostgroups.rb', line 15

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/zapix/proxies/hostgroups.rb', line 54

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(['hostid' => 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)


24
25
26
# File 'lib/zapix/proxies/hostgroups.rb', line 24

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

#extract_host_groups(group_names_and_ids) ⇒ Object



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

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



81
82
83
# File 'lib/zapix/proxies/hostgroups.rb', line 81

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

#get_allObject



74
75
76
77
78
79
# File 'lib/zapix/proxies/hostgroups.rb', line 74

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



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

def get_host_ids_of(hostgroup)
  result = client.hostgroup_get('filter' => {'name' => [hostgroup]}, 'selectHosts' => 'refer')
  extract_host_ids(result)
end

#get_id(name) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/zapix/proxies/hostgroups.rb', line 28

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



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

def mass_create(*names)
  names.each do |group_name|
    create(group_name)
  end
end

#mass_delete(*names) ⇒ Object



37
38
39
40
41
# File 'lib/zapix/proxies/hostgroups.rb', line 37

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