-
Notifications
You must be signed in to change notification settings - Fork 0
Launcher: implement gRPC transport option handling #116
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
52aa7f8
Launcher: implement gRPC transport option handling
greschd 1c3d25f
chore: adding changelog file 116.added.md [dependabot-skip]
pyansys-ci-bot 1983c97
Use ProductInstanceError
greschd 7b68a2b
Update LauncherProtocol
greschd 07ef370
Merge branch 'main' into feat/launcher-grpc-transport-options
greschd 2f8e4c0
Allow using the deprecated entry point
greschd 182db3e
Merge branch 'main' of https://github.com/ansys/ansys-tools-common in…
greschd be0b398
Revert accidental changes
greschd 7e03870
Merge branch 'main' into feat/launcher-grpc-transport-options
greschd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Launcher: implement gRPC transport option handling |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| # Copyright (C) 2025 ANSYS, Inc. and/or its affiliates. | ||
greschd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # SPDX-License-Identifier: MIT | ||
| # | ||
| # | ||
| # Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| # of this software and associated documentation files (the "Software"), to deal | ||
| # in the Software without restriction, including without limitation the rights | ||
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| # copies of the Software, and to permit persons to whom the Software is | ||
| # furnished to do so, subject to the following conditions: | ||
| # | ||
| # The above copyright notice and this permission notice shall be included in all | ||
| # copies or substantial portions of the Software. | ||
| # | ||
| # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| # SOFTWARE. | ||
|
|
||
| """Defines options for connecting to a gRPC server.""" | ||
|
|
||
| from abc import ABC, abstractmethod | ||
| from dataclasses import asdict, dataclass | ||
| import enum | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING, Any, ClassVar | ||
|
|
||
| import grpc | ||
|
|
||
| from .. import cyberchannel | ||
|
|
||
| __all__ = [ | ||
| "TransportMode", | ||
| "UDSOptions", | ||
| "WNUAOptions", | ||
| "MTLSOptions", | ||
| "InsecureOptions", | ||
| "TransportOptionsType", | ||
| ] | ||
|
|
||
| # For Python 3.10 and below, emulate the behavior of StrEnum by | ||
| # inheriting from str and enum.Enum. | ||
| # Note that this does *not* work on Python 3.11+, since the default | ||
| # Enum format method has changed and will not return the value of | ||
| # the enum member. | ||
| # When type checking, always use the Python 3.10 workaround, otherwise | ||
| # the StrEnum resolves as 'Any'. | ||
| if TYPE_CHECKING: # pragma: no cover | ||
|
|
||
| class StrEnum(str, enum.Enum): | ||
| """String enum.""" | ||
|
|
||
| else: | ||
| try: | ||
| from enum import StrEnum | ||
| except ImportError: | ||
| import enum | ||
|
|
||
| class StrEnum(str, enum.Enum): | ||
| """String enum.""" | ||
|
|
||
| pass | ||
|
|
||
|
|
||
| class TransportMode(StrEnum): | ||
| """Enumeration of transport modes supported by the FileTransfer Tool.""" | ||
|
|
||
| UDS = "uds" | ||
| WNUA = "wnua" | ||
| MTLS = "mtls" | ||
| INSECURE = "insecure" | ||
|
|
||
|
|
||
| class TransportOptionsBase(ABC): | ||
| """Base class for transport options.""" | ||
|
|
||
| _MODE: ClassVar[TransportMode] | ||
|
|
||
| @property | ||
| def mode(self) -> TransportMode: | ||
| """Transport mode.""" | ||
| return self._MODE | ||
|
|
||
| def create_channel(self, **extra_kwargs: Any) -> grpc.Channel: | ||
| """Create a gRPC channel using the transport options. | ||
| Parameters | ||
| ---------- | ||
| extra_kwargs : | ||
| Extra keyword arguments to pass to the channel creation function. | ||
| Returns | ||
| ------- | ||
| : | ||
| gRPC channel created using the transport options. | ||
| """ | ||
| return cyberchannel.create_channel(**self._to_cyberchannel_kwargs(), **extra_kwargs) | ||
|
|
||
| @abstractmethod | ||
| def _to_cyberchannel_kwargs(self) -> dict[str, Any]: | ||
| """Convert transport options to cyberchannel keyword arguments. | ||
| Returns | ||
| ------- | ||
| : | ||
| Dictionary of keyword arguments for cyberchannel. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| @dataclass(kw_only=True) | ||
| class UDSOptions(TransportOptionsBase): | ||
| """Options for UDS transport mode.""" | ||
|
|
||
| _MODE = TransportMode.UDS | ||
|
|
||
| uds_service: str | ||
| uds_dir: str | Path | None = None | ||
| uds_id: str | None = None | ||
|
|
||
| def _to_cyberchannel_kwargs(self) -> dict[str, Any]: | ||
| return asdict(self) | {"transport_mode": self.mode.value} | ||
|
|
||
|
|
||
| @dataclass(kw_only=True) | ||
| class WNUAOptions(TransportOptionsBase): | ||
| """Options for WNUA transport mode.""" | ||
|
|
||
| _MODE = TransportMode.WNUA | ||
|
|
||
| port: int | ||
|
|
||
| def _to_cyberchannel_kwargs(self) -> dict[str, Any]: | ||
| return asdict(self) | {"transport_mode": self.mode.value, "host": "localhost"} | ||
|
|
||
|
|
||
| @dataclass(kw_only=True) | ||
| class MTLSOptions(TransportOptionsBase): | ||
| """Options for mTLS transport mode.""" | ||
|
|
||
| _MODE = TransportMode.MTLS | ||
|
|
||
| certs_dir: str | Path | None = None | ||
| host: str = "localhost" | ||
| port: int | ||
| allow_remote_host: bool = False | ||
|
|
||
| def _to_cyberchannel_kwargs(self) -> dict[str, Any]: | ||
| if not self.allow_remote_host: | ||
| if self.host not in ("localhost", "127.0.0.1"): | ||
| raise ValueError(f"Remote host '{self.host}' specified without setting 'allow_remote_host=True'.") | ||
| res = asdict(self) | ||
| res.pop("allow_remote_host", None) | ||
| return res | {"transport_mode": self.mode.value} | ||
|
|
||
|
|
||
| @dataclass(kw_only=True) | ||
| class InsecureOptions(TransportOptionsBase): | ||
| """Options for insecure transport mode.""" | ||
|
|
||
| _MODE = TransportMode.INSECURE | ||
|
|
||
| host: str = "localhost" | ||
| port: int | ||
| allow_remote_host: bool = False | ||
|
|
||
| def _to_cyberchannel_kwargs(self) -> dict[str, Any]: | ||
| if not self.allow_remote_host: | ||
| if self.host not in ("localhost", "127.0.0.1"): | ||
| raise ValueError(f"Remote host '{self.host}' specified without setting 'allow_remote_host=True'.") | ||
| res = asdict(self) | ||
| res.pop("allow_remote_host", None) | ||
| return res | {"transport_mode": self.mode.value} | ||
|
|
||
|
|
||
| TransportOptionsType = UDSOptions | WNUAOptions | MTLSOptions | InsecureOptions | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.