hawkular-client 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +4 -4
  2. data/.coveralls.yml +1 -0
  3. data/.gitignore +2 -1
  4. data/.rubocop.yml +19 -6
  5. data/CHANGES.rdoc +13 -3
  6. data/README.rdoc +9 -3
  7. data/hawkularclient.gemspec +8 -4
  8. data/lib/alerts/alerts_api.rb +202 -32
  9. data/lib/hawkular.rb +35 -67
  10. data/lib/hawkular_all.rb +2 -0
  11. data/lib/inventory/inventory_api.rb +459 -130
  12. data/lib/metrics/metric_api.rb +14 -9
  13. data/lib/operations/operations_api.rb +247 -0
  14. data/lib/tokens/tokens_api.rb +33 -0
  15. data/lib/version.rb +1 -1
  16. data/spec/integration/alerts_spec.rb +272 -20
  17. data/spec/integration/hello-world-definitions.json +46 -0
  18. data/spec/integration/inventory_spec.rb +253 -89
  19. data/spec/integration/metric_spec.rb +36 -0
  20. data/spec/integration/operations_spec.rb +420 -0
  21. data/spec/integration/tokens_spec.rb +45 -0
  22. data/spec/resources/driver.jar +0 -0
  23. data/spec/resources/sample.war +0 -0
  24. data/spec/spec_helper.rb +30 -6
  25. data/spec/unit/base_spec.rb +22 -1
  26. data/spec/unit/canonical_path_spec.rb +92 -0
  27. data/spec/vcr/vcr_setup.rb +3 -4
  28. data/spec/vcr_cassettes/Alert/EndToEnd/Should_create_and_fire_a_trigger.yml +1187 -0
  29. data/spec/vcr_cassettes/Alert/Events/Should_list_events.yml +101 -0
  30. data/spec/vcr_cassettes/Alert/Events/Should_list_events_using_criteria.yml +79 -0
  31. data/spec/vcr_cassettes/Alert/Events/Should_not_list_events_using_criteria.yml +49 -0
  32. data/spec/vcr_cassettes/Alert/Triggers/Should_bulk_load_triggers.yml +225 -0
  33. data/spec/vcr_cassettes/Alert/Triggers/Should_create_a_basic_trigger_with_action.yml +355 -0
  34. data/spec/vcr_cassettes/Alert/Triggers/Should_create_an_action.yml +134 -0
  35. data/spec/vcr_cassettes/Alert/Triggers/Should_create_an_action_for_webhooks.yml +220 -0
  36. data/spec/vcr_cassettes/Alert/Triggers/Should_get_the_action_definitions.yml +218 -0
  37. data/spec/vcr_cassettes/Alert/Triggers/Should_not_create_an_action_for_unknown_plugin.yml +46 -0
  38. data/spec/vcr_cassettes/Alert/Triggers/Should_not_create_an_action_for_unknown_properties.yml +134 -0
  39. data/spec/vcr_cassettes/Counter_metrics/Should_get_metrics_with_limit_and_order.yml +270 -0
  40. data/spec/vcr_cassettes/Inventory/Client_should_listen_on_various_inventory_events.json +47 -0
  41. data/spec/vcr_cassettes/Inventory/Client_should_listen_on_various_inventory_events.yml +191 -0
  42. data/spec/vcr_cassettes/Inventory/Helpers/generate_some_events_for_websocket.yml +507 -0
  43. data/spec/vcr_cassettes/Inventory/Should_List_datasources_with_no_props.yml +74 -69
  44. data/spec/vcr_cassettes/Inventory/{Should_list_types_without_feed.yml → Should_create_a_feed.yml} +29 -32
  45. data/spec/vcr_cassettes/Inventory/Should_create_a_feed_again.yml +211 -0
  46. data/spec/vcr_cassettes/Inventory/Should_create_a_resource_.yml +355 -0
  47. data/spec/vcr_cassettes/Inventory/Should_create_a_resource_with_metric.yml +786 -0
  48. data/spec/vcr_cassettes/Inventory/Should_create_a_resourcetype.yml +205 -0
  49. data/spec/vcr_cassettes/Inventory/Should_create_and_delete_feed.yml +201 -0
  50. data/spec/vcr_cassettes/Inventory/Should_create_and_get_a_resource.yml +419 -0
  51. data/spec/vcr_cassettes/Inventory/Should_list_URLs.yml +39 -27
  52. data/spec/vcr_cassettes/Inventory/Should_list_WildFlys.yml +37 -29
  53. data/spec/vcr_cassettes/Inventory/Should_list_WildFlys_with_props.yml +53 -45
  54. data/spec/vcr_cassettes/Inventory/Should_list_all_the_resource_types.yml +177 -0
  55. data/spec/vcr_cassettes/Inventory/Should_list_children_of_WildFly.yml +230 -56
  56. data/spec/vcr_cassettes/Inventory/Should_list_feeds.yml +27 -24
  57. data/spec/vcr_cassettes/Inventory/Should_list_heap_metrics_for_WildFlys.yml +586 -119
  58. data/spec/vcr_cassettes/Inventory/Should_list_metrics_for_WildFlys.yml +220 -59
  59. data/spec/vcr_cassettes/Inventory/Should_list_metrics_of_given_metric_type.yml +613 -0
  60. data/spec/vcr_cassettes/Inventory/Should_list_metrics_of_given_resource_type.yml +333 -0
  61. data/spec/vcr_cassettes/Inventory/Should_list_recursive_children_of_WildFly.yml +2064 -0
  62. data/spec/vcr_cassettes/Inventory/Should_list_relationships_of_WildFly.yml +460 -0
  63. data/spec/vcr_cassettes/Inventory/Should_list_types_with_bad_feed.yml +26 -25
  64. data/spec/vcr_cassettes/Inventory/Should_list_types_with_feed.yml +102 -36
  65. data/spec/vcr_cassettes/Inventory/Should_not_find_an_unknown_resource.yml +54 -0
  66. data/spec/vcr_cassettes/Inventory/Tenants/Should_Get_Tenant_For_Explicit_Credentials.yml +50 -0
  67. data/spec/vcr_cassettes/Inventory/Tenants/Should_Get_Tenant_For_Implicit_Credentials.yml +50 -0
  68. data/spec/vcr_cassettes/Operation/Helpers/get_tenant.yml +50 -0
  69. data/spec/vcr_cassettes/Operation/Operation/Add_JDBC_driver_should_add_the_driver.json +26 -0
  70. data/spec/vcr_cassettes/Operation/Operation/Add_datasource_should_be_doable.json +26 -0
  71. data/spec/vcr_cassettes/Operation/Operation/Add_deployment_should_be_doable.json +26 -0
  72. data/spec/vcr_cassettes/Operation/Operation/Redeploy_can_be_run_multiple_times_in_parallel.json +40 -0
  73. data/spec/vcr_cassettes/Operation/Operation/Redeploy_should_be_performed_and_eventually_respond_with_success.json +26 -0
  74. data/spec/vcr_cassettes/Operation/Operation/Redeploy_should_not_be_performed_if_resource_path_is_wrong.json +26 -0
  75. data/spec/vcr_cassettes/Operation/Operation/Remove_JDBC_driver_should_be_performed_and_eventually_respond_with_success.json +26 -0
  76. data/spec/vcr_cassettes/Operation/Operation/Remove_datasource_should_be_performed_and_eventually_respond_with_success.json +26 -0
  77. data/spec/vcr_cassettes/Operation/Operation/Remove_deployment_should_be_performed_and_eventually_respond_with_success.json +26 -0
  78. data/spec/vcr_cassettes/Operation/Operation/Undeploy_should_be_performed_and_eventually_respond_with_success.json +26 -0
  79. data/spec/vcr_cassettes/Operation/Operation/should_not_be_possible_to_perform_on_closed_client.json +12 -0
  80. data/spec/vcr_cassettes/Operation/Websocket_connection/should_be_established.json +9 -0
  81. data/spec/vcr_cassettes/Tokens/Should_be_able_to_create_a_new_token_for_an_actual_user.yml +57 -0
  82. data/spec/vcr_cassettes/Tokens/Should_be_able_to_list_the_available_tokens.yml +49 -0
  83. data/spec/vcr_cassettes/Tokens/Should_get_a_401_when_attempting_to_create_a_token_with_a_wrong_password.yml +56 -0
  84. data/spec/vcr_cassettes/Tokens/Should_get_a_401_when_attempting_to_create_a_token_without_a_password.yml +55 -0
  85. metadata +175 -11
  86. data/lib/metrics/version.rb +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ed7c1be5e375b0389cf9a4849fb45c78641becd0
