diff --git a/awx/ui/client/src/adhoc/adhoc.controller.js b/awx/ui/client/src/adhoc/adhoc.controller.js new file mode 100644 index 0000000000..bb78c5564a --- /dev/null +++ b/awx/ui/client/src/adhoc/adhoc.controller.js @@ -0,0 +1,317 @@ +/************************************************* + * Copyright (c) 2015 Ansible, Inc. + * + * All Rights Reserved + *************************************************/ + +/** + * @ngdoc function + * @name controllers.function:Adhoc + * @description This controller controls the adhoc form creation, command launching and navigating to standard out after command has been succesfully ran. +*/ +function adhocController($q, $scope, $rootScope, $location, $routeParams, + CheckPasswords, PromptForPasswords, CreateLaunchDialog, adhocForm, + GenerateForm, Rest, ProcessErrors, ClearScope, GetBasePath, GetChoices, + KindChange, LookUpInit, CredentialList, Empty, Wait) { + + ClearScope(); + + // this is done so that we can access private functions for testing, but + // we don't want to populate the "public" scope with these internal + // functions + var privateFn = {}; + this.privateFn = privateFn; + + // note: put any urls that the controller will use in here!!!! + privateFn.setAvailableUrls = function() { + return { + adhocUrl: GetBasePath('inventory') + id + '/ad_hoc_commands/', + inventoryUrl: GetBasePath('inventory') + id + '/', + machineCredentialUrl: GetBasePath('credentials') + '?kind=ssh' + }; + }; + + var id = $routeParams.inventory_id, + urls = privateFn.setAvailableUrls(), + hostPattern = $rootScope.hostPatterns || "all"; + + // set the default options for the selects of the adhoc form + privateFn.setFieldDefaults = function(verbosity_options, forks_default) { + var verbosity; + for (verbosity in verbosity_options) { + if (verbosity_options[verbosity].isDefault) { + $scope.verbosity = verbosity_options[verbosity]; + } + } + $("#forks-number").spinner("value", forks_default); + $scope.forks = forks_default; + }; + + // set when "working" starts and stops + privateFn.setLoadingStartStop = function() { + var asyncHelper = {}, + formReadyPromise = 0; + + Wait('start'); + + if (asyncHelper.removeChoicesReady) { + asyncHelper.removeChoicesReady(); + } + asyncHelper.removeChoicesReady = $scope.$on('adhocFormReady', + isFormDone); + + // check to see if all requests have completed + function isFormDone() { + formReadyPromise++; + + if (formReadyPromise === 2) { + privateFn.setFieldDefaults($scope.adhoc_verbosity_options, + $scope.forks_field.default); + Wait('stop'); + } + } + }; + + privateFn.getInventoryNameForBreadcrumbs = function(url) { + + Rest.setUrl(url); + var promise = Rest.get(); + promise.then(function (response) { + $scope.inv_name = response.data.name; + }); + promise.catch(function (response) { + ProcessErrors($rootScope, response.data, response.status, null, { + hdr: 'Error!', + msg: 'Failed to get inventory name. GET returned status: ' + + response.status }); + $location.path("/inventories/"); + }); + return promise; + }; + + // set the arguments help to watch on change of the module + privateFn.instantiateArgumentHelp = function() { + $scope.$watch('module_name', function(val) { + if (val) { + // give the docs for the selected module in the popover + $scope.argsPopOver = '
These arguments are used with the ' + + 'specified module. You can find information about the ' + + val.value + ' module here.
'; + } else { + // no module selected + $scope.argsPopOver = "These arguments are used with the" + + " specified module.
"; + } + }, true); + + // initially set to the same as no module selected + $scope.argsPopOver = "These arguments are used with the " + + "specified module.
"; + }; + + // pre-populate host patterns from the inventory page and + // delete the value off of rootScope + privateFn.instantiateHostPatterns = function(hostPattern) { + $scope.limit = hostPattern; + $scope.providedHostPatterns = $scope.limit; + delete $rootScope.hostPatterns; + }; + + // call helpers to initialize lookup and select fields through get + // requests + privateFn.initializeFields = function(machineCredentialUrl, adhocUrl) { + // setup machine credential lookup + LookUpInit({ + url: machineCredentialUrl, + scope: $scope, + form: adhocForm, + current_item: (!Empty($scope.credential_id)) ? + $scope.credential_id : null, + list: CredentialList, + field: 'credential', + input_type: 'radio' + }); + + // setup module name select + GetChoices({ + scope: $scope, + url: adhocUrl, + field: 'module_name', + variable: 'adhoc_module_options', + callback: 'adhocFormReady' + }); + + // setup verbosity options select + GetChoices({ + scope: $scope, + url: adhocUrl, + field: 'verbosity', + variable: 'adhoc_verbosity_options', + callback: 'adhocFormReady' + }); + }; + + // instantiate all variables on scope for display in the partial + privateFn.initializeForm = function(id, urls, hostPattern) { + // inject the adhoc command form + GenerateForm.inject(adhocForm, + { mode: 'edit', related: true, scope: $scope }); + + // set when "working" starts and stops + privateFn.setLoadingStartStop(); + + // put the inventory id on scope for the partial to use + $scope.inv_id = id; + + // get the inventory name + privateFn.getInventoryNameForBreadcrumbs(urls.inventoryUrl); + + // set the arguments help to watch on change of the module + privateFn.instantiateArgumentHelp(); + + // pre-populate host patterns from the inventory page and + // delete the value off of rootScope + privateFn.instantiateHostPatterns(hostPattern); + + privateFn.initializeFields(urls.machineCredentialUrl, urls.adhocUrl); + }; + + privateFn.initializeForm(id, urls, hostPattern); + + // remove all data input into the form and reset the form back to defaults + $scope.formReset = function () { + GenerateForm.reset(); + + // pre-populate host patterns from the inventory page and + // delete the value off of rootScope + privateFn.instantiateHostPatterns($scope.providedHostPatterns); + + KindChange({ scope: $scope, form: adhocForm, reset: false }); + + // set the default options for the selects of the adhoc form + privateFn.setFieldDefaults($scope.adhoc_verbosity_options, + $scope.forks_default); + }; + + // launch the job with the provided form data + $scope.launchJob = function () { + var adhocUrl = GetBasePath('inventory') + $routeParams.inventory_id + + '/ad_hoc_commands/', fld, data={}, html; + + html = '