rubix 0.0.7 → 0.0.8

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.7
1
+ 0.0.8
@@ -10,7 +10,7 @@ module Rubix
10
10
 
11
11
  include Logs
12
12
 
13
- attr_reader :uri, :server, :auth, :request_id, :username, :password
13
+ attr_reader :uri, :server, :auth, :request_id, :username, :password, :last_response
14
14
 
15
15
  def initialize uri_or_string, username=nil, password=nil
16
16
  self.uri = uri_or_string
@@ -62,7 +62,7 @@ module Rubix
62
62
  when response.code.to_i >= 500
63
63
  raise ConnectionError.new("Too many consecutive failed requests (#{max_attempts}) to the Zabbix API at (#{uri}).")
64
64
  else
65
- Response.new(response)
65
+ @last_response = Response.new(response)
66
66
  end
67
67
  end
68
68
 
@@ -16,13 +16,37 @@ module Rubix
16
16
 
17
17
  attr_accessor :name
18
18
 
19
- def self.find_request options={}
20
- request('application.get', 'hostids' => [options[:host_id]], 'filter' => {'name' => options[:name], 'applicationid' => options[:id]}, "output" => "extend")
19
+ #
20
+ # == Associations ==
21
+ #
22
+
23
+ include Associations::BelongsToHost
24
+
25
+ #
26
+ # == Requests ==
27
+ #
28
+
29
+ def create_params
30
+ {:name => name, :hostid => host_id}
21
31
  end
22
32
 
33
+ def update_params
34
+ { id_field => id, :name => name, :hosts => {:hostid => host_id}}
35
+ end
36
+
37
+ def self.find_params options={}
38
+ super().merge({
39
+ :hostids => [options[:host_id]],
40
+ :filter => {
41
+ :name => options[:name],
42
+ id_field => options[:id]
43
+ }
44
+ })
45
+ end
46
+
23
47
  def self.build app
24
48
  params = {
25
- :id => app['applicationid'].to_i,
49
+ :id => app[id_field].to_i,
26
50
  :name => app['name']
27
51
  }
28
52
 
@@ -35,32 +59,5 @@ module Rubix
35
59
  new(params)
36
60
  end
37
61
 
38
- def self.id_field
39
- 'applicationid'
40
- end
41
-
42
-
43
- #
44
- # == Associations ==
45
- #
46
-
47
- include Associations::BelongsToHost
48
-
49
- #
50
- # == CRUD ==
51
- #
52
-
53
- def create_request
54
- request('application.create', 'name' => name, 'hostid' => host_id)
55
- end
56
-
57
- def update_request
58
- request('application.update', 'applicationid' => id, 'name' => name, 'hosts' => {'hostid' => host_id})
59
- end
60
-
61
- def destroy_request
62
- request('application.delete', [id])
63
- end
64
-
65
62
  end
66
63
  end
@@ -33,26 +33,6 @@ module Rubix
33
33
  self.user_macros = properties[:user_macros]
34
34
  end
35
35
 
36
- def self.find_request options={}
37
- request('host.get', 'filter' => {'host' => options[:name], 'hostid' => options[:id]}, 'select_groups' => 'refer', 'selectParentTemplates' => 'refer', 'select_profile' => 'refer', 'select_macros' => 'extend', 'output' => 'extend')
38
- end
39
-
40
- def self.build host
41
- new({
42
- :id => host['hostid'].to_i,
43
- :name => host['host'],
44
- :host_group_ids => host['groups'].map { |group| group['groupid'].to_i },
45
- :template_ids => host['parentTemplates'].map { |template| (template['templateid'] || template['hostid']).to_i },
46
- :user_macros => host['macros'].map { |um| UserMacro.new(:host_id => um['hostid'].to_i, :id => um['hostmacroid'], :value => um['value'], :macro => um['macro']) },
47
- :profile => host['profile'],
48
- :port => host['port']
49
- })
50
- end
51
-
52
- def self.id_field
53
- 'hostid'
54
- end
55
-
56
36
  #
57
37
  # == Associations ==
58
38
  #
@@ -71,37 +51,40 @@ module Rubix
71
51
  end
72
52
 
73
53
  #
74
- # == CRUD ==
54
+ # == Requests ==
75
55
  #
76
56
 
77
- def params
78
- {}.tap do |hp|
79
- hp['host'] = name
80
-
81
- hp['profile'] = profile if profile
82
- hp['status'] = status if status
57
+ def create_params
58
+ {
59
+ :host => name,
60
+ :groups => host_group_params,
61
+ :templates => template_params,
62
+ :macros => user_macro_params
63
+ }.tap do |hp|
64
+ hp[:profile] = profile if profile
65
+ hp[:status] = status if status
83
66
 
84
67
  if ip
85
- hp['ip'] = ip
86
- hp['useip'] = true
87
- hp['port'] = port || self.class::DEFAULT_PORT
68
+ hp[:ip] = ip
69
+ hp[:useip] = true
70
+ hp[:port] = port || self.class::DEFAULT_PORT
88
71
  else
89
- hp['ip'] = self.class::BLANK_IP
72
+ hp[:ip] = self.class::BLANK_IP
90
73
  end
91
-
92
74
  end
93
75
  end
94
76
 
95
- def create_request
96
- request('host.create', params.merge('groups' => host_group_params, 'templates' => template_params, 'macros' => user_macro_params))
97
- end
98
-
99
- def update_request
100
- request('host.update', params.merge('hostid' => id))
77
+ def update_params
78
+ create_params.tap do |cp|
79
+ cp.delete(:groups)
80
+ cp.delete(:templates)
81
+ cp.delete(:macros)
82
+ cp[id_field] = id
83
+ end
101
84
  end
