wavefront-sdk 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ module Wavefront
5
5
  # View and manage events. Events are identified by their millisecond
6
6
  # epoch timestamp.
7
7
  #
8
- class Event < Wavefront::Base
8
+ class Event < Base
9
9
  @update_keys = [:startTime, :endTime, :name, :annotations]
10
10
 
11
11
  # GET /api/v2/event
@@ -17,9 +17,8 @@ module Wavefront
17
17
  # @param to [Time, Integer] end ot time range. Can be epoch
18
18
  # millisecods or a Ruby time. If not supplied, defaults to the
19
19
  # current time.
20
- # @cursor [String] I think this is the 
21
- # must start with a timestamp.
22
- # @limit [Integer] the number of events to return
20
+ # @param cursor [String] I think this must start with a timestamp.
21
+ # @param limit [Integer] the number of events to return
23
22
  # @return [Hash]
24
23
  #
25
24
  def list(from = nil, to = nil, limit = 100, cursor = nil)
@@ -52,7 +51,7 @@ module Wavefront
52
51
  api_post('', body, 'application/json')
53
52
  end
54
53
 
55
- # DELETE /api/v2/event/{id}
54
+ # DELETE /api/v2/event/id
56
55
  # Delete a specific event.
57
56
  #
58
57
  # @param id [String] ID of the alert
@@ -63,7 +62,7 @@ module Wavefront
63
62
  api_delete(id)
64
63
  end
65
64
 
66
- # GET /api/v2/event/{id}
65
+ # GET /api/v2/event/id
67
66
  # Get a specific event / Get a specific historical version of a
68
67
  # specific event.
69
68
  #
@@ -79,7 +78,7 @@ module Wavefront
79
78
  api_get(fragments.uri_concat)
80
79
  end
81
80
 
82
- # PUT /api/v2/event/{id}
81
+ # PUT /api/v2/event/id
83
82
  # Update a specific event
84
83
  #
85
84
  # This method helps you update one or more properties of an event.
@@ -101,7 +100,7 @@ module Wavefront
101
100
  api_put(id, hash_for_update(describe(id), body), 'application/json')
102
101
  end
103
102
 
104
- # POST /api/v2/event/{id}/close
103
+ # POST /api/v2/event/id/close
105
104
  # Close a specific event.
106
105
  #
107
106
  # @param id [String] the ID of the event
@@ -111,11 +110,11 @@ module Wavefront
111
110
  api_post([id, 'close'].uri_concat)
112
111
  end
113
112
 
114
- # GET /api/v2/event/{id}/tag
113
+ # GET /api/v2/event/id/tag
115
114
  # Get all tags associated with a specific event
116
115
  #
117
116
  # @param id [String] ID of the event
118
- # @returns [Hash] object describing the event with status and
117
+ # @return [Hash] object describing the event with status and
119
118
  # response keys
120
119
  #
121
120
  def tags(id)
@@ -123,12 +122,12 @@ module Wavefront
123
122
  api_get([id, 'tag'].uri_concat)
124
123
  end
125
124
 
126
- # POST /api/v2/event/{id}/tag
125
+ # POST /api/v2/event/id/tag
127
126
  # Set all tags associated with a specific event.
128
127
  #
129
128
  # @param id [String] ID of the event
130
129
  # @param tags [Array] list of tags to set.
131
- # @returns [Hash] object describing the event with status and
130
+ # @return [Hash] object describing the event with status and
132
131
  # response keys
133
132
  #
134
133
  def tag_set(id, tags)
@@ -138,12 +137,12 @@ module Wavefront
138
137
  api_post([id, 'tag'].uri_concat, tags, 'application/json')
139
138
  end
140
139
 
141
- # DELETE /api/v2/event/{id}/tag/{tagValue}
140
+ # DELETE /api/v2/event/id/tag/tagValue
142
141
  # Remove a tag from a specific event.
143
142
  #
144
143
  # @param id [String] ID of the event
145
144
  # @param tag [String] tag to delete
