Пример #1
0
    def do_ls(self, args):
        """ lists resources """
        logger.info("CMD: ls %s" % args)
        try:
            if args:
                if args.startswith('/'):
                    temp_context = self.__get_context_for_path(
                        args[1:], self._context)
                else:
                    temp_context = self.__get_context_for_path(
                        args, curr_context)
            else:
                temp_context = curr_context

            if temp_context:

                sub_context = list(temp_context.keys())

                # Remove actions from here and print it separately
                actions_dict = None
                if ACTIONS_KEY in temp_context:
                    actions_dict = temp_context[ACTIONS_KEY]
                    sub_context.remove(ACTIONS_KEY)

                if ID_KEY in temp_context and BULK_KEY in temp_context:
                    # Print ids if 'bulk' is present
                    sub_context.remove(ID_KEY)
                    try:
                        cookie = self.__get_cookie()
                        response = ViPRConnection.submitHttpRequest(
                            'GET', curr_path + "/bulk", cookie)
                        if response:
                            self.__print_bulk_response(response.text)
                    except Exception as e:
                        print(str(e))
                        logger.error(str(e))
                elif ID_KEY in temp_context and ACTIONS_KEY in temp_context and 'GET' in temp_context[
                        ACTIONS_KEY]:
                    # Print ids if 'GET' is present
                    sub_context.remove(ID_KEY)
                    try:
                        cookie = self.__get_cookie()
                        response = ViPRConnection.submitHttpRequest(
                            'GET', curr_path, cookie)
                        if response:
                            self.__print_get_all_response(response.text)
                    except Exception as e:
                        print(str(e))
                        logger.error(str(e))

                print('  '.join(sub_context))

                # Print actions
                if actions_dict:
                    print('\nActions:-')
                    print('  '.join(actions_dict.keys()))
            else:
                print('Wrong path')
        except Exception as e:
            print(str(e))
Пример #2
0
    def do_ll(self, args):
        """ List resources in detail """
        logger.info("CMD: ll %s" % args)
        try:
            if args:
                if args.startswith('/'):
                    temp_context = self.__get_context_for_path(args[1:], self._context)
                else:
                    temp_context = self.__get_context_for_path(args, curr_context)
            else:
                temp_context = curr_context

            if temp_context:
                sub_context = list(temp_context.keys())

                # Remove actions from here and print it separately
                actions_dict = None
                if ACTIONS_KEY in temp_context:
                    actions_dict = temp_context[ACTIONS_KEY]
                    sub_context.remove(ACTIONS_KEY)

                if ID_KEY in temp_context and BULK_KEY in temp_context:
                    # Print ids if 'bulk' is present
                    sub_context.remove(ID_KEY)
                    try:
                        cookie = self.__get_cookie()
                        # TODO: check if bulk exists
                        response = ViPRConnection.submitHttpRequest('GET', curr_path+"/bulk", cookie)
                        if response:
                            detail_response = ViPRConnection.submitHttpRequest('POST', curr_path+"/bulk", cookie, payload=response.text)
                            if detail_response:
                                self.__print_ll_response(detail_response.text)
                    except Exception as e:
                        print(str(e))
                        logger.error(str(e))

                elif ID_KEY in temp_context and ACTIONS_KEY in temp_context and 'GET' in temp_context[ACTIONS_KEY]:
                    # Print ids if 'GET' is present
                    sub_context.remove(ID_KEY)
                    try:
                        cookie = self.__get_cookie()
                        response = ViPRConnection.submitHttpRequest('GET', curr_path, cookie)
                        if response:
                            self.__print_ll_response(response.text)
                    except Exception as e:
                        print(str(e))
                        logger.error(str(e))

                print('  '.join(sub_context))

                if actions_dict:
                    print('\nActions:-')
                    print('  '.join(actions_dict.keys()))
            else:
                print('Wrong path')
        except Exception as e:
            print(str(e))
            logger.error(str(e))
Пример #3
0
    def do_PUT(self, args):
        """ Update resource """
        logger.info("CMD: PUT %s" % args)
        try:
            if ACTIONS_KEY not in curr_context:
                print('PUT is not available')
                return

            actions_dict = curr_context[ACTIONS_KEY]
            if 'help' == args:
                if actions_dict['PUT']:
                    CommonUtil.print_query_params(
                        actions_dict['PUT'].query_params)
                    CommonUtil.print_attributes(
                        actions_dict['PUT'].method_name)
                return

            put_payload, query_str, content_type = self.__process_args(
                args, actions_dict['PUT'])
            cookie = self.__get_cookie()
            response = ViPRConnection.submitHttpRequest(
                'PUT',
                curr_path + query_str,
                cookie,
                payload=put_payload,
                contentType=content_type)
            if response:
                self.__print_response(response.text)
        except Exception as e:
            print(str(e))
            logger.error(str(e))