102
85
 
103
86
  def before_update
104
- response = request('host.massUpdate', { 'groups' => host_group_params, 'templates' => template_params, 'macros' => user_macro_params, 'hosts' => [{'hostid' => id}]})
87
+ response = request('host.massUpdate', { :groups => host_group_params, :templates => template_params, :macros => user_macro_params, :hosts => [{id_field => id}]})
105
88
  if response.has_data?
106
89
  true
107
90
  else
@@ -110,8 +93,33 @@ module Rubix
110
93
  end
111
94
  end
112
95
 
113
- def destroy_request
114
- request('host.delete', [{'hostid' => id}])
96
+ def destroy_params
97
+ [{id_field => id}]
98
+ end
99
+
100
+ def before_destroy
101
+ return true if user_macros.nil? || user_macros.empty?
102
+ user_macros.map { |um| um.destroy }.all?
103
+ end
104
+
105
+ def self.build host
106
+ new({
107
+ :id => host[id_field].to_i,
108
+ :name => host['host'],
109
+ :host_group_ids => host['groups'].map { |group| group['groupid'].to_i },
110
+ :template_ids => host['parentTemplates'].map { |template| (template['templateid'] || template[id_field]).to_i },
111
+ :user_macros => host['macros'].map { |um| UserMacro.new(:host_id => um[id_field].to_i, :id => um['hostmacroid'], :value => um['value'], :macro => um['macro']) },
112
+ :profile => host['profile'],
113
+ :port => host['port']
114
+ })
115
+ end
116
+
117
+ def self.get_params
118
+ super().merge({:select_groups => :refer, :selectParentTemplates => :refer, :select_profile => :refer, :select_macros => :extend})
119
+ end
120
+
121
+ def self.find_params options={}
122
+ get_params.merge(:filter => {:host => options[:name], id_field => options[:id]})
115
123
  end
116
124
 
117
125
  end
@@ -16,17 +16,6 @@ module Rubix
16
16
 
17
17
  attr_accessor :name
18
18
 
19
- def self.find_request options={}
20
- request('hostgroup.get', 'filter' => {'groupid' => options[:id], 'name' => options[:name]}, 'select_hosts' => 'refer', 'output' => 'extend')
21
- end
22
-
23
- def self.build host_group
24
- new({
25
- :id => host_group['groupid'].to_i,
26
- :name => host_group['name'],
27
- :host_ids => host_group['hosts'].map { |host_info| host_info['hostid'].to_i }
28
- })
29
- end
30
19
 
31
20
  def self.id_field
32
21
  'groupid'
@@ -39,20 +28,32 @@ module Rubix
39
28
  include Associations::HasManyHosts
40
29
 
41
30
  #
42
- # == CRUD ==
31
+ # == Requests ==
43
32
  #
44
-
45
- def create_request
46
- request('hostgroup.create', [{'name' => name}])
33
+
34
+ def create_params
35
+ {:name => name}
47
36
  end
48
37
 
49
- def update_request
50
- request('hostgroup.update', [{'groupid' => id, 'name' => name}])
38
+ def destroy_params
39
+ [{id_field => id}]
51
40
  end
52
41
 
53
- def destroy_request
54
- request('hostgroup.delete', [{'groupid' => id}])
42
+ def self.get_params
43
+ super().merge(:select_hosts => :refer)
55
44
  end
56
45
 
46
+ def self.find_params options={}
47
+ get_params.merge(:filter => {id_field => options[:id], :name => options[:name]})
48
+ end
49
+
50
+ def self.build host_group
51
+ new({
52
+ :id => host_group[id_field].to_i,
53
+ :name => host_group['name'],
54
+ :host_ids => host_group['hosts'].map { |host_info| host_info['hostid'].to_i }
55
+ })
56
+ end
57
+
57
58
  end
58
59
  end
@@ -66,25 +66,6 @@ module Rubix
66
66
  def resource_name
67
67
  "#{self.class.resource_name} #{self.key || self.id}"
68
68
  end
69
-
70
- def self.find_request options={}
71
- request('item.get', 'hostids' => [options[:host_id]], 'filter' => {'key_' => options[:key], 'id' => options[:id]}, "select_applications" => "refer", "output" => "extend")
72
- end
73
-
74
- def self.build item
75
- new({
76
- :id => item['itemid'].to_i,
77
- :host_id => item['hostid'].to_i,
78
- :description => item['description'],
79
- :value_type => item['value_type'] ? self::VALUE_NAMES[item['value_type'].to_i] : :character,
80
- :application_ids => (item['applications'] || []).map { |app| app['applicationid'].to_i },
81
- :key => item['key_']
82
- })
83
- end
84
-
85
- def self.id_field
86
- 'itemid'
87
- end
88
69
 
89
70
  def value_type= vt
90
71
  if vt.nil? || vt.to_s.empty? || !self.class::VALUE_CODES.include?(vt.to_sym)
@@ -102,10 +83,10 @@ module Rubix
102
83
  include Associations::HasManyApplications
103
84
 
104
85
  #
105
- # == CRUD ==
86
+ # == Requests ==
106
87
  #
107
88
 
