2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
6 * http://www.apache.org/licenses/LICENSE-2.0
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
20 .controller('GuidelinesController', GuidelinesController);
22 GuidelinesController.$inject = ['$http', '$uibModal', 'testapiApiUrl'];
25 * TestAPI Guidelines Controller
26 * This controller is for the '/guidelines' page where a user can browse
27 * through tests belonging to Interop WG defined capabilities.
29 function GuidelinesController($http, $uibModal, testapiApiUrl) {
32 ctrl.getVersionList = getVersionList;
34 ctrl.updateTargetCapabilities = updateTargetCapabilities;
35 ctrl.filterStatus = filterStatus;
36 ctrl.getObjectLength = getObjectLength;
37 ctrl.openTestListModal = openTestListModal;
39 /** The target OpenStack marketing program to show capabilities for. */
40 ctrl.target = 'platform';
42 /** The various possible capability statuses. */
51 * The template to load for displaying capability details.
53 ctrl.detailsTemplate = 'components/guidelines/partials/' +
54 'guidelineDetails.html';
57 * Retrieve an array of available guideline files from the TestAPI
58 * API server, sort this array reverse-alphabetically, and store it in
59 * a scoped variable. The scope's selected version is initialized to
60 * the latest (i.e. first) version here as well. After a successful API
61 * call, the function to update the capabilities is called.
62 * Sample API return array: ["2015.03.json", "2015.04.json"]
64 function getVersionList() {
65 var content_url = testapiApiUrl + '/guidelines';
66 ctrl.versionsRequest =
67 $http.get(content_url).success(function (data) {
68 ctrl.versionList = data.sort().reverse();
69 // Default to the first approved guideline which is expected
71 ctrl.version = ctrl.versionList[1];
73 }).error(function (error) {
74 ctrl.showError = true;
75 ctrl.error = 'Error retrieving version list: ' +
76 angular.toJson(error);
81 * This will contact the TestAPI API server to retrieve the JSON
82 * content of the guideline file corresponding to the selected
86 var content_url = testapiApiUrl + '/guidelines/' + ctrl.version;
88 $http.get(content_url).success(function (data) {
89 ctrl.guidelines = data;
90 ctrl.updateTargetCapabilities();
91 }).error(function (error) {
92 ctrl.showError = true;
93 ctrl.guidelines = null;
94 ctrl.error = 'Error retrieving guideline content: ' +
95 angular.toJson(error);
100 * This will update the scope's 'targetCapabilities' object with
101 * capabilities belonging to the selected OpenStack marketing program
102 * (programs typically correspond to 'components' in the Interop WG
103 * schema). Each capability will have its status mapped to it.
105 function updateTargetCapabilities() {
106 ctrl.targetCapabilities = {};
107 var components = ctrl.guidelines.components;
108 var targetCaps = ctrl.targetCapabilities;
110 // The 'platform' target is comprised of multiple components, so
111 // we need to get the capabilities belonging to each of its
113 if (ctrl.target === 'platform') {
114 var platform_components = ctrl.guidelines.platform.required;
116 // This will contain status priority values, where lower
117 // values mean higher priorities.
125 // For each component required for the platform program.
126 angular.forEach(platform_components, function (component) {
127 // Get each capability list belonging to each status.
128 angular.forEach(components[component],
129 function (caps, status) {
130 // For each capability.
131 angular.forEach(caps, function(cap) {
132 // If the capability has already been added.
133 if (cap in targetCaps) {
134 // If the status priority value is less
135 // than the saved priority value, update
137 if (statusMap[status] <
138 statusMap[targetCaps[cap]]) {
139 targetCaps[cap] = status;
143 targetCaps[cap] = status;
150 angular.forEach(components[ctrl.target],
151 function (caps, status) {
152 angular.forEach(caps, function(cap) {
153 targetCaps[cap] = status;
160 * This filter will check if a capability's status corresponds
161 * to a status that is checked/selected in the UI. This filter
162 * is meant to be used with the ng-repeat directive.
163 * @param {Object} capability
164 * @returns {Boolean} True if capability's status is selected
166 function filterStatus(capability) {
167 var caps = ctrl.targetCapabilities;
168 return (ctrl.status.required &&
169 caps[capability.id] === 'required') ||
170 (ctrl.status.advisory &&
171 caps[capability.id] === 'advisory') ||
172 (ctrl.status.deprecated &&
173 caps[capability.id] === 'deprecated') ||
174 (ctrl.status.removed &&
175 caps[capability.id] === 'removed');
179 * This function will get the length of an Object/dict based on
180 * the number of keys it has.
181 * @param {Object} object
182 * @returns {Number} length of object
184 function getObjectLength(object) {
185 return Object.keys(object).length;
189 * This will open the modal that will show a list of all tests
190 * belonging to capabilities with the selected status(es).
192 function openTestListModal() {
194 templateUrl: '/components/guidelines/partials' +
195 '/testListModal.html',
197 windowClass: 'modal',
199 controller: 'TestListModalController as modal',
202 version: function () {
203 return ctrl.version.slice(0, -5);
205 target: function () {
208 status: function () {
215 ctrl.getVersionList();
219 .module('testapiApp')
220 .controller('TestListModalController', TestListModalController);
222 TestListModalController.$inject = [
223 '$uibModalInstance', '$http', 'version',
224 'target', 'status', 'testapiApiUrl'
228 * Test List Modal Controller
229 * This controller is for the modal that appears if a user wants to see the
230 * test list corresponding to Interop WG capabilities with the selected
233 function TestListModalController($uibModalInstance, $http, version,
234 target, status, testapiApiUrl) {
238 ctrl.version = version;
239 ctrl.target = target;
240 ctrl.status = status;
242 ctrl.updateTestListString = updateTestListString;
245 ctrl.flagged = false;
247 // Check if the API URL is absolute or relative.
248 if (testapiApiUrl.indexOf('http') > -1) {
249 ctrl.url = testapiApiUrl;
252 ctrl.url = location.protocol + '//' + location.host +
257 * This function will close/dismiss the modal.
260 $uibModalInstance.dismiss('exit');
264 * This function will return a list of statuses based on which ones
267 function getStatusList() {
269 angular.forEach(ctrl.status, function(value, key) {
271 statusList.push(key);
278 * This will get the list of tests from the API and update the
279 * controller's test list string variable.
281 function updateTestListString() {
282 var statuses = getStatusList();
283 if (!statuses.length) {
284 ctrl.error = 'No tests matching selected criteria.';
288 ctrl.url, '/guidelines/', ctrl.version, '/tests?',
289 'target=', ctrl.target, '&',
290 'type=', statuses.join(','), '&',
291 'alias=', ctrl.aliases.toString(), '&',
292 'flag=', ctrl.flagged.toString()
294 ctrl.testListRequest =
295 $http.get(ctrl.testListUrl).
296 then(function successCallback(response) {
298 ctrl.testListString = response.data;
299 if (!ctrl.testListString) {
300 ctrl.testListCount = 0;
304 ctrl.testListString.split('\n').length;
306 }, function errorCallback(response) {
307 ctrl.testListString = null;
308 ctrl.testListCount = null;
309 if (angular.isObject(response.data) &&
310 response.data.message) {
311 ctrl.error = 'Error retrieving test list: ' +
312 response.data.message;
315 ctrl.error = 'Unknown error retrieving test list.';
320 updateTestListString();