Merge "bug fix: reporting Tempest"
[releng.git] / utils / test / testapi / 3rd_party / static / testapi-ui / components / pods / podsController.js
1 /*
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
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
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.
13  */
14
15 (function () {
16     'use strict';
17
18     angular
19         .module('testapiApp')
20         .controller('PodsController', PodsController);
21
22     PodsController.$inject = [
23         '$scope', '$http', '$filter', '$state', 'testapiApiUrl','raiseAlert'
24     ];
25
26     /**
27      * TestAPI Pods Controller
28      * This controller is for the '/pods' page where a user can browse
29      * through pods declared in TestAPI.
30      */
31     function PodsController($scope, $http, $filter, $state, testapiApiUrl,
32         raiseAlert) {
33         var ctrl = this;
34         ctrl.url = testapiApiUrl + '/pods';
35
36         ctrl.create = create;
37         ctrl.update = update;
38         ctrl.open = open;
39         ctrl.clearFilters = clearFilters;
40
41         ctrl.roles = ['community-ci', 'production-ci'];
42         ctrl.modes = ['metal', 'virtual'];
43         ctrl.createRequirements = [
44             {label: 'name', type: 'text', required: true},
45             {label: 'mode', type: 'select', selects: ctrl.modes},
46             {label: 'role', type: 'select', selects: ctrl.roles},
47             {label: 'details', type: 'textarea', required: false}
48         ];
49
50         ctrl.name = '';
51         ctrl.role = 'community-ci';
52         ctrl.mode = 'metal';
53         ctrl.details = '';
54
55         ctrl.pageHeader = 'Pods';
56         ctrl.pageParagraph = 'This page is used to create or query pods.';
57
58         /**
59          * This is called when the date filter calendar is opened. It
60          * does some event handling, and sets a scope variable so the UI
61          * knows which calendar was opened.
62          * @param {Object} $event - The Event object
63          * @param {String} openVar - Tells which calendar was opened
64          */
65         function open($event, openVar) {
66             $event.preventDefault();
67             $event.stopPropagation();
68             ctrl[openVar] = true;
69         }
70
71         /**
72          * This function will clear all filters and update the results
73          * listing.
74          */
75         function clearFilters() {
76             ctrl.update();
77         }
78
79         /**
80          * This will contact the TestAPI to create a new pod.
81          */
82         function create() {
83             ctrl.showError = false;
84
85             if(ctrl.name != ""){
86                 var pods_url = ctrl.url;
87                 var body = {
88                     name: ctrl.name,
89                     mode: ctrl.mode,
90                     role: ctrl.role,
91                     details: ctrl.details
92                 };
93                 ctrl.podsRequest =
94                     $http.post(pods_url, body).error(function (error) {
95                         ctrl.showError = true;
96                         ctrl.error =
97                             'Error creating the new pod from server: ' +
98                             angular.toJson(error);
99                     });
100             }
101             else{
102                 ctrl.showError = true;
103                         ctrl.error = 'Name is missing.'
104             }
105         }
106
107         /**
108          * This will contact the TestAPI to get a listing of declared pods.
109          */
110         function update() {
111             ctrl.showError = false;
112             ctrl.podsRequest =
113                 $http.get(ctrl.url).success(function (data) {
114                     ctrl.data = data;
115                 }).error(function (error) {
116                     ctrl.data = null;
117                     ctrl.showError = true;
118                     ctrl.error =
119                         'Error retrieving pods from server: ' +
120                         angular.toJson(error);
121                 });
122         }
123     }
124 })();