108
- def params
89
+ def create_params
109
90
  {
110
91
  :hostid => host_id,
111
92
  :description => (description || 'Unknown'),
@@ -116,17 +97,30 @@ module Rubix
116
97
  p[:applications] = application_ids if application_ids
117
98
  end
118
99
  end
119
-
120
- def create_request
121
- request('item.create', params)
122
- end
123
100
 
124
- def update_request
125
- request('item.update', params.merge('itemid' => id))
101
+ def self.get_params
102
+ super().merge(:select_applications => :refer)
103
+ end
104
+
105
+ def self.find_params options={}
106
+ super().merge({
107
+ :hostids => [options[:host_id]],
108
+ :filter => {
109
+ :key_ => options[:key],
110
+ :id => options[:id]
111
+ }
112
+ })
126
113
  end
127
114
 
128
- def destroy_request
129
- request('item.delete', [id])
115
+ def self.build item
116
+ new({
117
+ :id => item[id_field].to_i,
118
+ :host_id => item['hostid'].to_i,
119
+ :description => item['description'],
120
+ :value_type => item['value_type'] ? self::VALUE_NAMES[item['value_type'].to_i] : :character,
121
+ :application_ids => (item['applications'] || []).map { |app| app['applicationid'].to_i },
122
+ :key => item['key_']
123
+ })
130
124
  end
131
125
 
132
126
  end
@@ -9,22 +9,46 @@ module Rubix
9
9
  extend Logs
10
10
  include Logs
11
11
 
12
+ #
13
+ # == Identifiers ==
14
+ #
15
+
16
+ # This is the name of the resource as used inside Rubix -- Host,
17
+ # HostGroup, UserMacro, &c.
12
18
  def self.resource_name
13
19
  self.to_s.split('::').last
14
20
  end
15
21
 
22
+ # This is the name of *this* resource instance, using this
23
+ # object's 'name' property if possible.
16
24
  def resource_name
17
25
  "#{self.class.resource_name} #{respond_to?(:name) ? self.name : self.id}"
18
26
  end
27
+
28
+ # This is the name of the resource as used by Zabbix -- host,
29
+ # hostgroup, usermacro, &c.
30
+ def self.zabbix_name
31
+ resource_name.downcase
32
+ end
33
+
34
+ # This is the name of the id field returned in Zabbix responses --
35
+ # hostid, groupid, hostmacroid, &c.
36
+ def self.id_field
37
+ "#{zabbix_name}id"
38
+ end
39
+
40
+ def id_field
41
+ self.class.id_field
42
+ end
43
+
44
+ #
45
+ # == Initialization ==
46
+ #
19
47
 
20
48
  def initialize properties={}
21
49
  @properties = properties
22
50
  @id = properties[:id]
23
51
  end
24
-
25
- def new_record?
26
- @id.nil?
27
- end
28
52
 
29
53
  def request method, params
30
54
  self.class.request(method, params)
@@ -34,77 +58,84 @@ module Rubix
34
58
  Rubix.connection && Rubix.connection.request(method, params)
35
59
  end
36
60
 
37
- def self.find options={}
38
- response = find_request(options)
39
- case
40
- when response.has_data?
41
- build(response.result.first)
42
- when response.success?
43
- # a successful but empty response means it wasn't found
44
- else
45
- error("Error finding #{resource_name} using #{options.inspect}: #{response.error_message}")
46
- nil
47
- end
61
+ #
62
+ # == CRUD ==
63
+ #
64
+
65
+ def new_record?
66
+ @id.nil?
48
67
  end
49
68
 
50
- def self.find_or_create options={}
51
- response = find_request(options)
52
- case
53
- when response.has_data?
54
- build(response.result.first)
55
- when response.success?
56
- # doesn't exist
57
- obj = new(options)
58
- if obj.save
59
- obj
60
- else
61
- false
62
- end
63
- else
64
- error("Error creating #{resource_name} using #{options.inspect}: #{response.error_message}")
65
- false
66
- end
69
+ def save
70
+ new_record? ? create : update
67
71
  end
68
-
72
+
69
73
  def validate
70
74
  true
71
75
  end
72
-
76
+
77
+ def create_params
78
+ {}
79
+ end
80
+
81
+ def create_request
82
+ request("#{self.class.zabbix_name}.create", create_params)
83
+ end
84
+
73
85
  def create
74
86
  return false unless validate
75
87
  response = create_request
76
88
  if response.has_data?
77
- @id = response.result[self.class.id_field + 's'].first.to_i
89
+ @id = response.result[id_field + 's'].first.to_i
78
90
  info("Created #{resource_name}")
91
+ true
79
92
  else
80
93
  error("Error creating #{resource_name}: #{response.error_message}")
81
94
  return false
82
95
  end
83
96
  end
84
97
 
98
+ def update_params
99
+ create_params.merge({id_field => id})
100
+ end
101
+
102
+ def update_request
103
+ request("#{self.class.zabbix_name}.update", update_params)
104
+ end
105
+
85
106
  def update
86
107
  return false unless validate
87
108
  return create if new_record?
88
109
  return false unless before_update
89
110
  response = update_request
90
- if response.has_data?
111
+ case
112
+ when response.has_data? && response.result.values.first.map(&:to_i).include?(id)
91
113
  info("Updated #{resource_name}")
114
+ true
115
+ when response.has_data?
116
+ error("No error, but failed to update #{resource_name}")
117
+ false
92
118
  else
93
119
  error("Error updating #{resource_name}: #{response.error_message}")
94
- return false
120
+ false
95
121
  end
96
122
  end
97
123
 
98
124
  def before_update
99
125
  true
100
126
  end
101
-
102
- def save
103
- new_record? ? create : update
127
+
128
+ def destroy_params
129
+ [id]
104
130
  end
105
131
 
132
+ def destroy_request
133
+ request("#{self.class.zabbix_name}.delete", destroy_params)
134
+ end
135
+
106
136
  def destroy
107
- return false if new_record?
137
+ return true if new_record?
138
+ return false unless before_destroy
108
139
  response = destroy_request
109
140
  case
110
141
  when response.has_data? && response.result.values.first.first.to_i == id
@@ -119,5 +150,83 @@ module Rubix
119
150
  end
120
151
  end
121
152
 
153
+ def before_destroy
154
+ true
155
+ end
156
+
157
+ #
158
+ # == Index ==
159
+ #
160
+
161
+ def self.get_params
162
+ { :output => :extend }
163
+ end
164
+
165
+ def self.all_params options={}
166
+ get_params.merge(options)
167
+ end
168
+
169
+ def self.all_request options={}
170
+ request("#{zabbix_name}.get", all_params(options))
171
+ end
172
+
173
+ def self.all options={}
174
+ response = all_request(options)
175
+ if response.has_data?
176
+ response.result.map { |obj_data| build(obj_data) }
177
+ else
178
+ error("Error listing all #{resource_name}s: #{response.error_message}") unless response.success?
179
+ []
180
+ end
181
+ end
182
+
183
+ def self.each &block
184
+ all.each(&block)
185
+ end
186
+
187
+ #
188
+ # == Show ==
189
+ #
190
+
191
+ def self.find_params options={}
192
+ get_params.merge(options)
193
+ end
194
+
195
+ def self.find_request options={}
196
+ request("#{zabbix_name}.get", find_params(options))
197
+ end
198
+
199
+ def self.find options={}
200
+ response = find_request(options)
201
+ case
202
+ when response.has_data?
203
+ build(response.result.first)
204
+ when response.success?
205
+ # a successful but empty response means it wasn't found
206
+ else
207
+ error("Error finding #{resource_name} using #{options.inspect}: #{response.error_message}")
208
+ nil
209
+ end
210
+ end
211
+
212
+ def self.find_or_create options={}
213
+ response = find_request(options)
214
+ case
215
+ when response.has_data?
216
+ build(response.result.first)
217
+ when response.success?
218
+ # doesn't exist
219
+ obj = new(options)
220
+ if obj.save
221
+ obj
222
+ else
223
+ false
224
+ end
225
+ else
226
+ error("Error creating #{resource_name} using #{options.inspect}: #{response.error_message}")
227
+ false
228
+ end
229
+ end
230
+
122
231
  end
123
232
  end
@@ -19,30 +19,6 @@ module Rubix
19
19
  self.host_groups = properties[:host_groups]
20
20
  end
21
21
 
22
- def self.find_request options={}
23
- params = {'select_groups' => 'refer', 'select_hosts' => 'refer', 'output' => 'extend'}
24
- case
25
- when options[:id]
26
- params['templateids'] = [options[:id]]
27
- when options[:name]
28
- params['filter'] = { 'host' => options[:name] }
29
- end
30
- request('template.get', params)
31
- end
32
-
33
- def self.build template
34
- new({
35
- :id => (template['templateid'] || template['hostid']).to_i,
36
- :name => template['host'],
37
- :host_ids => template['hosts'].map { |host_info| host_info['hostid'].to_i },
38
- :host_group_ids => template['groups'].map { |group| group['groupid'].to_i }
39
- })
40
- end
41
-
42
- def self.id_field
43
- 'templateid'
44
- end
45
-
46
22
  #
47
23
  # == Validation ==
48
24
  #
@@ -63,16 +39,33 @@ module Rubix
63
39
  # == CRUD ==
64
40
  #
65
41
 
66
- def create_request
67
- request('template.create', {'host' => name, 'groups' => host_group_params})
42
+ def create_params
43
+ {:host => name, :groups => host_group_params}
68
44
  end
69
45
 
70
- def update_request
71
- request('template.update', [{'host' => name, 'templateid' => id, 'groups' => host_group_params}])
46
+ def update_params
47
+ [create_params.merge(id_field => id)]
72
48
  end
73
49
 
74
- def destroy_request
75
- request('template.delete', [{'templateid' => id}])
50
+ def destroy_params
51
+ [{id_field => id}]
52
+ end
53
+
54
+ def self.get_params
55
+ super().merge(:select_groups => :refer, :select_hosts => :refer)
56
+ end
57
+
58
+ def self.find_params options={}
59
+ get_params.merge(:filter => {:host => options[:name], :hostid => options[:id]})
60
+ end
61
+
62
+ def self.build template
63
+ new({
64
+ :id => (template[id_field] || template['hostid']).to_i,
65
+ :name => template['host'],
66
+ :host_ids => template['hosts'].map { |host_info| host_info['hostid'].to_i },
67
+ :host_group_ids => template['groups'].map { |group| group['groupid'].to_i }
68
+ })
76
69
  end
77
70
 
78
71
  end
@@ -6,30 +6,24 @@ module Rubix
6
6
  # == Properties & Finding ==
7
7
  #
8
8
 
9
- attr_accessor :name, :value
10
-
9
+ attr_accessor :value
10
+
11
11
  def initialize properties={}
12
12
  super(properties)
13
- @name = properties[:name] || self.class.unmacro_name(properties[:macro])
13
+ self.name = properties[:name] || self.class.unmacro_name(properties[:macro])
14
14
  @value = properties[:value]
15
15
 
16
16
  self.host = properties[:host]
17
17
  self.host_id = properties[:host_id]
18
18
  end
19
19
 
20
- def self.find_request options={}
21
- request('usermacro.get', 'hostids' => [options[:host_id]], 'filter' => {'macro' => macro_name(options[:name])}, "output" => "extend")
20
+ attr_reader :name
21
+ def name= n
22
+ return if n.nil? || n.empty?
23
+ raise ValidationError.new("Cannot change the name of a UserMacro once it's created.") if @name && (!new_record?)
24
+ @name = n
22
25
  end
23
26
 
24
- def self.build macro
25
- new({
26
- :id => macro['hostmacroid'].to_i,
27
- :name => unmacro_name(macro['macro']),
28
- :value => macro['value'],
29
- :host_id => macro['hostid']
30
- })
31
- end
32
-
33
27
  def self.unmacro_name name
34
28
  (name || '').gsub(/^\{\$/, '').gsub(/\}$/, '').upcase
35
29
  end
@@ -46,6 +40,10 @@ module Rubix
46
40
  'hostmacroid'
47
41
  end
48
42
 
43
+ def resource_name
44
+ "#{self.class.resource_name} #{self.name || self.id}"
45
+ end
46
+
49
47
  #
50
48
  # == Associations ==
51
49
  #
@@ -55,25 +53,48 @@ module Rubix
55
53
  #
56
54
  # == Validation ==
57
55
  #
56
+
58
57
  def validate
59
58
  raise ValidationError.new("A user macro must have both a 'name' and a 'value'") if name.nil? || name.strip.empty? || value.nil? || value.strip.empty?
60
59
  true
61
60
  end
62
61
 
63
62
  #
64
- # == CRUD ==
63
+ # == Requests ==
65
64
  #
65
+
66
+ def mass_add_params
67
+ { :macros => [{:macro => macro_name, :value => value}], :hosts => [{:hostid => host_id}] }
68
+ end
66
69
 
67
70
  def create_request
68
- request('usermacro.massAdd', 'macros' => [{'macro' => macro_name, 'value' => value}], 'hosts' => [{'hostid' => host_id}])
71
+ request('usermacro.massAdd', mass_add_params)
69
72
  end
70
73
 
71
74
  def update_request
72
- request('usermacro.massUpdate', 'macros' => [{'macro' => macro_name, 'value' => value}], 'hosts' => [{'hostid' => host_id}])
75
+ request('usermacro.massUpdate', mass_add_params)
73
76
  end
74
77
 
75
78
  def destroy_request
76
- request('usermacro.massRemove', 'hostids' => [host_id], 'macros' => [macro_name])
79
+ request('usermacro.massRemove', :hostids => [host_id], :macros => [macro_name])
80
+ end
81
+
82
+ def self.find_params options={}
83
+ super().merge({
84
+ :hostids => [options[:host_id]],
85
+ :filter => {
86
+ :macro => macro_name(options[:name])
87
+ }
88
+ })
89
+ end
90
+
91
+ def self.build macro
92
+ new({
93
+ :id => macro[id_field].to_i,
94
+ :name => unmacro_name(macro['macro']),
95
+ :value => macro['value'],
96
+ :host_id => macro['hostid']
97
+ })
77
98
  end
78
99
 
79
100
  end
@@ -4,19 +4,17 @@ describe "CRUD for hosts" do
4
4
 
5
5
  before do
6
6
  @hg = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- @hg.save
7
+ ensure_save(@hg)
8
8
 
9
9
  @h1 = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg])
