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 Provider: Cloud subnets refactor #1545

Merged
merged 3 commits into from Aug 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,37 +1,51 @@
ManageIQ.angular.app.controller('cloudSubnetFormController', ['$scope', 'cloudSubnetFormId', 'miqService', 'API', function($scope, cloudSubnetFormId, miqService, API) {
$scope.cloudSubnetModel = { name: '', ems_id: '', cloud_tenant_id: '', network_id: '' };
$scope.formId = cloudSubnetFormId;
$scope.afterGet = false;
$scope.modelCopy = angular.copy( $scope.cloudSubnetModel );
$scope.model = "cloudSubnetModel";
ManageIQ.angular.app.controller('cloudSubnetFormController', ['cloudSubnetFormId', 'miqService', 'API', function(cloudSubnetFormId, miqService, API) {
var vm = this;

ManageIQ.angular.scope = $scope;
var init = function() {
vm.afterGet = false;
vm.cloudSubnetModel = {
name: "",
ems_id: "",
cloud_tenant_id: "",
network_id: "",
};

if (cloudSubnetFormId == 'new') {
$scope.cloudSubnetModel.name = "";
$scope.cloudSubnetModel.dhcp_enabled = true;
$scope.cloudSubnetModel.network_protocol = '4';
$scope.newRecord = true;
} else {
miqService.sparkleOn();
API.get("/api/cloud_subnets/" + cloudSubnetFormId + "?expand=resources").then(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.network_protocol = data.network_protocol;
$scope.modelCopy = angular.copy( $scope.cloudSubnetModel );
miqService.sparkleOff();
}).catch(miqService.handleFailure);
}
vm.networkProtocols = ["ipv4", "ipv6"];
vm.formId = cloudSubnetFormId;
vm.model = "cloudSubnetModel";
vm.newRecord = cloudSubnetFormId === "new";
ManageIQ.angular.scope = vm;
vm.saveable = miqService.saveable;

$scope.addClicked = function() {
if (vm.newRecord) {
vm.cloudSubnetModel.dhcp_enabled = true;
vm.cloudSubnetModel.network_protocol = "ipv4";
vm.afterGet = true;
vm.modelCopy = angular.copy( vm.cloudSubnetModel );
} else {
miqService.sparkleOn();
API.get("/api/cloud_subnets/" + cloudSubnetFormId + "?expand=resources&attributes=ext_management_system.name,cloud_tenant.name,cloud_network.name").then(function(data) {
vm.cloudSubnetModel.name = data.name;
vm.cloudSubnetModel.ems_name = data.ext_management_system.name;
vm.cloudSubnetModel.tenant_name = angular.isDefined(data.cloud_tenant) ? data.cloud_tenant.name : undefined;
Copy link
Contributor

@himdel himdel Jun 28, 2017

Choose a reason for hiding this comment

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

FYI we're trying to remove angular.isDefined, this could be just data.cloud_tenant ? data.cloud_tenant.name : undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that was actually recommended recently, there are cases where the variable ends being undefined and would otherwise breaks.

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, if you need to check for definedness, use data.cloud_tenant !== undefined.

But since you just return undefined when cloud tenant is undefined, you don't need to check that.

This could also be written as data.cloud_tenant && data.cloud_tenant.name.

vm.cloudSubnetModel.network_name = data.cloud_network.name;
vm.cloudSubnetModel.dhcp_enabled = data.dhcp_enabled;
vm.cloudSubnetModel.cidr = data.cidr;
vm.cloudSubnetModel.gateway = data.gateway;
vm.cloudSubnetModel.network_protocol = data.network_protocol;
vm.afterGet = true;
vm.modelCopy = angular.copy( vm.cloudSubnetModel );
miqService.sparkleOff();
}).catch(miqService.handleFailure);
}
};

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

$scope.cancelClicked = function() {
vm.cancelClicked = function() {
if (cloudSubnetFormId == 'new') {
var url = '/cloud_subnet/create/new' + '?button=cancel';
} else {
Expand All @@ -40,28 +54,30 @@ ManageIQ.angular.app.controller('cloudSubnetFormController', ['$scope', 'cloudSu
miqService.miqAjaxButton(url);
};

$scope.saveClicked = function() {
vm.saveClicked = function() {
var url = '/cloud_subnet/update/' + cloudSubnetFormId + '?button=save';
miqService.miqAjaxButton(url, $scope.cloudSubnetModel, { complete: false });
miqService.miqAjaxButton(url, vm.cloudSubnetModel, { complete: false });
};

$scope.resetClicked = function() {
$scope.cloudSubnetModel = angular.copy( $scope.modelCopy );
$scope.angularForm.$setPristine(true);
vm.resetClicked = function(angularForm) {
vm.cloudSubnetModel = angular.copy( vm.modelCopy );
angularForm.$setPristine(true);
miqService.miqFlash("warn", "All changes have been reset");
};

$scope.filterNetworkManagerChanged = function(id) {
vm.filterNetworkManagerChanged = function(id) {
miqService.sparkleOn();
if (id) {
API.get("/api/cloud_networks?expand=resources&attributes=name,ems_ref&filter[]=ems_id=" + id).then(function(data) {
$scope.available_networks = data.resources;
vm.available_networks = data.resources;
}).catch(miqService.handleFailure);

miqService.getProviderTenants(function(data) {
$scope.available_tenants = data.resources;
vm.available_tenants = data.resources;
})(id);
}
miqService.sparkleOff();
};

init();
}]);
54 changes: 34 additions & 20 deletions app/controllers/cloud_subnet_controller.rb
Expand Up @@ -64,17 +64,24 @@ def create
@subnet = CloudSubnet.new
options = new_form_params
ems = ExtManagementSystem.find(options[:ems_id])
options.delete(:ems_id)
task_id = ems.create_cloud_subnet_queue(session[:userid], options)
if CloudSubnet.class_by_ems(ems).supports_create?
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")
if task_id.kind_of?(Integer)
initiate_wait_for_task(:task_id => task_id, :action => "create_finished")
else
javascript_flash(
:text => _("Cloud Subnet creation: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
:severity => :error,
:spinner_off => true
)
end
else
javascript_flash(
:text => _("Cloud Subnet creation: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
:severity => :error,
:spinner_off => true
)
@in_a_form = true
add_flash(_(CloudSubnet.unsupported_reason(:create)), :error)
drop_breadcrumb(:name => _("Add new Cloud Subnet "), :url => "/subnet/new")
javascript_flash
end
end
end
Expand Down Expand Up @@ -152,16 +159,23 @@ def update
cancel_action(_("Edit of Subnet \"%{name}\" was cancelled by the user") % {:name => @subnet.name})

when "save"
task_id = @subnet.update_cloud_subnet_queue(session[:userid], options)
if @subnet.supports_create?
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")
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
else
javascript_flash(
:text => _("Cloud Subnet update failed: Task start failed: ID [%{id}]") % {:id => task_id.to_s},
:severity => :error,
:spinner_off => true
)
add_flash(_("Couldn't initiate update of Cloud Subnet \"%{name}\": %{details}") % {
:name => @subnet.name,
:details => @subnet.unsupported_reason(:update)
}, :error)
end
end
end
Expand Down Expand Up @@ -215,7 +229,7 @@ def changed_form_params
end

def new_form_params
params[:network_protocol] ||= "4"
params[:network_protocol] ||= "ipv4"
params[:dhcp_enabled] ||= false
options = {}
options[:name] = params[:name] if params[:name]
Expand All @@ -225,8 +239,8 @@ def new_form_params
if params[:gateway]
options[:gateway] = params[:gateway].blank? ? nil : params[:gateway]
end
options[:ip_version] = params[:network_protocol]
options[:cloud_tenant] = find_record_with_rbac(CloudTenant, params[:cloud_tenant_id]) if params[:cloud_tenant_id]
options[:ip_version] = params[:network_protocol] =~ /4/ ? 4 : 6
options[:cloud_tenant] = find_record_with_rbac(CloudTenant, from_cid(params[:cloud_tenant_id])) if params[:cloud_tenant_id]
options[:network_id] = params[:network_id] if params[:network_id]
options[:enable_dhcp] = params[:dhcp_enabled]
# TODO: Add extra fields
Expand Down
54 changes: 36 additions & 18 deletions app/views/cloud_subnet/_common_new_edit.haml
Expand Up @@ -3,30 +3,48 @@
%label.col-md-2.control-label
= _('Subnet Name')
.col-md-8
%input.form-control{:type => "text",
:name => "name",
'ng-model' => "cloudSubnetModel.name",
'ng-maxlength' => 128,
:miqrequired => true,
:checkchange => true}
%input.form-control{"type" => "text",
"name" => "name",
"ng-model" => "vm.cloudSubnetModel.name",
"ng-maxlength" => 128}
.form-group
%label.col-md-2.control-label
= _('Gateway')
.col-md-8
%input.form-control{:type => "text",
:name => "gateway_ip",
'ng-model' => "cloudSubnetModel.gateway",
'ng-maxlength' => 20,
:miqrequired => false,
:checkchange => true}
%input.form-control{"type" => "text",
"name" => "gateway_ip",
"ng-model" => "vm.cloudSubnetModel.gateway",
"ng-maxlength" => 20}
.form-group
%label.control-label.col-md-2{"for" => "cloud_subnet_dhcp_enabled"}
= _('Enable DHCP')
.col-md-8
%input{"bs-switch" => "",
:data => {:on_text => _('Yes'), :off_text => _('No'), :size => 'mini'},
"type" => "checkbox",
"id" => "cloud_subnet_dhcp_enabled",
%input{"type" => "checkbox",
"name" => "dhcp_enabled",
"checkchange" => "true",
"ng-model" => "cloudSubnetModel.dhcp_enabled"}
"bs-switch" => "",
"data" => {:on_text => _('Yes'), :off_text => _('No'), :size => 'mini'},
"id" => "cloud_subnet_dhcp_enabled",
"ng-model" => "vm.cloudSubnetModel.dhcp_enabled"}
.form-group{"ng-class" => "{'has-error': angularForm.network_protocol.$invalid}"}
%label.col-md-2.control-label
= _('IP Version')
.col-md-8
%select{"name" => "network_protocol",
"ng-model" => "vm.cloudSubnetModel.network_protocol",
"required" => "",
"ng-disabled" => "!vm.newRecord",
"ng-options" => "protocol as protocol for protocol in vm.networkProtocols",
"pf-select" => true,
"selectpicker-for-select-tag" => ""}
%span.help-block{"ng-show" => "angularForm.network_protocol.$error.required"}
= _("Required")

.form-group
%label.col-md-2.control-label
= _('Subnet CIDR')
.col-md-8
%input.form-control{"type" => "text",
"name" => "cidr",
"ng-disabled" => "!vm.newRecord",
"ng-model" => "vm.cloudSubnetModel.cidr",
"ng-maxlength" => 20}
59 changes: 38 additions & 21 deletions app/views/cloud_subnet/edit.html.haml
@@ -1,33 +1,50 @@
%form#form_div{:name => "angularForm", 'ng-controller' => "cloudSubnetFormController"}
%form#form_div{"name" => "angularForm",
"form-changed" => true,
"miq-form" => true,
"model" => "vm.cloudSubnetModel",
"model-copy" => "vm.modelCopy",
"ng-cloak" => "",
"ng-controller" => "cloudSubnetFormController as vm",
"ng-show" => "vm.afterGet"}
= render :partial => "layouts/flash_msg"
%h3
= render :partial => "common_new_edit"

= _('Network Provider')
.form-horizontal
.form-group
%label.col-md-2.control-label
= _('IP Version')
= _('Network Manager')
.col-md-8
%input.form-control{:type => "text",
:name => "network_protocol",
'ng-model' => "cloudSubnetModel.network_protocol",
'ng-maxlength' => 2,
:miqrequired => true,
:disabled => true,
:checkchange => true}
%input.form-control{"type" => "text",
"name" => "ems_name",
"ng-model" => "vm.cloudSubnetModel.ems_name",
"ng-maxlength" => 128,
"readonly" => true}
%h3
= _('Placement')
.form-horizontal
.form-group
%label.col-md-2.control-label
= _('CIDR')
= _('Cloud Tenant')
.col-md-8
%input.form-control{:type => "text",
:name => "cidr",
'ng-model' => "cloudSubnetModel.cidr",
'ng-maxlength' => 20,
:miqrequired => false,
:disabled => true,
:checkchange => true}

= render :partial => "layouts/angular/x_edit_buttons_angular"
%input.form-control{"type" => "text",
"name" => "tenant_name",
"ng-model" => "vm.cloudSubnetModel.tenant_name",
"ng-maxlength" => 128,
"readonly" => true}
%h3
= _('Cloud Subnet details')
.form-horizontal
.form-group
%label.col-md-2.control-label
= _('Network')
.col-md-8
%input.form-control{"type" => "text",
"name" => "network_name",
"ng-model" => "vm.cloudSubnetModel.network_name",
"ng-maxlength" => 128,
"readonly" => true}
= render :partial => "common_new_edit"
= render :partial => "layouts/angular/generic_form_buttons"

:javascript
ManageIQ.angular.app.value('cloudSubnetFormId', '#{@subnet.id}');
Expand Down