Skip to content

Commit e5396d9

Browse files
impl v1 methods and conversions for admin stub
1 parent c0513b1 commit e5396d9

File tree

2 files changed

+142
-5
lines changed

2 files changed

+142
-5
lines changed

src/ansys/geometry/core/_grpc/_services/v1/admin.py

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@
2121
# SOFTWARE.
2222
"""Module containing the admin service implementation for v1."""
2323

24+
import warnings
25+
2426
import grpc
27+
import semver
2528

2629
from ansys.geometry.core.errors import protect_grpc
2730

2831
from ..base.admin import GRPCAdminService
32+
from .conversions import from_grpc_backend_type_to_backend_type
2933

3034

3135
class GRPCAdminServiceV1(GRPCAdminService): # pragma: no cover
@@ -43,18 +47,83 @@ class GRPCAdminServiceV1(GRPCAdminService): # pragma: no cover
4347

4448
@protect_grpc
4549
def __init__(self, channel: grpc.Channel): # noqa: D102
46-
from ansys.api.dbu.v1.admin_pb2_grpc import AdminStub
50+
from ansys.api.discovery.v1.commands.application_pb2_grpc import ApplicationStub
51+
from ansys.api.discovery.v1.commands.communication_pb2_grpc import CommunicationStub
4752

48-
self.stub = AdminStub(channel)
53+
self.admin_stub = ApplicationStub(channel)
54+
self.communication_stub = CommunicationStub(channel)
4955

5056
@protect_grpc
5157
def get_backend(self, **kwargs) -> dict: # noqa: D102
52-
raise NotImplementedError
58+
# TODO: Remove this context and filter once the protobuf UserWarning is downgraded to INFO
59+
# https://github.com/grpc/grpc/issues/37609
60+
with warnings.catch_warnings():
61+
warnings.filterwarnings(
62+
"ignore", "Protobuf gencode version", UserWarning, "google.protobuf.runtime_version"
63+
)
64+
from ansys.api.discovery.v1.commands.application_pb2 import GetBackendRequest
65+
66+
# Create the request - assumes all inputs are valid and of the proper type
67+
request = GetBackendRequest()
68+
69+
# Call the gRPC service
70+
response = self.admin_stub.GetBackend(request=request)
71+
72+
# COMPATIBILITY HACK: retrieve the backend version -- for versions after 24R1
73+
ver = response.version
74+
backend_version = semver.Version(ver.major_release, ver.minor_release, ver.service_pack)
75+
api_server_build_info = f"{ver.build_number}" if ver.build_number != 0 else "N/A"
76+
product_build_info = (
77+
response.backend_version_info.strip() if response.backend_version_info else "N/A"
78+
)
79+
80+
# Convert the response to a dictionary
81+
return {
82+
"backend": from_grpc_backend_type_to_backend_type(response.type),
83+
"version": backend_version,
84+
"api_server_build_info": api_server_build_info,
85+
"product_build_info": product_build_info,
86+
"additional_info": {k: v for k, v in response.additional_build_info.items()},
87+
}
5388

5489
@protect_grpc
5590
def get_logs(self, **kwargs) -> dict: # noqa: D102
56-
raise NotImplementedError
91+
from ansys.api.discovery.v1.commands.communication_pb2 import LogsRequest
92+
from ansys.api.discovery.v1.commonenums_pb2 import LogsPeriodType, LogsTarget
93+
94+
# Create the request - assumes all inputs are valid and of the proper type
95+
request = LogsRequest(
96+
target=LogsTarget.LOGSTARGET_CLIENT,
97+
period_type=(
98+
LogsPeriodType.LOGSPERIODTIME_CURRENT
99+
if not kwargs["all_logs"]
100+
else LogsPeriodType.LOGSPERIODTIME_ALL
101+
),
102+
null_path=None,
103+
null_period=None,
104+
)
105+
106+
# Call the gRPC service
107+
logs_generator = self.communication_stub.GetLogs(request)
108+
logs: dict[str, str] = {}
109+
110+
# Convert the response to a dictionary
111+
for chunk in logs_generator:
112+
if chunk.log_name not in logs:
113+
logs[chunk.log_name] = ""
114+
logs[chunk.log_name] += chunk.log_chunk.decode()
115+
116+
return {"logs": logs}
57117

58118
@protect_grpc
59119
def get_service_status(self, **kwargs) -> dict: # noqa: D102
60-
raise NotImplementedError
120+
from ansys.api.discovery.v1.commands.communication_pb2 import HealthRequest
121+
122+
# Create the request - assumes all inputs are valid and of the proper type
123+
request = HealthRequest()
124+
125+
# Call the gRPC service
126+
response = self.communication_stub.Health(request=request)
127+
128+
# Convert the response to a dictionary
129+
return {"healthy": True if response.message == "I am healthy!" else False}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Copyright (C) 2023 - 2025 ANSYS, Inc. and/or its affiliates.
2+
# SPDX-License-Identifier: MIT
3+
#
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in all
13+
# copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
# SOFTWARE.
22+
"""Module containing v1 related conversions from PyAnsys Geometry objects to gRPC messages."""
23+
24+
from typing import TYPE_CHECKING
25+
26+
from ansys.api.discovery.v1.commonenums_pb2 import BackendType as GRPCBackendType
27+
28+
if TYPE_CHECKING:
29+
from ansys.geometry.core.connection.backend import BackendType
30+
31+
def from_grpc_backend_type_to_backend_type(
32+
grpc_backend_type: GRPCBackendType,
33+
) -> "BackendType":
34+
"""Convert a gRPC backend type to a backend type.
35+
36+
Parameters
37+
----------
38+
backend_type : GRPCBackendType
39+
Source backend type.
40+
41+
Returns
42+
-------
43+
BackendType
44+
Converted backend type.
45+
"""
46+
from ansys.geometry.core.connection.backend import BackendType
47+
48+
# Map the gRPC backend type to the corresponding BackendType
49+
backend_type = None
50+
51+
if grpc_backend_type == GRPCBackendType.BACKENDTYPE_DISCOVERY:
52+
backend_type = BackendType.DISCOVERY
53+
elif grpc_backend_type == GRPCBackendType.BACKENDTYPE_SPACECLAIM:
54+
backend_type = BackendType.SPACECLAIM
55+
elif grpc_backend_type == GRPCBackendType.BACKENDTYPE_WINDOWS_DMS:
56+
backend_type = BackendType.WINDOWS_SERVICE
57+
elif grpc_backend_type == GRPCBackendType.BACKENDTYPE_LINUX_DMS:
58+
backend_type = BackendType.LINUX_SERVICE
59+
elif grpc_backend_type == GRPCBackendType.BACKENDTYPE_CORE_SERVICE_LINUX:
60+
backend_type = BackendType.CORE_LINUX
61+
elif grpc_backend_type == GRPCBackendType.BACKENDTYPE_CORE_SERVICE_WINDOWS:
62+
backend_type = BackendType.CORE_WINDOWS
63+
elif grpc_backend_type == GRPCBackendType.BACKENDTYPE_DISCOVERY_HEADLESS:
64+
backend_type = BackendType.DISCOVERY_HEADLESS
65+
else:
66+
raise ValueError(f"Invalid backend type: {grpc_backend_type}")
67+
68+
return backend_type

0 commit comments

Comments
 (0)