dff938a2755070a6e2b03e148abd7aec3a6b8188
[portal.git] / ecomp-portal-FE-common / client / app / views / microservice-onboarding / microservice-add-details / microservice-add-details.js
1 /*-
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 'use strict';
21 (function () {
22     class MicroserviceAddDetailsCtrl {
23         constructor($scope, $log, $interval, widgetsCatalogService, applicationsService, adminsService, microserviceService, errorMessageByCode, ECOMP_URL_REGEX, $window,userProfileService, confirmBoxService, $cookies,items) {
24             
25                 $scope.ngDialogData=items;
26             let getAvailableApps = () => {       
27                 applicationsService.getAppsForSuperAdminAndAccountAdmin().then(apps => {
28                         this.availableApps=[];
29                         apps.unshift({
30                         id: 1,
31                         name: "ECOMP Portal"
32                     });
33                         for(var i = 0; i < apps.length; i++) {
34                         this.availableApps.push({
35                             id: apps[i].id,
36                             name: apps[i].name
37                         });
38                         if(this.isEditMode == true && this.service.appId == apps[i].id){
39                                 this.service.application = this.availableApps[i];
40                         }
41                     }
42                 }).catch(err => {
43                     $log.error(err);
44                 });
45             };
46             
47             let getAvailableWidgets = () => {       
48                 microserviceService.getWidgetListByService(this.service.id).then(widgets =>{
49                         this.availableWidgets = [];
50                         for(var i = 0; i < widgets.length; i++){
51                                 this.availableWidgets.push({
52                                         name: widgets[i]
53                                 })
54                         }
55                 }).catch(err => {
56                     $log.error(err);
57                 });
58             };
59             
60             
61             let getAvailableSecurityTypes = () => {   
62                 this.availableSecurityTypes = [];
63                 this.availableSecurityTypes.push({
64                         id: 0,
65                         name: 'No Authentication'
66                 });
67                 this.availableSecurityTypes.push({
68                         id: 1,
69                         name: 'Basic Authentication'
70                 });
71                 this.availableSecurityTypes.push({
72                         id: 2,
73                         name: 'Cookie based Authentication'
74                 });
75             }
76
77             let init = () => {
78                 $log.info('MicroserviceAddDetailsCtrl::init');
79                 this.service = [];
80                 this.availableApps=[];
81                 this.service.parameterList = [];
82                 this.service.active = true;
83                 this.emptyServiceName = false;
84                 this.emptyServiceDesc = false;
85                 this.emptyServiceURL = false;
86                 this.emptyServiceApp = false;
87                 this.dupliateName = false;
88                 this.serviceList = $scope.ngDialogData.list;
89                 
90                 if ($scope.ngDialogData && $scope.ngDialogData.service) {
91                         
92                     this.isEditMode = true;
93                     this.service = _.clone($scope.ngDialogData.service);
94                     
95                     this.originalName  = this.service.name;
96                     if(this.service.active == 'Y')
97                         this.service.active = true;
98                     else
99                         this.service.active = false;
100                 } else {
101                     this.isEditMode = false;
102                 } 
103                 getAvailableApps();
104                 getAvailableSecurityTypes();
105                 //getAvailableWidgets();
106                 
107                 /**
108                                  * 0: Basic Authentication
109                                  * 
110                                  * TODO: change the structure
111                                  */
112                 if(this.service.securityType == "No Authentication"){
113                         this.service.security = this.availableSecurityTypes[0];
114                 }else if(this.service.securityType == "Basic Authentication"){
115                         this.service.security = this.availableSecurityTypes[1];
116                 }else if(this.service.securityType == "Cookie based Authentication"){
117                         this.service.security = this.availableSecurityTypes[2];
118                 }
119             };
120              
121             this.ECOMP_URL_REGEX = ECOMP_URL_REGEX;
122             this.conflictMessages = {};
123             this.scrollApi = {};
124
125             let resetConflict = fieldName => {
126                 delete this.conflictMessages[fieldName];
127                 if($scope.widgetForm[fieldName]){
128                     $scope.widgetForm[fieldName].$setValidity('conflict', true);
129                 }
130             };
131
132             this.addParameter = () => {
133                 this.service.parameterList.push({}); 
134             }
135             
136             this.closeThisDialog = () => {
137                 $scope.closeThisDialog(true);
138             }
139             
140             this.removeParamItem = (parameter) => {
141                 microserviceService.getUserParameterById(parameter.id).then((res) => {
142                         if(res.length > 0){
143                                 var message = res.length + " users have their own widget parameters. Are you sure you want to delete?";
144                                 confirmBoxService.editItem(message).then(isConfirmed => {
145                                         if(isConfirmed){
146                                                 microserviceService.deleteUserParameterById(parameter.id).then((res) => {
147                                                         for(var i = 0; i < this.service.parameterList.length; i++){
148                                                 if(this.service.parameterList[i].para_key == parameter.para_key
149                                                 && this.service.parameterList[i].para_value == parameter.para_value){
150                                                         this.service.parameterList.splice(i, 1);
151                                                         return;
152                                                 }
153                                         }
154                                                 });
155                                         }
156                                 });
157                         }
158                         else{
159                                 for(var i = 0; i < this.service.parameterList.length; i++){
160                                 if(this.service.parameterList[i].para_key == parameter.para_key
161                                 && this.service.parameterList[i].para_value == parameter.para_value){
162                                         this.service.parameterList.splice(i, 1);
163                                         return;
164                                 }
165                         }
166                         }
167                 });
168                 
169                 
170             }
171             
172             this.updateServiceName = () => {
173                 this.dupliateName = false;
174                 for(var i = 0; i < this.serviceList.length; i++){
175                         if(this.serviceList[i].name == this.service.name){
176                                 if(this.isEditMode && this.service.name == this.originalName)
177                                         continue;
178                                 this.dupliateName = true;
179                                 return;
180                         }
181                 }
182             }
183             
184             this.updateDesc = () => {
185                 this.emptyServiceDesc = false;
186             }
187             
188             this.updateURL = () => {
189                 this.emptyServiceURL  = false;
190             }
191             
192             this.updateApp = () => {
193                 this.emptyServiceApp = false;
194             }
195             
196           //This is a fix for dropdown selection, due to b2b dropdown only update value field
197                 $scope.$watch('microserviceAddDetails.service.application.name', (newVal, oldVal) => {
198                         for(var i=0;i<$scope.microserviceAddDetails.availableApps.length;i++){                  
199                                 if($scope.microserviceAddDetails.availableApps[i].name==newVal){
200                                         $scope.microserviceAddDetails.service.application=angular.copy($scope.microserviceAddDetails.availableApps[i]);
201                                 }
202                         }
203                 });
204                 $scope.$watch('microserviceAddDetails.service.security.name', (newVal, oldVal) => {
205                         for(var i=0;i<$scope.microserviceAddDetails.availableSecurityTypes.length;i++){                         
206                                 if($scope.microserviceAddDetails.availableSecurityTypes[i].name==newVal){
207                                         $scope.microserviceAddDetails.service.security=angular.copy($scope.microserviceAddDetails.availableSecurityTypes[i]);
208                                 }
209                         }
210                 });
211             
212             let emptyCookies = () => {
213                 userProfileService.getUserProfile()
214                 .then(profile=> {
215                     $log.info('AppDetailsModalCtrl::emptyCookies profile: ', profile);
216                     $scope.orgUserId = profile.orgUserId;
217                     $log.info('user has the following orgUserId: ' + profile.orgUserId);
218                     if ($cookies.getObject($scope.orgUserId + '_widget') != undefined && $cookies.getObject($scope.orgUserId + '_widget') != null) {
219                         $cookies.remove($scope.orgUserId + '_widget');
220                     }
221                 });
222             };
223             
224             this.testServiceURL = () =>{
225                 widgetsCatalogService.getServiceJSON(this.service.id).then(res => {
226                         document.getElementById("microservice-details-input-json").innerHTML = (JSON.stringify(res));
227                                 });
228             }
229             
230             this.saveChanges = () => {     
231                 /* TODO: add form validation */
232                 
233                 var isValid = true;
234                 this.updateServiceName();
235                 
236                 if(this.service.name == ''
237                 || this.service.name == undefined){
238                         this.emptyServiceName = true;
239                         isValid = false;
240                 }
241                 
242                 if(this.dupliateName == true){
243                         isValid = false;
244                 }
245                         
246                 
247                 if(this.service.desc == ''
248                 || this.service.desc == undefined){
249                         this.emptyServiceDesc = true;
250                         isValid = false;
251                 }
252                 
253                 if(this.service.url == ''
254                 || this.service.url == undefined){
255                         this.emptyServiceURL = true;
256                         isValid = false;
257                 }
258                         
259                 if(this.service.application == undefined
260                 || this.service.application == null){
261                         this.emptyServiceApp = true;
262                         isValid = false;
263                 }
264                 
265                 if(!isValid)
266                         return;
267                 
268                 /*
269                                  * Check the parameter list, delete those parameters that don't
270                                  * have key
271                                  */
272                 for(var i = 0; i < this.service.parameterList.length; i++){
273                         if(this.service.parameterList[i].para_key == undefined
274                         || this.service.parameterList[i].para_key == null
275                         || this.service.parameterList[i].para_key == ""){
276                                 this.service.parameterList.splice(i, 1);
277                                 i--;
278                         }
279                 }
280       
281                 var securityType;
282                 var username;
283                 var password;
284                 if(this.service.security == undefined ||
285                 this.service.security == null)
286                         securityType = "No Authentication";
287                 else{
288                         securityType = this.service.security.name;
289                         username = this.service.username;
290                         password = this.service.password;
291                 }
292                 
293                 var active = 'N';
294                 if(this.service.active == true)
295                         active = 'Y';
296                 
297                 var newService = {
298                                 name: this.service.name,
299                                 desc: this.service.desc,
300                                 appId: this.service.application.id,
301                                 url: this.service.url,
302                                 securityType: securityType,
303                                 username: username,
304                                 password: password,
305                                 active: active,
306                                 parameterList: this.service.parameterList
307                 };
308                 
309                 if(this.isEditMode){
310                         // console.log(this.service.parameterList);
311                         var message = "Are you sure you want to change '" + this.service.name + "'?"
312                         confirmBoxService.editItem(message).then(isConfirmed => {
313                                 if(isConfirmed){
314                                         microserviceService.updateService(this.service.id, newService).then(() => {
315                                                 // TODO: result validation check
316                                                 //this.closeThisDialog(); 
317                                                 $scope.$dismiss('cancel');
318                                         });
319                                 }
320                         });
321                 }else{
322                         microserviceService.createService(newService).then(() => {
323                                 // TODO: result validation check
324                                 //$scope.closeThisDialog(true);
325                                 $scope.$dismiss('cancel');
326                         });
327                 }
328                 
329             };
330             init();
331             $scope.$on('$stateChangeStart', e => {
332                 e.preventDefault();
333             });
334         }
335     }
336     MicroserviceAddDetailsCtrl.$inject = ['$scope', '$log', '$interval', 'widgetsCatalogService', 'applicationsService', 'adminsService', 'microserviceService', 'errorMessageByCode', 'ECOMP_URL_REGEX', '$window','userProfileService', 'confirmBoxService', '$cookies','items'];
337     angular.module('ecompApp').controller('MicroserviceAddDetailsCtrl', MicroserviceAddDetailsCtrl);
338 })();