Skip to content

Commit

Permalink
[EUWE] Cloud Subnet UI: Task queue, validation, buttons
Browse files Browse the repository at this point in the history
https://bugzilla.redhat.com/show_bug.cgi?id=1394282

This is an Euwe-only commit to resolve conflicts with:

	app/controllers/cloud_subnet_controller.rb

This PR is equvalent to #12045 for master
  • Loading branch information
Martin Povolny authored and tzumainn committed Jan 13, 2017
1 parent 44d558e commit e049370
Show file tree
Hide file tree
Showing 11 changed files with 331 additions and 240 deletions.
Expand Up @@ -9,13 +9,16 @@ ManageIQ.angular.app.controller('cloudSubnetFormController', ['$http', '$scope',

if (cloudSubnetFormId == 'new') {
$scope.cloudSubnetModel.name = "";
$scope.cloudSubnetModel.dhcp_enabled = true;
$scope.cloudSubnetModel.ip_version = '4';
$scope.newRecord = true;
} else {
miqService.sparkleOn();

$http.get('/cloud_subnet/cloud_subnet_form_fields/' + cloudSubnetFormId).success(function(data) {
$scope.afterGet = true;
$scope.cloudSubnetModel.name = data.name;
$scope.cloudSubnetModel.dhcp_enabled = data.dhcp_enabled;
$scope.cloudSubnetModel.cidr = data.cidr;
$scope.cloudSubnetModel.gateway = data.gateway;
$scope.cloudSubnetModel.ip_version = data.ip_version;
Expand Down
187 changes: 106 additions & 81 deletions app/controllers/cloud_subnet_controller.rb
Expand Up @@ -20,29 +20,35 @@ def button
params[:page] = @current_page unless @current_page.nil? # Save current page for list refresh

@refresh_div = "main_div"
return tag("CloudSubnet") if params[:pressed] == "cloud_subnet_tag"
delete_subnets if params[:pressed] == 'cloud_subnet_delete'

if params[:pressed] == "cloud_subnet_edit"
case params[:pressed]
when "cloud_subnet_tag"
return tag("CloudSubnet")
when 'cloud_subnet_delete'
delete_subnets
when "cloud_subnet_edit"
checked_subnet_id = get_checked_subnet_id(params)
javascript_redirect :action => "edit", :id => checked_subnet_id
elsif params[:pressed] == "cloud_subnet_new"
javascript_redirect :action => "new"
elsif !flash_errors? && @refresh_div == "main_div" && @lastaction == "show_list"
replace_gtl_main_div
else
render_flash
if params[:pressed] == "cloud_subnet_new"
javascript_redirect :action => "new"
elsif !flash_errors? && @refresh_div == "main_div" && @lastaction == "show_list"
replace_gtl_main_div
else
render_flash
end
end
end

def cloud_subnet_form_fields
assert_privileges("cloud_subnet_edit")
subnet = find_by_id_filtered(CloudSubnet, params[:id])
render :json => {
:name => subnet.name,
:cidr => subnet.cidr,
:gateway => subnet.gateway,
:ip_version => subnet.ip_version
:name => subnet.name,
:cidr => subnet.cidr,
:dhcp_enabled => subnet.dhcp_enabled,
:gateway => subnet.gateway,
:ip_version => subnet.ip_version,
}
end

Expand All @@ -68,41 +74,44 @@ def create
case params[:button]
when "cancel"
javascript_redirect :action => 'show_list',
:flash_msg => _("Add of new Subnet was cancelled by the user") % {:model => ui_lookup(:table => 'cloud_subnet')}
:flash_msg => _("Creation of a Cloud Subnet was cancelled by the user")

when "add"
@subnet = CloudSubnet.new
options = form_params
options = new_form_params
ems = ExtManagementSystem.find(options[:ems_id])
if CloudSubnet.class_by_ems(ems).supports_create?
begin
CloudSubnet.create_subnet(ems, options)
# TODO: To replace with targeted refresh when avail. or either use tasks
EmsRefresh.queue_refresh(ManageIQ::Providers::NetworkManager)
add_flash(_("Creating %{subnet} \"%{subnet_name}\"") % {
:subnet => ui_lookup(:table => 'cloud_subnet'),
:subnet_name => options[:name]})
rescue => ex
add_flash(_("Unable to create %{subnet} \"%{subnet_name}\": %{details}") % {
:subnet => ui_lookup(:table => 'cloud_subnet'),
:subnet_name => options[:name],
:details => ex}, :error)
end
@breadcrumbs.pop if @breadcrumbs
session[:flash_msgs] = @flash_array.dup if @flash_array
javascript_redirect :action => "show_list"
options.delete(:ems_id)
task_id = ems.create_cloud_subnet_queue(session[:userid], options)

if task_id.kind_of?(Integer)
initiate_wait_for_task(:task_id => task_id, :action => "create_finished")
else
@in_a_form = true
add_flash(_(CloudSubnet.unsupported_reason(:create)), :error)
drop_breadcrumb(
:name => _("Add New Subnet") % {:model => ui_lookup(:table => 'cloud_subnet')},
:url => "/cloud_subnet/new"
javascript_flash(
:text => _("Cloud Subnet creation: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
:severity => :error,
:spinner_off => true
)
javascript_flash
end
end
end

def create_finished
task_id = session[:async][:params][:task_id]
subnet_name = session[:async][:params][:name]
task = MiqTask.find(task_id)
if MiqTask.status_ok?(task.status)
add_flash(_("Cloud Subnet \"%{name}\" created") % { :name => subnet_name })
else
add_flash(_("Unable to create Cloud Subnet: %{details}") %
{ :name => subnet_name, :details => task.message }, :error)
end

@breadcrumbs.pop if @breadcrumbs
session[:edit] = nil
session[:flash_msgs] = @flash_array.dup if @flash_array
javascript_redirect :action => "show_list"
end

def delete_subnets
assert_privileges("cloud_subnet_delete")

Expand All @@ -125,17 +134,10 @@ def delete_subnets
add_flash(_("Subnet no longer exists.") % {:model => ui_lookup(:table => "cloud_subnet")}, :error)
elsif subnet.supports_delete?
subnets_to_delete.push(subnet)
else
add_flash(_("Couldn't initiate deletion of Subnet \"%{name}\": %{details}") % {
:name => subnet.name,
:details => subnet.unsupported_reason(:delete)
}, :error)
end
end
unless subnets_to_delete.empty?
process_cloud_subnets(subnets_to_delete, "destroy")
# TODO: To replace with targeted refresh when avail. or either use tasks
EmsRefresh.queue_refresh(ManageIQ::Providers::NetworkManager)
end

# refresh the list if applicable
Expand All @@ -147,6 +149,7 @@ def delete_subnets
if @flash_array.nil?
add_flash(_("The selected Subnet was deleted") % {:model => ui_lookup(:table => "cloud_subnet")})
end
javascript_redirect :action => 'show_list', :flash_msg => @flash_array[0][:message]
else
drop_breadcrumb(:name => 'dummy', :url => " ") # missing a bc to get correctly back so here's a dummy
session[:flash_msgs] = @flash_array.dup if @flash_array
Expand Down Expand Up @@ -178,7 +181,7 @@ def get_checked_subnet_id(params)
def update
assert_privileges("cloud_subnet_edit")
@subnet = find_by_id_filtered(CloudSubnet, params[:id])

options = changed_form_params
case params[:button]
when "cancel"
cancel_action(_("Edit of Subnet \"%{name}\" was cancelled by the user") % {
Expand All @@ -187,45 +190,79 @@ def update
})

when "save"
begin
@subnet.update_subnet(form_params)
add_flash(_("Updating Subnet \"%{name}\"") % {
:model => ui_lookup(:table => 'cloud_subnet'),
:name => @subnet.name
})
rescue Excon::Error::Unauthorized => e
add_flash(_("Unable to update Subnet \"%{name}\": The request you have made requires authentication.") % {
:name => @subnet.name,
:details => e
}, :error)
rescue => e
add_flash(_("Unable to update Subnet \"%{name}\": %{details}") % {
:name => @subnet.name,
:details => e
}, :error)
task_id = @subnet.update_cloud_subnet_queue(session[:userid], options)

if task_id.kind_of?(Integer)
initiate_wait_for_task(:task_id => task_id, :action => "update_finished")
else
javascript_flash(
:text => _("Cloud Subnet update failed: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
:severity => :error,
:spinner_off => true
)
end
end
end

session[:edit] = nil
session[:flash_msgs] = @flash_array.dup if @flash_array
javascript_redirect previous_breadcrumb_url
def update_finished
task_id = session[:async][:params][:task_id]
subnet_name = session[:async][:params][:name]
task = MiqTask.find(task_id)
if MiqTask.status_ok?(task.status)
add_flash(_("Cloud Subnet \"%{name}\" updated") % {:name => subnet_name })
else
add_flash(
_("Unable to update Cloud Subnet \"%{name}\": %{details}") % { :name => subnet_name,
:details => task.message }, :error)
end

session[:edit] = nil
session[:flash_msgs] = @flash_array.dup if @flash_array
javascript_redirect previous_breadcrumb_url
end

private

def form_params
def switch_to_bol(option)
if option && option =~ /on|true/i
true
else
false
end
end

def changed_form_params
# Fields allowed for update are: name, enable_dhcp, dns_nameservers, allocation_pools, host_routes, gateway_ip
options = {}
options[:name] = params[:name] unless @subnet.name == params[:name]

# A gateway address is automatically assigned by Openstack when gateway is null
unless @subnet.gateway == params[:gateway]
options[:gateway_ip] = params[:gateway].empty? ? nil : params[:gateway]
end
unless @subnet.dhcp_enabled == switch_to_bol(params[:dhcp_enabled])
options[:enable_dhcp] = switch_to_bol(params[:dhcp_enabled])
end
# TODO: Add dns_nameservers, allocation_pools, host_routes
options
end

def new_form_params
params[:ip_version] ||= "4"
params[:dhcp_enabled] ||= false
options = {}
options[:name] = params[:name] if params[:name]
options[:ems_id] = params[:ems_id] if params[:ems_id]
options[:cidr] = params[:cidr] if params[:cidr]
options[:gateway] = params[:gateway] if params[:gateway]
# An address is automatically assigned by Openstack when gateway is null
if params[:gateway]
options[:gateway] = params[:gateway].empty? ? nil : params[:gateway]
end
options[:ip_version] = params[:ip_version]
options[:cloud_tenant] = find_by_id_filtered(CloudTenant, params[:cloud_tenant_id]) if params[:cloud_tenant_id]
options[:network_id] = params[:network_id] if params[:network_id]
# TODO: Adds following fields for create/update
options[:dhcp_enabled] = params[:dhcp_enabled]
options[:enable_dhcp] = params[:dhcp_enabled]
# TODO: Add extra fields
options[:availability_zone_id] = params[:availability_zone_id] if params[:availability_zone_id]
if params[:ipv6_router_advertisement_mode]
options[:ipv6_router_advertisement_mode] = params[:ipv6_router_advertisement_mode]
Expand All @@ -251,19 +288,7 @@ def process_cloud_subnets(subnets, operation)
:userid => session[:userid]
}
AuditEvent.success(audit)
begin
subnet.delete_subnet
deleted_subnets += 1
rescue NotImplementedError
add_flash(_("Cannot delete Network %{name}: Not supported.") % {:name => subnet.name}, :error)
rescue MiqException::MiqCloudSubnetDeleteError => e
add_flash(_("Cannot delete Network %{name}: %{error_message}") % {:name => subnet.name, :error_message => e.message}, :error)
end
end
if deleted_subnets > 0
add_flash(n_("Delete initiated for %{number} Cloud Subnet.",
"Delete initiated for %{number} Cloud Subnets.",
deleted_subnets) % {:number => deleted_subnets})
subnet.delete_cloud_subnet_queue(session[:userid])
end
end
end
Expand Down
40 changes: 21 additions & 19 deletions app/helpers/application_helper/toolbar/cloud_subnets_center.rb
Expand Up @@ -13,25 +13,27 @@ class ApplicationHelper::Toolbar::CloudSubnetsCenter < ApplicationHelper::Toolba
t,
),
separator,
button(
:cloud_subnet_edit,
'pficon pficon-edit fa-lg',
t = N_('Edit selected Cloud Subnet'),
t,
:url_parms => 'main_div',
:enabled => false,
:onwhen => '1'
),
button(
:cloud_subnet_delete,
'pficon pficon-delete fa-lg',
t = N_('Delete selected Cloud Subnets'),
t,
:url_parms => 'main_div',
:confirm => N_('Warning: The selected Cloud Subnets and ALL of their components will be removed!'),
:enabled => false,
:onwhen => '1+'
),
# TODO: Uncomment until cross controllers show_list issue fully in place
# https://github.com/ManageIQ/manageiq/pull/12551
# button(
# :cloud_subnet_edit,
# 'pficon pficon-edit fa-lg',
# t = N_('Edit selected Cloud Subnet'),
# t,
# :url_parms => 'main_div',
# :enabled => false,
# :onwhen => '1'
# ),
# button(
# :cloud_subnet_delete,
# 'pficon pficon-delete fa-lg',
# t = N_('Delete selected Cloud Subnets'),
# t,
# :url_parms => 'main_div',
# :confirm => N_('Warning: The selected Cloud Subnets and ALL of their components will be removed!'),
# :enabled => false,
# :onwhen => '1+'
# ),
]
)])

Expand Down
27 changes: 0 additions & 27 deletions app/models/cloud_subnet.rb
@@ -1,9 +1,5 @@
class CloudSubnet < ApplicationRecord
include_concern 'Operations'

include NewWithTypeStiMixin
include ProviderObjectMixin
include AsyncDeleteMixin
include VirtualTotalMixin
include SupportsFeatureMixin

Expand Down Expand Up @@ -56,29 +52,6 @@ def self.class_by_ems(ext_management_system)
ext_management_system && ext_management_system.class::CloudSubnet
end

def self.create_subnet(ext_management_system, options = {})
raise ArgumentError, _("ext_management_system cannot be nil") if ext_management_system.nil?

klass = class_by_ems(ext_management_system)
klass.raw_create_subnet(ext_management_system, options)
end

def delete_subnet
raw_delete_subnet
end

def raw_delete_subnet
raise NotImplementedError, _("raw_delete_subnet must be implemented in a subclass")
end

def raw_update_subnet(_options = {})
raise NotImplementedError, _("raw_update_subnet must be implemented in a subclass")
end

def update_subnet(options = {})
raw_update_subnet(options) unless options.empty?
end

private

def extra_attributes_save(key, value)
Expand Down

0 comments on commit e049370

Please sign in to comment.