Skip to content

Commit

Permalink
Merge pull request #1997 from tzumainn/vm-security-group-operations
Browse files Browse the repository at this point in the history
Add vm security group operations
  • Loading branch information
h-kataria authored Oct 26, 2017
2 parents f315478 + 01932e2 commit 1cd88de
Show file tree
Hide file tree
Showing 15 changed files with 374 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
ManageIQ.angular.app.component('vmCloudAddSecurityGroupComponent', {
bindings: {
recordId: '@',
redirectUrl: '@',
},
controllerAs: 'vm',
controller: vmCloudAddSecurityGroupFormController,
templateUrl: '/static/vm_cloud/add_security_group.html.haml',
});

vmCloudAddSecurityGroupFormController.$inject = ['API', 'miqService', '$q'];

function vmCloudAddSecurityGroupFormController(API, miqService, $q) {
var vm = this;

vm.$onInit = function() {
vm.afterGet = false;
vm.vmCloudModel = {
security_group: '',
};
vm.security_groups = [];
vm.formId = vm.recordId;
vm.saveable = miqService.saveable;
vm.newRecord = true;
miqService.sparkleOn();

var currentSecurityGroups;

$q.all([
API.get("/api/instances/" + vm.recordId),
API.get("/api/instances/" + vm.recordId + "/security_groups?expand=resources&attributes=id,name"),
])
.then(function(data) {
var tenantId = data[0].cloud_tenant_id;
currentSecurityGroups = data[1].resources;

return API.get("/api/cloud_tenants/" + tenantId + "/security_groups?expand=resources&attributes=id,name");
})
.then(function(data) {
vm.security_groups = data.resources.filter(function(securityGroup) {
return ! _.find(currentSecurityGroups, { id: securityGroup.id });
});

vm.afterGet = true;
miqService.sparkleOff();
}).catch(miqService.handleFailure);
};

vm.cancelClicked = function() {
miqService.sparkleOn();
miqService.redirectBack(sprintf(__('Addition of security group was canceled by the user.')), 'warning', vm.redirectUrl);
};

vm.addClicked = function() {
var saveObject = {
name: vm.vmCloudModel.security_group,
action: 'add',
};
var saveMsg = sprintf(__('%s has been successfully added.'), vm.vmCloudModel.security_group);
miqService.sparkleOn();
API.post('/api/instances/' + vm.recordId + '/security_groups/', saveObject)
.then(miqService.redirectBack.bind(vm, saveMsg, 'success', vm.redirectUrl))
.catch(miqService.handleFailure);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
ManageIQ.angular.app.component('vmCloudRemoveSecurityGroupComponent', {
bindings: {
recordId: '@',
redirectUrl: '@',
},
controllerAs: 'vm',
controller: vmCloudRemoveSecurityGroupFormController,
templateUrl: '/static/vm_cloud/remove_security_group.html.haml',
});

vmCloudRemoveSecurityGroupFormController.$inject = ['API', 'miqService'];

function vmCloudRemoveSecurityGroupFormController(API, miqService) {
var vm = this;

vm.$onInit = function() {
vm.afterGet = false;
vm.vmCloudModel = {
security_group: '',
};
vm.security_groups = [];
vm.formId = vm.recordId;
vm.model = "vmCloudModel";
vm.saveable = miqService.saveable;
vm.newRecord = false;
miqService.sparkleOn();
API.get("/api/instances/" + vm.recordId + "/security_groups?expand=resources&attributes=id,name").then(function(data) {
vm.security_groups = data.resources;
vm.afterGet = true;
miqService.sparkleOff();
}).catch(miqService.handleFailure);
};

vm.cancelClicked = function() {
miqService.sparkleOn();
miqService.redirectBack(sprintf(__('Removal of security group was canceled by the user.')), 'warning', vm.redirectUrl);
};

vm.saveClicked = function() {
var saveObject = {
name: vm.vmCloudModel.security_group,
action: 'remove',
};
var saveMsg = sprintf(__('%s has been successfully removed.'), vm.vmCloudModel.security_group);
miqService.sparkleOn();
API.post('/api/instances/' + vm.recordId + '/security_groups/', saveObject)
.then(miqService.redirectBack.bind(vm, saveMsg, 'success', vm.redirectUrl))
.catch(miqService.handleFailure);
};
}
4 changes: 4 additions & 0 deletions app/controllers/application_controller/ci_processing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ module ApplicationController::CiProcessing
include Mixins::Actions::VmActions::Evacuate
include Mixins::Actions::VmActions::AssociateFloatingIp
include Mixins::Actions::VmActions::DisassociateFloatingIp
include Mixins::Actions::VmActions::AddSecurityGroup
include Mixins::Actions::VmActions::RemoveSecurityGroup
include Mixins::Actions::VmActions::Resize
include Mixins::Actions::VmActions::RightSize
include Mixins::Actions::VmActions::Reconfigure
Expand Down Expand Up @@ -1057,6 +1059,8 @@ def process_vm_buttons(pfx)
when "#{pfx}_migrate" then prov_redirect("migrate")
when "#{pfx}_publish" then prov_redirect("publish")
when "#{pfx}_terminate" then terminatevms
when "instance_add_security_group" then add_security_group_vms
when "instance_remove_security_group" then remove_security_group_vms
end
end

Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller/explorer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ def x_history
'evacuate' => :s2, 'service_dialog' => :s2, 'transform' => :s2,
'associate_floating_ip' => :s2,
'disassociate_floating_ip' => :s2,
'add_security_group' => :s2,
'remove_security_group' => :s2,

# specials
'perf' => :show,
Expand Down
43 changes: 43 additions & 0 deletions app/controllers/mixins/actions/vm_actions/add_security_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Mixins
module Actions
module VmActions
module AddSecurityGroup
def add_security_group_vms
assert_privileges("instance_add_security_group")
recs = checked_or_params
@record = find_record_with_rbac(VmCloud, recs.first)
if @record.supports_add_security_group? && @record.ext_management_system.present?
if @explorer
add_security_group
@refresh_partial = "vm_common/add_security_group"
else
javascript_redirect :controller => 'vm', :action => 'add_security_group', :rec_id => @record.id, :escape => false
end
else
add_flash(_("Unable to add Security Group to Instance \"%{name}\": %{details}") % {
:name => @record.name,
:details => @record.unsupported_reason(:add_security_group)
}, :error)
end
end

alias instance_add_security_group add_security_group_vms

def add_security_group
assert_privileges("instance_add_security_group")
@record ||= find_record_with_rbac(VmCloud, params[:rec_id])
unless @explorer
drop_breadcrumb(
:name => _("Add Security Group to '%{name}'") % {:name => @record.name},
:url => "/vm_cloud/add_security_group"
)
end
@sb[:explorer] = @explorer
@in_a_form = true
@add_security_group = true
render :action => "show" unless @explorer
end
end
end
end
end
43 changes: 43 additions & 0 deletions app/controllers/mixins/actions/vm_actions/remove_security_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Mixins
module Actions
module VmActions
module RemoveSecurityGroup
def remove_security_group_vms
assert_privileges("instance_remove_security_group")
recs = checked_or_params
@record = find_record_with_rbac(VmCloud, recs.first)
if @record.supports_remove_security_group? && @record.ext_management_system.present?
if @explorer
remove_security_group
@refresh_partial = "vm_common/remove_security_group"
else
javascript_redirect :controller => 'vm', :action => 'remove_security_group', :rec_id => @record.id, :escape => false
end
else
add_flash(_("Unable to remove Security Group from Instance \"%{name}\": %{details}") % {
:name => @record.name,
:details => @record.unsupported_reason(:remove_security_group)
}, :error)
end
end

alias instance_remove_security_group remove_security_group_vms

def remove_security_group
assert_privileges("instance_remove_security_group")
@record ||= find_record_with_rbac(VmCloud, params[:rec_id])
unless @explorer
drop_breadcrumb(
:name => _("Remove Security Group to '%{name}'") % {:name => @record.name},
:url => "/vm_cloud/remove_security_group"
)
end
@sb[:explorer] = @explorer
@in_a_form = true
@remove_security_group = true
render :action => "show" unless @explorer
end
end
end
end
end
16 changes: 14 additions & 2 deletions app/controllers/vm_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1241,7 +1241,7 @@ def replace_right_cell(options = {})
presenter.update(:main_div, r[:partial => partial, :locals => partial_locals])

locals = {:action_url => action, :record_id => @record.try(:id)}
if %w(clone migrate miq_request_new pre_prov publish
if %w(clone migrate miq_request_new pre_prov publish add_security_group remove_security_group
reconfigure resize live_migrate attach detach evacuate
associate_floating_ip disassociate_floating_ip).include?(@sb[:action])
locals[:no_reset] = true # don't need reset button on the screen
Expand Down Expand Up @@ -1331,7 +1331,7 @@ def replace_right_cell(options = {})
])
# these subviews use angular, so they need to use a special partial
# so the form buttons on the outer frame can be updated.
elsif %w(attach detach live_migrate resize evacuate ownership
elsif %w(attach detach live_migrate resize evacuate ownership add_security_group remove_security_group
associate_floating_ip disassociate_floating_ip).include?(@sb[:action])
presenter.update(:form_buttons_div, r[:partial => "layouts/angular/paging_div_buttons"])
elsif action != "retire" && action != "reconfigure_update"
Expand Down Expand Up @@ -1511,6 +1511,18 @@ def set_right_cell_vars
:name => name, :model => ui_lookup(:table => table)
}
action = "disassociate_floating_ip_vm"
when "add_security_group"
partial = "vm_common/add_security_group"
header = _("Add Security Group to %{model} \"%{name}\"") % {
:name => name, :model => ui_lookup(:table => table)
}
action = "add_security_group"
when "remove_security_group"
partial = "vm_common/remove_security_group"
header = _("Remove Security Group from %{model} \"%{name}\"") % {
:name => name, :model => ui_lookup(:table => table)
}
action = "remove_security_group"
when "clone", "migrate", "publish"
partial = "miq_request/prov_edit"
task_headers = {"clone" => _("Clone %{vm_or_template}"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ class ApplicationHelper::Toolbar::OpenstackVmCloudCenter < ApplicationHelper::To
t = N_('Disassociate a Floating IP from this Instance'),
t,
:klass => ApplicationHelper::Button::InstanceDisassociateFloatingIp),
button(
:instance_add_security_group,
'pficon pficon-cloud-security fa-lg',
t = N_('Add a Security Group to this Instance'),
t,
:klass => ApplicationHelper::Button::GenericFeatureButtonWithDisable,
:options => {:feature => :add_security_group}
),
button(
:instance_remove_security_group,
'pficon pficon-cloud-security fa-lg',
t = N_('Remove a Security Group from this Instance'),
t,
:klass => ApplicationHelper::Button::GenericFeatureButtonWithDisable,
:options => {:feature => :remove_security_group}
),
button(
:instance_resize,
'pficon pficon-edit fa-lg',
Expand Down
24 changes: 24 additions & 0 deletions app/views/static/vm_cloud/add_security_group.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%form#form_div{"name" => "angularForm",
"form-changed" => true,
"miq-form" => true,
"model" => "vm.vmCloudModel",
"ng-cloak" => "",
"ng-show" => "vm.afterGet"}
= render :partial => "layouts/flash_msg"
%h3
= _('Add Security Group')
.form-horizontal
.form-group
%label.col-md-2.control-label
= _('Security Group')
.col-md-8
%select{"name" => "security_group",
"ng-model" => "vm.vmCloudModel.security_group",
"ng-options" => "security_group.name as security_group.name for security_group in vm.security_groups",
"pf-select" => true,
"required" => "",
"selectpicker-for-select-tag" => ""}
= render :partial => "layouts/angular/generic_form_buttons"
24 changes: 24 additions & 0 deletions app/views/static/vm_cloud/remove_security_group.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%form#form_div{"name" => "angularForm",
"form-changed" => true,
"miq-form" => true,
"model" => "vm.vmCloudModel",
"ng-cloak" => "",
"ng-show" => "vm.afterGet"}
= render :partial => "layouts/flash_msg"
%h3
= _('Remove Security Group')
.form-horizontal
.form-group
%label.col-md-2.control-label
= _('Security Group')
.col-md-8
%select{"name" => "security_group",
"ng-model" => "vm.vmCloudModel.security_group",
"ng-options" => "security_group.name as security_group.name for security_group in vm.security_groups",
"pf-select" => true,
"required" => "",
"selectpicker-for-select-tag" => ""}
= render :partial => "layouts/angular/generic_form_buttons"
9 changes: 9 additions & 0 deletions app/views/vm_common/_add_security_group.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- @angular_form = true

= render :partial => "layouts/flash_msg"

%vm-cloud-add-security-group-component{"record-id" => @record.id,
"redirect-url" => "/#{controller_name}/explorer",}

:javascript
miq_bootstrap('vm-cloud-add-security-group-component');
9 changes: 9 additions & 0 deletions app/views/vm_common/_remove_security_group.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- @angular_form = true

= render :partial => "layouts/flash_msg"

%vm-cloud-remove-security-group-component{"record-id" => @record.id,
"redirect-url" => "/#{controller_name}/explorer",}

:javascript
miq_bootstrap('vm-cloud-remove-security-group-component');
4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3066,6 +3066,8 @@
associate_floating_ip_form_fields
disassociate_floating_ip
disassociate_floating_ip_form_fields
add_security_group
remove_security_group
retire
right_size
show
Expand Down Expand Up @@ -3129,6 +3131,8 @@
associate_floating_ip_form_fields
disassociate_floating_ip
disassociate_floating_ip_form_fields
add_security_group
remove_security_group
) +
compare_get,
:post => %w(
Expand Down
Loading

0 comments on commit 1cd88de

Please sign in to comment.