Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set user when queueing VM actions #326

Merged
merged 2 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/controllers/api/base_controller/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def queue_object_action(object, summary, options)

MiqTask.generic_action_with_callback(task_options, queue_options)
end

def queue_options(method, role = nil)
{
:method_name => method,
:role => role,
:user => {
:user_id => current_user.id,
:group_id => current_user.current_group.id,
:tenant_id => current_user.current_tenant.id
}
}
end
end
end
end
18 changes: 10 additions & 8 deletions app/controllers/api/instances_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class InstancesController < BaseController
include Subcollections::SecurityGroups
include Subcollections::Snapshots

DEFAULT_ROLE = 'ems_operations'.freeze

def terminate_resource(type, id = nil, _data = nil)
raise BadRequestError, "Must specify an id for terminating a #{type} resource" unless id

Expand Down Expand Up @@ -114,31 +116,31 @@ def instance_ident(instance)

def terminate_instance(instance)
desc = "#{instance_ident(instance)} terminating"
task_id = queue_object_action(instance, desc, :method_name => "vm_destroy", :role => "ems_operations")
task_id = queue_object_action(instance, desc, queue_options("vm_destroy", DEFAULT_ROLE))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

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

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

def pause_instance(instance)
desc = "#{instance_ident(instance)} pausing"
task_id = queue_object_action(instance, desc, :method_name => "pause", :role => "ems_operations")
task_id = queue_object_action(instance, desc, queue_options("pause", DEFAULT_ROLE))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
Expand All @@ -155,31 +157,31 @@ def validate_instance_for_action(instance, action)

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

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

def reboot_guest_instance(instance)
desc = "#{instance_ident(instance)} rebooting"
task_id = queue_object_action(instance, desc, :method_name => "reboot_guest", :role => "ems_operations")
task_id = queue_object_action(instance, desc, queue_options("reboot_guest", DEFAULT_ROLE))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

def reset_instance(instance)
desc = "#{instance_ident(instance)} resetting"
task_id = queue_object_action(instance, desc, :method_name => "reset", :role => "ems_operations")
task_id = queue_object_action(instance, desc, queue_options("reset", DEFAULT_ROLE))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
Expand Down
15 changes: 8 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", DEFAULT_ROLE))
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", DEFAULT_ROLE))
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", DEFAULT_ROLE))
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", DEFAULT_ROLE))
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", DEFAULT_ROLE))
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", DEFAULT_ROLE))
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, was the :role missing here by mistake ? we're now introducing it with queue_options.

task_id = queue_object_action(vm, desc, queue_options("destroy"))
action_result(true, desc, :task_id => task_id)
rescue => err
action_result(false, err.to_s)
Expand Down
32 changes: 32 additions & 0 deletions spec/requests/instances_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def update_raw_power_state(state, *instances)
:message => /#{instance.id}.* terminating/i,
:href => api_instance_url(nil, instance)
)
expect(MiqQueue.where(:method_name => "vm_destroy",
:user_id => @user.id,
:group_id => @user.current_group.id,
:tenant_id => @user.current_tenant.id).count).to eq(1)
end

it "terminates multiple valid Instances" do
Expand Down Expand Up @@ -119,6 +123,10 @@ def update_raw_power_state(state, *instances)
post(instance_url, :params => gen_request(:stop))

expect_single_action_result(:success => true, :message => "stopping", :href => api_instance_url(nil, instance), :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 valid instances" do
Expand Down Expand Up @@ -163,6 +171,10 @@ def update_raw_power_state(state, *instances)
post(instance_url, :params => gen_request(:start))

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

it "starts multiple instances" do
Expand Down Expand Up @@ -217,6 +229,10 @@ def update_raw_power_state(state, *instances)
post(instance_url, :params => gen_request(:pause))

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

it "pauses multiple instances" do
Expand Down Expand Up @@ -270,6 +286,10 @@ def update_raw_power_state(state, *instances)
post(instance_url, :params => gen_request(:suspend))

expect_single_action_result(:success => true, :message => "suspending", :href => api_instance_url(nil, instance), :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 instances" do
Expand Down Expand Up @@ -324,6 +344,10 @@ def update_raw_power_state(state, *instances)
post(instance_url, :params => gen_request(:shelve))

expect_single_action_result(:success => true, :message => 'shelving', :href => api_instance_url(nil, instance))
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 "cannot shelve a shelved instance" do
Expand Down Expand Up @@ -392,6 +416,10 @@ def update_raw_power_state(state, *instances)
post(instance_url, :params => gen_request(:reboot_guest))

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

it "reboots multiple valid instances" do
Expand Down Expand Up @@ -436,6 +464,10 @@ def update_raw_power_state(state, *instances)
post(instance_url, :params => gen_request(:reset))

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

it "resets multiple valid instances" do
Expand Down
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