4
- data.tar.gz: 03c792250b1ba9ed81c25f9ecda174b983c843e2
3
+ metadata.gz: 5bb18420ad9844b7c10d6e5e4e0df657dce7c172
4
+ data.tar.gz: 1bcac0ba50a633ecbf512a225c78aae0fb06fdf1
5
5
  SHA512:
6
- metadata.gz: b5b8f44790e31b63e0574a4d09b510de537275f121d88e93712e24fc33c1e7543ace9e9539f1beb410ff70f1fce8148edee7b4b7431601d7341407f820d4c05c
7
- data.tar.gz: b77f46b103e59f10d7568dd1c96de81399192a01fe1fef2038d0f3635fbb8d1a545bcedc785b52bf60cbfea0d29a94eaa2a92f39b7e20f7c6e166057d770bd97
6
+ metadata.gz: b2adc059140248e2a9abea8c4000ae4df32b6004534b7eb1c1eb02a2f234b5a3294993142abb4e37cc8f4d5319f47a45729fbb1f4c335aac66d978852b271ee8
7
+ data.tar.gz: 0945f21efb375d356404683b19888fecadc3698dd55e496eb33a727d9cd411caabd2a883e23e13fe77dd829578f4c082d107fbefd8de5756379226133daa51ba
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore CHANGED
@@ -9,6 +9,7 @@
9
9
  /test/version_tmp/
