Beispiel #1
0
    def __call__(self, environ, start_response):
       
        # Request method
        method = environ['REQUEST_METHOD']

        # /// URLs ///
        # /<router>/config/<module>/<_index>
        # /<router>/rpc/<module>/<func>
        path = environ['PATH_INFO'].split('/')
        router = None
        module_type = None # 'config' or 'rpc'
        module = None # name of a config module or a rpc module
        _index = None
        module_func = None
        if len(path) > 1:
            router = path[1]
        if len(path) > 2:
            module_type = path[2]
        if len(path) > 3:
            module = path[3]
        if len(path) > 4:
            if module_type == 'config': # Calls a config module
                _index = path[4]
            elif module_type == 'rpc': # Calls a rpc module
                module_func = '{}.{}'.format(module,path[4])
       
        # /// Methods ///
        # Method    NLAN operation
        # ------------------------
        # POST      --add or none(rpc) 
        # PUT       --update
        # DELETE    --delete
        # GET       --get
        # OPTIONS   --schema
        # /// Query parameters ///
        # -------------------------------------------------------
        # For POST(config)/PUT
        # Query parameters correspond to those --schema outputs
        # (excluding "_index").
        # -------------------------------------------------------
        # For POST(rpc)/DELETE/GET
        # args=<value1>,<value2>,...
        # -------------------------------------------------------
        # For OPTIONS
        # args=<module_name>
        # -------------------------------------------------------
        operations = {'POST': '--add',
                      'PUT': '--update',
                      'DELETE': '--delete',
                      'GET': '--get'}
        doc = None
        query = environ['QUERY_STRING'] 
        if method == 'POST' and module_type == 'config' or method == 'PUT': # add/update
            q = query.split('&')
            if _index:
                q.append('_index={}'.format(_index))
            doc = str(argsmodel.parse_args('add', module, *q))
        elif method == 'DELETE' or method == 'GET': # delete/get
            qq = query.split('=') 
            q = [] 
            if qq[0] == 'params':
                q = qq[1].split(',')
            if _index:
                q.append('_index={}'.format(_index))
            doc = str(argsmodel.parse_args('get', module, *q))
        elif method == 'POST' and module_type == 'rpc':
            doc = [] 
            doc.append(module_func)
            q = query.split('=') 
            if q[0] == 'params':
                doc.extend(q[1].split(','))
        elif method == 'OPTIONS': # schema
            if query: 
                qq = query.split('=')
                module = qq[1]

        # String buffer as a HTTP Response body
        out = StringIO()

        try:
            if method in operations and module_type == 'config': 
                operation = operations[method]
                results = nlan.main(router=router, operation=operation, doc=doc, output_stdout=True, rest_output=True)
                print >>out, json.dumps(results)
                start_response('200 OK', [('Content-type', 'application/json')])
            elif method == 'POST' and module_type == 'rpc':
                results = nlan.main(router=router, doc=doc, output_stdout=True, rest_output=True)
                print >>out, json.dumps(results)
                start_response('200 OK', [('Content-type', 'application/json')])
            elif method == 'OPTIONS':
                print >>out, argsmodel.schema_help(module, list_output=True)
                start_response('200 OK', [('Content-type', 'application/json')])
            else:
                print >>out, 'Not Implemented'
                start_response('501 Not Implemented', [('Content-type', 'text/plain')])
        except NlanException as e:
            results = e.get_result()
            print >>out, json.dumps(results)
            start_response('500 Internal Server Error', [('Content-type', 'application/json')])
        except Exception as e:
            print >>out, traceback.format_exc()
            start_response('500 Internal Server Error', [('Content-type', 'application/json')])

        # Return the result as a body of HTTP Response
        out.seek(0)
        return out 
Beispiel #2
0
    if options.target:
        router = options.target

    # Default state file
    if not crud and not option and len(args) == 1 and args[0] == 'deploy':
        args[0] = NLAN_STATE

    try: # Execution
        if options.time: # --wait
            timeout = options.time
            _wait(router, timeout, verbose)
        elif not crud and option: # --scp, --scpmod, --raw
            main(router=router, operation=option, doc=args, loglevel=loglevel, git=git, verbose=verbose, mime=mime)
        elif options.schema:
            if len(args) == 1:
                print argsmodel.schema_help(args[0])
            else:
                print argsmodel.schema_help(None)
        elif not crud and not option and len(args) > 0 and args[0].endswith('yaml'): # Batch operation
            for v in args:
                if v.endswith('yaml'):
                    if os.path.exists(os.path.join(NLAN_ETC, 'state', v)):
                        cmd_list = yamldiff.crud_diff(v, git=git)
                        if len(cmd_list) != 0:
                            main(router=router, operation='--batch', cmd_list=cmd_list, loglevel=loglevel, verbose=verbose, mime=mime)
                            if git == 0:
                                cmdutil.check_cmd('git', GIT_OPTIONS, 'add', v)
                                cmdutil.check_cmd('git', GIT_OPTIONS, 'commit -m updated')
        elif not crud and not option and len(args) > 0: # NLAN rpc module execution
            main(router=router, doc=args, loglevel=loglevel, verbose=verbose, mime=mime)
        elif crud and len(args) > 0: # CRUD operation
