blob: 2d88298cb09ef9ae1e995ce6b09a389de188472d [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010019'use strict';
20
21angular.module('xos.contentProvider', [
Matteo Scandolo15d692c2015-10-28 12:00:07 +010022 'ngResource',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010023 'ngCookies',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010024 'xos.helpers',
Matteo Scandolo40bea632016-05-17 16:51:29 -070025 'ui.router'
Matteo Scandolo15d692c2015-10-28 12:00:07 +010026])
Matteo Scandolo40bea632016-05-17 16:51:29 -070027.config(($stateProvider) => {
Matteo Scandolo7db08432015-11-06 18:49:33 +010028
Matteo Scandolod44ceb42015-11-06 17:14:28 +010029 $stateProvider
30 .state('list', {
31 url: '/',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010032 template: '<content-provider-list></content-provider-list>',
33 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010034 .state('details', {
Matteo Scandolo7db08432015-11-06 18:49:33 +010035 url: '/contentProvider/:id',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010036 template: '<content-provider-detail></content-provider-detail>'
37 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010038 .state('cdn', {
39 url: '/contentProvider/:id/cdn_prefix',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010040 template: '<content-provider-cdn></content-provider-cdn>'
41 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010042 .state('server', {
43 url: '/contentProvider/:id/origin_server',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010044 template: '<content-provider-server></content-provider-server>'
45 })
Matteo Scandolod44ceb42015-11-06 17:14:28 +010046 .state('users', {
47 url: '/contentProvider/:id/users',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010048 template: '<content-provider-users></content-provider-users>'
Matteo Scandolod44ceb42015-11-06 17:14:28 +010049 });
Matteo Scandolo15d692c2015-10-28 12:00:07 +010050})
51.config(function($httpProvider){
Matteo Scandolo15d692c2015-10-28 12:00:07 +010052 // add X-CSRFToken header for update, create, delete (!GET)
53 $httpProvider.interceptors.push('SetCSRFToken');
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010054 $httpProvider.interceptors.push('NoHyperlinks');
Matteo Scandolo15d692c2015-10-28 12:00:07 +010055})
56.service('ContentProvider', function($resource){
57 return $resource('/hpcapi/contentproviders/:id/', {id: '@id'}, {
58 'update': {method: 'PUT'}
59 });
60})
61.service('ServiceProvider', function($resource){
62 return $resource('/hpcapi/serviceproviders/:id/', {id: '@id'});
63})
64.service('CdnPrefix', function($resource){
65 return $resource('/hpcapi/cdnprefixs/:id/', {id: '@id'});
66})
67.service('OriginServer', function($resource){
68 return $resource('/hpcapi/originservers/:id/', {id: '@id'});
69})
70.service('User', function($resource){
71 return $resource('/xos/users/:id/', {id: '@id'});
72})
73.directive('cpActions', function(ContentProvider, $location){
74 return {
75 restrict: 'E',
76 scope: {
77 id: '=id',
78 },
79 bindToController: true,
80 controllerAs: 'vm',
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010081 templateUrl: 'templates/cp_actions.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010082 controller: function(){
83 this.deleteCp = function(id){
84 ContentProvider.delete({id: id}).$promise
85 .then(function(){
86 $location.url('/');
87 });
88 };
89 }
90 };
91})
Matteo Scandolo40bea632016-05-17 16:51:29 -070092.directive('contentProviderList', function(ContentProvider, _){
Matteo Scandolo15d692c2015-10-28 12:00:07 +010093 return {
94 restrict: 'E',
95 controllerAs: 'vm',
96 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +010097 templateUrl: 'templates/cp_list.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +010098 controller: function(){
Matteo Scandolo775d3242016-03-24 16:12:18 -070099 ['Name', 'Description', 'Status', 'Actions']
100 this.tableConfig = {
101 columns: [
102 {
103 label: 'Name',
104 field: 'humanReadableName'
105 },
106 {
107 label: 'Description',
108 field: 'description'
109 },
110 {
111 label: 'Status',
112 field: 'enabled'
113 }
114 ],
115 enableActions: true
116 };
117
Matteo Scandolo7db08432015-11-06 18:49:33 +0100118 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100119
120 ContentProvider.query().$promise
121 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100122 self.contentProviderList = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100123 })
124 .catch(function(e){
125 throw new Error(e);
126 });
127
128 this.deleteCp = function(id){
129 ContentProvider.delete({id: id}).$promise
130 .then(function(){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700131 _.remove(self.contentProviderList, {id: id});
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100132 });
133 };
134 }
135 };
136})
Matteo Scandolo7db08432015-11-06 18:49:33 +0100137.directive('contentProviderDetail', function(ContentProvider, ServiceProvider, $stateParams, $location){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100138 return {
139 restrict: 'E',
140 controllerAs: 'vm',
141 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100142 templateUrl: 'templates/cp_detail.html',
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100143 controller: function(){
144 this.pageName = 'detail';
Matteo Scandolo7db08432015-11-06 18:49:33 +0100145 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100146
Matteo Scandolo7db08432015-11-06 18:49:33 +0100147 if($stateParams.id){
148 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100149 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100150 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100151 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100152 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100153 status: 0,
154 msg: e.data.detail
155 };
156 });
157 }
158 else{
Matteo Scandolo7db08432015-11-06 18:49:33 +0100159 self.cp = new ContentProvider();
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100160 }
161
162 ServiceProvider.query().$promise
163 .then(function(sp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100164 self.sp = sp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100165 });
166
167 this.saveContentProvider = function(cp){
168 var p, isNew = false;
169
170 if(cp.id){
171 p = cp.$update();
172 }
173 else{
174 isNew = true;
175 cp.name = cp.humanReadableName;
176 p = cp.$save();
177 }
178
179 p.then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100180 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100181 status: 1,
182 msg: 'Content Provider Saved'
183 };
184 if(isNew){
185 $location.url('contentProvider/' + res.id + '/');
186 }
187 })
188 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100189 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100190 status: 0,
191 msg: e.data.detail
192 };
193 });
194 };
195 }
196 };
197})
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700198.directive('contentProviderCdn', function($stateParams, CdnPrefix, ContentProvider){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100199 return{
200 restrict: 'E',
201 controllerAs: 'vm',
202 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100203 templateUrl: 'templates/cp_cdn_prefix.html',
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700204 controller: function(_){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100205 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100206
207 this.pageName = 'cdn';
208
Matteo Scandolo7db08432015-11-06 18:49:33 +0100209 if($stateParams.id){
210 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100211 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100212 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100213 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100214 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100215 status: 0,
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700216 msg: e.data ? e.data.detail : ''
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100217 };
218 });
219 }
220
221 CdnPrefix.query().$promise
222 .then(function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100223 self.prf = prf;
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700224 // console.log(prf, $stateParams.id);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100225 // set the active CdnPrefix for this contentProvider
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700226 self.cp_prf = [];
227 self.cp_prf.push(_.find(prf, {contentProvider: parseInt($stateParams.id)}));
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100228 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100229 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100230 status: 0,
231 msg: e.data.detail
232 };
233 });
234
235 this.addPrefix = function(prf){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100236 prf.contentProvider = $stateParams.id;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100237
238 var item = new CdnPrefix(prf);
239
240 item.$save()
241 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100242 self.cp_prf.push(res);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100243 })
244 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100245 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100246 status: 0,
247 msg: e.data.detail
248 };
249 });
250 };
251
252 this.removePrefix = function(item){
253 item.$delete()
254 .then(function(){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700255 _.remove(self.cp_prf, item);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100256 })
257 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100258 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100259 status: 0,
260 msg: e.data.detail
261 };
262 });
263 };
264 }
265 };
266})
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700267.directive('contentProviderServer', function($stateParams, OriginServer, ContentProvider){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100268 return{
269 restrict: 'E',
270 controllerAs: 'vm',
271 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100272 templateUrl: 'templates/cp_origin_server.html',
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700273 controller: function(_){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100274 this.pageName = 'server';
Matteo Scandolo7db08432015-11-06 18:49:33 +0100275 this.protocols = {'http': 'HTTP', 'rtmp': 'RTMP', 'rtp': 'RTP', 'shout': 'SHOUTcast'};
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100276
Matteo Scandolo7db08432015-11-06 18:49:33 +0100277 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100278
Matteo Scandolo7db08432015-11-06 18:49:33 +0100279 if($stateParams.id){
280 ContentProvider.get({id: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100281 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100282 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100283 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100284 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100285 status: 0,
286 msg: e.data.detail
287 };
288 });
289 }
290
Matteo Scandolo7db08432015-11-06 18:49:33 +0100291 OriginServer.query({contentProvider: $stateParams.id}).$promise
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100292 .then(function(cp_os){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100293 self.cp_os = cp_os;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100294 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100295 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100296 status: 0,
297 msg: e.data.detail
298 };
299 });
300
301 this.addOrigin = function(os){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100302 os.contentProvider = $stateParams.id;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100303
304 var item = new OriginServer(os);
305
306 item.$save()
307 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100308 self.cp_os.push(res);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100309 })
310 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100311 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100312 status: 0,
313 msg: e.data.detail
314 };
315 });
316 };
317
318 this.removeOrigin = function(item){
319 item.$delete()
320 .then(function(){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700321 _.remove(self.cp_os, item);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100322 })
323 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100324 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100325 status: 0,
326 msg: e.data.detail
327 };
328 });
329 };
330 }
331 };
332})
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700333.directive('contentProviderUsers', function($stateParams, ContentProvider, User){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100334 return{
335 restrict: 'E',
336 controllerAs: 'vm',
337 scope: {},
Matteo Scandolobbcc01b2015-11-04 16:30:43 +0100338 templateUrl: 'templates/cp_user.html',
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700339 controller: function(_){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100340 var self = this;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100341
342 this.pageName = 'user';
343
344 this.cp_users = [];
345
Matteo Scandolo7db08432015-11-06 18:49:33 +0100346 if($stateParams.id){
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100347 User.query().$promise
348 .then(function(users){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100349 self.users = users;
350 return ContentProvider.get({id: $stateParams.id}).$promise;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100351 })
352 .then(function(res){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100353 res.users = self.populateUser(res.users, self.users);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100354 return res;
355 })
356 .then(function(cp){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100357 self.cp = cp;
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100358 }).catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100359 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100360 status: 0,
361 msg: e.data.detail
362 };
363 });
364 }
365
366 this.populateUser = function(ids, list){
367 for(var i = 0; i < ids.length; i++){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700368 ids[i] = _.find(list, {id: ids[i]});
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100369 }
370 return ids;
371 };
372
373 this.addUserToCp = function(user){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100374 self.cp.users.push(user);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100375 };
376
377 this.removeUserFromCp = function(user){
Matteo Scandolo40bea632016-05-17 16:51:29 -0700378 _.remove(self.cp.users, user);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100379 };
380
381 this.saveContentProvider = function(cp){
382
383 // flatten the user to id of array
Matteo Scandolo54bc5f72016-05-18 14:06:45 -0700384 cp.users = _.map(cp.users, 'id');
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100385
386 cp.$update()
387 .then(function(res){
388
Matteo Scandolo7db08432015-11-06 18:49:33 +0100389 self.cp.users = self.populateUser(res.users, self.users);
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100390
Matteo Scandolo7db08432015-11-06 18:49:33 +0100391 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100392 status: 1,
393 msg: 'Content Provider Saved'
394 };
395
396 })
397 .catch(function(e){
Matteo Scandolo7db08432015-11-06 18:49:33 +0100398 self.result = {
Matteo Scandolo15d692c2015-10-28 12:00:07 +0100399 status: 0,
400 msg: e.data.detail
401 };
402 });
403 };
404 }
405 };
406});