示例#1
0
    def __MakeApigatewayApiConfigGrpcServiceDefinitionMessage(
            self, proto_desc_contents, proto_desc_file):
        """Constructs a GrpcServiceDefinition message from a proto descriptor and the provided list of input files.

    Args:
      proto_desc_contents: The contents of the proto descriptor file.
      proto_desc_file: The path to the proto descriptor file.

    Returns:
      The constructed ApigatewayApiConfigGrpcServiceDefinition message.
    """

        messages = apigateway_base.GetMessagesModule()
        fds = descriptor.FileDescriptorSet.FromString(proto_desc_contents)
        proto_desc_dir = os.path.dirname(proto_desc_file)
        grpc_sources = []
        included_source_paths = []
        not_included_source_paths = []

        # Iterate over the file descriptors dependency files and attempt to resolve
        # the gRPC source proto files from it.
        for file_descriptor in fds.file:
            source_path = os.path.join(proto_desc_dir, file_descriptor.name)
            if os.path.exists(source_path):
                source_contents = endpoints.ReadServiceConfigFile(source_path)
                file = self.__MakeApigatewayApiConfigFileMessage(
                    source_contents, source_path)
                included_source_paths.append(source_path)
                grpc_sources.append(file)
            else:
                not_included_source_paths.append(source_path)

        if not_included_source_paths:
            log.warning(
                'Proto descriptor\'s source protos [{0}] were not found on'
                ' the file system and will not be included in the submitted'
                ' GRPC service definition. If you meant to include these'
                ' files, ensure the proto compiler was invoked in the same'
                ' directory where the proto descriptor [{1}] now resides.'.
                format(', '.join(not_included_source_paths), proto_desc_file))

        # Log which files are being passed in as to ensure the user is informed of
        # all files being passed into the gRPC service definition.
        if included_source_paths:
            log.info(
                'Added the source protos [{0}] to the GRPC service definition'
                ' for the provided proto descriptor [{1}].'.format(
                    ', '.join(included_source_paths), proto_desc_file))

        file_descriptor_set = self.__MakeApigatewayApiConfigFileMessage(
            proto_desc_contents, proto_desc_file, True)
        return messages.ApigatewayApiConfigGrpcServiceDefinition(
            fileDescriptorSet=file_descriptor_set, source=grpc_sources)
示例#2
0
    def __OpenApiMessage(self, open_api_specs):
        """Parses the Open API scoped configuraiton files into their necessary API Gateway message types.

    Args:
      open_api_specs: Specs to be used with the API Gateway API Configuration

    Returns:
      List of ApigatewayApiConfigOpenApiDocument messages

    Raises:
      BadFileException: If there is something wrong with the files
    """
        messages = apigateway_base.GetMessagesModule()
        config_files = []
        for config_file in open_api_specs:
            config_contents = endpoints.ReadServiceConfigFile(config_file)

            config_dict = self.__ValidJsonOrYaml(config_file, config_contents)
            if config_dict:
                if 'swagger' in config_dict:
                    # Always use YAML for OpenAPI because JSON is a subset of YAML.
                    document = self.__MakeApigatewayApiConfigFileMessage(
                        config_contents, config_file)
                    config_files.append(
                        messages.ApigatewayApiConfigOpenApiDocument(
                            document=document))
                elif 'openapi' in config_dict:
                    raise calliope_exceptions.BadFileException(
                        'API Gateway does not currently support OpenAPI v3 configurations.'
                    )
                else:
                    raise calliope_exceptions.BadFileException(
                        'The file {} is not a valid OpenAPI v2 configuration file.'
                        .format(config_file))
            else:
                raise calliope_exceptions.BadFileException(
                    'OpenAPI files should be of JSON or YAML format')
        return config_files
示例#3
0
    def __MakeApigatewayApiConfigFileMessage(self,
                                             file_contents,
                                             filename,
                                             is_binary=False):
        """Constructs a ConfigFile message from a config file.

    Args:
      file_contents: The contents of the config file.
      filename: The path to the config file.
      is_binary: If set to true, the file_contents won't be encoded.

    Returns:
      The constructed ApigatewayApiConfigFile message.
    """

        messages = apigateway_base.GetMessagesModule()
        if not is_binary:
            # File is human-readable text, not binary; needs to be encoded.
            file_contents = http_encoding.Encode(file_contents)
        return messages.ApigatewayApiConfigFile(
            contents=file_contents,
            path=os.path.basename(filename),
        )