Beispiel #3
0
    def __call__(self, environ, start_response):

        # Request method
        method = environ['REQUEST_METHOD']

        # /// URLs ///
        # /<router>/config/<module>/<_index>
        # /<router>/rpc/<module>/<func>
        path = environ['PATH_INFO'].split('/')
        router = None
        module_type = None  # 'config' or 'rpc'
        module = None  # name of a config module or a rpc module
        _index = None
        module_func = None
        if len(path) > 1:
            router = path[1]
        if len(path) > 2:
            module_type = path[2]
        if len(path) > 3:
            module = path[3]
        if len(path) > 4:
            if module_type == 'config':  # Calls a config module
                _index = path[4]
            elif module_type == 'rpc':  # Calls a rpc module
                module_func = '{}.{}'.format(module, path[4])

        # /// Methods ///
        # Method    NLAN operation
        # ------------------------
        # POST      --add or none(rpc)
        # PUT       --update
        # DELETE    --delete
        # GET       --get
        # OPTIONS   --schema
        # /// Query parameters ///
        # -------------------------------------------------------
        # For POST(config)/PUT
        # Query parameters correspond to those --schema outputs
        # (excluding "_index").
        # -------------------------------------------------------
        # For POST(rpc)/DELETE/GET
        # args=<value1>,<value2>,...
        # -------------------------------------------------------
        # For OPTIONS
        # args=<module_name>
        # -------------------------------------------------------
        operations = {
            'POST': '--add',
            'PUT': '--update',
            'DELETE': '--delete',
            'GET': '--get'
        }
        doc = None
        query = environ['QUERY_STRING']
        if method == 'POST' and module_type == 'config' or method == 'PUT':  # add/update
            q = query.split('&')
            if _index:
                q.append('_index={}'.format(_index))
            doc = str(argsmodel.parse_args('add', module, *q))
        elif method == 'DELETE' or method == 'GET':  # delete/get
            qq = query.split('=')
            q = []
            if qq[0] == 'params':
                q = qq[1].split(',')
            if _index:
                q.append('_index={}'.format(_index))
            doc = str(argsmodel.parse_args('get', module, *q))
        elif method == 'POST' and module_type == 'rpc':
            doc = []
            doc.append(module_func)
            q = query.split('=')
            if q[0] == 'params':
                doc.extend(q[1].split(','))
        elif method == 'OPTIONS':  # schema
            if query:
                qq = query.split('=')
                module = qq[1]

        # String buffer as a HTTP Response body
        out = StringIO()

        try:
            if method in operations and module_type == 'config':
                operation = operations[method]
                results = nlan.main(router=router,
                                    operation=operation,
                                    doc=doc,
                                    output_stdout=True,
                                    rest_output=True)
                print >> out, json.dumps(results)
                start_response('200 OK',
                               [('Content-type', 'application/json')])
            elif method == 'POST' and module_type == 'rpc':
                results = nlan.main(router=router,
                                    doc=doc,
                                    output_stdout=True,
                                    rest_output=True)
                print >> out, json.dumps(results)
                start_response('200 OK',
                               [('Content-type', 'application/json')])
            elif method == 'OPTIONS':
                print >> out, argsmodel.schema_help(module, list_output=True)
                start_response('200 OK',
                               [('Content-type', 'application/json')])
            else:
                print >> out, 'Not Implemented'
                start_response('501 Not Implemented',
                               [('Content-type', 'text/plain')])
        except NlanException as e:
            results = e.get_result()
            print >> out, json.dumps(results)
            start_response('500 Internal Server Error',
                           [('Content-type', 'application/json')])
        except Exception as e:
            print >> out, traceback.format_exc()
            start_response('500 Internal Server Error',
                           [('Content-type', 'application/json')])

        # Return the result as a body of HTTP Response
        out.seek(0)
        return out