async def ApplyResourceChange(self, stream: grpclib.server.Stream) -> None: request = await stream.recv_message() resource = self.provider.resources[request.type_name] prior_state = utils.from_dynamic_value_proto(request.prior_state) planned_state = utils.from_dynamic_value_proto(request.planned_state) config = utils.from_dynamic_value_proto(request.config) planned_private = (json.loads(request.planned_private) if request.planned_private else None) destroy = planned_state is None create = prior_state is None if destroy: new_state = planned_state private = planned_private else: # TODO: implement me data = schemas.ResourceData(planned_state) if create: await resource.create(data=data) else: raise NotImplementedError new_state = dict(data) private = planned_private response = tfplugin5_1_pb2.ApplyResourceChange.Response( new_state=utils.to_dynamic_value_proto(new_state), private=json.dumps(private).encode("ascii"), ) await stream.send_message(response)
async def PlanResourceChange(self, stream: grpclib.server.Stream) -> None: request = await stream.recv_message() resource = self.provider.resources[request.type_name] prior_state = utils.from_dynamic_value_proto(request.prior_state) proposed_new_state = utils.from_dynamic_value_proto( request.proposed_new_state) config = utils.from_dynamic_value_proto(request.config) prior_private = (json.loads(request.prior_private) if request.prior_private else None) destroy = proposed_new_state is None create = prior_state is None if destroy: planned_state = proposed_new_state planned_private = prior_private else: # TODO: implement me planned_state = proposed_new_state if create: planned_state = unknowns.set_unknowns(planned_state, resource.to_block()) else: raise NotImplementedError planned_private = prior_private response = tfplugin5_1_pb2.PlanResourceChange.Response( planned_state=utils.to_dynamic_value_proto(planned_state), planned_private=json.dumps(planned_private).encode("ascii"), ) await stream.send_message(response)
async def Configure(self, stream: grpclib.server.Stream) -> None: request = await stream.recv_message() config = utils.from_dynamic_value_proto(request.config) self.provider.terraform_version = request.terraform_version or "0.11+compatible" self.provider.configure(config) response = tfplugin5_1_pb2.Configure.Response() await stream.send_message(response)
async def ValidateDataSourceConfig(self, stream: grpclib.server.Stream) -> None: request = await stream.recv_message() resource = self.provider.data_sources[request.type_name] config = utils.from_dynamic_value_proto(request.config) prepared_config = resource.dump(config) errors = resource.validate(prepared_config) resource_diagnostics = diagnostics.Diagnostics.from_schema_errors( errors) response = tfplugin5_1_pb2.ValidateDataSourceConfig.Response( diagnostics=resource_diagnostics.to_proto()) await stream.send_message(response)
async def ReadDataSource(self, stream: grpclib.server.Stream) -> None: request = await stream.recv_message() resource = self.provider.data_sources[request.type_name] config = utils.from_dynamic_value_proto(request.config) data = schemas.ResourceData(config) await resource.read(data=data) if not data.get("id"): data.set_id("-") data = resource.dump(data) response = tfplugin5_1_pb2.ReadDataSource.Response( state=utils.to_dynamic_value_proto(dict(data))) await stream.send_message(response)
async def test_prepare_provider_config( provider: schemas.Provider, input_config: typing.Dict[str, typing.Any], expected_output_config: typing.Dict[str, typing.Any], ): service = plugin.ProviderService(provider=provider) async with ChannelFor([service]) as channel: stub = tfplugin5_1_grpc.ProviderStub(channel) request = tfplugin5_1_pb2.PrepareProviderConfig.Request( config=utils.to_dynamic_value_proto(input_config) ) response = await stub.PrepareProviderConfig(request) assert ( utils.from_dynamic_value_proto(response.prepared_config) == expected_output_config )
async def PrepareProviderConfig(self, stream: grpclib.server.Stream) -> None: request = await stream.recv_message() config = utils.from_dynamic_value_proto(request.config) prepared_config = self.provider.dump(config) errors = self.provider.validate(prepared_config) provider_diagnostics = diagnostics.Diagnostics.from_schema_errors( errors) provider_diagnostics = provider_diagnostics.include_attribute_path_in_summary( ) response = tfplugin5_1_pb2.PrepareProviderConfig.Response( prepared_config=utils.to_dynamic_value_proto(prepared_config), diagnostics=provider_diagnostics.to_proto(), ) await stream.send_message(response)