Skip to content

Commit 4df970b

Browse files
author
Samuel Beaulieu
committed
(maint) Fix for ABS PR#306 that includes json responses
Before this change ABS sometiimes returned a string of JSON with escaped quotes, that had to be parsed again With this change, floaty should accept both the legacy and the new full JSON responses.
1 parent f3cd540 commit 4df970b

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

lib/vmfloaty/abs.rb

+23-5
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ def self.get_active_requests(verbose, url, user)
7777
requests.each do |req|
7878
next if req == 'null'
7979

80-
if valid_json?(req)
80+
if valid_json?(req) # legacy ABS had another JSON string always-be-scheduling/pull/306
8181
req_hash = JSON.parse(req)
82+
elsif req.is_a?(Hash)
83+
req_hash = req
8284
else
8385
FloatyLogger.warn "Warning: couldn't parse request returned from abs/status/queue"
8486
next
@@ -170,7 +172,11 @@ def self.list(verbose, url, os_filter = nil)
170172
res_body = JSON.parse(res.body)
171173
if res_body.key?('vmpooler_platforms')
172174
os_list << '*** VMPOOLER Pools ***'
173-
os_list += JSON.parse(res_body['vmpooler_platforms'])
175+
if res_body['vmpooler_platforms'].is_a?(Hash)
176+
os_list += res_body['vmpooler_platforms']
177+
else
178+
os_list += JSON.parse(res_body['vmpooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
179+
end
174180
end
175181
end
176182

@@ -180,7 +186,11 @@ def self.list(verbose, url, os_filter = nil)
180186
if res_body.key?('ondemand_vmpooler_platforms') && res_body['ondemand_vmpooler_platforms'] != '[]'
181187
os_list << ''
182188
os_list << '*** VMPOOLER ONDEMAND Pools ***'
183-
os_list += JSON.parse(res_body['ondemand_vmpooler_platforms'])
189+
if res_body['ondemand_vmpooler_platforms'].is_a?(Hash)
190+
os_list += res_body['ondemand_vmpooler_platforms']
191+
else
192+
os_list += JSON.parse(res_body['ondemand_vmpooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
193+
end
184194
end
185195
end
186196

@@ -190,7 +200,11 @@ def self.list(verbose, url, os_filter = nil)
190200
if res_body.key?('nspooler_platforms')
191201
os_list << ''
192202
os_list << '*** NSPOOLER Pools ***'
193-
os_list += JSON.parse(res_body['nspooler_platforms'])
203+
if res_body['nspooler_platforms'].is_a?(Hash)
204+
os_list += res_body['nspooler_platforms']
205+
else
206+
os_list += JSON.parse(res_body['nspooler_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
207+
end
194208
end
195209
end
196210

@@ -200,7 +214,11 @@ def self.list(verbose, url, os_filter = nil)
200214
if res_body.key?('aws_platforms')
201215
os_list << ''
202216
os_list << '*** AWS Pools ***'
203-
os_list += JSON.parse(res_body['aws_platforms'])
217+
if res_body['aws_platforms'].is_a?(Hash)
218+
os_list += res_body['aws_platforms']
219+
else
220+
os_list += JSON.parse(res_body['aws_platforms']) # legacy ABS had another JSON string always-be-scheduling/pull/306
221+
end
204222
end
205223
end
206224

spec/vmfloaty/abs_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@
7171
# rubocop:disable Layout/LineLength
7272
@active_requests_response = '
7373
[
74-
"{ \"state\":\"allocated\",\"last_processed\":\"2019-12-16 23:00:34 +0000\",\"allocated_resources\":[{\"hostname\":\"take-this.delivery.puppetlabs.net\",\"type\":\"win-2012r2-x86_64\",\"engine\":\"vmpooler\"}],\"audit_log\":{\"2019-12-13 16:45:29 +0000\":\"Allocated take-this.delivery.puppetlabs.net for job 1576255517241\"},\"request\":{\"resources\":{\"win-2012r2-x86_64\":1},\"job\":{\"id\":\"1576255517241\",\"tags\":{\"user\":\"test-user\"},\"user\":\"test-user\",\"time-received\":1576255519},\"priority\":1}}",
74+
{ "state":"allocated","last_processed":"2019-12-16 23:00:34 +0000","allocated_resources":[{"hostname":"take-this.delivery.puppetlabs.net","type":"win-2012r2-x86_64","engine":"vmpooler"}],"audit_log":{"2019-12-13 16:45:29 +0000":"Allocated take-this.delivery.puppetlabs.net for job 1576255517241"},"request":{"resources":{"win-2012r2-x86_64":1},"job":{"id":"1576255517241","tags":{"user":"test-user"},"user":"test-user","time-received":1576255519},"priority":1}},
7575
"null",
76-
"{\"state\":\"allocated\",\"last_processed\":\"2019-12-16 23:00:34 +0000\",\"allocated_resources\":[{\"hostname\":\"not-this.delivery.puppetlabs.net\",\"type\":\"win-2012r2-x86_64\",\"engine\":\"vmpooler\"}],\"audit_log\":{\"2019-12-13 16:46:14 +0000\":\"Allocated not-this.delivery.puppetlabs.net for job 1576255565159\"},\"request\":{\"resources\":{\"win-2012r2-x86_64\":1},\"job\":{\"id\":\"1576255565159\",\"tags\":{\"user\":\"not-test-user\"},\"user\":\"not-test-user\",\"time-received\":1576255566},\"priority\":1}}"
76+
{"state":"allocated","last_processed":"2019-12-16 23:00:34 +0000","allocated_resources":[{"hostname":"not-this.delivery.puppetlabs.net","type":"win-2012r2-x86_64","engine":"vmpooler"}],"audit_log":{"2019-12-13 16:46:14 +0000":"Allocated not-this.delivery.puppetlabs.net for job 1576255565159"},"request":{"resources":{"win-2012r2-x86_64":1},"job":{"id":"1576255565159","tags":{"user":"not-test-user"},"user":"not-test-user","time-received":1576255566},"priority":1}}
7777
]'
7878
# rubocop:enable Layout/LineLength
7979
@token = 'utpg2i2xswor6h8ttjhu3d47z53yy47y'
@@ -101,7 +101,7 @@
101101
# rubocop:disable Layout/LineLength
102102
@active_requests_response = '
103103
[
104-
"{ \"state\":\"allocated\", \"last_processed\":\"2020-01-17 22:29:13 +0000\", \"allocated_resources\":[{\"hostname\":\"craggy-chord.delivery.puppetlabs.net\", \"type\":\"centos-7-x86_64\", \"engine\":\"vmpooler\"}, {\"hostname\":\"visible-revival.delivery.puppetlabs.net\", \"type\":\"centos-7-x86_64\", \"engine\":\"vmpooler\"}], \"audit_log\":{\"2020-01-17 22:28:45 +0000\":\"Allocated craggy-chord.delivery.puppetlabs.net, visible-revival.delivery.puppetlabs.net for job 1579300120799\"}, \"request\":{\"resources\":{\"centos-7-x86_64\":2}, \"job\":{\"id\":\"1579300120799\", \"tags\":{\"user\":\"test-user\"}, \"user\":\"test-user\", \"time-received\":1579300120}, \"priority\":3}}"
104+
{ "state":"allocated", "last_processed":"2020-01-17 22:29:13 +0000", "allocated_resources":[{"hostname":"craggy-chord.delivery.puppetlabs.net", "type":"centos-7-x86_64", "engine":"vmpooler"}, {"hostname":"visible-revival.delivery.puppetlabs.net", "type":"centos-7-x86_64", "engine":"vmpooler"}], "audit_log":{"2020-01-17 22:28:45 +0000":"Allocated craggy-chord.delivery.puppetlabs.net, visible-revival.delivery.puppetlabs.net for job 1579300120799"}, "request":{"resources":{"centos-7-x86_64":2}, "job":{"id":"1579300120799", "tags":{"user":"test-user"}, "user":"test-user", "time-received":1579300120}, "priority":3}}
105105
]'
106106
@return_request = { '{"job_id":"1579300120799","hosts":{"hostname":"craggy-chord.delivery.puppetlabs.net","type":"centos-7-x86_64","engine":"vmpooler"},{"hostname":"visible-revival.delivery.puppetlabs.net","type":"centos-7-x86_64","engine":"vmpooler"}}'=>true }
107107
# rubocop:enable Layout/LineLength

0 commit comments

Comments
 (0)