146
- # @returns [Hash] object with 'status' key and empty 'repsonse'
145
+ # @return [Hash] object with 'status' key and empty 'repsonse'
147
146
  #
148
147
  def tag_delete(id, tag)
149
148
  wf_event_id?(id)
@@ -151,12 +150,12 @@ module Wavefront
151
150
  api_delete([id, 'tag', tag].uri_concat)
152
151
  end
153
152
 
154
- # PUT /api/v2/event/{id}/tag/{tagValue}
153
+ # PUT /api/v2/event/id/tag/tagValue
155
154
  # Add a tag to a specific event.
156
155
  #
157
156
  # @param id [String] ID of the event
158
157
  # @param tag [String] tag to set.
159
- # @returns [Hash] object with 'status' key and empty 'repsonse'
158
+ # @return [Hash] object with 'status' key and empty 'repsonse'
160
159
  #
161
160
  def tag_add(id, tag)
162
161
  wf_event_id?(id)
@@ -164,10 +163,4 @@ module Wavefront
164
163
  api_put([id, 'tag', tag].uri_concat)
165
164
  end
166
165
  end
167
-
168
- # A standard response.
169
- #
170
- class Response
171
- class Event < Base; end
172
- end
173
166
  end
@@ -21,7 +21,6 @@ module Wavefront
21
21
  class InvalidPoint < ::Exception; end
22
22
  class InvalidPrefixLength < ::Exception; end
23
23
  class InvalidProxyId < ::Exception; end
24
- class InvalidResponse < ::Exception; end
25
24
  class InvalidSavedSearchId < ::Exception; end
26
25
  class InvalidSavedSearchEntity < ::Exception; end
27
26
  class InvalidSourceId < ::Exception; end
@@ -34,6 +33,7 @@ module Wavefront
34
33
  class InvalidWebhookId < ::Exception; end
35
34
  class InvalidVersion < ::Exception; end
36
35
  class NotImplemented < ::Exception; end
36
+ class UnparseableResponse < ::Exception; end
37
37
  class ValueOutOfRange < ::Exception; end
38
38
  end
39
39
  end
@@ -4,7 +4,7 @@ module Wavefront
4
4
  #
5
5
  # Manage and query Wavefront external links.
6
6
  #
7
- class ExternalLink < Wavefront::Base
7
+ class ExternalLink < Base
8
8
  def api_base
9
9
  '/extlink'
10
10
  end
@@ -22,9 +22,7 @@ module Wavefront
22
22
  # POST /api/v2/extlink
23
23
  # Create a specific external link.
24
24
  #
25
- # @param name [String] a plaintext name for the link
26
- # @param template [String] a mustache template for the link target
27
- # @param description [String] a plaintext description of the link
25
+ # @param body [Hash] a description of the external link.
28
26
  # @return [Hash]
29
27
  #
30
28
  def create(body)
@@ -32,7 +30,7 @@ module Wavefront
32
30
  api_post('', body, 'application/json')
33
31
  end
34
32
 
35
- # DELETE /api/v2/extlink/{id}
33
+ # DELETE /api/v2/extlink/id
36
34
  # Delete a specific external link.
37
35
  #
38
36
  # @param id [String] ID of the link
@@ -43,7 +41,7 @@ module Wavefront
43
41
  api_delete(id)
44
42
  end
45
43
 
46
- # GET /api/v2/extlink/{id}
44
+ # GET /api/v2/extlink/id
47
45
  # Get a specific external link.
48
46
  #
49
47
  # @param id [String] ID of the limnk
@@ -54,11 +52,11 @@ module Wavefront
54
52
  api_get(id)
55
53
  end
56
54
 
57
- # PUT /api/v2/extlink/{id}
55
+ # PUT /api/v2/extlink/id
58
56
  # Update a specific external link.
59
57
  #
60
58
  # @param id [String] ID of the link
61
- # @param payload [Hash] a key:value hash where the key is the
59
+ # @param body [Hash] a key:value hash where the key is the
62
60
  # property to change and the value is its desired value