10
- @h1.save
10
+ ensure_save(@h1)
11
11
 
12
12
  @h2 = Rubix::Host.new(:name => 'rubix_spec_host_2', :host_groups => [@hg])
13
- @h2.save
13
+ ensure_save(@h2)
14
14
  end
15
15
 
16
16
  after do
17
- @h1.destroy
18
- @h2.destroy
19
- @hg.destroy
17
+ ensure_destroy(@h1, @h2, @hg)
20
18
  end
21
19
 
22
20
  it "should be able to create, update, and destroy a host" do
@@ -24,22 +22,28 @@ describe "CRUD for hosts" do
24
22
 
25
23
  Rubix::Application.find(:name => 'rubix_spec_app_1', :host_id => @h1.id).should be_nil
26
24
 
27
- app = Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @h1.id)
28
- app.save
29
- new_a = Rubix::Application.find(:name => 'rubix_spec_app_1', :host_id => @h1.id)
30
- new_a.should_not be_nil
31
- new_a.id.should == app.id
32
- new_a.host_id.should == @h1.id
33
- id = app.id
25
+ app1 = Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @h1.id)
26
+ app1.save.should be_true
27
+ id = app1.id
34
28
  id.should_not be_nil
35
-
36
- app.name = 'rubix_spec_app_2'
37
- app.update
38
- new_a = Rubix::Application.find(:id => id, :name => 'rubix_spec_app_2', :host_id => @h1.id)
39
- new_a.should_not be_nil
40
- new_a.name.should == 'rubix_spec_app_2'
41
29
 
