{"size": 3070, "ext": "js", "max_stars_repo_path": "assets/app/features/upgrade/controllers/upgrade-openstack-backup.controller.js", "max_stars_repo_name": "Openstack-projects/crowbar-ui", "max_stars_repo_stars_event_min_datetime": "2016-09-14T12:53:29.000Z", "max_stars_repo_stars_event_max_datetime": "2019-05-07T00:44:02.000Z", "max_issues_repo_path": "assets/app/features/upgrade/controllers/upgrade-openstack-backup.controller.js", "max_issues_repo_name": "crowbar/crowbar-ui", "max_issues_repo_issues_event_min_datetime": "2016-09-14T11:27:02.000Z", "max_issues_repo_issues_event_max_datetime": "2021-01-18T12:32:58.000Z", "max_forks_repo_path": "assets/app/features/upgrade/controllers/upgrade-openstack-backup.controller.js", "max_forks_repo_name": "Openstack-projects/crowbar-ui", "max_forks_repo_forks_event_min_datetime": "2016-10-10T08:11:59.000Z", "max_forks_repo_forks_event_max_datetime": "2020-03-05T13:10:20.000Z"}
{"max_stars_count": 5, "max_issues_count": 122, "max_forks_count": 10, "avg_line_length": 31.0101010101, "max_line_length": 90, "alphanum_fraction": 0.5882736156}
(function() {
    'use strict';

    /**
    * @ngdoc function
    * @name crowbarApp.upgrade.controller:UpgradeOpenStackBackupController
    * @description
    * # UpgradeOpenStackBackupController
    * This is the controller used on the OpenStack Backup page
    */
    angular.module('crowbarApp.upgrade')
        .controller('UpgradeOpenStackBackupController', UpgradeOpenStackBackupController);

    UpgradeOpenStackBackupController.$inject = [
        'upgradeFactory',
        'upgradeStepsFactory',
        'upgradeStatusFactory',
        'UPGRADE_STEPS',
        'UNEXPECTED_ERROR_DATA',
        'OPENSTACK_BACKUP_TIMEOUT_INTERVAL',
    ];
    // @ngInject
    function UpgradeOpenStackBackupController(
        upgradeFactory,
        upgradeStepsFactory,
        upgradeStatusFactory,
        UPGRADE_STEPS,
        UNEXPECTED_ERROR_DATA,
        OPENSTACK_BACKUP_TIMEOUT_INTERVAL
    ) {
        var vm = this;

        vm.openStackBackup = {
            completed: false,
            running: false,
            spinnerVisible: false,
            createBackup: createBackup,
        };

        activate();

        function activate() {
            upgradeStatusFactory.syncStatusFlags(
                UPGRADE_STEPS.backup_openstack, vm.openStackBackup,
                waitForBackupToEnd, createBackupSuccess, createBackupError
            );
        }

        /**
         * Trigger creation of OpenStack backup
         */
        function createBackup() {
            vm.openStackBackup.running = true;

            upgradeFactory.createOpenstackBackup()
                .then(
                    waitForBackupToEnd,
                    createBackupError
                );
        }

        /**
         * Start polling for status and wait until backup is created
         */
        function waitForBackupToEnd() {
            upgradeStatusFactory.waitForStepToEnd(
                UPGRADE_STEPS.backup_openstack,
                OPENSTACK_BACKUP_TIMEOUT_INTERVAL,
                createBackupSuccess,
                createBackupError
            );
        }

        function createBackupSuccess(response) {
            vm.openStackBackup.running = false;
            vm.openStackBackup.completed = true;

            vm.openStackBackup.backupPath = response.data.openstack_backup;

            // don't enable "Next" if another step is already active
            if (upgradeStepsFactory.activeStep.state === 'upgrade.openstack-backup') {
                upgradeStepsFactory.setCurrentStepCompleted()
            }
        }

        function createBackupError(errorResponse) {
            vm.openStackBackup.running = false;
            // Expose the error list
            if (angular.isDefined(errorResponse.data.errors)) {
                vm.errors = errorResponse.data;
            } else if (angular.isDefined(errorResponse.data.steps)) {
                vm.errors = { errors: errorResponse.data.steps.backup_openstack.errors };
            } else {
                vm.errors = UNEXPECTED_ERROR_DATA;
            }
        }
    }
})();