63
61
  # @return [Hash]
64
62
  #
@@ -68,10 +66,4 @@ module Wavefront
68
66
  api_put(id, body)
69
67
  end
70
68
  end
71
-
72
- # A standard response.
73
- #
74
- class Response
75
- class ExternalLink < Base; end
76
- end
77
69
  end
@@ -4,7 +4,7 @@ module Wavefront
4
4
  #
5
5
  # Manage and query Wavefront maintenance windows
6
6
  #
7
- class MaintenanceWindow < Wavefront::Base
7
+ class MaintenanceWindow < Base
8
8
 
9
9
  # GET /api/v2/maintenancewindow
10
10
  # Get all maintenance windows for a customer.
@@ -31,7 +31,7 @@ module Wavefront
31
31
  api_post('', body, 'application/json')
32
32
  end
33
33
 
34
- # DELETE /api/v2/maintenancewindow/{id}
34
+ # DELETE /api/v2/maintenancewindow/id
35
35
  # Delete a specific maintenance window.
36
36
  #
37
37
  # @param id [String, Integer] ID of the maintenance window
@@ -42,7 +42,7 @@ module Wavefront
42
42
  api_delete(id)
43
43
  end
44
44
 
45
- # GET /api/v2/maintenancewindow/{id}
45
+ # GET /api/v2/maintenancewindow/id
46
46
  # Get a specific maintenance window.
47
47
  #
48
48
  # @param id [String, Integer] ID of the maintenance window
@@ -53,7 +53,7 @@ module Wavefront
53
53
  api_get(id)
54
54
  end
55
55
 
56
- # PUT /api/v2/maintenancewindow/{id}
56
+ # PUT /api/v2/maintenancewindow/id
57
57
  # Update a specific maintenance window.
58
58
  #
59
59
  # @param body [Hash] a hash of parameters describing the window.
@@ -66,10 +66,4 @@ module Wavefront
66
66
  api_put(id, body)
67
67
  end
68
68
  end
69
-
70
- # A standard response.
71
- #
72
- class Response
73
- class MaintenanceWindow < Base; end
74
- end
75
69
  end
@@ -4,7 +4,7 @@ module Wavefront
4
4
  #
5
5
  # Manage and query Wavefront messages.
6
6
  #
7
- class Message < Wavefront::Base
7
+ class Message < Base
8
8
 
9
9
  # GET /api/v2/message
10
10
  # Gets messages applicable to the current user, i.e. within time
@@ -17,20 +17,14 @@ module Wavefront
17
17
  api_get('', { offset: offset, limit: limit, unreadOnly: unread_only })
18
18
  end
19
19
 
20
- # POST /api/v2/message/{id}/read
20
+ # POST /api/v2/message/id/read
21
21
  # Mark a specific message as read
22
22
  #
23
- # @param [id] message ID to mark as read
23
+ # @param id [String] message ID to mark as read
24
24
  #
25
25
  def read(id)
26
26
  wf_message_id?(id)
27
27
  api_post([id, 'read'].uri_concat)
28
28
  end
29
29
  end
30
-
31
- # A standard response.
32
- #
33
- class Response
34
- class Message < Base; end
35
- end
36
30
  end
@@ -4,7 +4,7 @@ module Wavefront
4
4
  #
5
5
  # Query Wavefront metrics.
6
6
  #
7
- class Metric < Wavefront::Base
7
+ class Metric < Base
8
8
  def api_base
9
9
  'chart/metric'
10
10
  end
@@ -13,8 +13,9 @@ module Wavefront
13
13
  # Get more details on a metric, including reporting sources and
14
14
  # approximate last time reported
15
15
  #
16
- # @param offset [Int] agent at which the list begins
17
- # @param limit [Int] the number of agents to return
16
+ # @param metric [String] the metric to fetch
17
+ # @param sources [Array[String]] a list of sources to check.
18
+ # @param cursor [String] optionally begin from this result
18
19
  #
19
20
  def detail(metric, sources = [], cursor = nil)
20
21
  raise ArgumentError unless metric.is_a?(String)
