diff --git a/FrostFS.SDK.sln b/FrostFS.SDK.sln index 4120cd8..424a077 100644 --- a/FrostFS.SDK.sln +++ b/FrostFS.SDK.sln @@ -10,8 +10,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FrostFS.SDK.Cryptography", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FrostFS.SDK.ModelsV2", "src\FrostFS.SDK.ModelsV2\FrostFS.SDK.ModelsV2.csproj", "{3F6E5EE6-2AAC-45BE-A5E2-B83D11F90CC3}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FrostFS.SDK.Service", "src\FrostFS.SDK.Service\FrostFS.SDK.Service.csproj", "{5807CC9E-BDA3-48B1-9EF3-F76F0F522981}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -38,9 +36,5 @@ Global {3F6E5EE6-2AAC-45BE-A5E2-B83D11F90CC3}.Debug|Any CPU.Build.0 = Debug|Any CPU {3F6E5EE6-2AAC-45BE-A5E2-B83D11F90CC3}.Release|Any CPU.ActiveCfg = Release|Any CPU {3F6E5EE6-2AAC-45BE-A5E2-B83D11F90CC3}.Release|Any CPU.Build.0 = Release|Any CPU - {5807CC9E-BDA3-48B1-9EF3-F76F0F522981}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5807CC9E-BDA3-48B1-9EF3-F76F0F522981}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5807CC9E-BDA3-48B1-9EF3-F76F0F522981}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5807CC9E-BDA3-48B1-9EF3-F76F0F522981}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/FrostFS.SDK.ClientV2/Client.cs b/src/FrostFS.SDK.ClientV2/Client.cs index 64c16d1..26339aa 100644 --- a/src/FrostFS.SDK.ClientV2/Client.cs +++ b/src/FrostFS.SDK.ClientV2/Client.cs @@ -3,7 +3,6 @@ using FrostFS.Container; using FrostFS.Netmap; using FrostFS.Object; using FrostFS.SDK.ClientV2.Interfaces; -using FrostFS.SDK.ClientV2.Mappers.GRPC; using FrostFS.SDK.Cryptography; using FrostFS.SDK.ModelsV2; using FrostFS.Session; @@ -38,13 +37,12 @@ public partial class Client: IFrostFSClient CheckFrostFsVersionSupport(); } - private void CheckFrostFsVersionSupport() + private async void CheckFrostFsVersionSupport() { - var localNodeInfo = GetLocalNodeInfoAsync().Result; - var frostFsVersion = localNodeInfo.Body.Version.ToModel(); - if (!frostFsVersion.IsSupported(Version)) + var localNodeInfo = await GetLocalNodeInfoAsync(); + if (!localNodeInfo.Version.IsSupported(Version)) { - var msg = $"FrostFS {frostFsVersion} is not supported."; + var msg = $"FrostFS {localNodeInfo.Version} is not supported."; Console.WriteLine(msg); throw new ApplicationException(msg); } diff --git a/src/FrostFS.SDK.ClientV2/Interfaces/IFrostFSClient.cs b/src/FrostFS.SDK.ClientV2/Interfaces/IFrostFSClient.cs index 0575797..fcc3235 100644 --- a/src/FrostFS.SDK.ClientV2/Interfaces/IFrostFSClient.cs +++ b/src/FrostFS.SDK.ClientV2/Interfaces/IFrostFSClient.cs @@ -1,20 +1,15 @@ -using FrostFS.Container; -using FrostFS.Object; -using FrostFS.Refs; -using DeleteResponse = FrostFS.Container.DeleteResponse; -using GetResponse = FrostFS.Container.GetResponse; -using PutResponse = FrostFS.Container.PutResponse; +using FrostFS.SDK.ModelsV2; namespace FrostFS.SDK.ClientV2.Interfaces; public interface IFrostFSClient { - Task ListContainersAsync(); - Task CreateContainerAsync(Container.Container container); - Task GetContainerAsync(ContainerID containerId); - Task DeleteContainerAsync(ContainerID containerId); - Task GetObjectHeadAsync(ContainerID containerId, ObjectID objectId); - Task GetObjectAsync(ContainerID containerId, ObjectID objectId); - Task PutObjectAsync(Header header, Stream payload); - Task DeleteObjectAsync(ContainerID containerId, ObjectID objectId); + Task GetContainerAsync(ContainerId containerId); + Task ListContainersAsync(); + Task CreateContainerAsync(ModelsV2.Container container); + Task DeleteContainerAsync(ContainerId containerId); + Task GetObjectHeadAsync(ContainerId containerId, ObjectId objectId); + Task GetObjectAsync(ContainerId containerId, ObjectId objectId); + Task PutObjectAsync(ObjectHeader header, Stream payload); + Task DeleteObjectAsync(ContainerId containerId, ObjectId objectId); } \ No newline at end of file diff --git a/src/FrostFS.SDK.ClientV2/Mappers/GRPC/Netmap/NodeInfo.cs b/src/FrostFS.SDK.ClientV2/Mappers/GRPC/Netmap/NodeInfo.cs new file mode 100644 index 0000000..91bbf28 --- /dev/null +++ b/src/FrostFS.SDK.ClientV2/Mappers/GRPC/Netmap/NodeInfo.cs @@ -0,0 +1,21 @@ +using FrostFS.SDK.ModelsV2.Enums; +using FrostFS.SDK.ModelsV2.Netmap; + +namespace FrostFS.SDK.ClientV2.Mappers.GRPC.Netmap; + +public static class NodeInfoMapper +{ + public static NodeInfo ToModel(this FrostFS.Netmap.LocalNodeInfoResponse.Types.Body nodeInfo) + { + var nodeStateName = Enum.GetName(typeof(NodeState), nodeInfo.NodeInfo.State); + if (nodeStateName is null) + { + throw new ArgumentException($"Unknown NodeState. Value: '{nodeInfo.NodeInfo.State}'."); + } + return new NodeInfo + { + State = Enum.Parse(nodeStateName), + Version = nodeInfo.Version.ToModel() + }; + } +} \ No newline at end of file diff --git a/src/FrostFS.SDK.ClientV2/Services/Container.cs b/src/FrostFS.SDK.ClientV2/Services/Container.cs index 53d37b0..86ebc25 100644 --- a/src/FrostFS.SDK.ClientV2/Services/Container.cs +++ b/src/FrostFS.SDK.ClientV2/Services/Container.cs @@ -1,26 +1,28 @@ using FrostFS.Container; using FrostFS.Refs; using FrostFS.SDK.ClientV2.Mappers.GRPC; +using FrostFS.SDK.ModelsV2; namespace FrostFS.SDK.ClientV2; public partial class Client { - public async Task GetContainerAsync(ContainerID cid) + public async Task GetContainerAsync(ContainerId cid) { var request = new GetRequest { Body = new GetRequest.Types.Body { - ContainerId = cid + ContainerId = cid.ToGrpcMessage() }, }; request.AddMetaHeader(); request.Sign(_key); - return await _containerServiceClient.GetAsync(request); + var response = await _containerServiceClient.GetAsync(request); + return response.Body.Container.ToModel(); } - public async Task ListContainersAsync() + public async Task ListContainersAsync() { var request = new ListRequest { @@ -31,38 +33,43 @@ public partial class Client }; request.AddMetaHeader(); request.Sign(_key); - return await _containerServiceClient.ListAsync(request); + var response = await _containerServiceClient.ListAsync(request); + return response.Body.ContainerIds.Select( + cid => ContainerId.FromHash(cid.Value.ToByteArray()) + ).ToArray(); } - public async Task CreateContainerAsync(Container.Container container) + public async Task CreateContainerAsync(ModelsV2.Container container) { - container.OwnerId = _owner.ToGrpcMessage(); - container.Version = Version.ToGrpcMessage(); + var cntnr = container.ToGrpcMessage(); + cntnr.OwnerId = _owner.ToGrpcMessage(); + cntnr.Version = Version.ToGrpcMessage(); var request = new PutRequest { Body = new PutRequest.Types.Body { - Container = container, - Signature = _key.SignRFC6979(container), + Container = cntnr, + Signature = _key.SignRFC6979(cntnr), } }; request.AddMetaHeader(); request.Sign(_key); - return await _containerServiceClient.PutAsync(request); + var response = await _containerServiceClient.PutAsync(request); + return ContainerId.FromHash(response.Body.ContainerId.Value.ToByteArray()); } - public async Task DeleteContainerAsync(ContainerID cid) + public async Task DeleteContainerAsync(ContainerId cid) { var request = new DeleteRequest { Body = new DeleteRequest.Types.Body { - ContainerId = cid, - Signature = _key.SignRFC6979(cid.Value) + ContainerId = cid.ToGrpcMessage(), + Signature = _key.SignRFC6979(cid.ToGrpcMessage().Value) } }; request.AddMetaHeader(); request.Sign(_key); - return await _containerServiceClient.DeleteAsync(request); + await _containerServiceClient.DeleteAsync(request); } } \ No newline at end of file diff --git a/src/FrostFS.SDK.ClientV2/Services/Netmap.cs b/src/FrostFS.SDK.ClientV2/Services/Netmap.cs index 816eabc..23c0bbc 100644 --- a/src/FrostFS.SDK.ClientV2/Services/Netmap.cs +++ b/src/FrostFS.SDK.ClientV2/Services/Netmap.cs @@ -1,10 +1,11 @@ using FrostFS.Netmap; +using FrostFS.SDK.ClientV2.Mappers.GRPC.Netmap; namespace FrostFS.SDK.ClientV2; public partial class Client { - public async Task GetLocalNodeInfoAsync() + public async Task GetLocalNodeInfoAsync() { var request = new LocalNodeInfoRequest { @@ -12,7 +13,8 @@ public partial class Client }; request.AddMetaHeader(); request.Sign(_key); - return await _netmapServiceClient.LocalNodeInfoAsync(request); + var response = await _netmapServiceClient.LocalNodeInfoAsync(request); + return response.Body.ToModel(); } public async Task GetNetworkInfoAsync() diff --git a/src/FrostFS.SDK.ClientV2/Services/Object.cs b/src/FrostFS.SDK.ClientV2/Services/Object.cs index 636acd7..2f21561 100644 --- a/src/FrostFS.SDK.ClientV2/Services/Object.cs +++ b/src/FrostFS.SDK.ClientV2/Services/Object.cs @@ -11,7 +11,7 @@ namespace FrostFS.SDK.ClientV2; public partial class Client { - public async Task GetObjectHeadAsync(ContainerID cid, ObjectID oid) + public async Task GetObjectHeadAsync(ContainerId cid, ObjectId oid) { var request = new HeadRequest { @@ -19,17 +19,18 @@ public partial class Client { Address = new Address { - ContainerId = cid, - ObjectId = oid + ContainerId = cid.ToGrpcMessage(), + ObjectId = oid.ToGrpcMessage() } }, }; request.AddMetaHeader(); request.Sign(_key); - return await _objectServiceClient.HeadAsync(request); + var response = await _objectServiceClient.HeadAsync(request); + return response.Body.Header.Header.ToModel(); } - public async Task GetObjectAsync(ContainerID cid, ObjectID oid) + public async Task GetObjectAsync(ContainerId cid, ObjectId oid) { var sessionToken = await CreateSessionAsync(uint.MaxValue); var request = new GetRequest @@ -39,22 +40,23 @@ public partial class Client Raw = false, Address = new Address { - ContainerId = cid, - ObjectId = oid + ContainerId = cid.ToGrpcMessage(), + ObjectId = oid.ToGrpcMessage() }, } }; request.AddMetaHeader(); request.AddObjectSessionToken( sessionToken, - cid, - oid, + cid.ToGrpcMessage(), + oid.ToGrpcMessage(), ObjectSessionContext.Types.Verb.Get, _key ); request.Sign(_key); - return await GetObject(request); + var response = await GetObject(request); + return response.ToModel(); } private async Task GetObject(GetRequest request) @@ -87,14 +89,15 @@ public partial class Client }; } - public async Task PutObjectAsync(Header header, Stream payload) + public async Task PutObjectAsync(ObjectHeader header, Stream payload) { var sessionToken = await CreateSessionAsync(uint.MaxValue); - header.OwnerId = _owner.ToGrpcMessage(); - header.Version = Version.ToGrpcMessage(); + var hdr = header.ToGrpcMessage(); + hdr.OwnerId = _owner.ToGrpcMessage(); + hdr.Version = Version.ToGrpcMessage(); var oid = new ObjectID { - Value = header.Sha256() + Value = hdr.Sha256() }; var request = new PutRequest { @@ -102,14 +105,14 @@ public partial class Client { Init = new PutRequest.Types.Body.Types.Init { - Header = header + Header = hdr }, } }; request.AddMetaHeader(); request.AddObjectSessionToken( sessionToken, - header.ContainerId, + hdr.ContainerId, oid, ObjectSessionContext.Types.Verb.Put, _key @@ -131,7 +134,8 @@ public partial class Client bufferLength = payload.Read(buffer, 0, Constants.ObjectChunkSize); } - return await stream.Close(); + var response = await stream.Close(); + return ObjectId.FromHash(response.Body.ObjectId.Value.ToByteArray()); } private async Task PutObjectInit(PutRequest initRequest) @@ -146,7 +150,7 @@ public partial class Client return new ObjectStreamer { Call = call }; } - public async Task DeleteObjectAsync(ContainerID cid, ObjectID oid) + public async Task DeleteObjectAsync(ContainerId cid, ObjectId oid) { var request = new DeleteRequest { @@ -154,14 +158,14 @@ public partial class Client { Address = new Address { - ContainerId = cid, - ObjectId = oid + ContainerId = cid.ToGrpcMessage(), + ObjectId = oid.ToGrpcMessage() } } }; request.AddMetaHeader(); request.Sign(_key); - return await _objectServiceClient.DeleteAsync(request); + await _objectServiceClient.DeleteAsync(request); } } diff --git a/src/FrostFS.SDK.ModelsV2/Enums/NodeState.cs b/src/FrostFS.SDK.ModelsV2/Enums/NodeState.cs new file mode 100644 index 0000000..2293c0f --- /dev/null +++ b/src/FrostFS.SDK.ModelsV2/Enums/NodeState.cs @@ -0,0 +1,9 @@ +namespace FrostFS.SDK.ModelsV2.Enums; + +public enum NodeState +{ + Unspecified = 0, + Online = 1, + Offline = 2, + Maintenance = 3 +} \ No newline at end of file diff --git a/src/FrostFS.SDK.ModelsV2/Netmap/NodeInfo.cs b/src/FrostFS.SDK.ModelsV2/Netmap/NodeInfo.cs new file mode 100644 index 0000000..7e86912 --- /dev/null +++ b/src/FrostFS.SDK.ModelsV2/Netmap/NodeInfo.cs @@ -0,0 +1,9 @@ +using FrostFS.SDK.ModelsV2.Enums; + +namespace FrostFS.SDK.ModelsV2.Netmap; + +public class NodeInfo +{ + public NodeState State { get; set; } + public Version Version { get; set; } +} \ No newline at end of file diff --git a/src/FrostFS.SDK.Service/FrostFS.SDK.Service.csproj b/src/FrostFS.SDK.Service/FrostFS.SDK.Service.csproj deleted file mode 100644 index 38e2646..0000000 --- a/src/FrostFS.SDK.Service/FrostFS.SDK.Service.csproj +++ /dev/null @@ -1,13 +0,0 @@ - - - - net6.0 - enable - enable - - - - - - - diff --git a/src/FrostFS.SDK.Service/Service.cs b/src/FrostFS.SDK.Service/Service.cs deleted file mode 100644 index 2449e91..0000000 --- a/src/FrostFS.SDK.Service/Service.cs +++ /dev/null @@ -1,81 +0,0 @@ -using FrostFS.SDK.ClientV2; -using FrostFS.SDK.ClientV2.Interfaces; -using FrostFS.SDK.ClientV2.Mappers.GRPC; -using FrostFS.SDK.ModelsV2; - -namespace FrostFS.SDK.Service; - -// TODO: Проверять ответы Grpc -public class FrostFsService -{ - private readonly IFrostFSClient _client; - - public FrostFsService(IFrostFSClient client) - { - _client = client; - } - - public async Task ListContainersAsync() - { - var listContainersResponse = await _client.ListContainersAsync(); - - return listContainersResponse.Body.ContainerIds.Select( - cid => ContainerId.FromHash(cid.Value.ToByteArray()) - ).ToArray(); - } - - public async Task CreateContainerAsync(ModelsV2.Container container) - { - var createContainerResponse = await _client.CreateContainerAsync(container.ToGrpcMessage()); - return ContainerId.FromHash(createContainerResponse.Body.ContainerId.Value.ToByteArray()); - } - - public async Task GetContainerAsync(ContainerId containerId) - { - var getContainerResponse = await _client.GetContainerAsync(containerId.ToGrpcMessage()); - return getContainerResponse.Body.Container.ToModel(); - } - - public async Task DeleteContainerAsync(ContainerId containerId) - { - var deleteContainerResponse = await _client.DeleteContainerAsync(containerId.ToGrpcMessage()); - } - - public async Task GetObjectHeadAsync(ContainerId containerId, ObjectId objectId) - { - var getObjectHeadResponse = await _client.GetObjectHeadAsync( - containerId.ToGrpcMessage(), - objectId.ToGrpcMessage() - ); - return getObjectHeadResponse.Body.Header.Header.ToModel(); - } - - public async Task GetObjectAsync(ContainerId containerId, ObjectId objectId) - { - var obj = await _client.GetObjectAsync( - containerId.ToGrpcMessage(), - objectId.ToGrpcMessage() - ); - return obj.ToModel(); - } - - public async Task PutObjectAsync(ObjectHeader header, Stream payload) - { - var putObjectResponse = await _client.PutObjectAsync(header.ToGrpcMessage(), payload); - return ObjectId.FromHash(putObjectResponse.Body.ObjectId.Value.ToByteArray()); - } - - public async Task PutObjectAsync(ObjectHeader header, byte[] payload) - { - var putObjectResponse = await _client.PutObjectAsync(header.ToGrpcMessage(), new MemoryStream(payload)); - return ObjectId.FromHash(putObjectResponse.Body.ObjectId.Value.ToByteArray()); - } - - public async Task DeleteObjectAsync(ContainerId containerId, ObjectId objectId) - { - var deleteObjectResponse = await _client.DeleteObjectAsync( - containerId.ToGrpcMessage(), - objectId.ToGrpcMessage() - ); - } -} \ No newline at end of file