blob: ed37af951c443b97d07700694980c33e0a9e7aed [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 Scandolo86f3f282016-08-11 11:21:33 -070019(function () {
20 'use strict';
21 angular.module('xos.UITutorial')
22 .service('ExploreCmd', function($injector, ResponseHandler, ErrorHandler){
23
Matteo Scandoloef969922016-08-11 13:49:12 -070024 this.resourceExec = (cmd, args, done) => {
25 switch(args[0]){
26 case 'list':
27 return this.listAvailableResources(done);
28 break;
29 default:
30 // use the resource
31 const resourceName = args.shift();
32 const method = args.shift();
33 return this.consumeResource(resourceName, method, args, done);
34 }
35 };
36
37 this.resourceCompletion = (cmd, arg, line, done) => {
38 const args = ['list'].concat(this.getAvailableResources());
39 if(line.text.match(/resource\s[A-Z][a-z]+\s/)){
40 // if arg is a resource, then return available methods
41 if(args.indexOf(arg) !== -1){
42 arg = '';
Matteo Scandolo86f3f282016-08-11 11:21:33 -070043 }
Matteo Scandoloef969922016-08-11 13:49:12 -070044 const methods = ['query', 'get', 'save', '$save', 'delete'];
45 return done(this.shell.bestMatch(arg, methods));
46 }
47 return done(this.shell.bestMatch(arg, args));
48 };
49
50 this.setup = (shell) => {
51 this.shell = shell;
52 shell.setCommandHandler('resource', {
53 exec: this.resourceExec,
54 completion: this.resourceCompletion
Matteo Scandolo86f3f282016-08-11 11:21:33 -070055 });
56 };
57
Matteo Scandoloef969922016-08-11 13:49:12 -070058 this.getAvailableResources = () => {
Matteo Scandolo86f3f282016-08-11 11:21:33 -070059 return angular.module('xos.helpers')._invokeQueue
60 .filter((d) => {
61 if(d[1] !== 'service'){
62 return false;
63 }
64 const serviceDeps = d[2][1];
65 return serviceDeps.indexOf('$resource') !== -1;
66 })
67 .reduce((list, i) => list.concat([i[2][0]]), []);
68 }
69
Matteo Scandoloef969922016-08-11 13:49:12 -070070 this.listAvailableResources = (done) => {
71 // TODO use a template
72 const resources = this.getAvailableResources()
Matteo Scandolo86f3f282016-08-11 11:21:33 -070073 .reduce((html, i) => `${html}${i}<br/>`, '');
74 done(resources);
75 }
76
Matteo Scandoloef969922016-08-11 13:49:12 -070077 this.consumeResource = (resourceName, method, args, done) => {
Matteo Scandolo86f3f282016-08-11 11:21:33 -070078
79 // TODO if not resourceName/method print cmd instructions
80
Matteo Scandoloef969922016-08-11 13:49:12 -070081 if(this.getAvailableResources().indexOf(resourceName) === -1){
Matteo Scandolo86f3f282016-08-11 11:21:33 -070082 return ErrorHandler.print(`Resource "${resourceName}" does not exists`, done);
83 }
84
85 if(['query', 'get', 'save', '$save', 'delete'].indexOf(method) === -1){
86 return ErrorHandler.print(`Method "${method}" not allowed`, done);
87 }
88
Matteo Scandoloef969922016-08-11 13:49:12 -070089 // TODO @Teo if get/delete check for arguments
90 let params = {};
91
92 // if the method require arguments checks for them
93 if(['get', '$save', 'delete'].indexOf(method) !== -1){
94 if(args.length === 0){
95 return ErrorHandler.print(`Method "${method}" require parameters`, done);
96 }
97 }
98
99 // if there are arguments parse them
100 // TODO wrap in a try catch, we have no guarantee that a user insert the correct params
101 if(args.length > 0){
102 params = eval(`(${args[0]})`);
103 }
104
105 // if it is a query is not possible to use id as parameter
106 if(method === 'query' && angular.isDefined(params.id)){
107 return ErrorHandler.print(`Is not possible to use "id" as filter in method "${method}", use "get" instead!`, done);
108 }
109
Matteo Scandolo86f3f282016-08-11 11:21:33 -0700110 let Resource;
111 try{
112 Resource = $injector.get(resourceName);
Matteo Scandolo86f3f282016-08-11 11:21:33 -0700113 Resource[method](params).$promise
114 .then(res => {
Matteo Scandoloef969922016-08-11 13:49:12 -0700115 const jsCode = `${resourceName}.${method}(${Object.keys(params).length > 0 ? JSON.stringify(params): ''})`;
116 return ResponseHandler.parse(res, jsCode, done);
117 })
118 .catch(e => {
119 if(e.status === 404){
120 return ErrorHandler.print(`${resourceName} with method "${method}" and parameters ${JSON.stringify(params)} ${e.data.detail}`, done);
121 }
Matteo Scandolo86f3f282016-08-11 11:21:33 -0700122 });
123 }
124 catch(e){
Matteo Scandoloef969922016-08-11 13:49:12 -0700125 console.error(e);
Matteo Scandolo86f3f282016-08-11 11:21:33 -0700126 return ErrorHandler.print(`Failed to inject resource "${resourceName}"`, done);
127 }
128 };
129
130 });
131})();