10
10
  /tmp/
11
11
  spec/vcr.log
12
+ Gemfile.lock
12
13
 
13
14
  ## Specific to RubyMotion:
14
15
  .dat*
@@ -28,7 +29,7 @@ build/
28
29
 
29
30
  # for a library or gem, you might want to ignore these files since the code is
30
31
  # intended to run in multiple environments; otherwise, check them in:
31
- # Gemfile.lock
32
+ Gemfile.lock
32
33
  # .ruby-version
33
34
  # .ruby-gemset
34
35
 
data/.rubocop.yml CHANGED
@@ -1,14 +1,27 @@
1
- MethodLength:
2
- Enabled: false
1
+ # possible values:
2
+ # https://github.com/bbatsov/rubocop/blob/master/config/
3
+
4
+ #
5
+ # Overrides
6
+ #
7
+ CyclomaticComplexity:
8
+ Severity: refactor
9
+ Metrics/LineLength:
10
+ Max: 120
11
+ Metrics/ParameterLists:
12
+ Max: 8
13
+ #
14
+ # Enabled/Disabled
15
+ #
3
16
  ClassLength:
4
17
  Enabled: false
5
- ModuleLength:
18
+ Documentation:
19
+ Enabled: false
20
+ MethodLength:
6
21
  Enabled: false
7
22
  Metrics/AbcSize:
8
23
  Enabled: false
9
- Documentation:
24
+ ModuleLength:
10
25
  Enabled: false
11
26
  Style/ClassAndModuleChildren:
12
27
  Enabled: false
13
- Metrics/LineLength:
14
- Max: 100
data/CHANGES.rdoc CHANGED
@@ -1,12 +1,22 @@
1
1
  == Changes
2
2
 
