ams-zapix3 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,32 @@
1
+ require_relative 'base'
2
+
3
+ class Usergroups < Base
4
+ def create(options)
5
+ client.usergroup_create(options) unless exists?(options)
6
+ end
7
+
8
+ def exists?(options)
9
+ # client.usergroup_exists(options)
10
+ result = client.usergroup_get('filter' => { 'name' => options['name'] })
11
+ if result.empty? || result.nil?
12
+ false
13
+ else
14
+ true
15
+ end
16
+ end
17
+
18
+ def get_id(options)
19
+ if exists?(options)
20
+ result = client.usergroup_get('filter' => { 'name' => options['name'] })
21
+ result.first['usrgrpid']
22
+ else
23
+ raise NonExistingUsergroup, "Usergroup #{options['name']} does not exist !"
24
+ end
25
+ end
26
+
27
+ def delete(*group_ids)
28
+ client.usergroup_delete(group_ids)
29
+ end
30
+
31
+ class NonExistingUsergroup < StandardError; end
32
+ end
@@ -0,0 +1,26 @@
1
+ require_relative 'base'
2
+
3
+ class Users < Base
4
+ def create(options)
5
+ client.user_create(options) unless exists?(options)
6
+ end
7
+
8
+ def exists?(options)
9
+ result = client.user_get('filter' => { 'alias' => options['alias'] })
10
+ result.empty? ? false : true
11
+ end
12
+
13
+ def get_id(options)
14
+ if exists?(options)
15
+ client.user_get('filter' => { 'alias' => options['alias'] }).first['userid']
16
+ else
17
+ raise NonExistingUser, "User #{options['alias']} does not exist !"
18
+ end
19
+ end
20
+
21
+ def delete(user_ids)
22
+ client.user_delete(user_ids)
23
+ end
24
+
25
+ class NonExistingUser < StandardError; end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Zapix
2
+ VERSION = '0.2.6'.freeze
3
+ end
@@ -0,0 +1,51 @@
1
+ class Host
2
+ attr_accessor :group_ids, :template_ids, :interfaces, :macros
3
+
4
+ def initialize
5
+ @group_ids = []
6
+ @template_ids = []
7
+ @interfaces = []
8
+ @macros = []
9
+ @properties = {}
10
+ end
11
+
12
+ def add_name(name)
13
+ @properties.merge!('host' => name)
14
+ end
15
+
16
+ def add_visible_name(visible_name)
17
+ @properties.merge!('name' => visible_name)
18
+ end
19
+
20
+ def add_group_ids(*ids)
21
+ ids.each do |id|
22
+ group_ids << { 'groupid' => id }
23
+ end
24
+ @properties.merge!('groups' => group_ids)
25
+ end
26
+
27
+ def add_encryption_options(opts)
28
+ @properties.merge!(opts)
29
+ end
30
+
31
+ def add_interfaces(*ifaces)
32
+ interfaces.concat(ifaces)
33
+ @properties.merge!('interfaces' => interfaces)
34
+ end
35
+
36
+ def add_template_ids(*ids)
37
+ ids.each do |id|
38
+ template_ids << { 'templateid' => id }
39
+ end
40
+ @properties.merge!('templates' => template_ids)
41
+ end
42
+
43
+ def add_macros(*host_macros)
44
+ macros.concat(host_macros)
45
+ @properties.merge!('macros' => host_macros)
46
+ end
47
+
48
+ def to_hash
49
+ @properties
50
+ end
51
+ end
@@ -0,0 +1,28 @@
1
+ class Interface
2
+ attr_reader :type, :main, :useip, :ip, :dns, :port
3
+
4
+ # for more info see
5
+ # https://www.zabbix.com/documentation/2.0/manual/appendix/api/hostinterface/definitions#host_interface
6
+ # we assume ip and dns shall always be set
7
+
8
+ def initialize(attributes)
9
+ @type = attributes['type'] ||= 1
10
+ @main = attributes['main'] ||= 1
11
+ @useip = attributes['useip'] ||= 1
12
+ @ip = attributes['ip'] = attributes['ip']
13
+ @dns = attributes['dns'] = attributes['dns']
14
+ @port = attributes['port'] = attributes['port'] ||= 10_050
15
+ @result = {
16
+ 'type' => type,
17
+ 'main' => main,
18
+ 'useip' => useip,
19
+ 'ip' => ip,
20
+ 'dns' => dns,
21
+ 'port' => port
22
+ }
23
+ end
24
+
25
+ def to_hash
26
+ @result
27
+ end
28
+ end
@@ -0,0 +1,64 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'json'
4
+
5
+ class ZabbixRPCClient
6
+ attr_reader :uri, :debug
7
+
8
+ def initialize(options)
9
+ @uri = URI.parse(options[:service_url])
10
+ @username = options[:username]
11
+ @password = options[:password]
12
+ @use_ssl = options.fetch(:use_ssl, true)
13
+ @debug = options[:debug]
14
+ @auth_token = authenticate
15
+ end
16
+
17
+ def method_missing(name, *args)
18
+ method_name = map_name(name)
19
+
20
+ post_body = {
21
+ 'method' => method_name,
22
+ 'params' => args.first,
23
+ 'id' => id,
24
+ 'jsonrpc' => '2.0',
25
+ 'auth' => @auth_token
26
+ }.to_json
27
+
28
+ resp = JSON.parse(http_post_request(post_body))
29
+ raise JSONRPCError, resp['error'] if resp['error']
30
+ puts "[DEBUG] Get answer: #{resp}" if debug
31
+ resp['result']
32
+ end
33
+
34
+ def http_post_request(post_body)
35
+ http = Net::HTTP.new(uri.host, uri.port)
36
+ http.use_ssl = @use_ssl
37
+ request = Net::HTTP::Post.new(uri.request_uri)
38
+ request.content_type = 'application/json'
39
+ request.body = post_body
40
+ puts "[DEBUG] Send request: #{request.body}" if debug
41
+
42
+ begin
43
+ return http.request(request).body
44
+ rescue
45
+ puts "[DEBUG] Retrying sending request: #{request.body}" if debug
46
+ retry
47
+ end
48
+ end
49
+
50
+ def authenticate
51
+ user_login('user' => @username, 'password' => @password)
52
+ user_login('user' => @username, 'password' => @password)
53
+ end
54
+
55
+ def id
56
+ rand(100_000)
57
+ end
58
+
59
+ def map_name(name)
60
+ name.to_s.sub('_', '.')
61
+ end
62
+
63
+ class JSONRPCError < RuntimeError; end
64
+ end
@@ -0,0 +1,18 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'zapix'
8
+ RSpec.configure do |config|
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.run_all_when_everything_filtered = true
11
+ config.filter_run :focus
12
+
13
+ # Run specs in random order to surface order dependencies. If you find an
14
+ # order dependency and want to debug it, you can fix the order by providing
15
+ # the seed, which is printed after each run.
16
+ # --seed 1234
17
+ config.order = 'random'
18
+ end
@@ -0,0 +1,516 @@
1
+ require_relative 'spec_helper'
2
+
3
+ zrc = ZabbixAPI.connect(
4
+ service_url: ENV['ZABBIX_API_URL'],
5
+ username: ENV['ZABBIX_API_LOGIN'],
6
+ password: ENV['ZABBIX_API_PASSWORD'],
7
+ debug: true
8
+ )
9
+
10
+ hostgroup = 'hostgroup123'
11
+ another_hostgroup = 'anotherhostgroup'
12
+ hostgroup_with_hosts = 'withhosts'
13
+ template_1 = 'Template OS Linux'
14
+ template_2 = 'Template App POP Service'
15
+ templates_hostgroup = 'Templates'
16
+ application = 'web scenarios'
17
+ host = 'hostname'
18
+ scenario = 'scenario'
19
+ trigger_description = 'Webpage failed on {HOST.NAME}'
20
+ # trigger_expression = "{#{host}:web.test.fail[#{scenario}].max(#3)}#0"
21
+ trigger_expression = "{#{host}:system.cpu.load[percpu,avg1].last()}>5"
22
+ non_existing_trigger_expression = '{vfs.file.cksum[/etc/passwd].diff(0)}>0'
23
+ existing_action_name = 'Report problems to Zabbix administrators'
24
+ non_existing_action_name = 'No action defined'
25
+ test_usergroup = 'Operation managers test'
26
+ existing_usergroup = 'Zabbix administrators'
27
+ non_existing_usergroup = 'Smurfs'
28
+ existing_user = 'Admin'
29
+ non_existing_user = 'Tweegle'
30
+ test_user = 'Jim'
31
+ test_action = 'Test Action'
32
+
33
+ describe ZabbixAPI do
34
+ context 'hostgroup' do
35
+ before(:all) do
36
+ zrc.hostgroups.create(hostgroup)
37
+ zrc.hostgroups.create(another_hostgroup)
38
+ end
39
+
40
+ after(:all) do
41
+ zrc.hostgroups.delete(hostgroup)
42
+ zrc.hostgroups.delete(another_hostgroup)
43
+ end
44
+
45
+ it 'creates or updates a hostgroup' do
46
+ zrc.hostgroups.create_or_update(hostgroup)
47
+ result = zrc.hostgroups.create_or_update(hostgroup)
48
+ result.should include('groupids')
49
+ end
50
+
51
+ it 'returns false if hostgroup does not exist' do
52
+ result = zrc.hostgroups.exists?('nonexisting')
53
+ result.should be false
54
+ end
55
+
56
+ it 'succeeds if a hostgroup exist' do
57
+ result = zrc.hostgroups.exists?(hostgroup)
58
+ result.should be true
59
+ end
60
+
61
+ it 'returns hostgroup id' do
62
+ result = zrc.hostgroups.get_id(hostgroup)
63
+ result.to_i.should >= 0
64
+ end
65
+
66
+ it 'throws exception if hostgroup id does not exist' do
67
+ expect { zrc.hostgroups.get_id('nonexisting') }.to raise_error(HostGroups::NonExistingHostgroup)
68
+ end
69
+
70
+ it 'throws exception if someone checks for attached hosts of nonexisting group' do
71
+ expect { zrc.hostgroups.any_hosts?('nonexisting') }.to raise_error(HostGroups::NonExistingHostgroup)
72
+ end
73
+
74
+ it 'returns false if a hostgroup has no attached hosts' do
75
+ zrc.hostgroups.any_hosts?(hostgroup).should be false
76
+ end
77
+
78
+ it 'returns all hostgroups' do
79
+ zrc.hostgroups.get_all.should include(hostgroup, another_hostgroup)
80
+ end
81
+ end
82
+ context 'complex hostgroup consisting hosts' do
83
+ before(:each) do
84
+ zrc.hostgroups.create(hostgroup_with_hosts)
85
+ hostgroup_id = zrc.hostgroups.get_id(hostgroup_with_hosts)
86
+ example_host = Host.new
87
+ example_host.add_name(host)
88
+ example_host.add_interfaces(create_interface.to_hash)
89
+ example_host.add_group_ids(hostgroup_id)
90
+ example_host.add_template_ids(zrc.templates.get_id(template_1), zrc.templates.get_id(template_2))
91
+ example_host.add_macros('macro' => '{$TESTMACRO}', 'value' => 'test123')
92
+ zrc.hosts.create_or_update(example_host.to_hash)
93
+ end
94
+
95
+ it 'deletes a hostgroup with attached hosts' do
96
+ zrc.hosts.exists?(host).should be true
97
+ zrc.hostgroups.delete(hostgroup_with_hosts)
98
+ zrc.hostgroups.exists?(hostgroup_with_hosts).should be false
99
+ end
100
+ end
101
+
102
+ context 'complex hostgroup' do
103
+ before(:each) do
104
+ zrc.hostgroups.create(hostgroup_with_hosts)
105
+ hostgroup_id = zrc.hostgroups.get_id(hostgroup_with_hosts)
106
+ example_host = Host.new
107
+ example_host.add_name(host)
108
+ example_host.add_interfaces(create_interface.to_hash)
109
+ example_host.add_macros('macro' => '{$TESTMACRO}', 'value' => 'test123')
110
+ example_host.add_group_ids(hostgroup_id)
111
+ example_host.add_template_ids(zrc.templates.get_id(template_1), zrc.templates.get_id(template_2))
112
+ zrc.hosts.create_or_update(example_host.to_hash)
113
+
114
+ # create application for the host
115
+ application_options = {}
116
+ application_options['name'] = application
117
+ application_options['hostid'] = zrc.hosts.get_id(host)
118
+ zrc.applications.create(application_options)
119
+
120
+ # creates web scenarios for host
121
+ webcheck_options = {}
122
+ webcheck_options['hostid'] = zrc.hosts.get_id(host)
123
+ webcheck_options['name'] = scenario
124
+ webcheck_options['applicationid'] = zrc.applications.get_id(application_options)
125
+ webcheck_options['steps'] = [{ 'name' => 'Homepage', 'url' => 'm.test.de', 'status_codes' => 200, 'no' => 1 }]
126
+ zrc.scenarios.create(webcheck_options)
127
+
128
+ # creates a trigger
129
+ options = {}
130
+ options['description'] = trigger_description
131
+ options['expression'] = trigger_expression
132
+ options['priority'] = 2 # 2 means Warning
133
+ zrc.triggers.create(options)
134
+ end
135
+
136
+ after(:each) do
137
+ zrc.hostgroups.delete(hostgroup_with_hosts)
138
+ end
139
+
140
+ describe 'hosts' do
141
+ it 'returns true if a hostgroup has attached hosts' do
142
+ zrc.hostgroups.any_hosts?(hostgroup_with_hosts).should be true
143
+ end
144
+
145
+ it 'returns all the host ids of a hosts belonging to a hostgroup' do
146
+ host_id = zrc.hosts.get_id(host)
147
+ zrc.hostgroups.get_host_ids_of(hostgroup_with_hosts).should include(host_id)
148
+ end
149
+
150
+ it 'gets the right template id for host' do
151
+ result = zrc.templates.get_templates_for_host(zrc.hosts.get_id(host))
152
+ result.should include(zrc.templates.get_id(template_1))
153
+ result.should include(zrc.templates.get_id(template_2))
154
+ end
155
+
156
+ it 'unlinks all templates for host' do
157
+ host_id = zrc.hosts.get_id(host)
158
+ options = {}
159
+ options['template_ids'] = zrc.templates.get_templates_for_host(host_id)
160
+ options['host_id'] = host_id
161
+ result = zrc.hosts.unlink_and_clear_templates(options)
162
+ result.should_not include(zrc.templates.get_id(template_1))
163
+ result.should_not include(zrc.templates.get_id(template_2))
164
+ end
165
+
166
+ it 'throws an exception if updating a host without specifying the hostname' do
167
+ example_host = Host.new
168
+ example_host.add_interfaces(create_interface.to_hash)
169
+ expect { zrc.hosts.create_or_update(example_host.to_hash) }.to raise_error(Hosts::EmptyHostname)
170
+ end
171
+
172
+ it "updates host's interface after unlinking all belonging templates" do
173
+ # unlinking all items
174
+ host_id = zrc.hosts.get_id(host)
175
+ options = {}
176
+ zrc.templates.get_templates_for_host(host_id)
177
+ options['template_ids'] = zrc.templates.get_templates_for_host(host_id)
178
+ options['host_id'] = host_id
179
+ result = zrc.hosts.unlink_and_clear_templates(options)
180
+ # now it should be safe to update the interface of the host
181
+ example_host = Host.new
182
+ example_host.add_interfaces(create_interface.to_hash)
183
+ example_host.add_name(host)
184
+ zrc.hosts.create_or_update(example_host.to_hash)
185
+ end
186
+
187
+ it "updates host's templates" do
188
+ host_id = zrc.hosts.get_id(host)
189
+ options = {}
190
+ options['host_id'] = host_id
191
+ template_id = zrc.templates.get_id('Template App FTP Service')
192
+ options['template_ids'] = [template_id]
193
+ zrc.hosts.update_templates(options)
194
+ zrc.templates.get_templates_for_host(host_id).should include(template_id)
195
+ end
196
+
197
+ it "updates host's macro" do
198
+ host_id = zrc.hosts.get_id(host)
199
+ options = {}
200
+ options['host_id'] = host_id
201
+ options['macros'] = [{ 'macro' => '{$TESTMACRO}', 'value' => 'this is only a test macro' }]
202
+ zrc.hosts.update_macros(options)
203
+ end
204
+
205
+ it 'creates a template' do
206
+ template_name = 'Template Tomcat'
207
+ options = { 'host' => template_name }
208
+ options['groups'] = zrc.hostgroups.get_id(templates_hostgroup)
209
+ zrc.templates.create(options)
210
+ zrc.templates.exists?(template_name).should be true
211
+ end
212
+ end
213
+
214
+ describe 'applications' do
215
+ it 'returns false if an application does not exist' do
216
+ options = {}
217
+ options['name'] = 'nonexisting'
218
+ options['hostid'] = zrc.hosts.get_id(host)
219
+ zrc.applications.exists?(options).should be false
220
+ end
221
+
222
+ it 'returns true if an application exists' do
223
+ options = {}
224
+ options['name'] = application
225
+ options['hostid'] = zrc.hosts.get_id(host)
226
+ zrc.applications.exists?(options).should be true
227
+ end
228
+
229
+ it 'get an application id by application name and host' do
230
+ options = {}
231
+ options['name'] = application
232
+ options['hostid'] = zrc.hosts.get_id(host)
233
+ result = zrc.applications.get_id(options)
234
+ result.to_i.should >= 0
235
+ end
236
+
237
+ it 'throws exception on non existing application' do
238
+ options = {}
239
+ options['name'] = 'nonexisting'
240
+ options['hostid'] = zrc.hosts.get_id(host)
241
+ expect { zrc.applications.get_id(options) }.to raise_error(Applications::NonExistingApplication)
242
+ end
243
+ end
244
+
245
+ describe 'web scenarios' do
246
+ it 'returns true if web scenarios exists' do
247
+ options = {}
248
+ options['name'] = scenario
249
+ options['hostid'] = zrc.hosts.get_id(host)
250
+ zrc.scenarios.exists?(options).should be true
251
+ end
252
+
253
+ it 'returns all web scenarios' do
254
+ options = {}
255
+ zrc.scenarios.get_all.should include(scenario)
256
+ end
257
+
258
+ it 'gets the id of a web scenario' do
259
+ options = {}
260
+ options['name'] = scenario
261
+ options['hostid'] = zrc.hosts.get_id(host)
262
+ zrc.scenarios.exists?(options).should be true
263
+ result = zrc.scenarios.get_id(options)
264
+ result['result'].to_i.should >= 0
265
+ end
266
+
267
+ it 'returns false if a web scenario does not exist' do
268
+ options = {}
269
+ options['name'] = 'nonexisting'
270
+ options['hostid'] = zrc.hosts.get_id(host)
271
+ zrc.scenarios.exists?(options).should be false
272
+ end
273
+
274
+ it 'deletes a web scenario' do
275
+ options = {}
276
+ options['name'] = scenario
277
+ options['hostid'] = zrc.hosts.get_id(host)
278
+ scenario_id = zrc.scenarios.get_id(options)
279
+ zrc.scenarios.delete([scenario_id])
280
+ zrc.scenarios.exists?(options).should be false
281
+ end
282
+ end
283
+
284
+ describe 'triggers' do
285
+ it 'deletes a trigger' do
286
+ options = {}
287
+ options['expression'] = trigger_expression
288
+ zrc.triggers.get_id(options).to_i.should >= 0
289
+ id = zrc.triggers.get_id(options)
290
+ zrc.triggers.delete(id)
291
+ expect { zrc.triggers.get_id(options) }.to raise_error(Triggers::NonExistingTrigger)
292
+ end
293
+
294
+ it 'gets an id of a trigger' do
295
+ options = {}
296
+ options['expression'] = trigger_expression
297
+ zrc.triggers.get_id(options).to_i >= 0
298
+ end
299
+
300
+ it 'throws exception if trying to get id of a non-existing trigger' do
301
+ options = {}
302
+ options['expression'] = non_existing_trigger_expression
303
+ expect { zrc.triggers.get_id(options) }.to raise_error(Triggers::NonExistingTrigger)
304
+ end
305
+ end
306
+
307
+ describe 'hostinterfaces' do
308
+ it 'creates jmx interface for host' do
309
+ jmx_iface = create_jmx_interface
310
+ jmx_iface_hash = jmx_iface.to_hash
311
+ jmx_iface_hash['hostid'] = zrc.hosts.get_id(host)
312
+ zrc.hostinterfaces.create(jmx_iface_hash)
313
+ zrc.hostinterfaces.exists?(jmx_iface_hash).should be true
314
+ end
315
+
316
+ it 'check if interface exists for host' do
317
+ options = {}
318
+ options['hostid'] = zrc.hosts.get_id(host)
319
+ options['port'] = 10_050
320
+ options['type'] = 1
321
+ zrc.hostinterfaces.exists?(options).should be true
322
+
323
+ options['port'] = 9003
324
+ options['type'] = 4
325
+ zrc.hostinterfaces.exists?(options).should be false
326
+ end
327
+
328
+ it 'gets interface id' do
329
+ pending 'Not implemented'
330
+ end
331
+
332
+ it 'deletes an interface' do
333
+ pending 'Not implemented'
334
+ end
335
+ end
336
+
337
+ describe 'actions' do
338
+ before(:each) do
339
+ options = {}
340
+ usergroup_options = {}
341
+ usergroup_options['name'] = existing_usergroup
342
+ options['name'] = test_action
343
+ options['eventsource'] = 0
344
+ options['evaltype'] = 1 # AND
345
+ options['status'] = 1 # Disabled
346
+ options['esc_period'] = 3600
347
+ options['def_shortdata'] = '{TRIGGER.NAME}: {TRIGGER.STATUS}'
348
+ options['def_longdata'] = "{TRIGGER.NAME}: {TRIGGER.STATUS}\r\nLast value: {ITEM.LASTVALUE}\r\n\r\n{TRIGGER.URL}"
349
+ options['conditions'] = [{
350
+ 'conditiontype' => 0, # Hostgroup
351
+ 'operator' => 0, # =
352
+ 'value' => zrc.hostgroups.get_id('Templates')
353
+ },
354
+ # not in maintenance
355
+ {
356
+ 'conditiontype' => 16, # Maintenance
357
+ 'operator' => 7, # not in
358
+ 'value' => 'maintenance'
359
+ }]
360
+ options['operations'] = [{
361
+ 'operationtype' => 0,
362
+ 'esc_period' => 0,
363
+ 'esc_step_from' => 1,
364
+ 'esc_step_to' => 1,
365
+ 'evaltype' => 0,
366
+ 'opmessage_grp' => [{
367
+ 'usrgrpid' => zrc.usergroups.get_id(usergroup_options)
368
+ }],
369
+ 'opmessage' => {
370
+ 'default_msg' => 1,
371
+ 'mediatypeid' => 1
372
+ }
373
+ }]
374
+ zrc.actions.create(options)
375
+ end
376
+
377
+ after(:each) do
378
+ options = {}
379
+ options['name'] = test_action
380
+ action_id = zrc.actions.get_id(options)
381
+ zrc.actions.delete(action_id)
382
+ end
383
+
384
+ it 'checks if an action exists' do
385
+ options = {}
386
+ options['name'] = existing_action_name
387
+ zrc.actions.exists?(options).should be true
388
+ options['name'] = non_existing_action_name
389
+ zrc.actions.exists?(options).should be false
390
+ end
391
+
392
+ it 'gets an id of an action' do
393
+ options = {}
394
+ options['name'] = test_action
395
+ result = zrc.actions.get_id(options)
396
+ result.to_i.should >= 0
397
+ end
398
+ end
399
+
400
+ describe 'usergroups' do
401
+ before(:each) do
402
+ options = {}
403
+ options['name'] = test_usergroup
404
+ options['rights'] = {
405
+ 'permission' => 3,
406
+ 'id' => zrc.hostgroups.get_id(hostgroup_with_hosts)
407
+ }
408
+ zrc.usergroups.create(options)
409
+ end
410
+
411
+ after(:each) do
412
+ options = {}
413
+ options['name'] = test_usergroup
414
+ usergroup_id = zrc.usergroups.get_id(options)
415
+ zrc.usergroups.delete(usergroup_id)
416
+ end
417
+
418
+ it 'checks if a usergroup exists' do
419
+ options = {}
420
+ options['name'] = existing_usergroup
421
+ zrc.usergroups.exists?(options).should be true
422
+ options['name'] = non_existing_usergroup
423
+ zrc.usergroups.exists?(options).should be false
424
+ end
425
+
426
+ it 'gets the id of a usergroup by name' do
427
+ options = {}
428
+ options['name'] = test_usergroup
429
+ result = zrc.usergroups.get_id(options)
430
+ result.to_i.should >= 0
431
+ options['name'] = non_existing_usergroup
432
+ expect { zrc.usergroups.get_id(options) }.to raise_error(Usergroups::NonExistingUsergroup)
433
+ end
434
+ end
435
+
436
+ describe 'user' do
437
+ before(:each) do
438
+ user_options = {}
439
+ group_options = {}
440
+ group_options['name'] = existing_usergroup
441
+ group_id = zrc.usergroups.get_id(group_options)
442
+ user_options['alias'] = test_user
443
+ user_options['passwd'] = random_string
444
+ user_options['usrgrps'] = [{
445
+ 'usrgrpid' => group_id
446
+ }]
447
+
448
+ user_options['user_medias'] = [{
449
+ 'mediatypeid' => 1,
450
+ 'sendto' => 'support@company.com',
451
+ 'active' => 0,
452
+ 'severity' => 63,
453
+ 'period' => '1-7,00:00-24:00'
454
+ }]
455
+ zrc.users.create(user_options)
456
+ end
457
+
458
+ after(:each) do
459
+ user_options = {}
460
+ user_options['alias'] = test_user
461
+ user_options['userid'] = zrc.users.get_id(user_options)
462
+ zrc.users.delete([user_options['userid']])
463
+ end
464
+
465
+ it 'checks if a user exists' do
466
+ options = {}
467
+ options['alias'] = test_user
468
+ zrc.users.exists?(options).should be true
469
+ options['alias'] = non_existing_user
470
+ zrc.users.exists?(options).should be false
471
+ end
472
+
473
+ it 'gets the id of a user' do
474
+ options = {}
475
+ options['alias'] = test_user
476
+ result = zrc.users.get_id(options)
477
+ result.to_i.should >= 0
478
+ options['alias'] = non_existing_user
479
+ expect { zrc.users.get_id(options) }.to raise_error(Users::NonExistingUser)
480
+ end
481
+ end
482
+ end
483
+
484
+ def create_interface
485
+ Interface.new(
486
+ 'ip' => random_local_ip,
487
+ 'dns' => random_domain
488
+ )
489
+ end
490
+
491
+ def create_jmx_interface
492
+ Interface.new(
493
+ 'ip' => random_local_ip,
494
+ 'dns' => random_domain,
495
+ 'type' => 4, # JMX
496
+ 'main' => 1, # default jmx interface
497
+ 'port' => 9003
498
+ )
499
+ end
500
+
501
+ def random_string
502
+ rand(36**7...36**8).to_s(36)
503
+ end
504
+
505
+ def random_int
506
+ rand(64)
507
+ end
508
+
509
+ def random_local_ip
510
+ "127.0.0.#{random_int}"
511
+ end
512
+
513
+ def random_domain
514
+ "#{random_string}.our-cloud.de"
515
+ end
516
+ end