|
| 1 | +# Copyright 2019 Lightbend Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import platform |
| 16 | +from dataclasses import dataclass |
| 17 | +from pprint import pprint |
| 18 | +from typing import List |
| 19 | + |
| 20 | +from google.protobuf.descriptor_pb2 import FileDescriptorSet, FileDescriptorProto |
| 21 | +from google.protobuf.descriptor_pool import Default |
| 22 | + |
| 23 | +from cloudstate import entity_pb2 |
| 24 | +from cloudstate.entity_pb2_grpc import EntityDiscoveryServicer |
| 25 | +from cloudstate.event_sourced_entity import EventSourcedEntity |
| 26 | + |
| 27 | + |
| 28 | +@dataclass |
| 29 | +class CloudStateEntityDiscoveryServicer(EntityDiscoveryServicer): |
| 30 | + event_sourced_entities: List[EventSourcedEntity] |
| 31 | + |
| 32 | + def discover(self, request, context): |
| 33 | + pprint(request) |
| 34 | + descriptor_set = FileDescriptorSet() |
| 35 | + for entity in self.event_sourced_entities: |
| 36 | + for descriptor in entity.file_descriptors: |
| 37 | + descriptor_set.file.append(FileDescriptorProto.FromString(descriptor.serialized_pb)) |
| 38 | + descriptor_set.file.append( |
| 39 | + FileDescriptorProto.FromString(Default().FindFileByName('google/protobuf/empty.proto').serialized_pb) |
| 40 | + ) |
| 41 | + descriptor_set.file.append( |
| 42 | + FileDescriptorProto.FromString(Default().FindFileByName('cloudstate/entity_key.proto').serialized_pb) |
| 43 | + ) |
| 44 | + descriptor_set.file.append( |
| 45 | + FileDescriptorProto.FromString(Default().FindFileByName('google/protobuf/descriptor.proto').serialized_pb) |
| 46 | + ) |
| 47 | + descriptor_set.file.append( |
| 48 | + FileDescriptorProto.FromString(Default().FindFileByName('google/api/annotations.proto').serialized_pb) |
| 49 | + ) |
| 50 | + descriptor_set.file.append( |
| 51 | + FileDescriptorProto.FromString(Default().FindFileByName('google/api/http.proto').serialized_pb) |
| 52 | + ) |
| 53 | + spec = entity_pb2.EntitySpec( |
| 54 | + service_info=entity_pb2.ServiceInfo( |
| 55 | + service_version='0.1.0', |
| 56 | + service_runtime='Python ' + platform.python_version() + ' [' + platform.python_implementation() + ' ' + |
| 57 | + platform.python_compiler() + ']', |
| 58 | + support_library_name='cloudstate-python-support', |
| 59 | + support_library_version='0.1.0' |
| 60 | + ), |
| 61 | + entities=[ |
| 62 | + entity_pb2.Entity( |
| 63 | + entity_type=entity.entity_type(), |
| 64 | + service_name=entity.service_descriptor.full_name, |
| 65 | + persistence_id=entity.persistence_id, |
| 66 | + ) |
| 67 | + for entity in self.event_sourced_entities], |
| 68 | + proto=descriptor_set.SerializeToString() |
| 69 | + ) |
| 70 | + return spec |
| 71 | + |
| 72 | + def reportError(self, request, context): |
| 73 | + pprint(request) |
| 74 | + return |
0 commit comments