Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python - segregate grpc transport #369

Open
wants to merge 4 commits into
base: sustain_1.xx
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions openapiart/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,24 @@
import io
import sys
import time
import grpc
from google.protobuf import json_format
import sanity_pb2_grpc as pb2_grpc
import sanity_pb2 as pb2

try:
from typing import Union, Dict, List, Any, Literal
except ImportError:
from typing_extensions import Literal

# grpc-begin
import grpc
from google.protobuf import json_format
import sanity_pb2_grpc as pb2_grpc
import sanity_pb2 as pb2

# grpc-end
if sys.version_info[0] == 3:
unicode = str


openapi_warnings = []
# grpc-begin
class Transport:
HTTP = "http"
GRPC = "grpc"
Expand Down Expand Up @@ -87,6 +91,54 @@ class send_recv method to communicate with a REST based server.
raise Exception(msg % (ext, err))


# grpc-end
# http-begin
def api(
location=None,
verify=True,
logger=None,
loglevel=logging.INFO,
ext=None,
):
"""Create an instance of an Api class

generator.Generator outputs a base Api class with the following:
- an abstract method for each OpenAPI path item object
- a concrete properties for each unique OpenAPI path item parameter.

generator.Generator also outputs an HttpApi class that inherits the base
Api class, implements the abstract methods and uses the common HttpTransport
class send_recv method to communicate with a REST based server.

Args
----
- location (str): The location of an Open Traffic Generator server.
- transport (enum["http", "grpc"]): Transport Type
- verify (bool): Verify the server's TLS certificate, or a string, in which
case it must be a path to a CA bundle to use. Defaults to `True`.
When set to `False`, requests will accept any TLS certificate presented by
the server, and will ignore hostname mismatches and/or expired
certificates, which will make your application vulnerable to
man-in-the-middle (MitM) attacks. Setting verify to `False`
may be useful during local development or testing.
- logger (logging.Logger): A user defined logging.logger, if none is provided
then a default logger with a stdout handler will be provided
- loglevel (logging.loglevel): The logging package log level.
The default loglevel is logging.INFO
- ext (str): Name of an extension package
"""
params = locals()
if ext is None:
return HttpApi(**params)
try:
lib = importlib.import_module("{}_{}".format(__name__, ext))
return lib.Api(**params)
except ImportError as err:
msg = "Extension %s is not installed or invalid: %s"
raise Exception(msg % (ext, err))


# http-end
class HttpTransport(object):
def __init__(self, **kwargs):
"""Use args from api() method to instantiate an HTTP transport"""
Expand Down
43 changes: 30 additions & 13 deletions openapiart/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ def __init__(
protobuf_package_name,
output_dir=None,
extension_prefix=None,
py_generate_grpc=True,
):
self._parsers = {}
self._base_url = ""
self._generated_methods = []
self._generated_classes = []
self._generated_top_level_factories = []
self._py_generate_grpc = py_generate_grpc
self._openapi_filename = openapi_filename
self._extension_prefix = extension_prefix
self.__python = os.path.normpath(sys.executable)
Expand Down Expand Up @@ -142,19 +144,33 @@ def generate(self):
os.path.join(os.path.dirname(__file__), "common.py"), "r"
) as fp:
common_content = fp.read()
cnf_text = "import sanity_pb2_grpc as pb2_grpc"
modify_text = "try:\n from {pkg_name} {text}\nexcept ImportError:\n {text}".format(
pkg_name=self._package_name,
text=cnf_text.replace("sanity", self._protobuf_package_name),
)
common_content = common_content.replace(cnf_text, modify_text)
if self._py_generate_grpc:
grpc_chop = re.compile("# grpc-begin|# grpc-end", re.DOTALL)
common_content = grpc_chop.sub("", common_content)
http_chop = re.compile("# http-begin.*?# http-end", re.DOTALL)
common_content = http_chop.sub("", common_content)
cnf_text = "import sanity_pb2_grpc as pb2_grpc"
modify_text = "try:\n from {pkg_name} {text}\nexcept ImportError:\n {text}".format(
pkg_name=self._package_name,
text=cnf_text.replace(
"sanity", self._protobuf_package_name
),
)
common_content = common_content.replace(cnf_text, modify_text)