42
- app.destroy
43
- Rubix::Application.find(:id => id, :host_id => @h1.id).should be_nil
30
+ ensure_destroy(app1) do
31
+
32
+ app2 = Rubix::Application.find(:name => 'rubix_spec_app_1', :host_id => @h1.id)
33
+ app2.should_not be_nil
34
+ app2.id.should == app1.id
35
+ app2.host_id.should == @h1.id
36
+
37
+ app1.name = 'rubix_spec_app_2'
38
+ app1.save.should be_true
39
+
40
+ app2 = Rubix::Application.find(:id => id, :name => 'rubix_spec_app_2', :host_id => @h1.id)
41
+ app2.should_not be_nil
42
+ app2.name.should == 'rubix_spec_app_2'
43
+
44
+ app1.destroy.should be_true
45
+ Rubix::Application.find(:id => id, :host_id => @h1.id).should be_nil
46
+ Rubix::Application.find(:id => id, :host_id => @h2.id).should be_nil
47
+ end
44
48
  end
45
49
  end
@@ -7,17 +7,25 @@ describe "CRUD for host groups" do
7
7
 
8
8
  Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1').should be_nil
9
9
 
10
- hg = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
11
- hg.save
12
- Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1').should_not be_nil
13
- id = hg.id
10
+ hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
11
+ hg1.save.should be_true
12
+ id = hg1.id
14
13
  id.should_not be_nil