Пример #4
0
    def do_POST(self, args):
        """ Create resource """
        logger.info("CMD: POST %s" % args)
        global execute_action
        try:
            if ACTIONS_KEY not in curr_context:
                print('POST is not available')
                return

            actions_dict = curr_context[ACTIONS_KEY]
            # this is used when any default action is called
            if execute_action:
                curr_action = execute_action
            else:
                curr_action = 'POST'

            if 'help' == args:
                if actions_dict[curr_action]:
                    CommonUtil.print_query_params(actions_dict[curr_action].query_params)
                    CommonUtil.print_attributes(actions_dict[curr_action].method_name)
                return

            post_payload, query_str, content_type = self.__process_args(args, actions_dict[curr_action])
            cookie = self.__get_cookie()
            if execute_action:
                post_url = curr_path + '/' + execute_action
            else:
                post_url = curr_path

            response = ViPRConnection.submitHttpRequest('POST', post_url+query_str, cookie, payload=post_payload, contentType=content_type)
            if response:
                self.__print_response(response.text)
        except Exception as e:
            print(str(e))
            logger.error(str(e))
Пример #5
0
    def do_GET(self, args):
        """ GET resource """
        logger.info("CMD: GET %s" % args)
        try:
            if ACTIONS_KEY not in curr_context:
                print('GET is not available')
                return

            actions_dict = curr_context[ACTIONS_KEY]
            if 'help' == args:
                if actions_dict['GET']:
                    CommonUtil.print_query_params(
                        actions_dict['GET'].query_params)
                return

            cookie = self.__get_cookie()
            args_dict = self.__convert_args_to_dict(args)
            query_str = ''
            if actions_dict['GET'] and actions_dict['GET'].query_params:
                query_str = self.__process_return_query_params(
                    args_dict, actions_dict['GET'].query_params)
            accept_type = args_dict[
                RESPONSE_TYPE_KEY] if RESPONSE_TYPE_KEY in args_dict else ''
            response = ViPRConnection.submitHttpRequest(
                'GET',
                curr_path + query_str,
                cookie,
                xml=True if accept_type == RESPONSE_TYPE_XML else False)
            if response:
                self.__print_response(response.text, accept_type)
        except Exception as e:
            print(str(e))
            logger.error(str(e))
Пример #6
0
    def do_GET(self, args):
        """ GET resource """
        logger.info("CMD: GET %s" % args)
        try:
            if ACTIONS_KEY not in curr_context:
                print('GET is not available')
                return

            actions_dict = curr_context[ACTIONS_KEY]
            if 'help' == args:
                if actions_dict['GET']:
                    CommonUtil.print_query_params(actions_dict['GET'].query_params)
                return

            cookie = self.__get_cookie()
            args_dict = self.__convert_args_to_dict(args)
            query_str = ''
            if actions_dict['GET'] and actions_dict['GET'].query_params:
                query_str = self.__process_return_query_params(args_dict, actions_dict['GET'].query_params)
            accept_type = args_dict[RESPONSE_TYPE_KEY] if RESPONSE_TYPE_KEY in args_dict else ''
            response = ViPRConnection.submitHttpRequest('GET', curr_path+query_str, cookie, xml=True if accept_type == RESPONSE_TYPE_XML else False)
            if response:
                self.__print_response(response.text, accept_type)
        except Exception as e:
            print(str(e))
            logger.error(str(e))
def login(user, pswd):
    """ Log into ViPR """
    cookie = ViPRConnection.login(user, pswd)
    if cookie:
        #print("Copying cookie to: %s" % os.path.join(COOKIE_DIR, COOKIE_FILE_NAME))
        with open(os.path.join(ConfigUtil.COOKIE_DIR_ABS_PATH, Constants.COOKIE_FILE_NAME), 'w+') as f:
            f.write(cookie)
    return cookie
