lm_rest 1.0.1 → 1.0.5
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.
- checksums.yaml +4 -4
- data/README.md +8 -23
- data/api.json +64 -9
- data/bin/system_category_cleaner.rb +57 -0
- data/lib/lm_rest/api_client.rb +71 -71
- data/lib/lm_rest/version.rb +1 -1
- data/lm_rest.gemspec +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c01ece27d96964546705d1d5a610b33e5a5c173d63a44f9a20252846192cb23
|
4
|
+
data.tar.gz: 8ab3dc099b5088d46d0d752de09c73a273a662d17b05b2a753363f6f9498ed6b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6180a42bdb7fdd738651da70f41c5a4158dd5ecd177e37bc45ce67434f4ec711183743526dea241d0a99ab73c247bfd13d879cf8119c5b89af88250c9f81f35
|
7
|
+
data.tar.gz: b9e563ccf97f75174a23dd88e4d58222fa40b58a1078a22ba06d2f199ae8252cd8ed4249b007ee1efbbaa3b73c3c30fe43edde48bd395d962d41e78c6751e005
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# LMRest
|
2
2
|
|
3
|
-
|
3
|
+
An Unofficial Ruby gem for the LogicMonitor REST API.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -20,8 +20,8 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Supported API Resources
|
22
22
|
|
23
|
-
Every API resource defined in the `api.json` file and its associated defined
|
24
|
-
methods are supported
|
23
|
+
Every API resource is defined in the `api.json` file and its associated defined
|
24
|
+
methods are supported. You can easily add your own if you can't wait for me
|
25
25
|
to update this.
|
26
26
|
|
27
27
|
Each method (`get_*, add_*, update_*, delete_*`) works the same
|
@@ -45,21 +45,10 @@ use the gem.
|
|
45
45
|
```ruby
|
46
46
|
require 'lm_rest'
|
47
47
|
|
48
|
+
# Create an instance of the API Client, passing in an API token for
|
49
|
+
# authentication. Pretend this portal is at `company.logicmonitor.com`:
|
48
50
|
|
49
|
-
|
50
|
-
credential = {company: 'company',
|
51
|
-
access_key:'api_access_key',
|
52
|
-
access_id:'api_access_id'}
|
53
|
-
|
54
|
-
|
55
|
-
# Authenticate with Basic Auth (not preferred):
|
56
|
-
credential = {company: 'company',
|
57
|
-
user: 'user',
|
58
|
-
password: 'password'}
|
59
|
-
|
60
|
-
# Create an instance of the API Client
|
61
|
-
lm = LMRest::APIClient.new(credential)
|
62
|
-
|
51
|
+
lm = LMRest::APIClient.new('company', 'access_id', 'access_key')
|
63
52
|
|
64
53
|
# returns array of Resource objects
|
65
54
|
lm.get_datasources
|
@@ -100,7 +89,6 @@ lm.delete_device(device)
|
|
100
89
|
lm.add_device(device)
|
101
90
|
|
102
91
|
|
103
|
-
|
104
92
|
# Get your Santaba version info
|
105
93
|
lm.get_version
|
106
94
|
|
@@ -119,12 +107,9 @@ lm.run_report(id)
|
|
119
107
|
|
120
108
|
```
|
121
109
|
|
110
|
+
## TODO
|
122
111
|
|
123
|
-
|
124
|
-
|
125
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
126
|
-
|
127
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
112
|
+
* Handle nested stuff, and resource-specific operations
|
128
113
|
|
129
114
|
|
130
115
|
## Contributing
|
data/api.json
CHANGED
@@ -23,7 +23,7 @@
|
|
23
23
|
]
|
24
24
|
},
|
25
25
|
"Collector": {
|
26
|
-
"url": "/setting/collectors",
|
26
|
+
"url": "/setting/collector/collectors",
|
27
27
|
"method_names": {
|
28
28
|
"singular": "collector",
|
29
29
|
"plural": "collectors"
|
@@ -34,6 +34,28 @@
|
|
34
34
|
"delete"
|
35
35
|
]
|
36
36
|
},
|
37
|
+
"CollectorGroups": {
|
38
|
+
"url": "/setting/collector/groups",
|
39
|
+
"method_names": {
|
40
|
+
"singular": "collector_group",
|
41
|
+
"plural": "collector_groups"
|
42
|
+
},
|
43
|
+
"actions": [
|
44
|
+
"get",
|
45
|
+
"add",
|
46
|
+
"update",
|
47
|
+
"delete"
|
48
|
+
]
|
49
|
+
},
|
50
|
+
"CollectorVersions": {
|
51
|
+
"url": "/setting/collector/collectors/versions",
|
52
|
+
"method_names": {
|
53
|
+
"singular": "collector_versions"
|
54
|
+
},
|
55
|
+
"actions": [
|
56
|
+
"get"
|
57
|
+
]
|
58
|
+
},
|
37
59
|
"Datasource": {
|
38
60
|
"url": "/setting/datasources",
|
39
61
|
"method_names": {
|
@@ -87,7 +109,7 @@
|
|
87
109
|
"delete"
|
88
110
|
]
|
89
111
|
},
|
90
|
-
"
|
112
|
+
"AppliesToFunction": {
|
91
113
|
"url": "/setting/functions",
|
92
114
|
"method_names": {
|
93
115
|
"singular": "function",
|
@@ -126,11 +148,11 @@
|
|
126
148
|
"delete"
|
127
149
|
]
|
128
150
|
},
|
129
|
-
"
|
130
|
-
"url": "/
|
151
|
+
"Website": {
|
152
|
+
"url": "/website/websites",
|
131
153
|
"method_names": {
|
132
|
-
"singular": "
|
133
|
-
"plural": "
|
154
|
+
"singular": "website",
|
155
|
+
"plural": "websites"
|
134
156
|
},
|
135
157
|
"actions": [
|
136
158
|
"get",
|
@@ -143,7 +165,7 @@
|
|
143
165
|
]
|
144
166
|
},
|
145
167
|
"SiteMonitorCheckpoint": {
|
146
|
-
"url": "/
|
168
|
+
"url": "/website/smcheckpoints",
|
147
169
|
"method_names": {
|
148
170
|
"singular": "smcheckpoint",
|
149
171
|
"plural": "smcheckpoints"
|
@@ -153,7 +175,7 @@
|
|
153
175
|
]
|
154
176
|
},
|
155
177
|
"ServiceGroup": {
|
156
|
-
"url": "/
|
178
|
+
"url": "/website/groups",
|
157
179
|
"method_names": {
|
158
180
|
"singular": "service_group",
|
159
181
|
"plural": "service_groups"
|
@@ -195,7 +217,7 @@
|
|
195
217
|
]
|
196
218
|
},
|
197
219
|
"APIToken": {
|
198
|
-
"url": "/setting/
|
220
|
+
"url": "/setting/admin/apitokens",
|
199
221
|
"method_names": {
|
200
222
|
"singular": "api_token",
|
201
223
|
"plural": "api_tokens"
|
@@ -265,6 +287,19 @@
|
|
265
287
|
],
|
266
288
|
"children": []
|
267
289
|
},
|
290
|
+
"DashboardGroups": {
|
291
|
+
"url": "/dashboard/groups",
|
292
|
+
"method_names": {
|
293
|
+
"singular": "dashboard_group",
|
294
|
+
"plural": "dashboard_groups"
|
295
|
+
},
|
296
|
+
"actions": [
|
297
|
+
"get",
|
298
|
+
"add",
|
299
|
+
"update",
|
300
|
+
"delete"
|
301
|
+
]
|
302
|
+
},
|
268
303
|
"Widget": {
|
269
304
|
"url": "/dashboard/widgets",
|
270
305
|
"method_names": {
|
@@ -279,6 +314,17 @@
|
|
279
314
|
],
|
280
315
|
"children": []
|
281
316
|
},
|
317
|
+
"Debug": {
|
318
|
+
"url": "/debug",
|
319
|
+
"method_names": {
|
320
|
+
"singular": "debug"
|
321
|
+
},
|
322
|
+
"actions": [
|
323
|
+
"get",
|
324
|
+
"add"
|
325
|
+
],
|
326
|
+
"children": []
|
327
|
+
},
|
282
328
|
"Report": {
|
283
329
|
"url": "/report/reports",
|
284
330
|
"method_names": {
|
@@ -374,6 +420,15 @@
|
|
374
420
|
"get"
|
375
421
|
]
|
376
422
|
},
|
423
|
+
"APIPerfStats": {
|
424
|
+
"url": "/apiStats/externalApis",
|
425
|
+
"method_names": {
|
426
|
+
"singular": "api_perf_stats"
|
427
|
+
},
|
428
|
+
"actions": [
|
429
|
+
"get"
|
430
|
+
]
|
431
|
+
},
|
377
432
|
"Version": {
|
378
433
|
"url": "/version",
|
379
434
|
"method_names": {
|
@@ -0,0 +1,57 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
#
|
4
|
+
# Based on a script written originally by Matt Dunham
|
5
|
+
#
|
6
|
+
require 'lm_rest'
|
7
|
+
|
8
|
+
def usage
|
9
|
+
puts "USAGE:\t" + $PROGRAM_NAME + ' account id key applies_to category'
|
10
|
+
puts ""
|
11
|
+
puts "\taccount - just the beginning of your portal name, like 'hooli'"
|
12
|
+
puts "\tid - API key access id"
|
13
|
+
puts "\tkey - API key access key"
|
14
|
+
puts "\tapplies_to - AppliesTo (in quotes on one line) matching devices you want to scrub"
|
15
|
+
puts "\tcategory - The system category value you wish to remove."
|
16
|
+
end
|
17
|
+
|
18
|
+
if ARGV.length ==5
|
19
|
+
@account = ARGV[0]
|
20
|
+
@id = ARGV[1]
|
21
|
+
@key = ARGV[2]
|
22
|
+
@at = ARGV[3]
|
23
|
+
@category = ARGV[4]
|
24
|
+
@lm = LMRest::APIClient.new(@account, @id, @key)
|
25
|
+
else
|
26
|
+
usage
|
27
|
+
fail 'Bad arguments.'
|
28
|
+
end
|
29
|
+
|
30
|
+
request = {
|
31
|
+
currentAppliesTo: @at,
|
32
|
+
needInheritProps: true,
|
33
|
+
originalAppliesTo: @at,
|
34
|
+
type: "testAppliesTo"
|
35
|
+
}
|
36
|
+
|
37
|
+
devices = @lm.request(:post, "/functions", request)['originalMatches'].map do |device|
|
38
|
+
[device['id'], device['name']]
|
39
|
+
end
|
40
|
+
|
41
|
+
devices.each do |id, name|
|
42
|
+
puts "Fetching device id #{id}, #{name}"
|
43
|
+
current = @lm.request(:get, "/device/devices/#{id}/properties/system.categories", nil)
|
44
|
+
if (@lm.remaining.to_f / @lm.limit.to_f) * 100 <= 10
|
45
|
+
puts "sleeping for #{@lm.window} to avoid rate limit violation"
|
46
|
+
sleep @lm.window
|
47
|
+
end
|
48
|
+
if current['value'].split(',').include? @category
|
49
|
+
new_string = current['value'].split(',') - [@category]
|
50
|
+
new = current
|
51
|
+
new['value'] = new_string.join(",")
|
52
|
+
@lm.request(:put, "/device/devices/#{id}/properties/system.categories", new)
|
53
|
+
puts " Successfully scrubbed!"
|
54
|
+
else
|
55
|
+
puts " No scrubbing needed."
|
56
|
+
end
|
57
|
+
end
|
data/lib/lm_rest/api_client.rb
CHANGED
@@ -17,6 +17,7 @@ module LMRest
|
|
17
17
|
BASE_URL_SUFFIX = '.logicmonitor.com/santaba/rest'
|
18
18
|
|
19
19
|
attr_reader :company, :api_url, :access_id
|
20
|
+
attr_reader :limit, :remaining, :window
|
20
21
|
|
21
22
|
def initialize(company = nil, access_id = nil, access_key = nil)
|
22
23
|
APIClient.setup
|
@@ -38,14 +39,6 @@ module LMRest
|
|
38
39
|
uri.split("?")[0].split("/").join("/")
|
39
40
|
end
|
40
41
|
|
41
|
-
def snakerize(string)
|
42
|
-
string.gsub(/::/, '/').
|
43
|
-
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
|
44
|
-
gsub(/([a-z\d])([A-Z])/,'\1_\2').
|
45
|
-
tr("-", "_").
|
46
|
-
downcase
|
47
|
-
end
|
48
|
-
|
49
42
|
def sign(method, uri, data = nil)
|
50
43
|
|
51
44
|
resource_uri = uri_to_resource_uri(uri)
|
@@ -86,15 +79,20 @@ module LMRest
|
|
86
79
|
|
87
80
|
json_params = params.to_json
|
88
81
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
82
|
+
begin
|
83
|
+
case method
|
84
|
+
when :get
|
85
|
+
response = RestClient.get(url, headers)
|
86
|
+
when :post
|
87
|
+
response = RestClient.post(url, json_params, headers)
|
88
|
+
when :put
|
89
|
+
response = RestClient.put(url, json_params, headers)
|
90
|
+
when :delete
|
91
|
+
response = RestClient.delete(url, headers: headers)
|
92
|
+
end
|
93
|
+
rescue => e
|
94
|
+
puts e.http_body
|
95
|
+
raise
|
98
96
|
end
|
99
97
|
|
100
98
|
if response.code != 200
|
@@ -102,6 +100,9 @@ module LMRest
|
|
102
100
|
raise
|
103
101
|
end
|
104
102
|
|
103
|
+
@limit = response.headers['x_rate_limit_limit']
|
104
|
+
@remaining = response.headers['x_rate_limit_remaining']
|
105
|
+
@window = response.headers['x_rate_limit_window']
|
105
106
|
|
106
107
|
JSON.parse(response.body)
|
107
108
|
end
|
@@ -180,76 +181,75 @@ module LMRest
|
|
180
181
|
end
|
181
182
|
end
|
182
183
|
|
183
|
-
def self.
|
184
|
+
def self.define_action_methods(resource_type, attributes)
|
185
|
+
singular = attributes['method_names']['singular']
|
186
|
+
plural = attributes['method_names']['plural']
|
184
187
|
resource_uri = attributes['url']
|
185
|
-
@@api_json[paths].keys.each do |path|
|
186
188
|
|
187
|
-
|
188
|
-
|
189
|
-
|
189
|
+
attributes['actions'].each do |action|
|
190
|
+
case action
|
191
|
+
when 'get'
|
190
192
|
|
191
|
-
|
192
|
-
method_name = snakerize(@@api_json['paths'][path][action][operationId])
|
193
|
+
uri = lambda { |params| "#{resource_uri}#{RequestParams.parameterize(params)}"}
|
193
194
|
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
end
|
195
|
+
unless plural.nil?
|
196
|
+
# Define a method to fetch multiple resources with optional params
|
197
|
+
define_method("get_#{plural}") do |params = {}|
|
198
|
+
Resource.parse paginate(uri, params)
|
199
199
|
end
|
200
|
+
end
|
200
201
|
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
end
|
202
|
+
# Define a method to get one resource by it's id number, with optional
|
203
|
+
# params, thought now that I think about it I'm not sure why you'd pass
|
204
|
+
# params when grabbing just one resource.
|
205
|
+
|
206
|
+
# Some resources are Singletons
|
207
|
+
unless singular.nil?
|
208
|
+
define_method("get_#{singular}") do |*args|
|
209
|
+
case args.size
|
210
|
+
when 0
|
211
|
+
Resource.parse request(:get, "#{resource_uri}", nil)
|
212
|
+
when 1
|
213
|
+
Resource.parse request(:get, "#{resource_uri}/#{args[0]}", nil)
|
214
|
+
when 2
|
215
|
+
Resource.parse request(:get, "#{resource_uri}/#{args[0]}#{RequestParams.parameterize(args[1])}", nil)
|
216
|
+
else
|
217
|
+
raise ArgumentError.new("wrong number for arguments (#{args.count} for 1..2)")
|
218
218
|
end
|
219
219
|
end
|
220
|
+
end
|
220
221
|
|
221
|
-
|
222
|
+
when 'add'
|
222
223
|
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
end
|
224
|
+
# Define a method to add a new resource to the account
|
225
|
+
define_method("add_#{singular}") do |properties|
|
226
|
+
if properties.class == LMRest::Resource
|
227
|
+
Resource.parse request(:post, "#{resource_uri}", properties.to_h)
|
228
|
+
else
|
229
|
+
Resource.parse request(:post, "#{resource_uri}", properties)
|
230
230
|
end
|
231
|
+
end
|
231
232
|
|
232
|
-
|
233
|
+
when 'update'
|
233
234
|
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
end
|
235
|
+
# Define a method to update a resource
|
236
|
+
define_method("update_#{singular}") do |id, properties = {}|
|
237
|
+
if id.class == LMRest::Resource
|
238
|
+
Resource.parse request(:put, "#{resource_uri}/#{id.id}", id.to_h)
|
239
|
+
else
|
240
|
+
Resource.parse request(:put, "#{resource_uri}/#{id}", properties)
|
241
241
|
end
|
242
|
+
end
|
242
243
|
|
243
|
-
|
244
|
+
when 'delete'
|
244
245
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
end
|
246
|
+
# Define a method to delete the resource
|
247
|
+
define_method("delete_#{singular}") do |id|
|
248
|
+
if id.class == LMRest::Resource
|
249
|
+
id = id.id
|
250
|
+
Resource.parse request(:delete, "#{resource_uri}/#{id}", nil)
|
251
|
+
else
|
252
|
+
Resource.parse request(:delete, "#{resource_uri}/#{id}", nil)
|
253
253
|
end
|
254
254
|
end
|
255
255
|
end
|
data/lib/lm_rest/version.rb
CHANGED
data/lm_rest.gemspec
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lm_rest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Rodrigues
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -72,14 +72,14 @@ dependencies:
|
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: 2.1
|
75
|
+
version: 2.5.1
|
76
76
|
type: :runtime
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: 2.1
|
82
|
+
version: 2.5.1
|
83
83
|
description: Interact programmatically with your LogicMonitor account via the REST
|
84
84
|
API.
|
85
85
|
email:
|
@@ -99,6 +99,7 @@ files:
|
|
99
99
|
- bin/console
|
100
100
|
- bin/ds_checker.rb
|
101
101
|
- bin/setup
|
102
|
+
- bin/system_category_cleaner.rb
|
102
103
|
- lib/lm_rest.rb
|
103
104
|
- lib/lm_rest/api_client.rb
|
104
105
|
- lib/lm_rest/request_params.rb
|
@@ -124,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
124
125
|
- !ruby/object:Gem::Version
|
125
126
|
version: '0'
|
126
127
|
requirements: []
|
127
|
-
rubygems_version: 3.
|
128
|
+
rubygems_version: 3.2.15
|
128
129
|
signing_key:
|
129
130
|
specification_version: 4
|
130
131
|
summary: API Wrapper for LogicMonitor Rest API v2.
|