@@ -32,21 +33,4 @@ module Wavefront
32
33
  api_get('detail', q)
33
34
  end
34
35
  end
35
-
36
- class Response
37
-
38
- # The Metric response forges status and response methods to look
39
- # like other classes and create a more consistent interface.
40
- #
41
- class Metric < Base
42
- def populate(raw, status)
43
- @response = Struct.new(*raw.keys).new(*raw.values).freeze
44
-
45
- result = status == 200 ? 'OK' : 'ERROR'
46
-
47
- @status = Struct.new(:result, :message, :code).
48
- new(result, raw[:message] || raw[:error], status)
49
- end
50
- end
51
- end
52
36
  end
@@ -4,7 +4,7 @@ module Wavefront
4
4
  #
5
5
  # Manage and query Wavefront proxies.
6
6
  #
7
- class Proxy < Wavefront::Base
7
+ class Proxy < Base
8
8
 
9
9
  # GET /api/v2/proxy
10
10
  # Get all proxies for a customer
@@ -16,7 +16,7 @@ module Wavefront
16
16
  api_get('', { offset: offset, limit: limit })
17
17
  end
18
18
 
19
- # DELETE /api/v2/proxy/{id}
19
+ # DELETE /api/v2/proxy/id
20
20
  # Delete a specific proxy
21
21
  #
22
22
  # Deleting an active proxy moves it to 'trash', from where it
@@ -31,7 +31,7 @@ module Wavefront
31
31
  api_delete(id)
32
32
  end
33
33
 
34
- # GET /api/v2/proxy/{id}
34
+ # GET /api/v2/proxy/id
35
35
  # Get a specific proxy
36
36
  #
37
37
  # @param id [String] ID of the proxy
@@ -42,7 +42,7 @@ module Wavefront
42
42
  api_get(id)
43
43
  end
44
44
 
45
- # POST /api/v2/proxy/{id}/undelete
45
+ # POST /api/v2/proxy/id/undelete
46
46
  # Undelete a specific proxy
47
47
  #
48
48
  # Move an proxy from 'trash' back into active service.
@@ -55,7 +55,7 @@ module Wavefront
55
55
  api_post([id, 'undelete'].uri_concat)
56
56
  end
57
57
 
58
- # PUT /api/v2/proxy/{id}
58
+ # PUT /api/v2/proxy/id
59
59
  # Update the name of a specific proxy
60
60
  #
61
61
  # Rename an proxy. This changes the human-readable name, not the
@@ -86,10 +86,4 @@ module Wavefront
86
86
  api_put(id, payload)
87
87
  end
88
88
  end
89
-
90
- # A standard response
91
- #
92
- class Response
93
- class Proxy < Base; end
94
- end
95
89
  end
@@ -4,7 +4,7 @@ module Wavefront
4
4
  #
5
5
  # Query Wavefront metrics.
6
6
  #
7
- class Query < Wavefront::Base
7
+ class Query < Base
8
8
  def api_base
9
9
  'chart'
10
10
  end
@@ -25,6 +25,8 @@ module Wavefront
25
25
  # @param t_end [Time, Integer] The end of the query window.
26
26
  # May be a Ruby Time object, or epoch milliseconds.
27
27
  # @param options [Hash] any other options defined in the API
28
+ # @raise [ArgumentError] if query is not a string
29
+ # @return [Wavefront::Response::Query]
28
30
  #
29
31
  def query(query, granularity = nil, t_start = nil, t_end = nil,
30
32
  options = {})
@@ -55,7 +57,7 @@ module Wavefront
55
57
  # (cannot contain wildcards).
56
58
  # @param t_start [Time, Integer] start time of window: defaults
57
59
  # to one hour before t_end
58
- # @param t_start [Time, Integer] start time of window: defaults
60
+ # @param t_end [Time, Integer] end time of window: defaults
59
61
  # to now
60
62
  #
61
63
  def raw(metric, source = nil, t_start = nil, t_end = nil)