3
- This document describes the relevant changes beween releases of the
3
+ This document describes the relevant changes between releases of the
4
4
  _hawkular-client_ project.
5
5
 
6
+ === V 0.2.1
7
+
8
+ * Add support to create items in inventory
9
+ * Add support to percentiles, limit and order parameters in Metrics API
10
+ * Add support to run operations on WildFly agent via websockets
11
+ * Add support to create items in alerts
12
+ * Add support for auth tokens (can be created by the client on the server)
13
+ * Add support to consume inventory events via websockets
14
+ * Add support to (offline) test websockets by recorded communication (VCR)
15
+
6
16
  === V 0.2.0
7
17
 
8
18
  * Add support for Hawkular-Inventory and Hawkular-Alerts
9
- * Reshuffeling of inner classes
19
+ * Reshuffling of inner classes
10
20
  * The sub-url that is passed to `http_*` methods is escaped via
11
21
  `Addressable::URI.escape()`
12
22
 
@@ -18,5 +28,5 @@ _hawkular-client_ project.
18
28
  === V 0.1.1
19
29
 
20
30
  * Add VCR tests
21
- * Metrics::Client.new now requries a hash for credentials
31
+ * Metrics::Client.new now requires a hash for credentials
22
32
  * Code cleanup and enforcement via RuboCop
