Refactor BasePortController

Move the LocalLinkConnection object into a separate external
LocalLinkConnectionMgr class. I believe replacing the inline object
with an external class makes the controller code more readable
and maintainable.

Change-Id: Ied1ad3f7b0d34ea42df4f94d3661d7775dc71573
This commit is contained in:
Peter Piela 2017-06-15 15:30:07 -04:00
parent 5bc2aba67d
commit 5e1b372590
4 changed files with 87 additions and 64 deletions

View File

@ -31,7 +31,7 @@
];
/**
* Utility class for managing form fields
* @description Utility class for managing form fields
*
* @param {object} args - Valid properties are:
* value - Initial value of the field
@ -92,6 +92,83 @@
};
}
/**
* @description Utility class used to manage local-link-connection
* form fields.
*
* @param {string} validMacAddressPattern - Regular expression
* pattern used to test for valid mac addresses.
* @param {string} validDatapathIdPattern - Regular expression
* pattern used to test for valid datapath ids.
* @return {void}
*/
function LocalLinkConnectionMgr(validMacAddressPattern,
validDatapathIdPattern) {
this.port_id = new Field({});
this.switch_id = new Field({
desc: gettext("MAC address or OpenFlow datapath ID"),
pattern: validMacAddressPattern + '|' + validDatapathIdPattern});
this.switch_info = new Field({});
this.fields = {
port_id: this.port_id,
switch_id: this.switch_id,
switch_info: this.switch_info
};
/**
* Update the required property of each field based on current values
*
* @return {void}
*/
this.update = function() {
var required = this.port_id.hasValue() || this.switch_id.hasValue();
this.port_id.required = required;
this.switch_id.required = required;
};
/**
* Generate an attribute object that conforms to the format
* required for port creation using the Ironic client
*
* @return {object|null} local_link_connection attribute object.
* A value of null is returned if the local-link-connection
* information is incomplete.
*/
this.toPortAttr = function() {
var attr = null;
if (this.port_id.hasValue() &&
this.switch_id.hasValue()) {
attr = {};
attr.port_id = this.port_id.value;
attr.switch_id = this.switch_id.value;
if (this.switch_info.hasValue()) {
attr.switch_info = this.switch_info.value;
}
}
return attr;
};
/**
* dis/enable the local-link-connection form fields
*
* @param {boolean} disabled - True if the local-link-connection form
* fields should be disabled
* @param {string} reason - Optional reason for the state change
* @return {void}
*/
this.setDisabled = function(disabled, reason) {
angular.forEach(this.fields, function(field) {
field.disabled = disabled;
field.info = reason;
});
};
}
function BasePortController($uibModalInstance,
validMacAddressPattern,
validDatapathIdPattern,
@ -104,64 +181,9 @@
ctrl.pxeEnabled = new Field({value: 'True'});
// Object used to manage local-link-connection form fields
ctrl.localLinkConnection = {
port_id: new Field({}),
switch_id: new Field({
desc: gettext("MAC address or OpenFlow datapath ID"),
pattern: validMacAddressPattern + '|' + validDatapathIdPattern}),
switch_info: new Field({}),
/**
* Update the required property of each field based on current values
*
* @return {void}
*/
$update: function() {
var required = this.port_id.hasValue() || this.switch_id.hasValue();
this.port_id.required = required;
this.switch_id.required = required;
},
/**
* Generate an attribute object that conforms to the format
* required for port creation using the Ironic client
*
* @return {object} local_link_connection attribute object.
* A value of null is returned if the local-link-connection
* information is incomplete.
*/
$toPortAttr: function() {
var attr = {};
if (this.port_id.hasValue() &&
this.switch_id.hasValue()) {
attr.port_id = this.port_id.value;
attr.switch_id = this.switch_id.value;
if (this.switch_info.hasValue()) {
attr.switch_info = this.switch_info.value;
}
}
return attr;
},
/**
* dis/enable the local-link-connection form fields
*
* @param {boolean} disabled - True if the local-link-connection form
* fields should be disabled
* @param {string} reason - Optional reason for the state change
* @return {void}
*/
$setDisabled: function(disabled, reason) {
angular.forEach(this, function(item) {
if (item instanceof Field) {
item.disabled = disabled;
item.info = reason;
}
});
}
};
ctrl.localLinkConnection =
new LocalLinkConnectionMgr(validMacAddressPattern,
validDatapathIdPattern);
/**
* Cancel the modal

View File

@ -61,7 +61,8 @@
class="well well-sm">
<h4 translate>Local link connection</h4>
<div class="form-group"
ng-repeat="(propertyName, propertyObj) in ctrl.localLinkConnection"
ng-repeat="(propertyName, propertyObj) in
ctrl.localLinkConnection.fields"
ng-class="{'has-error': LocalLinkConnectionForm.{$ propertyName $}.$invalid &&
LocalLinkConnectionForm.{$ propertyName $}.$dirty}">
<label class="control-label"
@ -87,7 +88,7 @@
placeholder="{$ propertyObj.desc $}"
ng-pattern="propertyObj.pattern"
ng-disabled="propertyObj.disabled"
ng-change="ctrl.localLinkConnection.$update()"/>
ng-change="ctrl.localLinkConnection.update()"/>
</div>
</form>

View File

@ -56,7 +56,7 @@
var port = angular.copy(ctrl.port);
port.node_uuid = node.id;
var attr = ctrl.localLinkConnection.$toPortAttr();
var attr = ctrl.localLinkConnection.toPortAttr();
if (attr) {
port.local_link_connection = attr;
}

View File

@ -80,7 +80,7 @@
});
if (cannotEditConnectivityAttr) {
ctrl.localLinkConnection.$setDisabled(
ctrl.localLinkConnection.setDisabled(
true,
UNABLE_TO_UPDATE_CONNECTIVITY_ATTR_MSG);
}
@ -102,7 +102,7 @@
ctrl.pxeEnabled.value,
"/pxe_enabled");
patcher.buildPatch(port.local_link_connection,
ctrl.localLinkConnection.$toPortAttr(),
ctrl.localLinkConnection.toPortAttr(),
"/local_link_connection");
patcher.buildPatch(port.extra, ctrl.port.extra, "/extra");