public async Task <ImportResourceState.Types.Response> ImportResourceState(ImportResourceState.Types.Request request) { var imported = await _resourceProvider.ImportAsync(request.Id); var response = new ImportResourceState.Types.Response(); response.ImportedResources.AddRange(imported.Select(resource => new ImportResourceState.Types.ImportedResource { TypeName = request.TypeName, State = SerializeDynamicValue(resource), })); return(response); }
private void ReadResult(Type resType, Type resultType, object result, ImportResourceState.Types.Request request, ImportResourceState.Types.Response response) { var diagnostics = ((TFDiagnostics)resultType .GetProperty(nameof(response.Diagnostics)) .GetValue(result)); if (diagnostics.Count() > 0) { response.Diagnostics.Add(diagnostics.All()); } var importedEnum = resultType.GetProperty(nameof(response.ImportedResources)) .GetValue(result) as System.Collections.IEnumerable; if (importedEnum != null) { var importedType = typeof(TFImportedResource <>).MakeGenericType(resType); var stateProp = importedType .GetProperty(nameof(TFImportedResource <object> .State)); var privateProp = importedType .GetProperty(nameof(TFImportedResource <object> .Private)); var importedResult = new List <ImportResourceState.Types.ImportedResource>(); foreach (var imported in importedEnum) { importedResult.Add(new ImportResourceState.Types.ImportedResource { // We assume the outgoing is the same as incoming TypeName = request.TypeName, // Convert over private and schema-defined state State = stateProp.GetValue(imported).MarshalToDynamicValue(resType), Private = ByteString.CopyFrom((byte[])privateProp.GetValue(imported)), }); } response.ImportedResources.Add(importedResult); } }
public override async Task <ImportResourceState.Types.Response> ImportResourceState( ImportResourceState.Types.Request request, ServerCallContext context) { var response = new ImportResourceState.Types.Response(); var providerType = _schemaResolver.PluginDetails.Provider; var resType = _schemaResolver.GetResourceSchemas()[request.TypeName].Type; if (providerType.HasImportResourceStateSkill(resType)) { providerType.InvokeImportResourceStateSkill( PluginProviderInstance, resType, writeInput: (inputType, input) => WriteInput(resType, request, inputType, input), readResult: (resultType, result) => ReadResult(resType, resultType, result, request, response) ); } else { _log.LogWarning("provider does not handle importing state for resource [{type}]", resType); } return(await Task.FromResult(response)); }