Exemple #1
0
def main():
    # Get user input.
    metadata_api_url, rest_navigation_url, output_dir, verify, enable_filtering, GENERATE_METAMODEL, SPECIFICATION, GENERATE_UNIQUE_OP_IDS, TAG_SEPARATOR = connection.get_input_params()
    # Maps enumeration id to enumeration info
    enumeration_dict = {}
    # Maps structure_id to structure_info
    structure_dict = {}
    # Maps service_id to service_info
    service_dict = {}
    # Maps service url to service id
    service_urls_map = {}

    start = timeit.default_timer()
    print('Trying to connect ' + metadata_api_url)
    session = requests.session()
    session.verify = False
    connector = get_requests_connector(session, url=metadata_api_url)
    if not enable_filtering:
        connector.set_application_context(
            ApplicationContext({SHOW_UNRELEASED_APIS: "True"}))
    print('Connected to ' + metadata_api_url)
    component_svc = connection.get_component_service(connector)
    dict_processing.populate_dicts(
        component_svc,
        enumeration_dict,
        structure_dict,
        service_dict,
        service_urls_map,
        rest_navigation_url,
        GENERATE_METAMODEL)
    if enable_filtering:
        service_urls_map = dict_processing.get_service_urls_from_rest_navigation(
            rest_navigation_url, verify)

    http_error_map = utils.HttpErrorMap(component_svc)
    
    # package_dict_api holds list of all service urls which come under /api
    package_dict_api, package_dict = dict_processing.add_service_urls_using_metamodel(
        service_urls_map, service_dict, rest_navigation_url)

    rest = RestUrlProcessing()
    api = ApiUrlProcessing()

    threads = []
    for package, service_urls in six.iteritems(package_dict):
        worker = threading.Thread(
            target=rest.process_service_urls,
            args=(
                package,
                service_urls,
                output_dir,
                structure_dict,
                enumeration_dict,
                service_dict,
                service_urls_map,
                http_error_map,
                rest_navigation_url,
                enable_filtering,
                SPECIFICATION,
                GENERATE_UNIQUE_OP_IDS))
        worker.daemon = True
        worker.start()
        threads.append(worker)

    for package, service_urls in six.iteritems(package_dict_api):
        worker = threading.Thread(
            target=api.process_service_urls,
            args=(
                package,
                service_urls,
                output_dir,
                structure_dict,
                enumeration_dict,
                service_dict,
                service_urls_map,
                http_error_map,
                rest_navigation_url,
                enable_filtering,
                SPECIFICATION,
                GENERATE_UNIQUE_OP_IDS))
        worker.daemon = True
        worker.start()
        threads.append(worker)
    for worker in threads:
        worker.join()

    # api.json contains list of packages which is used by UI to dynamically
    # populate dropdown.
    api_files_list = []
    for name in list(package_dict.keys()):
        api_files_list.append("rest_" + name)

    for name in list(package_dict_api.keys()):
        api_files_list.append("api_" + name)

    api_files = {'files': api_files_list}
    utils.write_json_data_to_file(
        output_dir +
        os.path.sep +
        'api.json',
        api_files)
    stop = timeit.default_timer()
    print('Generated swagger files at ' + output_dir + ' for ' +
          metadata_api_url + ' in ' + str(stop - start) + ' seconds')
Exemple #2
0
    def test_populate_response_map(self):
        # get response map corresponding to errors in operation information
        user_defined_type_mock = mock.Mock()
        user_defined_type_mock.resource_type = 'com.vmware.vapi.structure'
        user_defined_type_mock.resource_id = 'com.vmware.package.mock'
        output_mock =  mock.Mock()
        output_mock_type = mock.Mock()
        output_mock_type.category = 'USER_DEFINED'
        output_mock_type.user_defined_type = user_defined_type_mock
        output_mock.documentation = 'mock output description'
        output_mock.type = output_mock_type
        error_mock =  mock.Mock()
        error_mock.structure_id = 'com.vmware.vapi.std.errors.not_found'
        error_mock.documentation = 'mock error description'
        errors = [error_mock]
        type_dict = {
                'com.vmware.vapi.std.errors.not_found' : {},
                'com.vmware.package.mock' : { 
                    'description' : 'mock description',
                    'type': 'string',
                    'enum': ['enum-1', 'enum-2']
                }
            }
        '''
        Mock parameters : output and errors
        output( documentation = 'mock output description', 
        type = Type( category = 'USER_DEFINED', 
        user_defined_type = UserDefinedType( resource_id = 'com.vmware.package.mock', 
        resource_type = 'com.vmware.vapi.structure')))

        errors = [error( documentation = 'mock error description', structure_id = 'com.vmware.vapi.std.errors.not_found')]
        '''
        structure_info_mock = mock.Mock()
        metadata_mock = mock.Mock()
        element_value_mock = mock.Mock()
        element_value_mock.string_value = '404'
        metadata_mock.elements = { 'code' : element_value_mock}
        structure_info_mock.metadata = { 'Response' : metadata_mock}
        structures = { 'com.vmware.mock.structure' : structure_info_mock  }
        structures_obj = mock.Mock()
        structures_obj.structures = structures
        info_mock = mock.Mock()
        info_mock.packages = { 'com.vmware.vapi.std.errors' : structures_obj}
        component_svc_obj = mock.Mock()
        component_svc_obj.info = info_mock
        component_svc_mock = { 'com.vmware.vapi' : component_svc_obj}
        '''
        Mock parameter : component_svc
        component_svc = { 'com.vmware.vapi' : Component( info = ComponentInfo(
            packages = {'com.vmware.vapi.std.errors' : StructureInfo (structures = {
                'com.vmware.mock.structure' : StructureInfo(metadata = {
                    'Response' : { ElementMap( elements = ElementValue( string_value = '404'))}
                })
            })}
        ))}
        '''
        op_metadata = {'Response': metadata_mock}
        http_error_map = utils.HttpErrorMap(component_svc_mock)
        api_swagger_resphandler = ApiSwaggerRespHandler()
        response_map_actual = api_swagger_resphandler.populate_response_map(output_mock, errors, 
                                    http_error_map,type_dict, {}, {}, 'mock-service-id',
                                   'mock-operation-id', op_metadata, False)
        
        response_map_expected = {
            404: {
                'description': 'mock output description',
                'schema': {'$ref': '#/definitions/ComVmwarePackageMock'}
            },
            500: {
                'description': 'mock error description',
                'schema': {'$ref': '#/definitions/ComVmwareVapiStdErrorsNotFound'}
            }
        }
        self.assertEqual(response_map_expected, response_map_actual)
