Skip to content
This repository was archived by the owner on Jun 2, 2021. It is now read-only.

Commit f150687

Browse files
gdankovdanail-branekovgcapizzi
authored
Implement get and list methods for tasks in OPI (cloudfoundry#1892)
* Implement get and list methods for tasks in OPI [#174335347] Co-authored-by: Danail Branekov <danailster@gmail.com> Co-authored-by: Giuseppe Capizzi <gcapizzi@pivotal.io> Co-authored-by: Danail Branekov <danailster@gmail.com>
1 parent 1f662ab commit f150687

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

lib/cloud_controller/opi/task_client.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,33 @@ def desire_task(task, domain)
2121
end
2222
end
2323

24-
def fetch_task(_)
25-
Diego::Bbs::Models::Task.new
24+
def fetch_task(guid)
25+
resp = client.get("/tasks/#{guid}")
26+
27+
return if resp.status_code == 404
28+
29+
if resp.status_code != 200
30+
raise CloudController::Errors::ApiError.new_from_details('TaskError', "response status code: #{resp.status_code}")
31+
end
32+
33+
task = JSON.parse(resp.body)
34+
task[:task_guid] = task.delete('guid')
35+
OPI.recursive_ostruct(task)
2636
end
2737

2838
def fetch_tasks
29-
[]
39+
resp = client.get('/tasks')
40+
41+
if resp.status_code != 200
42+
raise CloudController::Errors::ApiError.new_from_details('TaskError', "response status code: #{resp.status_code}")
43+
end
44+
45+
tasks = JSON.parse(resp.body)
46+
tasks.each do |task|
47+
task['task_guid'] = task.delete('guid')
48+
end
49+
50+
tasks.map { |t| OPI.recursive_ostruct(t) }
3051
end
3152

3253
def cancel_task(guid)

spec/unit/lib/cloud_controller/opi/task_client_spec.rb

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,24 +138,80 @@
138138
end
139139

140140
describe 'can fetch a task' do
141-
it 'should return a empty dummy task' do
142-
task = client.fetch_task('some-guid')
143-
expect(task).to eq(Diego::Bbs::Models::Task.new)
141+
let(:expected_body) {
142+
{ 'guid': 'foo' }.to_json
143+
}
144+
145+
before(:each) do
146+
stub_request(:get, "#{opi_url}/tasks/some-task-guid").
147+
to_return(status: 200, body: expected_body)
144148
end
145149

146-
it 'should not send any request to opi' do
147-
expect(a_request(:any, opi_url)).not_to have_been_made
150+
it 'should return a parsed task' do
151+
task = client.fetch_task('some-task-guid')
152+
153+
expect(WebMock).to have_requested(:get, "#{opi_url}/tasks/some-task-guid")
154+
155+
expect(task).to eq(OpenStruct.new(task_guid: 'foo'))
156+
end
157+
158+
context 'when the task is not found' do
159+
before(:each) do
160+
stub_request(:get, "#{opi_url}/tasks/some-task-guid").
161+
to_return(status: 404)
162+
end
163+
164+
it 'should return nil' do
165+
expect(client.fetch_task('some-task-guid')).to be_nil
166+
end
167+
end
168+
169+
context 'when fetching the task fails' do
170+
before(:each) do
171+
stub_request(:get, "#{opi_url}/tasks/some-task-guid").
172+
to_return(status: 500)
173+
end
174+
175+
it 'should raise an ApiError' do
176+
expect { client.fetch_task('some-task-guid') }.to raise_error(CloudController::Errors::ApiError, /response status code: 500/) do |e|
177+
expect(e.name).to eq('TaskError')
178+
end
179+
end
148180
end
149181
end
150182

151183
describe 'can fetch all tasks' do
152-
it 'should return an empty list' do
184+
let(:expected_body) {
185+
[{ 'guid': 'foo' }, { 'guid': 'bar' }].to_json
186+
}
187+
188+
before(:each) do
189+
stub_request(:get, "#{opi_url}/tasks").
190+
to_return(status: 200, body: expected_body)
191+
end
192+
193+
it 'should return a parsed list of tasks' do
153194
tasks = client.fetch_tasks
154-
expect(tasks).to be_empty
195+
196+
expect(WebMock).to have_requested(:get, "#{opi_url}/tasks")
197+
198+
expect(tasks).to match_array([
199+
OpenStruct.new(task_guid: 'foo'),
200+
OpenStruct.new(task_guid: 'bar'),
201+
])
155202
end
156203

157-
it 'should not send any request to opi' do
158-
expect(a_request(:any, opi_url)).not_to have_been_made
204+
context 'when fetching the tasks fails' do
205+
before(:each) do
206+
stub_request(:get, "#{opi_url}/tasks").
207+
to_return(status: 500)
208+
end
209+
210+
it 'should raise an ApiError' do
211+
expect { client.fetch_tasks }.to raise_error(CloudController::Errors::ApiError, /response status code: 500/) do |e|
212+
expect(e.name).to eq('TaskError')
213+
end
214+
end
159215
end
160216
end
161217

0 commit comments

Comments
 (0)