Skip to content

Commit

Permalink
Set user when queueing VM actions
Browse files Browse the repository at this point in the history
When VM actions are queued, the user should be set so that it is available as the requester in automate.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1546375
  • Loading branch information
Jillian Tullo committed Feb 21, 2018
1 parent 2de9dcc commit 8284f6d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
28 changes: 21 additions & 7 deletions app/controllers/api/vms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class VmsController < BaseController

VALID_EDIT_ATTRS = %w(description child_resources parent_resource).freeze
RELATIONSHIP_COLLECTIONS = %w(vms templates).freeze
DEFAULT_ROLE = 'ems_operations'.freeze

def start_resource(type, id = nil, _data = nil)
raise BadRequestError, "Must specify an id for starting a #{type} resource" unless id
Expand Down Expand Up @@ -322,55 +323,55 @@ def validate_vm_for_remote_console(vm, protocol = nil)

def start_vm(vm)
desc = "#{vm_ident(vm)} starting"
task_id = queue_object_action(vm, desc, :method_name => "start", :role => "ems_operations")
task_id = queue_object_action(vm, desc, queue_options("start"))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

def stop_vm(vm)
desc = "#{vm_ident(vm)} stopping"
task_id = queue_object_action(vm, desc, :method_name => "stop", :role => "ems_operations")
task_id = queue_object_action(vm, desc, queue_options("stop"))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

def suspend_vm(vm)
desc = "#{vm_ident(vm)} suspending"
task_id = queue_object_action(vm, desc, :method_name => "suspend", :role => "ems_operations")
task_id = queue_object_action(vm, desc, queue_options("suspend"))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

def pause_vm(vm)
desc = "#{vm_ident(vm)} pausing"
task_id = queue_object_action(vm, desc, :method_name => "pause", :role => "ems_operations")
task_id = queue_object_action(vm, desc, queue_options("pause"))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

def shelve_vm(vm)
desc = "#{vm_ident(vm)} shelving"
task_id = queue_object_action(vm, desc, :method_name => "shelve", :role => "ems_operations")
task_id = queue_object_action(vm, desc, queue_options("shelve"))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

def shelve_offload_vm(vm)
desc = "#{vm_ident(vm)} shelve-offloading"
task_id = queue_object_action(vm, desc, :method_name => "shelve_offload", :role => "ems_operations")
task_id = queue_object_action(vm, desc, queue_options("shelve_offload"))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

def destroy_vm(vm)
desc = "#{vm_ident(vm)} deleting"
task_id = queue_object_action(vm, desc, :method_name => "destroy")
task_id = queue_object_action(vm, desc, queue_options("destroy").except(:role))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
Expand Down Expand Up @@ -477,5 +478,18 @@ def request_console_vm(vm, protocol)
rescue => err
action_result(false, err.to_s)
end

def queue_options(method)
current_user = User.current_user
{
:method_name => method,
:role => DEFAULT_ROLE,
:user => {
:user_id => current_user.id,
:group_id => current_user.current_group.id,
:tenant_id => current_user.current_tenant.id
}
}
end
end
end
32 changes: 31 additions & 1 deletion spec/requests/vms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ def update_raw_power_state(state, *vms)
expect(MiqQueue.where(:class_name => vm.class.name,
:instance_id => vm.id,
:method_name => "start",
:zone => zone.name).count).to eq(1)
:zone => zone.name,
:user_id => @user.id,
:group_id => @user.current_group.id,
:tenant_id => @user.current_tenant.id).count).to eq(1)
end

it "starts multiple vms" do
Expand Down Expand Up @@ -338,6 +341,10 @@ def update_raw_power_state(state, *vms)
post(vm_url, :params => gen_request(:stop))

expect_single_action_result(:success => true, :message => "stopping", :href => api_vm_url(nil, vm), :task => true)
expect(MiqQueue.where(:method_name => "stop",
:user_id => @user.id,
:group_id => @user.current_group.id,
:tenant_id => @user.current_tenant.id).count).to eq(1)
end

it "stops multiple vms" do
Expand Down Expand Up @@ -391,6 +398,10 @@ def update_raw_power_state(state, *vms)
post(vm_url, :params => gen_request(:suspend))

expect_single_action_result(:success => true, :message => "suspending", :href => api_vm_url(nil, vm), :task => true)
expect(MiqQueue.where(:method_name => "suspend",
:user_id => @user.id,
:group_id => @user.current_group.id,
:tenant_id => @user.current_tenant.id).count).to eq(1)
end

it "suspends multiple vms" do
Expand Down Expand Up @@ -444,6 +455,13 @@ def update_raw_power_state(state, *vms)
post(vm_url, :params => gen_request(:pause))

expect_single_action_result(:success => true, :message => "pausing", :href => api_vm_url(nil, vm), :task => true)
expect(MiqQueue.where(:class_name => vm.class.name,
:instance_id => vm.id,
:method_name => "pause",
:zone => zone.name,
:user_id => @user.id,
:group_id => @user.current_group.id,
:tenant_id => @user.current_tenant.id).count).to eq(1)
end

it "pauses multiple vms" do
Expand Down Expand Up @@ -517,6 +535,10 @@ def update_raw_power_state(state, *vms)
post(vm_openstack_url, :params => gen_request(:shelve))

expect_single_action_result(:success => true, :message => "shelving", :href => api_vm_url(nil, vm_openstack), :task => true)
expect(MiqQueue.where(:method_name => "shelve",
:user_id => @user.id,
:group_id => @user.current_group.id,
:tenant_id => @user.current_tenant.id).count).to eq(1)
end

it "shelve for a VMWare vm is not supported" do
Expand Down Expand Up @@ -620,6 +642,10 @@ def update_raw_power_state(state, *vms)
expect_single_action_result(:success => true,
:message => "shelve-offloading",
:href => api_vm_url(nil, vm_openstack))
expect(MiqQueue.where(:method_name => "shelve_offload",
:user_id => @user.id,
:group_id => @user.current_group.id,
:tenant_id => @user.current_tenant.id).count).to eq(1)
end

it "shelve_offload for a VMWare vm is not supported" do
Expand Down Expand Up @@ -677,6 +703,10 @@ def update_raw_power_state(state, *vms)
post(vm_url, :params => gen_request(:delete))

expect_single_action_result(:success => true, :message => "deleting", :href => api_vm_url(nil, vm), :task => true)
expect(MiqQueue.where(:method_name => "destroy",
:user_id => @user.id,
:group_id => @user.current_group.id,
:tenant_id => @user.current_tenant.id).count).to eq(1)
end

it "deletes a vm via a resource DELETE" do
Expand Down

0 comments on commit 8284f6d

Please sign in to comment.