@@ -74,23 +76,4 @@ module Wavefront
74
76
  api_get('raw', options)
75
77
  end
76
78
  end
77
-
78
- class Response
79
-
80
- # The Query response forges status and response methods to look
81
- # like other classes and create a more consistent interface
82
- #
83
- class Query < Base
84
- def populate(raw, status)
85
- @response = Struct.new(*raw.keys).new(*raw.values).freeze
86
-
87
- result = status == 200 ? 'OK' : 'ERROR'
88
-
89
- if raw.key?(:warnings) || raw.key?(:error)
90
- @status = Struct.new(:result, :message, :code).
91
- new(result, raw[:message] || raw[:error], status)
92
- end
93
- end
94
- end
95
- end
96
79
  end
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require 'map'
2
3
  require_relative './exception'
3
4
 
4
5
  module Wavefront
@@ -6,50 +7,77 @@ module Wavefront
6
7
  # Every API path has its own response class, which allows us to
7
8
  # provide a stable interface. If the API changes underneath us,
8
9
  # the SDK will break in a predictable way, throwing a
9
- # Wavefront::Exception::InvalidResponse exception.
10
+ # Wavefront::Exception::UnparseableResponse exception.
10
11
  #
11
12
  # Most Wavefront::Response classes present the returned data in
12
13
  # two parts, each accessible by dot notation.
13
- # #status is the status object, which normally contains
14
- # 'result', 'message', and 'code'. It is a struct.
15
- # #response contains the JSON response body, turned into a Ruby
16
- # hash.
14
+ #
15
+ # @!attribute [r] status
16
+ # @return [Wavefront::Types::Status]
17
+ # @!attribute [r] response
18
+ # @return [Map] the response from the API turned into a Map,
19
+ # which allows
17
20
  #
18
21
  class Response
19
- class Base
20
- attr_reader :status, :response, :debug
21
-
22
- # Create and return a Wavefront::Response object
23
- # @param json [String] a raw response body from the Wavefront API
24
- # @param status [Integer] HTTP return code from the API
25
- # @param debug [Boolean] whether or not to print the exception
26
- # message if one is thrown
27
- # @raise Wavefront::InvalidResponse if the response cannot be
28
- # parsed
29
- # @return a Wavefront::Response object
22
+ attr_reader :status, :response
23
+
24
+ # Create and return a Wavefront::Response object
25
+ # @param json [String] a raw response body from the Wavefront API
26
+ # @param status [Integer] HTTP return code from the API
27
+ # @param debug [Boolean] whether or not to print the exception
28
+ # message if one is thrown
29
+ # @raise [Wavefront::Exception::UnparseableResponse] if the
30
+ # response cannot be parsed. This may be because the API
31
+ # has changed underneath us.
32
+ #
33
+ def initialize(json, status, debug = false)
34
+ raw = json.empty? {} || JSON.parse(json, symbolize_names: true)
35
+
36
+ @status = build_status(raw, status)
37
+ @response = build_response(raw)
38
+ p self if debug
39
+ rescue => e
40
+ puts "could not parse:\n#{json}" if debug
41
+ puts e.message if debug
42
+ raise Wavefront::Exception::UnparseableResponse
43
+ end
44
+
45
+ def build_status(raw, status)
46
+ Wavefront::Type::Status.new(raw, status)
47
+ end
48
+
49
+ def build_response(raw)
50
+ Map(raw.is_a?(Hash) && raw.key?(:response) ? raw[:response] : raw)
51
+ end
52
+ end
53
+
54
+ class Type
55
+ #
56
+ # An object which provides information about whether the request
57
+ # was successful or not. Ordinarily this is easy to construct
58
+ # from the API's JSON response, but some classes, for instance
59
+ # Wavefront::Write fake it by constructing their own.
60
+ #
61
+ # @!attribute [r] result
62
+ # @return [OK, ERROR] a string telling us how the request went
63
+ # @!attribute [r] message
64
+ # @return [String] Any informational message from the API
65
+ # @!attribute [r] code
66
+ # @return [Integer] the HTTP response code from the API
67
+ # request
68
+ #
69
+ class Status
70
+ attr_reader :result, :message, :code
71
+
72
+ # @param raw [Hash] the API response, turned into a hash
73
+ # @param status [Integer] HTTP status code
30
74
  #