Exemple #3
0
def main():
    # Get user input.
    metadata_api_url, \
    rest_navigation_url, \
    output_dir, \
    verify, \
    show_unreleased_apis, \
    GENERATE_METAMODEL, \
    SPECIFICATION, \
    GENERATE_UNIQUE_OP_IDS, \
    TAG_SEPARATOR, \
    DEPRECATE_REST,\
    fetch_auth_metadata,\
    auto_rest_services = connection.get_input_params()
    # Maps enumeration id to enumeration info
    enumeration_dict = {}
    # Maps structure_id to structure_info
    structure_dict = {}
    # Maps service_id to service_info
    service_dict = {}
    # Maps service url to service id
    service_urls_map = {}

    rest_navigation_handler = RestNavigationHandler(rest_navigation_url)

    start = timeit.default_timer()
    print('Trying to connect ' + metadata_api_url)
    session = requests.session()
    session.verify = False
    connector = get_requests_connector(session, url=metadata_api_url)

    if show_unreleased_apis:
        connector.set_application_context(
            ApplicationContext({SHOW_UNRELEASED_APIS: "True"}))
    print('Connected to ' + metadata_api_url)
    component_svc = connection.get_component_service(connector)

    auth_navigator = None
    if fetch_auth_metadata:
        # Fetch authentication metadata and initialize the authentication data navigator
        auth_component_svc = connection.get_authentication_component_service(
            connector)
        auth_dict = authentication_metadata_processing.get_authentication_dict(
            auth_component_svc)
        auth_navigator = AuthenticationDictNavigator(auth_dict)

    dict_processing.populate_dicts(component_svc, enumeration_dict,
                                   structure_dict, service_dict,
                                   service_urls_map, rest_navigation_url,
                                   GENERATE_METAMODEL)

    http_error_map = utils.HttpErrorMap(component_svc)

    deprecation_handler = None

    # package_dict_api holds list of all service urls which come under /api
    # package_dict_deprecated holds a list of all service urls which come under /rest, but are
    # deprecated with /api
    # replacement_dict contains information about the deprecated /rest to /api mappings
    package_dict_api, package_dict, package_dict_deprecated, replacement_dict = dict_processing.add_service_urls_using_metamodel(
        service_urls_map, service_dict, rest_navigation_handler,
        auto_rest_services, DEPRECATE_REST)

    utils.combine_dicts_with_list_values(package_dict, package_dict_deprecated)
    if DEPRECATE_REST:
        deprecation_handler = RestDeprecationHandler(replacement_dict)

    rest = RestMetadataProcessor()
    api = ApiMetadataProcessor()

    rest_package_spec_dict = {}
    api_package_spec_dict = {}

    with futures.ThreadPoolExecutor() as executor:
        rest_package_future_dict = {
            package:
            executor.submit(rest.get_path_and_type_dicts, package,
                            service_urls, structure_dict, enumeration_dict,
                            service_dict, service_urls_map, http_error_map,
                            rest_navigation_handler, show_unreleased_apis,
                            SPECIFICATION, auth_navigator, deprecation_handler)
            for package, service_urls in six.iteritems(package_dict)
        }

        api_package_future_dict = {
            package:
            executor.submit(api.get_path_and_type_dicts, package, service_urls,
                            structure_dict, enumeration_dict, service_dict,
                            service_urls_map, http_error_map,
                            show_unreleased_apis, SPECIFICATION,
                            auth_navigator)
            for package, service_urls in six.iteritems(package_dict_api)
        }

        rest_package_spec_dict = {
            package: future.result()
            for package, future in six.iteritems(rest_package_future_dict)
        }
        api_package_spec_dict = {
            package: future.result()
            for package, future in six.iteritems(api_package_future_dict)
        }

    file_handler = FileOutputHandler(rest_package_spec_dict,
                                     api_package_spec_dict, output_dir,
                                     GENERATE_UNIQUE_OP_IDS, SPECIFICATION)
    file_handler.output_files()

    stop = timeit.default_timer()
    print('Generated swagger files at ' + output_dir + ' for ' +
          metadata_api_url + ' in ' + str(stop - start) + ' seconds')