cnf_text = "import sanity_pb2 as pb2"
modify_text = "try:\n from {pkg_name} {text}\nexcept ImportError:\n {text}".format(
pkg_name=self._package_name,
text=cnf_text.replace("sanity", self._protobuf_package_name),
)
common_content = common_content.replace(cnf_text, modify_text)
cnf_text = "import sanity_pb2 as pb2"
modify_text = "try:\n from {pkg_name} {text}\nexcept ImportError:\n {text}".format(
pkg_name=self._package_name,
text=cnf_text.replace(
"sanity", self._protobuf_package_name
),
)
common_content = common_content.replace(cnf_text, modify_text)
else:
grpc_chop = re.compile("# grpc-begin.*?# grpc-end", re.DOTALL)
common_content = grpc_chop.sub("", common_content)
http_chop = re.compile("# http-begin|# http-end", re.DOTALL)
common_content = http_chop.sub("", common_content)

if re.search(r"def[\s+]api\(", common_content) is not None:
self._generated_top_level_factories.append("api")
Expand All @@ -172,7 +188,8 @@ def generate(self):
methods, factories, rpc_methods = self._get_methods_and_factories()
self._write_api_class(methods, factories)
self._write_http_api_class(methods)
self._write_rpc_api_class(rpc_methods)
if self._py_generate_grpc:
self._write_rpc_api_class(rpc_methods)
self._write_init()
return self

Expand Down
79 changes: 41 additions & 38 deletions openapiart/openapiart.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def _document(self):
except Exception as e:
print("Bypassed creation of static documentation: {}".format(e))

