Class: HostGroups
- Inherits:
-
Basic
show all
- Defined in:
- lib/zapix/proxies/hostgroups.rb
Defined Under Namespace
Classes: NonExistingHostgroup
Instance Method Summary
collapse
Methods inherited from Basic
#initialize
Constructor Details
This class inherits a constructor from Basic
Instance Method Details
#any_hosts?(hostgroup) ⇒ Boolean
48
49
50
51
52
53
54
55
56
|
# 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
if(result >= 1)
true
else
false
end
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
58
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 58
def delete(name)
if(exists?(name))
if(any_hosts?(name))
host_ids = get_host_ids_of(name)
host_ids.each do |id|
@client.host_delete(['hostid' => id])
end
@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
24
25
26
|
# File 'lib/zapix/proxies/hostgroups.rb', line 24
def exists?(name)
@client.hostgroup_exists({'name' => name})
end
|
90
91
92
93
94
|
# File 'lib/zapix/proxies/hostgroups.rb', line 90
def (group_names_and_ids)
group_names_and_ids.map do |hostgroup|
hostgroup['name']
end
end
|
86
87
88
|
# File 'lib/zapix/proxies/hostgroups.rb', line 86
def (query_result)
query_result.first['hosts'].map { |host| host['hostid'] }
end
|
#get_all ⇒ Object
79
80
81
82
83
84
|
# File 'lib/zapix/proxies/hostgroups.rb', line 79
def get_all
host_groups_with_ids = @client.hostgroup_get({'output' => ['name']})
(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')
(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
|