Class: HostGroups
- Inherits:
-
Base
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
53
54
55
56
57
58
|
# File 'lib/zapix/proxies/hostgroups.rb', line 53
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/zapix/proxies/hostgroups.rb', line 60
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([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
27
28
29
30
31
|
# File 'lib/zapix/proxies/hostgroups.rb', line 24
def exists?(name)
result = client.hostgroup_get({'filter' => {'name' => [name]}})
if (result == nil || result.empty?)
return false
else
return true
end
end
|
91
92
93
94
95
|
# File 'lib/zapix/proxies/hostgroups.rb', line 91
def (group_names_and_ids)
group_names_and_ids.map do |hostgroup|
hostgroup['name']
end
end
|
87
88
89
|
# File 'lib/zapix/proxies/hostgroups.rb', line 87
def (query_result)
query_result.first['hosts'].map { |host| host['hostid'] }
end
|
#get_all ⇒ Object
80
81
82
83
84
85
|
# File 'lib/zapix/proxies/hostgroups.rb', line 80
def get_all
host_groups_with_ids = client.hostgroup_get({'output' => ['name']})
(host_groups_with_ids)
end
|
#get_host_ids_of(hostgroup) ⇒ Object
48
49
50
51
|
# File 'lib/zapix/proxies/hostgroups.rb', line 48
def get_host_ids_of(hostgroup)
result = client.hostgroup_get('filter' => {'name' => [hostgroup]}, 'selectHosts' => 'refer')
(result)
end
|
#get_id(name) ⇒ Object
33
34
35
36
37
38
39
40
|
# File 'lib/zapix/proxies/hostgroups.rb', line 33
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
42
43
44
45
46
|
# File 'lib/zapix/proxies/hostgroups.rb', line 42
def mass_delete(*names)
names.each do |group_name|
delete(group_name)
end
end
|