diff --git a/instill/clients/instance.py b/instill/clients/instance.py index 50d219b..d9b4679 100644 --- a/instill/clients/instance.py +++ b/instill/clients/instance.py @@ -1,14 +1,15 @@ from typing import Union -import grpc +import grpc +import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service -import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service class InstillInstance: def __init__(self, stub, url: str, token: str, secure: bool, asyncio: bool): + self.url: str = url self.token: str = token self.asyncio: bool = asyncio if not secure: @@ -26,7 +27,7 @@ def __init__(self, stub, url: str, token: str, secure: bool, asyncio: bool): call_creds = grpc.access_token_call_credentials(token) creds = grpc.composite_channel_credentials(ssl_creds, call_creds) channel = grpc.secure_channel(target=url, credentials=creds) - self.metadata = "" + self.metadata = (("", ""),) if asyncio: async_channel = grpc.aio.secure_channel(target=url, credentials=creds) self.channel: grpc.Channel = channel diff --git a/instill/clients/mgmt.py b/instill/clients/mgmt.py index a774fdb..21a4dd7 100644 --- a/instill/clients/mgmt.py +++ b/instill/clients/mgmt.py @@ -1,16 +1,17 @@ # pylint: disable=no-member,wrong-import-position from typing import Dict + import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck # mgmt import instill.protogen.core.mgmt.v1alpha.metric_pb2 as metric_interface import instill.protogen.core.mgmt.v1alpha.mgmt_pb2 as mgmt_interface import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service +from instill.clients.base import Client # common from instill.clients.constant import DEFAULT_INSTANCE from instill.clients.instance import InstillInstance -from instill.clients.base import Client from instill.configuration import global_config from instill.utils.error_handler import grpc_handler diff --git a/instill/clients/model.py b/instill/clients/model.py index 6fb949c..1f77def 100644 --- a/instill/clients/model.py +++ b/instill/clients/model.py @@ -1,21 +1,20 @@ # pylint: disable=no-member,wrong-import-position import time -from typing import Iterable, Tuple, Union, Dict +from typing import Dict, Iterable, Tuple, Union from google.longrunning import operations_pb2 from google.protobuf import field_mask_pb2 +# common +import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck +import instill.protogen.model.model.v1alpha.model_definition_pb2 as model_definition_interface # model import instill.protogen.model.model.v1alpha.model_pb2 as model_interface import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service -import instill.protogen.model.model.v1alpha.model_definition_pb2 as model_definition_interface - -# common -import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck +from instill.clients.base import Client from instill.clients.constant import DEFAULT_INSTANCE from instill.clients.instance import InstillInstance -from instill.clients.base import Client from instill.configuration import global_config from instill.utils.error_handler import grpc_handler diff --git a/instill/clients/pipeline.py b/instill/clients/pipeline.py index 7377e57..b021392 100644 --- a/instill/clients/pipeline.py +++ b/instill/clients/pipeline.py @@ -1,20 +1,20 @@ # pylint: disable=no-member,wrong-import-position -from typing import Iterable, Tuple, Union, Dict +from typing import Dict, Iterable, Tuple, Union from google.longrunning import operations_pb2 from google.protobuf import field_mask_pb2 +# common +import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck + # pipeline import instill.protogen.vdp.pipeline.v1alpha.connector_pb2 as connector_interface import instill.protogen.vdp.pipeline.v1alpha.operator_definition_pb2 as operator_interface import instill.protogen.vdp.pipeline.v1alpha.pipeline_pb2 as pipeline_interface import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service - -# common -import instill.protogen.common.healthcheck.v1alpha.healthcheck_pb2 as healthcheck +from instill.clients.base import Client from instill.clients.constant import DEFAULT_INSTANCE from instill.clients.instance import InstillInstance -from instill.clients.base import Client from instill.configuration import global_config from instill.utils.error_handler import grpc_handler diff --git a/instill/tests/test_client.py b/instill/tests/test_client.py index 8d6d00b..f1ab196 100644 --- a/instill/tests/test_client.py +++ b/instill/tests/test_client.py @@ -1,49 +1,118 @@ # pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,no-name-in-module -from collections import defaultdict - +import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service +import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service +import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service from instill.clients import MgmtClient, ModelClient, PipelineClient +from instill.clients.instance import InstillInstance def describe_client(): def describe_instance(): def when_not_set(expect): - mgmt_client = MgmtClient() + mgmt_client = MgmtClient(False) expect(mgmt_client.instance) == "" - model_client = ModelClient(namespace="") + model_client = ModelClient(namespace="", asyncio=False) expect(model_client.instance) == "" - pipeline_client = PipelineClient(namespace="") + pipeline_client = PipelineClient(namespace="", asyncio=False) expect(pipeline_client.instance) == "" def when_set_correct_type(expect): - mgmt_client = MgmtClient() + mgmt_client = MgmtClient(False) mgmt_client.instance = "staging" expect(mgmt_client.instance) == "staging" - model_client = ModelClient(namespace="") + model_client = ModelClient(namespace="", asyncio=False) model_client.instance = "staging" expect(model_client.instance) == "staging" - pipeline_client = PipelineClient(namespace="") + pipeline_client = PipelineClient(namespace="", asyncio=False) pipeline_client.instance = "staging" expect(pipeline_client.instance) == "staging" def describe_host(): def when_not_set(expect): - mgmt_client = MgmtClient() + mgmt_client = MgmtClient(False) expect(mgmt_client.hosts) is None - model_client = ModelClient(namespace="") + model_client = ModelClient(namespace="", asyncio=False) expect(model_client.hosts) is None - pipeline_client = PipelineClient(namespace="") + pipeline_client = PipelineClient(namespace="", asyncio=False) expect(pipeline_client.hosts) is None - def when_set_correct_type(expect): - mgmt_client = MgmtClient() - d = defaultdict(dict) # type: ignore - d["test_instance"] = dict({"url": "test_url"}) - mgmt_client.hosts = d - expect(mgmt_client.hosts["test_instance"]["url"]) == "test_url" - model_client = ModelClient(namespace="") - model_client.hosts = d - expect(model_client.hosts["test_instance"]["url"]) == "test_url" - pipeline_client = PipelineClient(namespace="") - pipeline_client.hosts = d - expect(pipeline_client.hosts["test_instance"]["url"]) == "test_url" + def when_set_correct_type_url(expect): + mgmt_instance = { + "test_instance": InstillInstance( + mgmt_service.MgmtPublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + mgmt_client = MgmtClient(False) + mgmt_client.hosts = mgmt_instance + expect(mgmt_client.hosts["test_instance"].url) == "test_url" + + model_instance = { + "test_instance": InstillInstance( + model_service.ModelPublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + model_client = ModelClient(namespace="", asyncio=False) + model_client.hosts = model_instance + expect(model_client.hosts["test_instance"].url) == "test_url" + + pipeline_instance = { + "test_instance": InstillInstance( + pipeline_service.PipelinePublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + pipeline_client = PipelineClient(namespace="", asyncio=False) + pipeline_client.hosts = pipeline_instance + expect(pipeline_client.hosts["test_instance"].url) == "test_url" + + def when_set_correct_type_token(expect): + mgmt_instance = { + "test_instance": InstillInstance( + mgmt_service.MgmtPublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + mgmt_client = MgmtClient(False) + mgmt_client.hosts = mgmt_instance + expect(mgmt_client.hosts["test_instance"].token) == "token" + + model_instance = { + "test_instance": InstillInstance( + model_service.ModelPublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + model_client = ModelClient(namespace="", asyncio=False) + model_client.hosts = model_instance + expect(model_client.hosts["test_instance"].token) == "token" + + pipeline_instance = { + "test_instance": InstillInstance( + pipeline_service.PipelinePublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + pipeline_client = PipelineClient(namespace="", asyncio=False) + pipeline_client.hosts = pipeline_instance + expect(pipeline_client.hosts["test_instance"].token) == "token" diff --git a/tests/test_client.py b/tests/test_client.py index 8d6d00b..f1ab196 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -1,49 +1,118 @@ # pylint: disable=redefined-outer-name,unused-variable,expression-not-assigned,no-name-in-module -from collections import defaultdict - +import instill.protogen.core.mgmt.v1alpha.mgmt_public_service_pb2_grpc as mgmt_service +import instill.protogen.model.model.v1alpha.model_public_service_pb2_grpc as model_service +import instill.protogen.vdp.pipeline.v1alpha.pipeline_public_service_pb2_grpc as pipeline_service from instill.clients import MgmtClient, ModelClient, PipelineClient +from instill.clients.instance import InstillInstance def describe_client(): def describe_instance(): def when_not_set(expect): - mgmt_client = MgmtClient() + mgmt_client = MgmtClient(False) expect(mgmt_client.instance) == "" - model_client = ModelClient(namespace="") + model_client = ModelClient(namespace="", asyncio=False) expect(model_client.instance) == "" - pipeline_client = PipelineClient(namespace="") + pipeline_client = PipelineClient(namespace="", asyncio=False) expect(pipeline_client.instance) == "" def when_set_correct_type(expect): - mgmt_client = MgmtClient() + mgmt_client = MgmtClient(False) mgmt_client.instance = "staging" expect(mgmt_client.instance) == "staging" - model_client = ModelClient(namespace="") + model_client = ModelClient(namespace="", asyncio=False) model_client.instance = "staging" expect(model_client.instance) == "staging" - pipeline_client = PipelineClient(namespace="") + pipeline_client = PipelineClient(namespace="", asyncio=False) pipeline_client.instance = "staging" expect(pipeline_client.instance) == "staging" def describe_host(): def when_not_set(expect): - mgmt_client = MgmtClient() + mgmt_client = MgmtClient(False) expect(mgmt_client.hosts) is None - model_client = ModelClient(namespace="") + model_client = ModelClient(namespace="", asyncio=False) expect(model_client.hosts) is None - pipeline_client = PipelineClient(namespace="") + pipeline_client = PipelineClient(namespace="", asyncio=False) expect(pipeline_client.hosts) is None - def when_set_correct_type(expect): - mgmt_client = MgmtClient() - d = defaultdict(dict) # type: ignore - d["test_instance"] = dict({"url": "test_url"}) - mgmt_client.hosts = d - expect(mgmt_client.hosts["test_instance"]["url"]) == "test_url" - model_client = ModelClient(namespace="") - model_client.hosts = d - expect(model_client.hosts["test_instance"]["url"]) == "test_url" - pipeline_client = PipelineClient(namespace="") - pipeline_client.hosts = d - expect(pipeline_client.hosts["test_instance"]["url"]) == "test_url" + def when_set_correct_type_url(expect): + mgmt_instance = { + "test_instance": InstillInstance( + mgmt_service.MgmtPublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + mgmt_client = MgmtClient(False) + mgmt_client.hosts = mgmt_instance + expect(mgmt_client.hosts["test_instance"].url) == "test_url" + + model_instance = { + "test_instance": InstillInstance( + model_service.ModelPublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + model_client = ModelClient(namespace="", asyncio=False) + model_client.hosts = model_instance + expect(model_client.hosts["test_instance"].url) == "test_url" + + pipeline_instance = { + "test_instance": InstillInstance( + pipeline_service.PipelinePublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + pipeline_client = PipelineClient(namespace="", asyncio=False) + pipeline_client.hosts = pipeline_instance + expect(pipeline_client.hosts["test_instance"].url) == "test_url" + + def when_set_correct_type_token(expect): + mgmt_instance = { + "test_instance": InstillInstance( + mgmt_service.MgmtPublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + mgmt_client = MgmtClient(False) + mgmt_client.hosts = mgmt_instance + expect(mgmt_client.hosts["test_instance"].token) == "token" + + model_instance = { + "test_instance": InstillInstance( + model_service.ModelPublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + model_client = ModelClient(namespace="", asyncio=False) + model_client.hosts = model_instance + expect(model_client.hosts["test_instance"].token) == "token" + + pipeline_instance = { + "test_instance": InstillInstance( + pipeline_service.PipelinePublicServiceStub, + "test_url", + "token", + False, + False, + ) + } + pipeline_client = PipelineClient(namespace="", asyncio=False) + pipeline_client.hosts = pipeline_instance + expect(pipeline_client.hosts["test_instance"].token) == "token"