Skip to content

Commit

Permalink
refactored to use API for add/remove
Browse files Browse the repository at this point in the history
  • Loading branch information
tzumainn committed Oct 21, 2017
1 parent d859d2e commit 6751442
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 147 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,32 @@ function vmCloudAddSecurityGroupFormController(API, miqService, $q) {
vm.$onInit = function() {
vm.afterGet = false;
vm.vmCloudModel = {
security_group: null,
security_group: '',
};
vm.security_groups = [];
vm.formId = vm.recordId;
vm.model = "vmCloudModel";
vm.saveable = miqService.saveable;
vm.newRecord = true;
miqService.sparkleOn();

var currentSecurityGroups;

$q.all([
API.get("/api/vms/" + vm.recordId),
API.get("/api/vms/" + vm.recordId + "/security_groups?expand=resources&attributes=id,name"),
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");
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;
vm.modelCopy = angular.copy(vm.vmCloudModel);
miqService.sparkleOff();
}).catch(miqService.handleFailure);
};
Expand All @@ -54,7 +52,14 @@ function vmCloudAddSecurityGroupFormController(API, miqService, $q) {
};

vm.addClicked = function() {
var url = '/vm_cloud/add_security_group_vm/' + vm.recordId + '?button=submit';
miqService.miqAjaxButton(url, vm.vmCloudModel, { complete: false });
var saveObject = {
security_group: 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
Expand Up @@ -16,18 +16,17 @@ function vmCloudRemoveSecurityGroupFormController(API, miqService) {
vm.$onInit = function() {
vm.afterGet = false;
vm.vmCloudModel = {
security_group: null,
security_group: '',
};
vm.security_groups = [];
vm.formId = vm.recordId;
vm.model = "vmCloudModel";
vm.saveable = miqService.saveable;
vm.newRecord = true;
vm.newRecord = false;
miqService.sparkleOn();
API.get("/api/vms/" + vm.recordId + "/security_groups?expand=resources&attributes=id,name").then(function(data) {
API.get("/api/instances/" + vm.recordId + "/security_groups?expand=resources&attributes=id,name").then(function(data) {
vm.security_groups = data.resources;
vm.afterGet = true;
vm.modelCopy = angular.copy( vm.vmCloudModel );
miqService.sparkleOff();
}).catch(miqService.handleFailure);
};
Expand All @@ -37,8 +36,15 @@ function vmCloudRemoveSecurityGroupFormController(API, miqService) {
miqService.redirectBack(sprintf(__('Removal of security group was canceled by the user.')), 'warning', vm.redirectUrl);
};

vm.addClicked = function() {
var url = '/vm_cloud/remove_security_group_vm/' + vm.recordId + '?button=submit';
miqService.miqAjaxButton(url, vm.vmCloudModel, { complete: false });
vm.saveClicked = function() {
var saveObject = {
security_group: 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);
};
}
48 changes: 0 additions & 48 deletions app/controllers/mixins/actions/vm_actions/add_security_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,54 +37,6 @@ def add_security_group
@add_security_group = true
render :action => "show" unless @explorer
end

def add_security_group_vm
assert_privileges("instance_add_security_group")
@record = find_record_with_rbac(VmCloud, params[:id])
case params[:button]
when "cancel" then add_handle_cancel_button
when "submit" then add_handle_submit_button
end

if @sb[:explorer]
replace_right_cell
else
session[:flash_msgs] = @flash_array.dup
javascript_redirect previous_breadcrumb_url
end
end

def add_handle_cancel_button
add_flash(_("Addition of Security Group to Instance \"%{name}\" was cancelled by the user") % {:name => @record.name})
@record = @sb[:action] = nil
end

def add_handle_submit_button
if @record.supports_add_security_group?
security_group = params[:security_group]["name"]
begin
@record.add_security_group_queue(session[:userid], security_group)
add_flash(_("Adding Security Group %{security_group} to Instance \"%{name}\"") % {
:security_group => security_group,
:name => @record.name
})
rescue => ex
add_flash(_("Unable to add Security Group %{security_group} to Instance \"%{name}\": %{details}") % {
:security_group => security_group,
:name => @record.name,
:details => get_error_message_from_fog(ex.to_s)
}, :error)
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
params[:id] = @record.id.to_s # reset id in params for show
@record = nil
@sb[:action] = nil
end
end
end
end
Expand Down
49 changes: 0 additions & 49 deletions app/controllers/mixins/actions/vm_actions/remove_security_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,55 +37,6 @@ def remove_security_group
@remove_security_group = true
render :action => "show" unless @explorer
end

def remove_security_group_vm
assert_privileges("instance_remove_security_group")
@record = find_record_with_rbac(VmCloud, params[:id])
case params[:button]
when "cancel" then remove_handle_cancel_button
when "submit" then remove_handle_submit_button
end
end

private

def remove_handle_cancel_button
add_flash(_("Removal of Security Group from Instance \"%{name}\" was cancelled by the user") % {:name => @record.name})
@record = @sb[:action] = nil
end

def remove_handle_submit_button
if @record.supports_remove_security_group?
security_group = params[:security_group]["name"]
begin
@record.remove_security_group_queue(session[:userid], security_group)
add_flash(_("Removing Security Group %{security_group} from Instance \"%{name}\"") % {
:security_group => security_group,
:name => @record.name
})
rescue => ex
add_flash(_("Unable to remove Security Group %{security_group} from Instance \"%{name}\": %{details}") % {
:security_group => security_group,
:name => @record.name,
:details => get_error_message_from_fog(ex.to_s)
}, :error)
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
params[:id] = @record.id.to_s # reset id in params for show
@record = nil
@sb[:action] = nil
if @sb[:explorer]
replace_right_cell
else
session[:flash_msgs] = @flash_array.dup
javascript_redirect previous_breadcrumb_url
end
end
end
end
end
Expand Down
25 changes: 12 additions & 13 deletions app/views/static/vm_cloud/add_security_group.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
%form#form_div{:name => "angularForm",
"form-changed" => true,
"miq-form" => true,
"model" => "vm.vmCloudModel",
"model-copy" => "vm.modelCopy",
"ng-cloak" => "",
"ng-show" => "vm.afterGet"}
%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"
Expand All @@ -15,11 +14,11 @@
%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 for security_group in vm.security_groups',
'pf-select' => true,
'required' => "",
'selectpicker-for-select-tag' => ""}
%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"
25 changes: 12 additions & 13 deletions app/views/static/vm_cloud/remove_security_group.html.haml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
%form#form_div{:name => "angularForm",
"form-changed" => true,
"miq-form" => true,
"model" => "vm.vmCloudModel",
"model-copy" => "vm.modelCopy",
"ng-cloak" => "",
"ng-show" => "vm.afterGet"}
%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"
Expand All @@ -15,11 +14,11 @@
%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 for security_group in vm.security_groups',
'pf-select' => true,
'required' => "",
'selectpicker-for-select-tag' => ""}
%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"
1 change: 0 additions & 1 deletion app/views/vm_common/_add_security_group.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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

:javascript
miq_bootstrap('vm-cloud-add-security-group-component');
4 changes: 0 additions & 4 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3078,8 +3078,6 @@
live_migrate_vm
associate_floating_ip_vm
disassociate_floating_ip_vm
add_security_group_vm
remove_security_group_vm
retire
right_size
set_checked_items
Expand Down Expand Up @@ -3188,8 +3186,6 @@
ownership_update
associate_floating_ip_vm
disassociate_floating_ip_vm
add_security_group_vm
remove_security_group_vm
wait_for_task
ownership_form_fields
) +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe('vm-cloud-add-security-group-component', function() {
}));

it('calls API.get with the appropriate URL', function () {
expect(API.get).toHaveBeenCalledWith('/api/vms/1111');
expect(API.get).toHaveBeenCalledWith('/api/vms/1111/security_groups?expand=resources&attributes=id,name');
expect(API.get).toHaveBeenCalledWith('/api/instances/1111');
expect(API.get).toHaveBeenCalledWith('/api/instances/1111/security_groups?expand=resources&attributes=id,name');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('vm-cloud-remove-security-group-component', function() {
}));

it('calls API.get with the appropriate URL', function () {
expect(API.get).toHaveBeenCalledWith('/api/vms/1111/security_groups?expand=resources&attributes=id,name');
expect(API.get).toHaveBeenCalledWith('/api/instances/1111/security_groups?expand=resources&attributes=id,name');
});
});
});

0 comments on commit 6751442

Please sign in to comment.