Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 19 | (function () { |
| 20 | 'use strict'; |
| 21 | angular.module('xos.UITutorial') |
| 22 | .service('ExploreCmd', function($injector, ResponseHandler, ErrorHandler){ |
| 23 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 24 | 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 Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 43 | } |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 44 | 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 Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 55 | }); |
| 56 | }; |
| 57 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 58 | this.getAvailableResources = () => { |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 59 | 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 Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 70 | this.listAvailableResources = (done) => { |
| 71 | // TODO use a template |
| 72 | const resources = this.getAvailableResources() |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 73 | .reduce((html, i) => `${html}${i}<br/>`, ''); |
| 74 | done(resources); |
| 75 | } |
| 76 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 77 | this.consumeResource = (resourceName, method, args, done) => { |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 78 | |
| 79 | // TODO if not resourceName/method print cmd instructions |
| 80 | |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 81 | if(this.getAvailableResources().indexOf(resourceName) === -1){ |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 82 | 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 Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 89 | // 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 Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 110 | let Resource; |
| 111 | try{ |
| 112 | Resource = $injector.get(resourceName); |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 113 | Resource[method](params).$promise |
| 114 | .then(res => { |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 115 | 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 Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 122 | }); |
| 123 | } |
| 124 | catch(e){ |
Matteo Scandolo | ef96992 | 2016-08-11 13:49:12 -0700 | [diff] [blame] | 125 | console.error(e); |
Matteo Scandolo | 86f3f28 | 2016-08-11 11:21:33 -0700 | [diff] [blame] | 126 | return ErrorHandler.print(`Failed to inject resource "${resourceName}"`, done); |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | }); |
| 131 | })(); |