3efb0d09b1a4bcfc993d8525cf99a81f7b556e49
[portal.git] / ecomp-portal-FE-common / client / app / services / basic-auth-account / basic-auth-account.service.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 'use strict';
39
40 (function () {
41     class BasicAuthAccountService {
42         constructor($q, $log, $http, conf,uuid, utilsService) {
43             this.$q = $q;
44             this.$log = $log;
45             this.$http = $http;
46             this.conf = conf;
47             this.uuid = uuid;
48             this.utilsService = utilsService;   
49         }
50         
51         createAccount(newAccount) {
52                 let deferred = this.$q.defer();
53                 this.$http({
54                 method: "POST",
55                 url: this.conf.api.basicAuthAccount,
56                 data: newAccount,
57                 cache: false,
58                 headers:{
59                         'X-ECOMP-RequestID':this.uuid.generate()
60                 }
61             }).then(res => {
62                 if (res == null || res.data == null) {
63                                 this.$log.error('BasicAuthAccountService::createAccount Failed: Result or result.data is null');
64                      deferred.reject("BasicAuthAccountService::createAccount Failed: Result or result.data is null");
65                 } else if (!this.utilsService.isValidJSON(res.data)) {
66                         this.$log.error('BasicAuthAccountService::createAccount Failed: Invalid JSON format');
67                     deferred.reject("BasicAuthAccountService::createAccount Failed: Invalid JSON format");
68                 } else {
69                     deferred.resolve(res.data);
70                 }
71             })
72             .catch(errRes => {
73                 deferred.reject(errRes);
74             });
75             return deferred.promise;
76         }
77         
78         updateAccount(accountId, newAccount) {
79                 let deferred = this.$q.defer();
80                 this.$http({
81                 method: "PUT",
82                 url: this.conf.api.basicAuthAccount + "/" + accountId,
83                 data: newAccount,
84                 cache: false,
85                 headers:{
86                         'X-ECOMP-RequestID':this.uuid.generate()
87                 }
88             }).then(res => {
89                 if (res == null || res.data == null) {
90                                 this.$log.error('BasicAuthAccountService::updateAccount Failed: Result or result.data is null');
91                      deferred.reject("BasicAuthAccountService::updateAccount Failed: Result or result.data is null");
92                 } else if (!this.utilsService.isValidJSON(res.data)) {
93                         this.$log.error('BasicAuthAccountService::updateAccount Failed: Invalid JSON format');
94                     deferred.reject("BasicAuthAccountService::updateAccount Failed: Invalid JSON format");
95                 } else {
96                     deferred.resolve(res.data);
97                 }
98             })
99             .catch(errRes => {
100                 deferred.reject(errRes);
101             });
102             return deferred.promise;
103         }
104         
105         getAccountList() {
106                 let deferred = this.$q.defer();
107                 this.$http({
108                 method: "GET",
109                 url: this.conf.api.basicAuthAccount,
110                 cache: false,
111                 headers:{
112                         'X-ECOMP-RequestID':this.uuid.generate()
113                 }
114             }).then(res => {
115                 if (res == null || res.data == null) {
116                                 this.$log.error('BasicAuthAccountService::getAccountList Failed: Result or result.data is null');
117                      deferred.reject("BasicAuthAccountService::getAccountList Failed: Result or result.data is null");
118                 } else if (!this.utilsService.isValidJSON(res.data)) {
119                         this.$log.error('BasicAuthAccountService::getAccountList Failed: Invalid JSON format');
120                     deferred.reject("BasicAuthAccountService::getAccountList Failed: Invalid JSON format");
121                 } else {
122                     deferred.resolve(res.data.response);
123                 }
124             })
125             .catch(errRes => {
126                 deferred.reject(errRes);
127             });
128             return deferred.promise;
129         }
130         
131         deleteAccount(accountId) {
132                 let deferred = this.$q.defer();
133                 this.$http({
134                 method: "DELETE",
135                 url: this.conf.api.basicAuthAccount + "/" + accountId,
136                 cache: false,
137                 headers:{
138                         'X-ECOMP-RequestID':this.uuid.generate()
139                 }
140             }).then(res => {
141                 if (res == null || res.data == null) {
142                                 this.$log.error('BasicAuthAccountService::deleteAccount Failed: Result or result.data is null');
143                      deferred.reject("BasicAuthAccountService::deleteAccount Failed: Result or result.data is null");
144                 } else if (!this.utilsService.isValidJSON(res.data)) {
145                         this.$log.error('BasicAuthAccountService::deleteAccount Failed: Invalid JSON format');
146                     deferred.reject("BasicAuthAccountService::deleteAccount Failed: Invalid JSON format");
147                 } else {
148                     deferred.resolve(res.data);
149                 }
150             })
151             .catch(errRes => {
152                 deferred.reject(errRes);
153             });
154             return deferred.promise;
155         }
156        
157     }
158     
159     BasicAuthAccountService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
160     angular.module('ecompApp').service('basicAuthAccountService', BasicAuthAccountService)
161 })();