Skip to content

Commit

Permalink
Merge pull request #662 from lpichler/do_policy_check_before_retiring…
Browse files Browse the repository at this point in the history
…_request

(CI Failure Fix ) Put make_retire_request on queue in VmsController
  • Loading branch information
abellotti authored Aug 27, 2019
2 parents 2bfcba8 + b29fd91 commit d3b5c8e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
17 changes: 17 additions & 0 deletions app/controllers/api/vms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,14 @@ def set_miq_server_resource(type, id, data)
action_result(false, "Failed to set miq_server - #{err}")
end

def request_retire_resource(type, id, _data = nil)
api_action(type, id) do |klass|
vm = resource_search(id, type, klass)
api_log_info("Retiring request of vm #{vm_ident(vm)}")
request_retire(vm)
end
end

private

def miq_server_message(miq_server)
Expand Down Expand Up @@ -487,5 +495,14 @@ def request_console_vm(vm, protocol)
rescue => err
action_result(false, err.to_s)
end

def request_retire(virtual_machine)
desc = "#{vm_ident(virtual_machine)} request retire"

task_id = queue_object_action(virtual_machine, desc, queue_options("make_retire_request", "automate"))
action_result(true, desc, :task_id => task_id)
rescue StandardError => err
action_result(false, err.to_s)
end
end
end
51 changes: 36 additions & 15 deletions spec/requests/vms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1098,37 +1098,58 @@ def update_raw_power_state(state, *vms)
context "valid" do
it "to a single Vm" do
api_basic_authorize(action_identifier(:vms, :request_retire))
message = "VM id:#{vm.id} name:'#{vm.name}' request retire"
task_id = MiqTask.find_by(:name => message)&.id
expect(task_id).to be_nil

post(vm_url, :params => gen_request(:request_retire))

task = MiqTask.find_by(:name => message)
expected = {
"href" => a_string_matching(api_requests_url),
"message" => a_string_matching(/VM Retire - Request Created/),
"options" => a_hash_including("src_ids" => a_collection_including(vm.id))
"success" => true,
"message" => "VM id:#{vm.id} name:'#{vm.name}' request retire",
"task_id" => task.id.to_s,
"task_href" => api_task_url(nil, task),
"href" => api_vm_url(nil, vm)
}

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it "to multiple Vms" do
api_basic_authorize(collection_action_identifier(:vms, :request_retire))
message_vm1 = "VM id:#{vm1.id} name:'#{vm1.name}' request retire"
task_id_vm1 = MiqTask.find_by(:name => message_vm1)&.id
expect(task_id_vm1).to be_nil

message_vm2 = "VM id:#{vm2.id} name:'#{vm2.name}' request retire"
task_id_vm2 = MiqTask.find_by(:name => message_vm2)&.id
expect(task_id_vm2).to be_nil

post(api_vms_url, :params => gen_request(:request_retire, [{"href" => vm1_url}, {"href" => vm2_url}]))
task_vm1 = MiqTask.find_by(:name => message_vm1)
task_vm2 = MiqTask.find_by(:name => message_vm2)

expected = {
"results" => a_collection_containing_exactly(
a_hash_including(
"message" => a_string_matching(/VM Retire - Request Created/),
"href" => a_string_matching(api_requests_url),
"options" => a_hash_including("src_ids" => a_collection_containing_exactly(vm1.id))
),
a_hash_including(
"message" => a_string_matching(/VM Retire - Request Created/),
"href" => a_string_matching(api_requests_url),
"options" => a_hash_including("src_ids" => a_collection_containing_exactly(vm2.id))
)
)
"results" => [
{
"success" => true,
"message" => a_string_matching(/#{vm1.id}.* request retire/i),
"task_id" => task_vm1.id.to_s,
"task_href" => api_task_url(nil, task_vm1),
"href" => api_vm_url(nil, vm1)
},
{
"success" => true,
"message" => message_vm2,
"task_id" => task_vm2.id.to_s,
"task_href" => api_task_url(nil, task_vm2),
"href" => api_vm_url(nil, vm2)
}
]
}

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
Expand Down

0 comments on commit d3b5c8e

Please sign in to comment.