data/README.rdoc CHANGED
@@ -1,3 +1,6 @@
1
+ {<img src="https://coveralls.io/repos/github/hawkular/hawkular-client-ruby/badge.svg?branch=master" alt="Coverage Status" />}[https://coveralls.io/github/hawkular/hawkular-client-ruby?branch=master]
2
+ {<img src="https://travis-ci.org/hawkular/hawkular-client-ruby.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/hawkular/hawkular-client-ruby]
3
+
1
4
  = hawkular-client-ruby
2
5
 
3
6
  A Ruby Hawkular Client.
@@ -10,7 +13,7 @@ A Ruby Hawkular Client.
10
13
  * Start a feature/bugfix branch
11
14
  * Commit and push until you are happy with your contribution
12
15
  * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Run your code through RuboCop (which is default when running `rake`) and fix complaints.
16
+ * Run your code through RuboCop (which is default when running +rake+) and fix complaints.
14
17
  * When you open a pull request, watch out for failures on Travis.
15
18
  * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is
16
19
  otherwise necessary, that is fine, but please isolate to its own commit so we can cherry-pick around it.
@@ -20,11 +23,14 @@ A Ruby Hawkular Client.
20
23
  Integration tests are recorded and played against cassettes recorded with VCR
21
24
  (http://www.relishapp.com/vcr/vcr/docs)
22
25
 
23
- * Edit the endpoint properties in the file `spec/endpoint.yml`(user, password and host name) if needed
26
+ * Edit the endpoint properties in the file +spec/endpoint.yml+ (user, password and host name) if needed
24
27
  * From command line run
25
28
  rake spec
26
- * To run the tests against a live server, set VCR_OFF to 1 as in
29
+ * To run the tests against a live server, set +VCR_OFF+ to +1+ as in
27
30
  VCR_OFF=1 rake spec
31
+ * To run the tests for operations against a live server using the WebSockets, set +WEBSOCKET_ON+ to +1+ as in
32
+ WEBSOCKET_ON=1 rake spec
33
+ (WebSocket communication can't be recorded by VCR)
28
34
 
29
35
  == Logging
30
36
 
@@ -6,8 +6,8 @@ require 'version'
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = 'hawkular-client'
8
8
  gem.version = Hawkular::VERSION
9
- gem.authors = ['Libor Zoubek', 'Heiko W. Rupp', 'Federico Simoncelli']
10
- gem.email = %w(lzoubek@redhat.com hrupp@redhat.com)
9
+ gem.authors = ['Libor Zoubek', 'Heiko W. Rupp', 'Jirka Kremser', 'Federico Simoncelli']
10
+ gem.email = %w(lzoubek@redhat.com hrupp@redhat.com jkremser@redhat.com)
11
11
  gem.homepage = 'https://github.com/hawkular/hawkular-client-ruby'
12
12
  gem.summary = 'A Ruby client for Hawkular'
13
13
  gem.license = 'ASL 2.0'
@@ -22,13 +22,17 @@ Gem::Specification.new do |gem|
22
22
  gem.require_paths = ['lib']
23
23
 
24
24
  gem.add_runtime_dependency('rest-client')
25
+ gem.add_runtime_dependency('websocket-client-simple', '~> 0.3.0')
26
+ gem.add_runtime_dependency('addressable')
25
27
  gem.add_development_dependency('shoulda')
26
- gem.add_development_dependency('rspec-rails', '~> 2.6')
27
- gem.add_development_dependency('rake')
28
+ gem.add_development_dependency('rspec-rails', '~> 3.0')
29
+ gem.add_development_dependency('rake', '< 11')
30
+ gem.add_development_dependency('simple-websocket-vcr', '= 0.0.4')
28
31
  gem.add_development_dependency('yard')
29
32
  gem.add_development_dependency('webmock')
30
33
  gem.add_development_dependency('vcr')
31
34
  gem.add_development_dependency('rubocop', '= 0.34.2')
35
+ gem.add_development_dependency('coveralls')
32
36
 
33
37
  gem.rdoc_options << '--title' << gem.name <<
34
38
  '--main' << 'README.rdoc' << '--line-numbers' << '--inline-source'
@@ -2,8 +2,10 @@ require 'hawkular'
2
2
  require 'ostruct'
3
3
 
4
4
  # Alerts module provides access to Hawkular-Alerts.
5
- # There are two main parts here: Triggers, that define alertable conditions
6
- # and Alerts that represent a fired trigger.
5
+ # There are three main parts here:
6
+ # Triggers, that define alertable conditions
7
+ # Alerts, that represent a fired Alert trigger
8
+ # Events, that represent a fired Event trigger or an externally injected event (hawkular, not miq event)
7
9
  # @see http://www.hawkular.org/docs/rest/rest-alerts.html
8
10
  module Hawkular::Alerts
9
11
  # Interface to use to talk to the Hawkular-Alerts component.
@@ -11,7 +13,7 @@ module Hawkular::Alerts
11
13
  # http://localhost:8080/hawkular/alerts
12
14
  # @param credentials [Hash{String=>String}] Hash of username, password, token(optional)
13
15
  class AlertsClient < Hawkular::BaseClient
14
- def initialize(entrypoint = nil, credentials = {})
16
+ def initialize(entrypoint = 'http://localhost:8080/hawkular/alerts', credentials = {})
15
17
  @entrypoint = entrypoint
16
18
 
17
19
  super(entrypoint, credentials)
@@ -53,6 +55,88 @@ module Hawkular::Alerts
53
55
  trigger
54
56
  end
55
57
 
58
+ # Import multiple trigger or action definitions specified as a hash to the server.
59
+ # @param [Hash] hash The hash with the trigger and action definitions.
60
+ # see the https://git.io/va5UO for more details about the structure
61
+ # @return [Hash] The newly entities as hash
62
+ def bulk_import_triggers(hash)
63
+ http_post 'import/all', hash
64
+ end
65
+
66
+ # Creates the trigger definition.
67
+ # @param [Trigger] trigger The trigger to be created
68
+ # @param [Array<Condition>] conditions Array of associated conditions
69
+ # @param [Array<Dampening>] dampenings Array of associated dampenings
70
+ # @return [Trigger] The newly created trigger
71
+ def create_trigger(trigger, conditions = [], dampenings = [], _actions = [])
72
+ full_trigger = {}
73
+ full_trigger[:trigger] = trigger.to_h
74
+ conds = []
75
+ conditions.each { |c| conds.push(c.to_h) }
76
+ full_trigger[:conditions] = conds
77
+ damps = []
78
+ dampenings.each { |d| damps.push(d.to_h) } unless dampenings.nil?
79
+ full_trigger[:dampenings] = damps
80
+
81
+ http_post 'triggers/trigger', full_trigger
82
+ end
83
+
84
+ # Deletes the trigger definition.
85
+ # @param [String] trigger_id Id of the trigger to delete
86
+ def delete_trigger(trigger_id)
87
+ http_delete "/triggers/#{trigger_id}"
88
+ end
89
+
90
+ # Obtains action definition/plugin from the server.
91
+ # @param [String] action_plugin Id of the action plugin to fetch. If nil, all the plugins are fetched
92
+ def get_action_definition(action_plugin = nil)
93
+ if action_plugin.nil?
94
+ plugins = http_get('plugins')
95
+ else
96
+ plugins = [action_plugin]
97
+ end
98
+ ret = {}
99
+ plugins.each do |p|
100
+ ret[p] = http_get("/plugins/#{p}")
101
+ end
102
+ ret
103
+ end
104
+
105
+ # Creates the action.
106
+ # @param [String] plugin The id of action definition/plugin
107
+ # @param [String] action_id The id of action
108
+ # @param [Hash] properties Troperties of action
109
+ # @return [Action] The newly created action
110
+ def create_action(plugin, action_id, properties = {})
111
+ the_plugin = hawk_escape plugin
112
+ # Check if plugin exists
113
+ http_get("/plugins/#{the_plugin}")
114
+
115
+ payload = { actionId: action_id, actionPlugin: plugin, properties: properties }
116
+ ret = http_post('/actions', payload)
117
+ Trigger::Action.new(ret)
118
+ end
119
+
120
+ # Obtains one action of given action plugin from the server.
121
+ # @param [String] plugin Id of the action plugin
122
+ # @param [String] action_id Id of the action
123
+ # @return [Action] the selected trigger
124
+ def get_action(plugin, action_id)
125
+ the_plugin = hawk_escape plugin
126
+ the_action_id = hawk_escape action_id
127
+ ret = http_get "/actions/#{the_plugin}/#{the_action_id}"
128
+ Trigger::Action.new(ret)
129
+ end
130
+
131
+ # Deletes the action of given action plugin.
132
+ # @param [String] plugin Id of the action plugin
133
+ # @param [String] action_id Id of the action
134
+ def delete_action(plugin, action_id)
135
+ the_plugin = hawk_escape plugin
136
+ the_action_id = hawk_escape action_id
137
+ http_delete "/actions/#{the_plugin}/#{the_action_id}"
138
+ end
139
+
56
140
  # Obtain the alerts for the Trigger with the passed id
57
141
  # @param [String] trigger_id Id of the trigger that has fired the alerts
58
142
  # @return [Array<Alert>] List of alerts for the trigger. Can be empty
@@ -67,9 +151,11 @@ module Hawkular::Alerts
67
151
  end
68
152
 
69
153
  # List fired alerts
154
+ # @param [Hash]criteria optional query criteria
70
155
  # @return [Array<Alert>] List of alerts in the system. Can be empty
71
- def list_alerts
72
- ret = http_get('/')
156
+ def list_alerts(criteria = {})
157
+ query = generate_query_params(criteria)
158
+ ret = http_get('/' + query)
73
159
  val = []
74
160
  ret.each { |a| val.push(Alert.new(a)) }
75
161
  val
@@ -109,20 +195,42 @@ module Hawkular::Alerts
109
195
 
110
196
  true
111
197
  end
198
+
199
+ # List Events given optional criteria. Criteria keys are strings (not symbols):
200
+ # startTime numeric, milliseconds from epoch
201
+ # endTime numeric, milliseconds from epoch
202
+ # eventIds array of strings
203
+ # triggerIds array of strings
204
+ # categories array of strings
205
+ # tags array of strings, each tag of format 'name|value'. Specify '*' for value to match all values
206
+ # thin boolean, return lighter events (omits triggering data for trigger-generated events)
207
+ # @param [Hash] criteria optional query criteria
208
+ # @return [Array<Event>] List of events. Can be empty
209
+ def list_events(*criteria)
210
+ query = generate_query_params(*criteria)
211
+ http_get('/events' + query).map { |e| Event.new(e) }
212
+ end
112
213
  end
113
214
 
114
215
  # Representation of one Trigger
216
+ ## (22 known properties: "enabled", "autoResolveMatch", "name", "memberOf", "autoEnable",
217
+ # "firingMatch", "tags", "id", "source", "tenantId", "eventText", "context", "eventType",
218
+ # "autoResolveAlerts", "dataIdMap", "eventCategory", "autoDisable", "type", "description",
219
+ # "severity", "autoResolve", "actions"])
115
220
  class Trigger
116
- attr_reader :id, :name, :context, :actions, :auto_disable, :auto_enable
117
- attr_reader :auto_resolve, :auto_resolve_alerts
118
- attr_reader :tenant, :description, :group, :severity
119
- attr_reader :conditions, :dampenings, :event_type
120
- attr_accessor :enabled
221
+ attr_accessor :id, :name, :context, :actions, :auto_disable, :auto_enable
222
+ attr_accessor :auto_resolve, :auto_resolve_alerts, :tags, :type
223
+ attr_accessor :tenant, :description, :group, :severity, :event_type
224
+ attr_reader :conditions, :dampenings
225
+ attr_accessor :enabled, :actions
121
226
 
122
227
  def initialize(trigger_hash)
228
+ return if trigger_hash.nil?
229
+
123
230
  @_hash = trigger_hash
124
231
  @conditions = []
125
232
  @dampenings = []
233
+ @actions = []
126
234
  @id = trigger_hash['id']
127
235
  @name = trigger_hash['name']
128
236
  @enabled = trigger_hash['enabled']
@@ -134,41 +242,71 @@ module Hawkular::Alerts
134
242
  @description = trigger_hash['description']
135
243
  @auto_enable = trigger_hash['autoEnable']
136
244
  @auto_disable = trigger_hash['autoDisable']
245
+ @context = trigger_hash['context']
246
+ @type = trigger_hash['type']
247
+ @tags = trigger_hash['tags']
248
+ # acts = trigger_hash['actions']
249
+ # acts.each { |a| @actions.push(Action.new(a)) } unless acts.nil?
137
250
  end
138
251
 
139
- # def enable
140
- # @enabled = true
141
- # @_hash['enabled'] = true
142
- # url = '/triggers/' + @id
143
- # Hawkular::BaseClient.http_put(url, @_hash)
144
- # end
145
- #
146
- # def disable
147
- # @enabled = false
148
- # url = '/triggers/' + @id
149
- # AlertsClient.http_put(url, self)
150
- # end
252
+ def to_h
253
+ trigger_hash = {}
254
+ to_camel = lambda do |x|
255
+ ret = x.to_s.split('_').collect(&:capitalize).join
256
+ ret[0, 1].downcase + ret[1..-1]
257
+ end
258
+ fields = [:id, :name, :enabled, :severity, :auto_resolve, :auto_resolve_alerts, :event_type,
259
+ :description, :auto_enable, :auto_disable, :context, :type, :tags]
260
+
261
+ fields.each do |field|
262
+ camelized_field = to_camel.call(field)
263
+ field_value = __send__ field
264
+ trigger_hash[camelized_field] = field_value unless field_value.nil?
265
+ end
266
+
267
+ trigger_hash['tenantId'] = @tenant unless @tenant.nil?
268
+ trigger_hash['actions'] = []
269
+ @actions.each { |d| trigger_hash['actions'].push d.to_h }
270
+
271
+ trigger_hash
272
+ end
151
273
 
152
274
  # Representing of one Condition
153
275
  class Condition
154
- attr_reader :condition_id, :type, :operator_low, :operator_high, :threshold_low
155
- attr_reader :in_range, :threshold_high
276
+ attr_accessor :condition_id, :type, :operator, :threshold
277
+ attr_accessor :trigger_mode, :data_id
278
+ attr_reader :condition_set_size, :condition_set_index, :trigger_id
156
279
 
157
280
  def initialize(cond_hash)
158
281
  @condition_id = cond_hash['conditionId']
159
282
  @type = cond_hash['type']
160
- @operator_low = cond_hash['operatorLow']
161
- @operator_high = cond_hash['operatorHigh']
162
- @threshold_low = cond_hash['thresholdLow']
163
- @threshold_high = cond_hash['thresholdHigh']
164
- @in_range = cond_hash['inRange']
283
+ @operator = cond_hash['operator']
284
+ @threshold = cond_hash['threshold']
285
+ @type = cond_hash['type']
286
+ @trigger_mode = cond_hash['triggerMode']
287
+ @data_id = cond_hash['dataId']
288
+ @trigger_id = cond_hash['triggerId']
289
+ end
290
+
291
+ def to_h
292
+ cond_hash = {}
293
+ cond_hash['conditionId'] = @condition_id
294
+ cond_hash['type'] = @type
295
+ cond_hash['operator'] = @operator
296
+ cond_hash['threshold'] = @threshold
297
+ cond_hash['type'] = @type
298
+ cond_hash['triggerMode'] = @trigger_mode
299
+ cond_hash['dataId'] = @data_id
300
+ cond_hash['triggerId'] = @trigger_id
301
+ cond_hash
165
302
  end
166
303
  end
167
304
 
168
305
  # Representation of one Dampening setting
169
306
  class Dampening
170
- attr_reader :dampening_id, :type, :eval_true_setting, :eval_total_setting, :eval_time_setting
171
- attr_reader :current_evals
307
+ attr_accessor :dampening_id, :type, :eval_true_setting, :eval_total_setting,
308
+ :eval_time_setting
309
+ attr_accessor :current_evals
172
310
 
173
311
  def initialize(damp_hash)
174
312
  @current_evals = {}
@@ -179,15 +317,47 @@ module Hawkular::Alerts
179
317
  @eval_time_setting = damp_hash['evalTimeSetting']
180
318
  end
181
319
  end
320
+
321
+ class Action
322
+ attr_accessor :action_plugin, :action_id, :states, :tenant_id
323
+
324
+ def initialize(action_hash)
325
+ return if action_hash.nil?
326
+
327
+ @action_plugin = action_hash['actionPlugin']
328
+ @action_id = action_hash['actionId']
329
+ @tenant_id = action_hash['tenantId']
330
+ @states = action_hash['states']
331
+ end
332
+
333
+ def to_h
334
+ action_hash = {}
335
+ action_hash['actionPlugin'] = @action_plugin
336
+ action_hash['actionId'] = @action_id
337
+ action_hash['tenantId'] = @tenant_id
338
+ action_hash['states'] = @states
339
+ action_hash
340
+ end
341
+ end
182
342
  end
183
343
 
184
344
  # Representation of one alert.
185
345
  # The name of the members are literally what they are in the JSON sent from the
186
- # server and not 'rubyfied'. So 'alertId' and not 'alert_ic'
346
+ # server and not 'rubyfied'. So 'alertId' and not 'alert_id'
187
347
  # Check http://www.hawkular.org/docs/rest/rest-alerts.html#Alert for details
188
348
  class Alert < OpenStruct
189
349
  def initialize(alert_hash)
190
350
  super(alert_hash)
191
351
  end
192
352
  end
353
+
354
+ # Representation of one event.
355
+ # The name of the members are literally what they are in the JSON sent from the
356
+ # server and not 'rubyfied'. So 'eventId' and not 'event_id'
357
+ # Check http://www.hawkular.org/docs/rest/rest-alerts.html#Event for details
358
+ class Event < OpenStruct
359
+ def initialize(event_hash)
360
+ super(event_hash)
361
+ end
362
+ end
193
363
  end