15
14
 
16
- hg.name = 'rubix_spec_host_group_2'
17
- hg.update
18
- Rubix::HostGroup.find(:id => id).name.should == 'rubix_spec_host_group_2'
19
-
20
- hg.destroy
21
- Rubix::HostGroup.find(:id => id).should be_nil
15
+ ensure_destroy(hg1) do
16
+ hg2 = Rubix::HostGroup.find(:name => 'rubix_spec_host_group_1')
17
+ hg2.should_not be_nil
18
+ hg2.name.should == 'rubix_spec_host_group_1'
19
+
20
+ hg1.name = 'rubix_spec_host_group_2'
21
+ hg1.save.should be_true
22
+
23
+ hg2 = Rubix::HostGroup.find(:name => 'rubix_spec_host_group_2')
24
+ hg2.should_not be_nil
25
+ hg2.name.should == 'rubix_spec_host_group_2'
26
+
27
+ hg1.destroy
28
+ Rubix::HostGroup.find(:id => id).should be_nil
29
+ end
22
30
  end
23
31
  end
@@ -4,27 +4,23 @@ describe "CRUD for hosts" do
4
4
 
5
5
  before do
6
6
  @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- @hg1.save
7
+ ensure_save(@hg1)
8
8
 
9
9
  @hg2 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_2')
10
- @hg2.save
10
+ ensure_save(@hg2)
11
11
 
12
12
  @t1 = Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@hg1])
13
- @t1.save
13
+ ensure_save(@t1)
14
14
 
15
15
  @t2 = Rubix::Template.new(:name => 'rubix_spec_template_2', :host_groups => [@hg2])
16
- @t2.save
16
+ ensure_save(@t2)
17
17
 
18
18
  @um1 = Rubix::UserMacro.new(:name => 'rubix_spec_macro_1', :value => 'rubix_spec_value_1')
19
- @um2 = Rubix::UserMacro.new(:name => 'rubix_spec_macro_2', :value => 'rubix_spec_value_2')
20
19
 
21
20
  end
22
21
 
23
22
  after do
24
- @t1.destroy
25
- @t2.destroy
26
- @hg1.destroy
27
- @hg2.destroy
23
+ ensure_destroy(@um1, @t1, @t2, @hg1, @hg2)
28
24
  end
29
25
 
30
26
  it "should be able to create, update, and destroy a host" do
@@ -32,39 +28,36 @@ describe "CRUD for hosts" do
32
28
 
33
29
  Rubix::Host.find(:name => 'rubix_spec_host_1').should be_nil
34
30
 
35
- h = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg1], :templates => [@t1], :user_macros => [@um1])
36
- h.save
37
- begin
38
- new_h = Rubix::Host.find(:name => 'rubix_spec_host_1')
39
- new_h.should_not be_nil
40
- new_h.template_ids.should include(@t1.id)
41
- new_h.host_group_ids.should include(@hg1.id)
42
- new_h.user_macros.size.should == 1
43
- new_h.user_macros.first.name.should == 'RUBIX_SPEC_MACRO_1'
44
- new_h.user_macros.first.value.should == 'rubix_spec_value_1'
45
-
46
- id = h.id
47
- id.should_not be_nil
31
+ h1 = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg1], :templates => [@t1], :user_macros => [@um1])
32
+ h1.save.should be_true
33
+ id = h1.id
34
+ id.should_not be_nil
35
+
36
+ ensure_destroy(h1) do
37
+ h2 = Rubix::Host.find(:name => 'rubix_spec_host_1')
38
+ h2.should_not be_nil
39
+ h2.template_ids.should include(@t1.id)
40
+ h2.host_group_ids.should include(@hg1.id)
41
+ h2.user_macros.size.should == 1
42
+ h2.user_macros.first.name.should == 'RUBIX_SPEC_MACRO_1'
43
+ h2.user_macros.first.value.should == 'rubix_spec_value_1'
48
44
 
49
- h.name = 'rubix_spec_host_2'
50
- h.host_groups = [@hg2]
51
- h.templates = [@t2]
52
- h.user_macros = [@um2]
53
- h.update
45
+ h1.name = 'rubix_spec_host_2'
46
+ h1.host_groups = [@hg2]
47
+ h1.templates = [@t2]
48
+ h1.save.should be_true
54
49
 
55
- new_h = Rubix::Host.find(:name => 'rubix_spec_host_2')
56
- new_h.should_not be_nil
57
- new_h.template_ids.should include(@t2.id)
58
- new_h.host_group_ids.should include(@hg2.id)
59
- new_h.user_macros.size.should == 1
60
- new_h.user_macros.first.name.should == 'RUBIX_SPEC_MACRO_2'
61
- new_h.user_macros.first.value.should == 'rubix_spec_value_2'
62
- rescue => e
63
- puts "#{e.class} -- #{e.message}"
64
- puts e.backtrace
65
- ensure
66
- h.destroy
50
+ h2 = Rubix::Host.find(:name => 'rubix_spec_host_2')
51
+ h2.should_not be_nil
52
+ h2.template_ids.should include(@t2.id)
53
+ h2.host_group_ids.should include(@hg2.id)
54
+ h2.user_macros.size.should == 1
55
+ h2.user_macros.first.name.should == 'RUBIX_SPEC_MACRO_1'
56
+ h2.user_macros.first.value.should == 'rubix_spec_value_1'
57
+
58
+ h1.destroy
59
+ Rubix::Host.find(:id => id).should be_nil
67
60
  end
