lm_rest 1.0.3 → 1.0.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9b62108ac0ae79610c225c5af3a0786fc20e2e8e5a6834e9eac3de9029984f25
4
- data.tar.gz: 2fadc9fe2120160c4d49e9bdb77b729f235c312b9c63363a46d7dbef0a7ae0d5
3
+ metadata.gz: 9e0f1787b39c49b60f1431d41adde6a144152b1f2aed2b1017e7db5d1b45a75a
4
+ data.tar.gz: 42196790b7103ebdb125758e1e1bc3992553518384016c2d4c66eda1d55ff3fd
5
5
  SHA512:
6
- metadata.gz: 33b396fea0c3e99de8fa68be8cd6e91af96e0d7b273accfcbdb5dc60c6dd993aff5a1efb1701eb69511afd2841ccd598d1b123c37913d4edc0fc31898d9897ca
7
- data.tar.gz: 7f33737751f03dab145f01ec2703ea50082f963875d7a1dec27f8f8fbd89ff6678a432585eb1114d8bd67adc77fcabea6915c94d6e6ebd4b1caf71a49d57d8c3
6
+ metadata.gz: 2c33110c07110f9a8e82225f1cfc7950916d8aa2613e3180069ef5ded01cf71f85773fce6ee2d3910ee4d2697a6dc28b3976efe43ed73eb1fac3266bbb4dd3d8
7
+ data.tar.gz: ddf554ea7252633c7f9aa41a4e6a46573d3d9ac4c23b6269d2b0a2c0b32a75dbdbda044518e8923a41c7f95d4fc771c88aa04687a25ef80cce71d45a84a7ae6c
@@ -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: "true()",
32
+ needInheritProps: true,
33
+ originalAppliesTo: "true()",
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
@@ -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
@@ -78,15 +79,20 @@ module LMRest
78
79
 
79
80
  json_params = params.to_json
80
81
 
81
- case method
82
- when :get
83
- response = RestClient.get(url, headers)
84
- when :post
85
- response = RestClient.post(url, json_params, headers)
86
- when :put
87
- response = RestClient.put(url, json_params, headers)
88
- when :delete
89
- response = RestClient.delete(url, headers: headers)
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
90
96
  end
91
97
 
92
98
  if response.code != 200
@@ -94,6 +100,9 @@ module LMRest
94
100
  raise
95
101
  end
96
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']
97
106
 
98
107
  JSON.parse(response.body)
99
108
  end
@@ -1,3 +1,3 @@
1
1
  module LMRest
2
- VERSION = '1.0.3'
2
+ VERSION = '1.0.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lm_rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Rodrigues
@@ -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