def GeneratePythonSdk(self, package_name):
def GeneratePythonSdk(self, package_name, generate_grpc=True):
"""Generates a Python UX Sdk
Args
----
Expand All @@ -141,7 +141,8 @@ def GeneratePythonSdk(self, package_name):
```
"""
self._python_module_name = package_name
self._generate_proto_file()
if generate_grpc:
self._generate_proto_file()
if self._python_module_name is not None:
module = importlib.import_module("openapiart.generator")
python_ux = getattr(module, "Generator")(
Expand All @@ -150,47 +151,49 @@ def GeneratePythonSdk(self, package_name):
self._protobuf_package_name,
output_dir=self._output_dir,
extension_prefix=self._extension_prefix,
py_generate_grpc=generate_grpc,
)
python_ux.generate()
try:
python_sdk_dir = os.path.normpath(
os.path.join(self._output_dir, self._python_module_name)
)
process_args = [
sys.executable,
"-m",
"grpc_tools.protoc",
"--python_out={}".format(python_sdk_dir),
"--grpc_python_out={}".format(python_sdk_dir),
"--proto_path={}".format(self._output_dir),
"--experimental_allow_proto3_optional",
"{}.proto".format(self._protobuf_package_name),
]
print(
"Generating python grpc stubs: {}".format(
" ".join(process_args)
python_sdk_dir = os.path.normpath(
os.path.join(self._output_dir, self._python_module_name)
)
if generate_grpc:
try:
process_args = [
sys.executable,
"-m",
"grpc_tools.protoc",
"--python_out={}".format(python_sdk_dir),
"--grpc_python_out={}".format(python_sdk_dir),
"--proto_path={}".format(self._output_dir),
"--experimental_allow_proto3_optional",
"{}.proto".format(self._protobuf_package_name),
]
print(
"Generating python grpc stubs: {}".format(
" ".join(process_args)
)
)
)
subprocess.check_call(process_args, shell=False)
subprocess.check_call(process_args, shell=False)

pb2_grpc_file = os.path.join(
python_sdk_dir,
"{}_pb2_grpc.py".format(self._protobuf_package_name),
)
current_text = (
"import {proto_name}_pb2 as {proto_name}__pb2".format(
proto_name=self._protobuf_package_name
pb2_grpc_file = os.path.join(
python_sdk_dir,
"{}_pb2_grpc.py".format(self._protobuf_package_name),
)
)
new_text = "try:\n {text}\nexcept ImportError:\n from {pkg_name} {text}".format(
text=current_text, pkg_name=self._python_module_name
)
with open(pb2_grpc_file) as f:
file_contents = f.read().replace(current_text, new_text)
with open(pb2_grpc_file, "w") as f:
f.write(file_contents)
except Exception as e:
print("Bypassed creation of python stubs: {}".format(e))
current_text = (
"import {proto_name}_pb2 as {proto_name}__pb2".format(
proto_name=self._protobuf_package_name
)
)
new_text = "try:\n {text}\nexcept ImportError:\n from {pkg_name} {text}".format(
text=current_text, pkg_name=self._python_module_name
)
with open(pb2_grpc_file) as f:
file_contents = f.read().replace(current_text, new_text)
with open(pb2_grpc_file, "w") as f:
f.write(file_contents)
except Exception as e:
print("Bypassed creation of python stubs: {}".format(e))
# Auto formatting generated python SDK with Black
if sys.version_info[0] == 3:
process_args = [
Expand Down
88 changes: 88 additions & 0 deletions openapiart/tests/test_without_grpc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import os
import sys
import time
import pytest
import importlib
import logging
from openapiart.openapiart import OpenApiArt as openapiart_class
from .server import app

pytest.withoutgrpc_module = "withoutgrpc"


def create_without_grpc_artifacts(openapiart_class):
open_api = openapiart_class(
api_files=[
os.path.join(os.path.dirname(__file__), "./api/info.yaml"),
os.path.join(os.path.dirname(__file__), "./common/common.yaml"),
os.path.join(os.path.dirname(__file__), "./api/api.yaml"),
],
artifact_dir=os.path.join(
os.path.dirname(__file__), "..", "..", "art_without_grpc"
),
extension_prefix=pytest.withoutgrpc_module,
)
open_api.GeneratePythonSdk(
package_name=pytest.withoutgrpc_module,
generate_grpc=False,
)


def test_module():
create_without_grpc_artifacts(openapiart_class)
sys.path.append(
os.path.join(
os.path.dirname(__file__),
"..",
"..",
"art_without_grpc",
pytest.withoutgrpc_module,
)
)
module = importlib.import_module(pytest.withoutgrpc_module)
api = module.api()
assert api.__module__ == pytest.withoutgrpc_module
with pytest.raises(Exception) as execinfo:
importlib.import_module(pytest.withoutgrpc_module + "_pb2")
assert "No module named" in execinfo.value.args[0]
assert "withoutgrpc_pb2" in execinfo.value.args[0]


def test_http_client():
module = importlib.import_module(pytest.withoutgrpc_module)
api = module.api(
location="http://127.0.0.1:{}".format(app.PORT),
verify=False,
logger=None,
loglevel=logging.DEBUG,
)
# verify http server is up
attempts = 1
while True:
try:
api.get_config()
break
except Exception as e:
print(e)
if attempts > 5:
raise (e)
time.sleep(0.5)
attempts += 1
assert api.__module__ == pytest.withoutgrpc_module

config = api.prefix_config()
config.a = "asdf"
config.b = 1.1
config.c = 1
config.required_object.e_a = 1.1
config.required_object.e_b = 1.2
config.d_values = [config.A, config.B, config.C]
config.level.l1_p1.l2_p1.l3_p1 = "test"
config.level.l1_p2.l4_p1.l1_p2.l4_p1.l1_p1.l2_p1.l3_p1 = "test"
api.set_config(config)
_config = api.get_config()
assert config.serialize(config.DICT) == _config.serialize(_config.DICT)


if __name__ == "__main__":
pytest.main(["-v", "-s", __file__])