Skip to content

Commit

Permalink
Add import/export buttons & functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
jlrpnbbngtn committed Jan 18, 2022
1 parent f899c76 commit e014106
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 66 deletions.
10 changes: 9 additions & 1 deletion src/ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ import {
} from './js/controllers/admin_role.js';
import adminGardenController from './js/controllers/admin_garden.js';
import adminGardenViewController from './js/controllers/admin_garden_view.js';
import {
gardenConfigImportController,
gardenConfigImportModalController,
} from './js/controllers/garden/import_garden_config';
import gardenConfigExportController from './js/controllers/garden/export_garden_config';
import commandIndexController from './js/controllers/command_index.js';
import commandViewController from './js/controllers/command_view.js';
import requestIndexController from './js/controllers/request_index.js';
Expand Down Expand Up @@ -210,7 +215,7 @@ angular
.controller('AdminQueueController', adminQueueController)
.controller(
'AdminSystemForceDeleteController',
adminSystemForceDeleteController
adminSystemForceDeleteController,
)
.controller('AdminSystemController', adminSystemController)
.controller('AdminSystemLogsController', adminSystemLogsController)
Expand All @@ -220,6 +225,9 @@ angular
.controller('NewRoleController', newRoleController)
.controller('AdminGardenController', adminGardenController)
.controller('AdminGardenViewController', adminGardenViewController)
.controller('GardenConfigExportController', gardenConfigExportController)
.controller('GardenConfigImportController', gardenConfigImportController)
.controller('GardenConfigImportModalController', gardenConfigImportModalController)
.controller('CommandIndexController', commandIndexController)
.controller('CommandViewController', commandViewController)
.controller('RequestIndexController', requestIndexController)
Expand Down
38 changes: 31 additions & 7 deletions src/ui/src/js/controllers/admin_garden_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,21 @@ export default function adminGardenViewController(
$scope.$broadcast('schemaFormRedraw');
};

$scope.updateModelFromImport = (newGardenDefinition) => {
const existingData = $scope.data;
const newConnType = newGardenDefinition['connection_type'];
const newConnParams = newGardenDefinition['connection_params'];

const newData = {...existingData,
connection_type: newConnType,
connection_params: newConnParams};

$scope.data = newData;

generateGardenSF();
};