31
- def initialize(json, status, debug = false)
32
- @debug = debug
33
- populate(JSON.parse(json, symbolize_names: true), status)
34
- rescue => e
35
- puts e.message if debug
36
- raise Wavefront::Exception::InvalidResponse
37
- end
75
+ def initialize(raw, status)
76
+ obj = raw.key?(:status) ? raw[:status] : raw
38
77
 
39
- def populate(raw, _status = 200)
40
- if raw.key?(:status)
41
- @status = Struct.new(*raw[:status].keys).
42
- new(*raw[:status].values).freeze
43
- end
44
-
45
- if raw.key?(:response)
46
- if raw[:response].key?(:items)
47
- @response = Struct.new(*raw[:response].keys).
48
- new(*raw[:response].values).freeze
49
- else
50
- @response = raw[:response]
51
- end
52
- end
78
+ @result = obj[:result] || nil
79
+ @message = obj[:message] || nil
80
+ @code = obj[:code] || status
53
81
  end
54
82
  end
55
83
  end
@@ -5,7 +5,7 @@ module Wavefront
5
5
  # View and manage Cloud Integrations. These are identified by
6
6
  # a UUID.
7
7
  #
8
- class SavedSearch < Wavefront::Base
8
+ class SavedSearch < Base
9
9
 
10
10
  # GET /api/v2/savedsearch
11
11
  # Get all saved searches for a user.
@@ -30,7 +30,7 @@ module Wavefront
30
30
  api_post('', body, 'application/json')
31
31
  end
32
32
 
33
- # DELETE /api/v2/savedsearch/{id}
33
+ # DELETE /api/v2/savedsearch/id
34
34
  # Delete a specific saved search.
35
35
  #
36
36
  # @param id [String] ID of the saved search
@@ -41,7 +41,7 @@ module Wavefront
41
41
  api_delete(id)
42
42
  end
43
43
 
44
- # GET /api/v2/savedsearch/{id}
44
+ # GET /api/v2/savedsearch/id
45
45
  # Get a specific saved search.
46
46
  #
47
47
  # @param id [String] ID of the saved search
@@ -52,7 +52,7 @@ module Wavefront
52
52
  api_get(id)
53
53
  end
54
54
 
55
- # PUT /api/v2/savedsearch/{id}
55
+ # PUT /api/v2/savedsearch/id
56
56
  # Update a specific saved search.
57
57
  #
58
58
  # @param id [String] ID of the saved search
@@ -64,7 +64,7 @@ module Wavefront
64
64
  api_put(id, body)
65
65
  end
66
66
 
67
- # GET /api/v2/savedsearch/type/{entitytype}
67
+ # GET /api/v2/savedsearch/type/entitytype
68
68
  # Get all saved searches for a specific entity type for a user.
69
69
  #
70
70
  # @param entitytype [String] type of entity to retrieve
@@ -79,10 +79,4 @@ module Wavefront
79
79
 
80
80
  end
81
81
  end
82
-
83
- # A standard response
84
- #
85
- class Response
86
- class SavedSearch < Base; end
87
- end
88
82
  end
@@ -7,7 +7,7 @@ module Wavefront
7
7
  # this class covers the whole API with two methods, but leaves a
8
8
  # lot up to the user. It may grow, with convenience methods.
9
9
  #
10
- class Search < Wavefront::Base
10
+ class Search < Base
11
11
 
12
12
  # POST /api/v2/search/agent
13
13
  # POST /api/v2/search/agent/deleted
@@ -49,10 +49,4 @@ module Wavefront
49
49
  api_post(path, body, 'application/json')
50
50
  end
51
51
  end
52
-
53
- # A standard response
54
- #
55
- class Response
56
- class Search < Base; end
57
- end
58
52
  end