68
- Rubix::Host.find(:id => id).should be_nil
61
+
69
62
  end
70
63
  end
@@ -4,28 +4,23 @@ describe "CRUD for items" do
4
4
 
5
5
  before do
6
6
  @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- @hg1.save
7
+ ensure_save(@hg1)
8
8
 
9
9
  @h1 = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg1])
10
- @h1.save
10
+ ensure_save(@h1)
11
11
 
12
12
  @h2 = Rubix::Host.new(:name => 'rubix_spec_host_2', :host_groups => [@hg1])
13
- @h2.save
13
+ ensure_save(@h2)
14
14
 
15
15
  @a1 = Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @h1.id)
16
- @a1.save
16
+ ensure_save(@a1)
17
17
 
18
18
  @a2 = Rubix::Application.new(:name => 'rubix_spec_app_1', :host_id => @h2.id)
19
- @a2.save
20
-
19
+ ensure_save(@a2)
21
20
  end
22
21
 
23
22
  after do
24
- @a1.destroy
25
- @a2.destroy
26
- @h1.destroy
27
- @h2.destroy
28
- @hg1.destroy
23
+ ensure_destroy(@a1, @a2, @h1, @h2, @hg1)
29
24
  end
30
25
 
31
26
  it "should be able to create, update, and destroy an item" do
@@ -33,36 +28,38 @@ describe "CRUD for items" do
33
28
 
34
29
  Rubix::Item.find(:key => 'rubix.spec1', :host_id => @h1.id).should be_nil
35
30
 
36
- item = Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @h1.id, :value_type => :character, :applications => [@a1])
37
- item.save
38
-
39
- new_i = Rubix::Item.find(:key => 'rubix.spec1', :host_id => @h1.id)
40
- new_i.should_not be_nil
41
- new_i.host.name.should == @h1.name
42
- new_i.key.should == 'rubix.spec1'
43
- new_i.description.should == 'rubix item description 1'
44
- new_i.value_type.should == :character
45
- new_i.application_ids.should include(@a1.id)
46
-
47
- id = item.id
31
+ item1 = Rubix::Item.new(:key => 'rubix.spec1', :description => 'rubix item description 1', :host_id => @h1.id, :value_type => :character, :applications => [@a1])
32
+ item1.save.should be_true
33
+ id = item1.id
48
34
  id.should_not be_nil
49
-
50
- item.key = 'rubix.spec2'
51
- item.description = 'rubix item description 2'
52
- item.value_type = :unsigned_int
53
- item.host_id = @h2.id
54
- item.applications = [@a2]
55
- item.update
56
-
57
- new_i = Rubix::Item.find(:key => 'rubix.spec2', :host_id => @h2.id)
58
- new_i.should_not be_nil
59
- new_i.host.name.should == @h2.name
60
- new_i.key.should == 'rubix.spec2'
61
- new_i.description.should == 'rubix item description 2'
62
- new_i.value_type.should == :unsigned_int
63
- new_i.application_ids.should include(@a2.id)
64
35
 
65
- item.destroy
66
- Rubix::Item.find(:id => id, :host_id => @h1.id).should be_nil
36
+ ensure_destroy(item1) do
37
+ item2 = Rubix::Item.find(:key => 'rubix.spec1', :host_id => @h1.id)
38
+ item2.should_not be_nil
39
+ item2.host.name.should == @h1.name
40
+ item2.key.should == 'rubix.spec1'
41
+ item2.description.should == 'rubix item description 1'
42
+ item2.value_type.should == :character
43
+ item2.application_ids.should include(@a1.id)
44
+
45
+ item1.key = 'rubix.spec2'
46
+ item1.description = 'rubix item description 2'
47
+ item1.value_type = :unsigned_int
48
+ item1.host_id = @h2.id
49
+ item1.applications = [@a2]
50
+ item1.save.should be_true
51
+
52
+ item2 = Rubix::Item.find(:key => 'rubix.spec2', :host_id => @h2.id)
53
+ item2.should_not be_nil
54
+ item2.host.name.should == @h2.name
55
+ item2.key.should == 'rubix.spec2'
56
+ item2.description.should == 'rubix item description 2'
57
+ item2.value_type.should == :unsigned_int
58
+ item2.application_ids.should include(@a2.id)
59
+
60
+ item1.destroy
61
+ Rubix::Item.find(:id => id, :host_id => @h1.id).should be_nil
62
+ Rubix::Item.find(:id => id, :host_id => @h2.id).should be_nil
63
+ end
67
64
  end
68
65
  end
@@ -4,16 +4,14 @@ describe "CRUD for templates" do
4
4
 
5
5
  before do
6
6
  @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- @hg1.save
7
+ ensure_save(@hg1)
8
8
 
9
9
  @hg2 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_2')
10
- @hg2.save
11
-
10
+ ensure_save(@hg2)
12
11
  end
13
12
 
14
13
  after do
15
- @hg1.destroy
16
- @hg2.destroy
14
+ ensure_destroy(@hg1, @hg2)
17
15
  end
18
16
 
19
17
  it "should be able to create, update, and destroy a template" do
@@ -21,27 +19,29 @@ describe "CRUD for templates" do
21
19
 
22
20
  Rubix::Template.find(:name => 'rubix_spec_template_1').should be_nil
23
21
 