Пример #8
0
 def do_logout(self, args):
     """ Log out from ViPR """
     cookie = None
     logger.info("CMD: logout")
     try:
         try:
             cookie = self.__get_cookie()
         except Exception as e:
             logger.info("Cookie not found")
             pass
         if cookie:
             print("Logging out")
             logger.info("Logging out user")
             ViPRConnection.logout(cookie)
             os.remove(os.path.join(ConfigUtil.COOKIE_DIR_ABS_PATH, COOKIE_FILE_NAME))
     except Exception as e:
         print(str(e))
         logger.error(str(e))
Пример #9
0
def login(user, pswd):
    """ Log into ViPR """
    cookie = ViPRConnection.login(user, pswd)
    if cookie:
        #print("Copying cookie to: %s" % os.path.join(COOKIE_DIR, COOKIE_FILE_NAME))
        with open(
                os.path.join(ConfigUtil.COOKIE_DIR_ABS_PATH,
                             Constants.COOKIE_FILE_NAME), 'w+') as f:
            f.write(cookie)
    return cookie
Пример #10
0
 def do_logout(self, args):
     """ Log out from ViPR """
     cookie = None
     logger.info("CMD: logout")
     try:
         try:
             cookie = self.__get_cookie()
         except Exception as e:
             logger.info("Cookie not found")
             pass
         if cookie:
             print("Logging out")
             logger.info("Logging out user")
             ViPRConnection.logout(cookie)
             os.remove(
                 os.path.join(ConfigUtil.COOKIE_DIR_ABS_PATH,
                              COOKIE_FILE_NAME))
     except Exception as e:
         print(str(e))
         logger.error(str(e))
Пример #11
0
 def __get_id_by_key(self, key, search_name, search_context):
     try:
         search_paths = CommonUtil.get_search_path_by_key(key, search_context)
         cookie = self.__get_cookie()
         # search for this name in all search paths
         for path in search_paths:
             response = ViPRConnection.submitHttpRequest('GET', path+"?name="+search_name, cookie)
             if response:
                 search_json = json.loads(response.text)
                 if search_json and search_json["resource"]:
                     return search_json["resource"][0]["id"]
     except Exception:
         raise Exception("Name: %s not found" % search_name)
Пример #12
0
 def __get_id_by_key(self, key, search_name, search_context):
     try:
         search_paths = CommonUtil.get_search_path_by_key(
             key, search_context)
         cookie = self.__get_cookie()
         # search for this name in all search paths
         for path in search_paths:
             response = ViPRConnection.submitHttpRequest(
                 'GET', path + "?name=" + search_name, cookie)
             if response:
                 search_json = json.loads(response.text)
                 if search_json and search_json["resource"]:
                     return search_json["resource"][0]["id"]
     except Exception:
         raise Exception("Name: %s not found" % search_name)
Пример #13
0
 def do_login(self, args):
     """ Log into ViPR """
     logger.info("CMD: login %s" % args)
     if not args:
         print("login -username name -password pswd")
     else:
         try:
             args_arr = args.split()
             if args_arr[0] == "help":
                 print("login -username name -password pswd")
                 return
             cookie = ViPRConnection.login(args_arr[1], args_arr[3])
             if cookie:
                 #print("Copying cookie to: %s" % os.path.join(COOKIE_DIR, COOKIE_FILE_NAME))
                 with open(os.path.join(ConfigUtil.COOKIE_DIR_ABS_PATH, COOKIE_FILE_NAME), 'w+') as f:
                     f.write(cookie)
         except Exception as e:
             print(str(e))
             logger.error(str(e))
Пример #14
0
    def do_POST(self, args):
        """ Create resource """
        logger.info("CMD: POST %s" % args)
        global execute_action
        try:
            if ACTIONS_KEY not in curr_context:
                print('POST is not available')
                return

            actions_dict = curr_context[ACTIONS_KEY]
            # this is used when any default action is called
            if execute_action:
                curr_action = execute_action
            else:
                curr_action = 'POST'

            if 'help' == args:
                if actions_dict[curr_action]:
                    CommonUtil.print_query_params(
                        actions_dict[curr_action].query_params)
                    CommonUtil.print_attributes(
                        actions_dict[curr_action].method_name)
                return

            post_payload, query_str, content_type = self.__process_args(
                args, actions_dict[curr_action])
            cookie = self.__get_cookie()
            if execute_action:
                post_url = curr_path + '/' + execute_action
            else:
                post_url = curr_path

            response = ViPRConnection.submitHttpRequest(
                'POST',
                post_url + query_str,
                cookie,
                payload=post_payload,
                contentType=content_type)
            if response:
                self.__print_response(response.text)
        except Exception as e:
            print(str(e))
            logger.error(str(e))
