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

Network router gateway setup #716

Merged
merged 4 commits into from
Mar 20, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ ManageIQ.angular.app.controller('networkRouterFormController', ['$http', '$scope

if (networkRouterFormId == 'new') {
$scope.networkRouterModel.name = "";
$scope.networkRouterModel.cloud_subnet_id = "";
$scope.networkRouterModel.enable_snat = true;
$scope.networkRouterModel.external_gateway = false;
$scope.networkRouterModel.cloud_subnet_id = null;
$scope.newRecord = true;
} else {
miqService.sparkleOn();
Expand Down Expand Up @@ -66,12 +68,25 @@ ManageIQ.angular.app.controller('networkRouterFormController', ['$http', '$scope
.catch(miqService.handleFailure);
};

$scope.filterCloudNetworkChanged = function(id) {
miqService.sparkleOn();
$http.get('/network_router/network_router_subnets_by_network/' + id)
Copy link
Contributor

@AparnaKarve AparnaKarve Mar 17, 2017

Choose a reason for hiding this comment

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

@gildub I see that you have added a new controller route and action -- network_router_subnets_by_network

Currently we have a new practice in place for all our angular forms, which is --
Use REST API calls and avoid creating new controller actions/routes, especially true for any new functionality being added in the forms.

Hence I recommend using an API call here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I checked that currently an API for network_routers does not exist, but it should not be too hard to add.

Copy link
Contributor

Choose a reason for hiding this comment

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

I missed the fact that this is an RFE BZ for 5.7... so maybe the REST API implementation can wait for now.
Can you create a follow-up PR later to call the REST API?

Copy link
Contributor Author

@gildub gildub Mar 18, 2017

Choose a reason for hiding this comment

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

@AparnaKarve,

Ack to create PR for REST API, I created following issue as a tracker:
#737

Thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

@gildub Thanks for creating the issue. We can work on this once this PR is merged.

.then(getNetworkRouterFormByNetworkData)
.catch(miqService.handleFailure);
};

function getNetworkRouterFormData(response) {
var data = response.data;

$scope.afterGet = true;
$scope.available_networks = data.available_networks;
$scope.available_subnets = data.available_subnets;
$scope.networkRouterModel.name = data.name;
$scope.networkRouterModel.cloud_subnet_id = '';
$scope.networkRouterModel.cloud_network_id = data.cloud_network_id;
$scope.networkRouterModel.cloud_subnet_id = data.cloud_subnet_id;
$scope.networkRouterModel.ems_id = data.ems_id;
$scope.networkRouterModel.enable_snat = data.enable_snat;
$scope.networkRouterModel.external_gateway = data.external_gateway;

$scope.modelCopy = angular.copy( $scope.networkRouterModel );
miqService.sparkleOff();
Expand All @@ -83,4 +98,11 @@ ManageIQ.angular.app.controller('networkRouterFormController', ['$http', '$scope
$scope.available_networks = data.available_networks;
miqService.sparkleOff();
}

function getNetworkRouterFormByNetworkData(response) {
var data = response.data;

$scope.available_subnets = data.available_subnets;
miqService.sparkleOff();
}
}]);
102 changes: 91 additions & 11 deletions app/controllers/network_router_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,51 @@ def button
def network_router_form_fields
assert_privileges("network_router_edit")
router = find_by_id_filtered(NetworkRouter, params[:id])
available_networks = get_networks_by_ems(router.ems_id)
network_id = nil
subnet_id = nil
external_gateway = false
enable_snat = true
available_subnets = {}

unless router.external_gateway_info.nil? || router.external_gateway_info.empty?
external_gateway = true
cloud_network_ref = router.external_gateway_info["network_id"]
enable_snat = router.external_gateway_info["enable_snat"]
network = CloudNetwork.where(:ems_ref => cloud_network_ref).first
network_id = network.id
available_subnets = get_subnets_by_network(network_id)
external_fixed_ips = router.external_gateway_info["external_fixed_ips"]
unless external_fixed_ips.nil? || external_fixed_ips.empty?
# TODO: Replace with array/table
subnet = CloudSubnet.where(:ems_ref => external_fixed_ips[0]["subnet_id"]).first
subnet_id = subnet.id
end
end

render :json => {
:name => router.name
:name => router.name,
:ems_id => router.ems_id,
:cloud_network_id => network_id,
:cloud_subnet_id => subnet_id,
:external_gateway => external_gateway,
:enable_snat => enable_snat,
:available_networks => available_networks,
:available_subnets => available_subnets
}
end

def network_router_networks_by_ems
assert_privileges("network_router_new")
networks = []
available_networks = CloudNetwork.where(:ems_id => params[:id]).find_each
available_networks.each do |network|
networks << { 'name' => network.name, 'id' => network.id }
end
render :json => {
:available_networks => networks
:available_networks => get_networks_by_ems(params[:id])
}
end

def network_router_subnets_by_network
assert_privileges("network_router_new")
render :json => {
:available_subnets => get_subnets_by_network(params[:id])
}
end

Expand Down Expand Up @@ -86,6 +117,7 @@ def create
when "add"
@router = NetworkRouter.new
options = form_params(params)
options.merge!(form_external_gateway(params)) if switch_to_bool(params[:external_gateway])
ems = ExtManagementSystem.find(options[:ems_id])
options.delete(:ems_id)
task_id = ems.create_network_router_queue(session[:userid], options)
Expand Down Expand Up @@ -181,6 +213,12 @@ def update
assert_privileges("network_router_edit")
@router = find_by_id_filtered(NetworkRouter, params[:id])
options = form_params(params)
if switch_to_bool(params[:external_gateway])
options.merge!(form_external_gateway(params))
else
options.merge!(form_external_gateway({}))
end

case params[:button]
when "cancel"
cancel_action(_("Edit of Router \"%{name}\" was cancelled by the user") % {
Expand Down Expand Up @@ -412,20 +450,54 @@ def remove_interface_finished
end

private

def get_networks_by_ems(id)
networks = []
available_networks = CloudNetwork.where(:ems_id => id).find_each
available_networks.each do |network|
networks << { 'name' => network.name, 'id' => network.id }
end
networks
end

def get_subnets_by_network(id)
subnets = []
available_subnets = CloudSubnet.where(:cloud_network_id => id).find_each
available_subnets.each do |subnet|
subnets << { 'name' => subnet.name, 'id' => subnet.id }
end
subnets
end

def textual_group_list
[%i(properties relationships), %i(tags)]
end
helper_method :textual_group_list

def form_params(in_params)
def form_external_gateway(params)
options = { :external_gateway_info => {} }
if params[:cloud_network_id] && !params[:cloud_network_id].empty?
network = find_by_id_filtered(CloudNetwork, params[:cloud_network_id])
options[:external_gateway_info][:network_id] = network.ems_ref
if params[:cloud_subnet_id] && !params[:cloud_subnet_id].empty?
subnet = find_by_id_filtered(CloudSubnet, params[:cloud_subnet_id])
options[:external_gateway_info][:external_fixed_ips] = [{ :subnet_id => subnet.ems_ref }]
end
options[:external_gateway_info][:enable_snat] = switch_to_bool(params[:enable_snat])
end
options
end

def form_params(params)
options = {}
[:name, :ems_id, :admin_state_up, :cloud_group_id,
:cloud_subnet_id, :cloud_network_id].each do |param|
options[param] = in_params[param] if in_params[param]
options[param] = params[param] if params[param]
end

options[:cloud_network_id].gsub!(/number:/, '') if options[:cloud_network_id]
if in_params[:cloud_tenant_id]
options[:cloud_tenant] = find_by_id_filtered(CloudTenant, in_params[:cloud_tenant_id])
if params[:cloud_tenant_id]
options[:cloud_tenant] = find_by_id_filtered(CloudTenant, params[:cloud_tenant_id])
end
options
end
Expand Down Expand Up @@ -453,4 +525,12 @@ def process_network_routers(routers, operation)
end

menu_section :net

def switch_to_bool(option)
if option && option =~ /on|true/i
true
else
false
end
end
end
76 changes: 76 additions & 0 deletions app/views/network_router/_common_new_edit.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
%h3
= _('Basic Information')
.form-horizontal
.form-group{"ng-class" => "{'has-error': angularForm.name.$invalid}"}
%label.col-md-2.control-label
= _('Router Name')
.col-md-8
%input.form-control{:type => "text",
:name => "name",
"ng-model" => "networkRouterModel.name",
"ng-maxlength" => 128,
"required" => "",
:checkchange => true}
%span.help-block{"ng-show" => "angularForm.name.$error.required"}
= _("Required")
%h3
= _('External Gateway')
.form-horizontal
.form-group
%label.col-md-2.control-label{"for" => "network_router_enable_snat"}
.col-md-8
%input{"bs-switch" => "",
:data => {:on_text => _('Yes'), :off_text => _('No'), :size => 'mini'},
"type" => "checkbox",
"id" => "network_router_external_gateway",
"name" => "external_gateway",
"checkchange" => "true",
"ng-model" => "networkRouterModel.external_gateway"}

.form-horizontal{"ng-if" => "networkRouterModel.external_gateway"}
.form-group{"ng-if" => "networkRouterModel.ems_id"}
%label.col-md-2.control-label{"for" => "network_router_enable_snat"}
= _('Enable Source NAT')
.col-md-8
%input{"bs-switch" => "",
:data => {:on_text => _('Yes'), :off_text => _('No'), :size => 'mini'},
"type" => "checkbox",
"id" => "network_router_enable_snat",
"name" => "enable_snat",
"checkchange" => "true",
"ng-model" => "networkRouterModel.enable_snat"}

.form-group{"ng-class" => "{'has-error': angularForm.cloud_network_id.$invalid}"}
%label.col-md-2.control-label
= _('Network')
.col-md-8
%select{"name" => "cloud_network_id",
"ng-model" => "networkRouterModel.cloud_network_id",
:checkchange => true,
"ng-options" => "network.id as network.name for network in available_networks",
"ng-change" => "filterCloudNetworkChanged(networkRouterModel.cloud_network_id)",
"pf-select" => true,
"ng-selected" => "networkRouterModel.cloud_network_id",
"ng-required" => "networkRouterModel.enable_snat == true",
"selectpicker-for-select-tag" => ""}
%option{"value" => ""}
= "<#{_('Choose')}>"
%span.help-block{"ng-show" => "angularForm.cloud_network_id.$error.required"}
= _("Required")

%h2
.form-horizontal{"ng-if" => "networkRouterModel.cloud_network_id"}
= _('Fixed IPs')
.form-group
%label.col-md-2.control-label
= _('Subnet')
.col-md-8
%select{"name" => "cloud_subnet_id",
"ng-model" => "networkRouterModel.cloud_subnet_id",
:checkchange => true,
"ng-options" => "subnet.id as subnet.name for subnet in available_subnets",
"pf-select" => true,
"ng-selected" => "networkRouterModel.cloud_subnet_id",
"selectpicker-for-select-tag" => ""}
%option{"value" => ""}
= "<#{_('Choose')}>"
1 change: 0 additions & 1 deletion app/views/network_router/add_interface_select.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
options_for_select([["<#{_('Choose')}>", nil]] + @subnet_choices.sort),
"ng-model" => "networkRouterModel.cloud_subnet_id",
"required" => "",
:miqrequired => true,
:checkchange => true,
"selectpicker-for-select-tag" => "")
= render :partial => "layouts/angular/x_custom_form_buttons_angular",
Expand Down
15 changes: 2 additions & 13 deletions app/views/network_router/edit.html.haml
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
%form#form_div{:name => "angularForm", 'ng-controller' => "networkRouterFormController"}
= render :partial => "layouts/flash_msg"
%h3
= _('Edit Router')
.form-horizontal
.form-group
%label.col-md-2.control-label
= _('Router Name')
.col-md-8
%input.form-control{:type => "text",
:name => "name",
'ng-model' => "networkRouterModel.name",
'ng-maxlength' => 128,
:miqrequired => true,
:checkchange => true}

= render :partial => "common_new_edit"

= render :partial => "layouts/angular/x_edit_buttons_angular"

Expand Down
39 changes: 8 additions & 31 deletions app/views/network_router/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,37 @@
%h3
= _('Network Provider')
.form-horizontal
.form-group
.form-group{"ng-class" => "{'has-error': angularForm.ems_id.$invalid}"}
%label.col-md-2.control-label
= _('Network Manager')
.col-md-8
= select_tag("ems_id",
options_for_select([["<#{_('Choose')}>", nil]] + @network_provider_choices.sort),
"ng-model" => "networkRouterModel.ems_id",
"required" => "",
:miqrequired => true,
:checkchange => true,
"ng-change" => "filterNetworkManagerChanged(networkRouterModel.ems_id)",
"selectpicker-for-select-tag" => "")
%span.help-block{"ng-show" => "angularForm.ems_id.$error.required"}
= _('Required')

%h3
= _('Basic Information')
.form-horizontal
.form-group
%label.col-md-2.control-label
= _('Router Name')
.col-md-8
%input.form-control{:type => "text",
:name => "name",
'ng-model' => "networkRouterModel.name",
'ng-maxlength' => 128,
:miqrequired => true,
:checkchange => true}
.form-group{"ng-if" => "networkRouterModel.ems_id"}
%label.col-md-2.control-label
= _('Network')
.col-md-8
%select{"name" => "cloud_network_id",
"ng-model" => "networkRouterModel.cloud_network_id",
"required" => "",
:miqrequired => true,
:checkchange => true,
'ng-options' => 'network.id as network.name for network in available_networks',
'pf-select' => true,
"selectpicker-for-select-tag" => ""}
%option{"value" => ""}
= "<#{_('Choose')}>"
= render :partial => "common_new_edit"

%h3
= _('Placement')
.form-horizontal
.form-group
.form-group{"ng-class" => "{'has-error': angularForm.cloud_tenant_id.$invalid}"}
%label.col-md-2.control-label
= _('Cloud Tenant')
.col-md-8
= select_tag("cloud_tenant_id",
options_for_select([["<#{_('Choose')}>", nil]] + @cloud_tenant_choices.sort),
"ng-model" => "networkRouterModel.cloud_tenant_id",
"required" => "",
:miqrequired => true,
:checkchange => true,
"selectpicker-for-select-tag" => "")
%span.help-block{"ng-show" => "angularForm.cloud_tenant_id.$error.required"}
= _("Required")

= render :partial => "layouts/angular/x_edit_buttons_angular"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
options_for_select([["<#{_('Choose')}>", nil]] + @subnet_choices.sort),
"ng-model" => "networkRouterModel.cloud_subnet_id",
"required" => "",
:miqrequired => true,
:checkchange => true,
"selectpicker-for-select-tag" => "")
= render :partial => "layouts/angular/x_custom_form_buttons_angular",
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,7 @@
index
network_router_form_fields
network_router_networks_by_ems
network_router_subnets_by_network
new
remove_interface_select
show
Expand Down
Loading