24
- t = Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@hg1])
25
- t.save
26
-
27
- new_t = Rubix::Template.find(:name => 'rubix_spec_template_1')
28
- new_t.should_not be_nil
29
- new_t.id.should == t.id
30
- new_t.host_group_ids.should include(@hg1.id)
31
- id = t.id
22
+ t1 = Rubix::Template.new(:name => 'rubix_spec_template_1', :host_groups => [@hg1])
23
+ t1.save.should be_true
24
+ id = t1.id
32
25
  id.should_not be_nil
33
26
 
34
- t.name = 'rubix_spec_template_2'
35
- t.host_groups = [@hg2]
36
- t.update
37
-
38
- new_t = Rubix::Template.find(:id => id)
39
- new_t.name.should == 'rubix_spec_template_2'
40
- new_t.host_group_ids.should_not include(@hg1.id)
41
- new_t.host_group_ids.should include(@hg2.id)
42
-
43
- t.destroy
44
- Rubix::Template.find(:id => id).should be_nil
27
+ ensure_destroy(t1) do
28
+ t2 = Rubix::Template.find(:name => 'rubix_spec_template_1')
29
+ t2.should_not be_nil
30
+ t2.id.should == id
31
+ t2.host_group_ids.should include(@hg1.id)
32
+
33
+ t1.name = 'rubix_spec_template_2'
34
+ t1.host_groups = [@hg2]
35
+ t1.save.should be_true
36
+
37
+ t2 = Rubix::Template.find(:id => id)
38
+ t2.name.should == 'rubix_spec_template_2'
39
+ t2.host_group_ids.should_not include(@hg1.id)
40
+ t2.host_group_ids.should include(@hg2.id)
41
+
42
+ t1.destroy
43
+ Rubix::Template.find(:id => id).should be_nil
44
+ end
45
45
  end
46
46
 
47
47
  it "should be able to import and export a template" do
@@ -4,15 +4,14 @@ describe "CRUD for user macros" do
4
4
 
5
5
  before do
6
6
  @hg1 = Rubix::HostGroup.new(:name => 'rubix_spec_host_group_1')
7
- @hg1.save
7
+ ensure_save(@hg1)
8
8
 
9
9
  @h1 = Rubix::Host.new(:name => 'rubix_spec_host_1', :host_groups => [@hg1])
10
- @h1.save
10
+ ensure_save(@h1)
11
11
  end
12
12
 
13
13
  after do
14
- @h1.destroy
15
- @hg1.destroy
14
+ ensure_destroy(@h1, @hg1)
16
15
  end
17
16
 
18
17
  it "should be able to create, update, and destroy a host" do
@@ -20,17 +19,25 @@ describe "CRUD for user macros" do
20
19
 
21
20
  Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id).should be_nil
22
21
 
23
- um = Rubix::UserMacro.new(:name => 'rubix_spec_macro_1', :value => 'rubix_spec_value_1', :host_id => @h1.id)
24
- um.save
25
- Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id).should_not be_nil
26
- id = um.id
22
+ um1 = Rubix::UserMacro.new(:name => 'rubix_spec_macro_1', :value => 'rubix_spec_value_1', :host_id => @h1.id)
23
+ um1.save.should be_true
24
+
25
+ id = um1.id
27
26
  id.should_not be_nil
28
27
 
29
- um.value = 'rubix_spec_value_2'
30
- um.update
31
- Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id).value.should == 'rubix_spec_value_2'
28
+ ensure_destroy(um1) do
29
+ um2 = Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id)
30
+ um2.should_not be_nil
31
+
32
+ um1.value = 'rubix_spec_value_2'
33
+ um1.save.should be_true
34
+
35
+ um2 = Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id)
36
+ um2.should_not be_nil
37
+ um2.value.should == 'rubix_spec_value_2'
32
38
 
33
- um.destroy
34
- Rubix::UserMacro.find(:name => 'rubix_spec_macro_2', :host_id => @h1.id).should be_nil
39
+ um1.destroy
40
+ Rubix::UserMacro.find(:name => 'rubix_spec_macro_1', :host_id => @h1.id).should be_nil
41
+ end
35
42
  end
36
43
  end
@@ -4,6 +4,43 @@ module Rubix
4
4
  def integration_test
5
5
  pending("A live Zabbix API to test against") unless $RUBIX_INTEGRATION_TEST
6
6
  end
7
+
8
+ def ensure_save(obj)
9
+ begin
10
+ raise Rubix::Error.new(Rubix.connection.last_response.error_message) unless obj.save
11
+ rescue => e
12
+ puts "#{e.class} -- #{e.message}"
13
+ puts e.backtrace
14
+ raise e
15
+ end
16
+ end
17
+
18
+ def ensure_destroy *objs, &block
19
+ begin
20
+ if block_given?
21
+ yield
22
+ else
23
+ errors = []
24
+ objs.each do |obj|
25
+ errors << Rubix.connection.last_response.error_message unless obj.destroy
26
+ end
27
+ raise Rubix::Error.new(errors.join("\n")) if errors.size > 0
28
+ end
29
+ rescue => e
30
+ puts "#{e.class} -- #{e.message}"
31
+ puts e.backtrace
32
+ objs.each do |obj|
33
+ begin
34
+ puts "COULD NOT DESTROY #{obj.resource_name}" unless obj.destroy
35
+ rescue => f
36
+ puts "COULD NOT DESTROY #{obj.resource_name}"
37
+ puts "#{e.class} -- #{e.message}"
38
+ puts e.backtrace
39
+ end
40
+ end
41
+ raise e
42
+ end
43
+ end
7
44
 
8
45
  end
9
46
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubix
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dhruv Bansal