Пример #15
0
 def do_login(self, args):
     """ Log into ViPR """
     logger.info("CMD: login %s" % args)
     if not args:
         print("login -username name -password pswd")
     else:
         try:
             args_arr = args.split()
             if args_arr[0] == "help":
                 print("login -username name -password pswd")
                 return
             cookie = ViPRConnection.login(args_arr[1], args_arr[3])
             if cookie:
                 #print("Copying cookie to: %s" % os.path.join(COOKIE_DIR, COOKIE_FILE_NAME))
                 with open(
                         os.path.join(ConfigUtil.COOKIE_DIR_ABS_PATH,
                                      COOKIE_FILE_NAME), 'w+') as f:
                     f.write(cookie)
         except Exception as e:
             print(str(e))
             logger.error(str(e))
Пример #16
0
    def do_PUT(self, args):
        """ Update resource """
        logger.info("CMD: PUT %s" % args)
        try:
            if ACTIONS_KEY not in curr_context:
                print('PUT is not available')
                return

            actions_dict = curr_context[ACTIONS_KEY]
            if 'help' == args:
                if actions_dict['PUT']:
                    CommonUtil.print_query_params(actions_dict['PUT'].query_params)
                    CommonUtil.print_attributes(actions_dict['PUT'].method_name)
                return

            put_payload, query_str, content_type = self.__process_args(args, actions_dict['PUT'])
            cookie = self.__get_cookie()
            response = ViPRConnection.submitHttpRequest('PUT', curr_path+query_str, cookie, payload=put_payload, contentType=content_type)
            if response:
                self.__print_response(response.text)
        except Exception as e:
            print(str(e))
            logger.error(str(e))
Пример #17
0
            print("python ViPRCommand -u name -p password")
            sys.exit()
        user = sys.argv[2]
        pswd = sys.argv[4]
        print("Connecting to ViPR: %s" % ConfigUtil.VIPR_HOST)
    else:
        print("Connecting to ViPR: %s" % ConfigUtil.VIPR_HOST)
        user = input("login as: ")
        pswd = getpass.getpass()

    # Login user
    cookie = login(user, pswd)
    logger.info("Logged in as user %s" % user)

    # Get ViPR version - used to name pickle file
    response = ViPRConnection.submitHttpRequest('GET', VERSION_URI, cookie)
    version_json = json.loads(response.text)
    vipr_version = version_json["target_version"]
    logger.info("ViPR Version: %s" % vipr_version)

    # pickle file name format:  {vipr_version}-context.pickle
    pickle_file_name = Constants.PICKLE_FILE_NAME.format(vipr_version)
    pickle_dir_path = CommonUtil.get_file_dir_location('pickles')
    pickle_file_path = os.path.join(pickle_dir_path, pickle_file_name)
    logger.info("Pickle file: %s" % pickle_file_name)

    # Check if this pickle version is available
    if not os.path.exists(pickle_file_path):
        logger.info("Pickle file not found, so creating")
        # Create directory if it doesn't exist
        descriptors_dir_path = CommonUtil.get_file_dir_location('descriptors')
Пример #18
0
        user = sys.argv[2]
        pswd = sys.argv[4]
        print("Connecting to ViPR: %s" % ConfigUtil.VIPR_HOST)
    else:
        print("Connecting to ViPR: %s" % ConfigUtil.VIPR_HOST)
        try: input = raw_input
        except NameError: pass
        user = input("login as: ")
        pswd = getpass.getpass()

    # Login user
    cookie = login(user, pswd)
    logger.info("Logged in as user %s" % user)

    # Get ViPR version - used to name pickle file
    response = ViPRConnection.submitHttpRequest('GET', VERSION_URI, cookie)
    version_json = json.loads(response.text)
    vipr_version = version_json["target_version"]
    logger.info("ViPR Version: %s" % vipr_version)

    # pickle file name format:  {vipr_version}-context.pickle
    pickle_file_name = Constants.PICKLE_FILE_NAME.format(vipr_version)
    pickle_dir_path = CommonUtil.get_file_dir_location('pickles')
    pickle_file_path = os.path.join(pickle_dir_path, pickle_file_name)
    logger.info("Pickle file: %s" % pickle_file_name)

    # Check if this pickle version is available
    if not os.path.exists(pickle_file_path):
        logger.info("Pickle file not found, so creating")
        # Create directory if it doesn't exist
        descriptors_dir_path = CommonUtil.get_file_dir_location('descriptors')