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: Use API; Fix fixed IPs subnet #2422

Merged
merged 32 commits into from
Jul 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
f7747a1
Network Router: Use API; Fix fixed IPs subnet
gildub Oct 17, 2017
db3a424
Use Object.assign
gildub Oct 19, 2017
528925e
Removed unused routes
gildub Oct 24, 2017
f61c5c4
Using init() and lodash isEmpty
gildub Nov 9, 2017
b2b0692
controller instead of angularForm data
gildub Nov 9, 2017
520c545
Address sparkleoff flow
gildub Nov 14, 2017
564048f
Added return(s) and fix new "id"
gildub Nov 21, 2017
6e45636
Placement dep and getSubnetByRef rename
gildub Nov 22, 2017
d99c8f0
Cleanup after rebase
gildub Nov 24, 2017
bb13be8
More returns to properly sync operations
gildub Nov 27, 2017
63b6766
Reduce exchanged data
gildub Dec 1, 2017
2cc7aec
network needs ems_id and snat needs network
gildub Dec 7, 2017
ad999eb
network required with external gw
gildub Dec 7, 2017
a39d274
Using bracket for literals
gildub Jan 16, 2018
565a222
Needs , multiline params and flow fix
gildub Apr 11, 2018
7ee595f
rebased after #3736, using static maxlength
gildub Apr 12, 2018
1921dc6
DRY url for add/cancel/save
gildub Apr 12, 2018
f2a9e02
JS better flow and cleansing
gildub Apr 13, 2018
7b80a4e
Use ems_id only
gildub Apr 13, 2018
aaa26ff
Added admin state up
gildub Apr 13, 2018
b5cb691
Single quotes preferrably
gildub Apr 13, 2018
6b1c9d0
move vm functions
gildub Apr 13, 2018
79da90c
Placement depends upon ems
gildub Apr 16, 2018
1be914b
fix wrong field names
gildub Apr 16, 2018
801922d
Promise: return needed; Admin up/down
gildub Apr 17, 2018
a2bdcf8
Consistent ext_management_system and default snat
gildub Apr 17, 2018
eab2980
Use getCloudSubnetsByNetworkID directly
gildub Apr 22, 2018
77c7aa5
function scope
gildub Apr 22, 2018
8c6a438
edit: default enable_snat is true
gildub May 11, 2018
479b15b
Use nesting fetch; Fixed mandatory field in form
May 31, 2018
b11a636
Use ems_id only
gildub Jun 5, 2018
3633a9f
No options filtering in cancel mode
gildub Jul 9, 2018
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
@@ -1,55 +1,100 @@
ManageIQ.angular.app.controller('networkRouterFormController', ['$http', '$scope', 'networkRouterFormId', 'miqService', 'API', function($http, $scope, networkRouterFormId, miqService, API) {
ManageIQ.angular.app.controller('networkRouterFormController', ['$scope', 'networkRouterFormId', 'miqService', 'API', function($scope, networkRouterFormId, miqService, API) {
var vm = this;

vm.networkRouterModel = {
name: '',
cloud_subnet_id: '',
};
vm.formId = networkRouterFormId;
vm.afterGet = false;
vm.modelCopy = angular.copy( vm.networkRouterModel );
vm.model = "networkRouterModel";
vm.ems = [];
var init = function() {
vm.afterGet = false;

vm.saveable = miqService.saveable;
vm.networkRouterModel = {
cloud_tenant: {
id: null
},
external_fixed_ips: [],
external_gateway: false,
extra_attributes: null,
//extra_attributes: { external_gateway_info: { enable_snat: true, external_fixed_ips: [], network_id: null }}
};
vm.ems = [];

ManageIQ.angular.scope = vm;
vm.formId = networkRouterFormId;
vm.model = 'networkRouterModel';
vm.newRecord = networkRouterFormId === 'new';
vm.saveable = miqService.saveable;

if (networkRouterFormId == 'new') {
vm.networkRouterModel.name = "";
vm.networkRouterModel.enable_snat = true;
vm.networkRouterModel.external_gateway = false;
vm.networkRouterModel.cloud_subnet_id = null;
vm.newRecord = true;
vm.saveUrl = vm.newRecord ? '/network_router/create/new' : '/network_router/update/' + networkRouterFormId;

miqService.networkProviders()
miqService.sparkleOn();
if (vm.newRecord) {
miqService.networkProviders()
.then(function(providers) {
vm.ems = providers;

vm.networkRouterModel.name = '';
vm.networkRouterModel.admin_state_up = true;
vm.networkRouterModel.cloud_subnet_id = null;

vm.afterGet = true;
vm.modelCopy = angular.copy(vm.networkRouterModel);
miqService.sparkleOff();
});
} else {
miqService.sparkleOn();
} else {
return API.get("/api/network_routers/" + networkRouterFormId + "?attributes=name,admin_state_up,cloud_network_id,cloud_tenant.name,ext_management_system.id,ext_management_system.name,extra_attributes").then(function(data) {
Object.assign(vm.networkRouterModel, data);
vm.networkRouterModel.admin_state_up = vm.networkRouterModel.admin_state_up == 't' ? true : false;
if (vm.networkRouterModel.extra_attributes.external_gateway_info && vm.networkRouterModel.extra_attributes.external_gateway_info != {}) {
vm.networkRouterModel.external_gateway = true;
if (vm.networkRouterModel.extra_attributes.external_gateway_info.external_fixed_ips) {
var ref = vm.networkRouterModel.extra_attributes.external_gateway_info.external_fixed_ips[0].subnet_id;
return vm.getSubnetByRef(ref);
}
}
}).then(function() {
return vm.getCloudNetworksByEms(vm.networkRouterModel.ext_management_system.id);
}).then(function() {
return vm.getCloudSubnetsByNetworkID(vm.networkRouterModel.cloud_network_id);
}).then(function() {
vm.afterGet = true;
vm.modelCopy = angular.copy(vm.networkRouterModel);
miqService.sparkleOff();
}).catch(miqService.handleFailure);
}
};

$http.get('/network_router/network_router_form_fields/' + networkRouterFormId)
.then(getNetworkRouterFormData)
.catch(miqService.handleFailure);
}
vm.getCloudNetworksByEms = function(id) {
if (id) {
return API.get("/api/cloud_networks?expand=resources&attributes=name,ems_ref&filter[]=external_facing=true&filter[]=ems_id=" + id).then(function(data) {
vm.available_networks = data.resources;
}).catch(miqService.handleFailure);
}
};

vm.getCloudSubnetsByNetworkID = function(id) {
if (id) {
return API.get("/api/cloud_subnets?expand=resources&attributes=name,ems_ref&filter[]=cloud_network_id=" + id).then(function(data) {
vm.available_subnets = data.resources;
}).catch(miqService.handleFailure);
}
};

vm.getSubnetByRef = function(ref) {
if (ref) {
return API.get("/api/cloud_subnets?expand=resources&attributes=name&filter[]=ems_ref=" + ref).then(function(data) {
vm.networkRouterModel.cloud_subnet_id = data.resources[0].id;
}).catch(miqService.handleFailure);
}
};

vm.addClicked = function() {
var url = 'create/new' + '?button=add';
var url = vm.saveUrl + '?button=add';
miqService.miqAjaxButton(url, vm.networkRouterModel, { complete: false });
};

vm.cancelClicked = function() {
if (networkRouterFormId == 'new') {
var url = '/network_router/create/new' + '?button=cancel';
} else {
var url = '/network_router/update/' + networkRouterFormId + '?button=cancel';
}
var url = vm.saveUrl + '?button=cancel';
miqService.miqAjaxButton(url);
};

vm.saveClicked = function() {
var url = '/network_router/update/' + networkRouterFormId + '?button=save';
var url = vm.saveUrl + '?button=save';
miqService.miqAjaxButton(url, vm.networkRouterModel, { complete: false });
};

Expand All @@ -68,50 +113,15 @@ ManageIQ.angular.app.controller('networkRouterFormController', ['$http', '$scope
vm.resetClicked = function() {
vm.networkRouterModel = angular.copy( vm.modelCopy );
$scope.angularForm.$setPristine(true);
miqService.miqFlash("warn", "All changes have been reset");
miqService.miqFlash('warn', __("All changes have been reset"));
};

vm.filterNetworkManagerChanged = function() {
miqService.sparkleOn();
$http.get('/network_router/network_router_networks_by_ems/' + vm.networkRouterModel.ems_id)
.then(getNetworkRouterFormByEmsData)
.catch(miqService.handleFailure);

vm.filterNetworkManagerChanged = function(id) {
vm.getCloudNetworksByEms(id);
miqService.getProviderTenants(function(data) {
vm.available_tenants = data.resources;
})(vm.networkRouterModel.ems_id);
};

vm.filterCloudNetworkChanged = function(id) {
miqService.sparkleOn();
$http.get('/network_router/network_router_subnets_by_network/' + id)
.then(getNetworkRouterFormByNetworkData)
.catch(miqService.handleFailure);
})(id);
};

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

vm.afterGet = true;

Object.assign(vm, _.pick(data, ['available_networks', 'available_subnets']));
Object.assign(vm.networkRouterModel, _.omit(data, ['available_networks', 'available_subnets']));

vm.modelCopy = angular.copy( vm.networkRouterModel );
miqService.sparkleOff();
}

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

vm.available_networks = data.available_networks;
miqService.sparkleOff();
}

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

vm.available_subnets = data.available_subnets;
miqService.sparkleOff();
}
init();
}]);
84 changes: 13 additions & 71 deletions app/controllers/network_router_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,57 +46,6 @@ def button
end
end

def network_router_form_fields
assert_privileges("network_router_edit")
router = find_record_with_rbac(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,
: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")
render :json => {
: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

def new
@router = NetworkRouter.new
assert_privileges("network_router_new")
Expand All @@ -118,11 +67,10 @@ def create
:flash_msg => _("Add of new Network Router was cancelled by the user")

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)

ems = ExtManagementSystem.find(params[:ems_id])
task_id = ems.create_network_router_queue(session[:userid], options)

add_flash(_("Network Router creation failed: Task start failed: ID [%{id}]") %
Expand Down Expand Up @@ -198,18 +146,18 @@ def edit
def update
assert_privileges("network_router_edit")
@router = find_record_with_rbac(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") % {:name => @router.name})

when "save"
options = form_params(params)
if switch_to_bool(params[:external_gateway])
options.merge!(form_external_gateway(params))
else
options.merge!(form_external_gateway({}))
end
task_id = @router.update_network_router_queue(session[:userid], options)

add_flash(_("Router update failed: Task start failed: ID [%{id}]") %
Expand Down Expand Up @@ -429,14 +377,6 @@ def remove_interface_finished

private

def get_networks_by_ems(id)
Rbac::Filterer.filtered(CloudNetwork.where(:ems_id => id)).select(:id, :name).as_json
end

def get_subnets_by_network(id)
Rbac::Filterer.filtered(CloudSubnet.where(:cloud_network_id => id)).select(:id, :name).as_json
end

def textual_group_list
[%i(properties relationships), %i(tags)]
end
Expand All @@ -451,14 +391,16 @@ def form_external_gateway(params)
subnet = find_record_with_rbac(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])
if params.fetch_path(:extra_attributes, :external_gateway_info, :enable_snat)
options[:external_gateway_info][:enable_snat] = params[:extra_attributes][:external_gateway_info][:enable_snat]
end
end
options
end

def form_params(params)
options = %i(name ems_id admin_state_up cloud_group_id cloud_subnet_id
cloud_network_id).each_with_object({}) do |param, opt|
options = %i(name admin_state_up ems_id cloud_group_id cloud_subnet_id
Copy link
Contributor

Choose a reason for hiding this comment

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

ems_id? Is this still being used? Because if so, you can't be using ext_management_system.id for the same purpose :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not using `ems_id. I replaced it, thanks.

cloud_network_id).each_with_object({}) do |param, opt|
opt[param] = params[param] if params[param]
end

Expand Down
Loading