$scope.successCallback = function(response) {
console.log('response', response);
$scope.response = response;
$scope.data = response.data;

Expand Down Expand Up @@ -121,8 +134,11 @@ export default function adminGardenViewController(
(entryPoint, fieldName) => `schemaForm.error.${entryPoint}.${fieldName}`;

const fieldErrorMessage =
(errorObject) =>
typeof errorObject === 'string' ? Array(errorObject) : errorObject;
(errorObject) => {
const error = typeof errorObject === 'string' ?
Array(errorObject) : errorObject;
return error[0];
};

const updateValidationMessages = (entryPoint, errorsObject) => {
for (const fieldName in errorsObject) {
Expand All @@ -144,8 +160,8 @@ export default function adminGardenViewController(
*/
if (response['data'] && response['data']['message']) {
const messageData = String(response['data']['message']);
console.log('MESSG', messageData, 'TYPE', typeof messageData);
const cleanedMessages = messageData.replace(/'/g, '"');
const singleQuoteRegExp = new RegExp('\'', 'g');
const cleanedMessages = messageData.replace(singleQuoteRegExp, '"');
const messages = JSON.parse(cleanedMessages);

if ('connection_params' in messages) {
Expand All @@ -171,8 +187,14 @@ export default function adminGardenViewController(
}
};

const clearScopeAlerts = () => {
while ($scope.alerts.length) {
$scope.alerts.pop();
}
};

$scope.submitGardenForm = function(form, model) {
$scope.alerts = [];
clearScopeAlerts();
resetAllValidationErrors();
$scope.$broadcast('schemaFormValidate');

Expand All @@ -183,6 +205,8 @@ export default function adminGardenViewController(
updatedGarden =
GardenService.formToServerModel($scope.data, model);
} catch (e) {
console.log(e);

$scope.alerts.push({
type: 'warning',
msg: e,
Expand All @@ -191,7 +215,7 @@ export default function adminGardenViewController(

if (updatedGarden) {
GardenService.updateGardenConfig(updatedGarden).then(
_.noop,
() => console.log('Garden update saved successfully'),
$scope.addErrorAlert,
);
}
Expand Down
51 changes: 51 additions & 0 deletions src/ui/src/js/controllers/garden/export_garden_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
gardenConfigExportController.$inject =
['$scope', '$rootScope', '$filter', 'GardenService'];

/**
* gardenConfigExportController - Controller for the garden config export page.
* @param {Object} $scope Angular's $scope object.
* @param {Object} $rootScope Angular's $rootScope object.
* @param {Object} $filter Filter
* @param {Object} GardenService Beer-Garden's garden service.
*/
export default function gardenConfigExportController(
$scope,
$rootScope,
$filter,
GardenService,
) {
$scope.response = $rootScope.sysResponse;

$scope.exportGardenConfig = (gardenName) => {
const filename =
`GardenExport_${gardenName}_` +
$filter('date')(new Date(Date.now()), 'yyyyMMdd_HHmmss');

const formModel = GardenService.formToServerModel($scope.data, $scope.gardenModel);

const [newConnectionInfo, newConnectionParams] = [{}, {}];
newConnectionInfo['connection_type'] = formModel['connection_type'];

if (formModel['connection_params']['http']) {
newConnectionParams['http'] = formModel['connection_params']['http'];
}

if (formModel['connection_params']['stomp']) {
newConnectionParams['stomp'] = formModel['connection_params']['stomp'];
}

newConnectionInfo['connection_params'] = newConnectionParams;

const blob = new Blob(
[JSON.stringify(newConnectionInfo)],
{
type: 'application/json;charset=utf-8',
},
);
const downloadLink = angular.element('<a></a>');

downloadLink.attr('href', window.URL.createObjectURL(blob));
downloadLink.attr('download', filename);
downloadLink[0].click();
};
}
101 changes: 101 additions & 0 deletions src/ui/src/js/controllers/garden/import_garden_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import template from '../../../templates/import_garden_config.html';

gardenConfigImportController.$inject = [
'$scope',
'$rootScope',
'$uibModal',
'$state',
];

/**
* gardenConfigImportController - Controller for garden config import.
* @param {Object} $scope Angular's $scope object.
* @param {Object} $rootScope Angular's $rootScope object.
* @param {Object} $uibModal Angular UI's $uibModal object.
* @param {Object} $state State
*/
export function gardenConfigImportController(
$scope,
$rootScope,
$uibModal,
$state,
) {
$scope.response = $rootScope.sysResponse;

$scope.openImportGardenConfigPopup = () => {
const popupInstance = $uibModal.open({
animation: true,
controller: 'GardenConfigImportModalController',
template: template,
});

popupInstance.result.then((resolvedResponse) => {
const jsonFileContents = resolvedResponse.jsonFileContents;
const newConfig = JSON.parse(jsonFileContents);

$scope.updateModelFromImport(newConfig);
}, angular.noop);
};
}

gardenConfigImportModalController.$inject =
['$scope', '$window', '$uibModalInstance'];

/**
* gardenConfigImportModalController - Controller for the garden config import
* popup window.
*
* @param {Object} $scope Angular's $scope object.
* @param {Object} $window Object for the browser window.
* @param {Object} $uibModalInstance Object for the modal popup window.
*/
export function gardenConfigImportModalController(
$scope, $window, $uibModalInstance) {
$scope.import = {};
$scope.fileName = undefined;
$scope.fileContents = undefined;
$scope.fileIsGoodJson = true;

$scope.inputClicker = function() {
$window.document.getElementById('fileSelectHiddenControl').click();
};

$scope.doImport = function() {
$scope.import['jsonFileContents'] = $scope.fileContents;
$uibModalInstance.close($scope.import);
};

$scope.cancelImport = function() {
$uibModalInstance.dismiss('cancel');
};

function isParsableJson(string) {
try {
JSON.parse(string);
} catch (e) {
return false;
}
return true;
}

$scope.onFileSelected = function(event) {
const theFile = event.target.files[0];
$scope.fileName = theFile.name;

const reader = new FileReader();
reader.onload = function(e) {
$scope.$apply(function() {
const result = reader.result;
const isGoodJson = isParsableJson(result);

if (isGoodJson) {
$scope.fileContents = result;
$scope.fileIsGoodJson = true;
} else {
$scope.fileIsGoodJson = false;
}
});
};
reader.readAsText(theFile);
};
}
Loading

0 comments on commit e014106

Please sign in to comment.