Skip to content

Commit

Permalink
Merge pull request #23 from DmitryNaumov/feature/support-storage-service
Browse files Browse the repository at this point in the history
Add support for Storage service
  • Loading branch information
DmitryNaumov authored Mar 20, 2024
2 parents 7b19c6a + 09369d3 commit 7634d83
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 5 deletions.
1 change: 1 addition & 0 deletions Example/Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>Example</RootNamespace>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
51 changes: 48 additions & 3 deletions Example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Threading.Tasks;
using Yandex.Cloud.Resourcemanager.V1;
using Yandex.Cloud;
using Yandex.Cloud.Credentials;
using Yandex.Cloud.Storage.V1;

namespace Example
{
public static class Program
{
public static void Main(string[] args)
public static async Task Main(string[] args)
{
var token = Environment.GetEnvironmentVariable("YC_TOKEN");
if (token == null)
Expand All @@ -18,12 +20,55 @@ public static void Main(string[] args)

var credProvider = new OAuthCredentialsProvider(token);
var sdk = new Sdk(credProvider);
var response = sdk.Services.Resourcemanager.CloudService.List(new ListCloudsRequest());

foreach (var c in response.Clouds)
var folder = UseResourceManager(sdk);
if (folder == null)
{
Console.WriteLine("No folder found");
return;
}

await UseStorage(sdk, folder);
}

private static Folder UseResourceManager(Sdk sdk)
{
var cloudsResponse = sdk.Services.Resourcemanager.CloudService.List(new ListCloudsRequest());

var cloudId = string.Empty;
Console.WriteLine("Clouds:");
foreach (var c in cloudsResponse.Clouds)
{
cloudId = c.Id;
Console.Out.WriteLine($"* {c.Name} ({c.Id})");
}

var folderResponse = sdk.Services.Resourcemanager.FolderService.List(new ListFoldersRequest { CloudId = cloudId });
foreach (var folder in folderResponse.Folders)
{
return folder;
}

return null;
}

private static async Task UseStorage(Sdk sdk, Folder folder)
{
var bucketName = $"sdk-example-bucket-{DateTime.Now:yyMMddHHmmss}";
var operation = await sdk.Services.Storage.BucketService
.CreateAsync(new CreateBucketRequest { FolderId = folder.Id, Name = bucketName }).ResponseAsync;

await sdk.WaitForCompletionAsync(operation);

var listResponse = await sdk.Services.Storage.BucketService.ListAsync(new ListBucketsRequest{FolderId = folder.Id}).ResponseAsync;

Console.WriteLine("Buckets:");
foreach (var bucket in listResponse.Buckets)
{
Console.WriteLine(bucket.Name);
}

await sdk.Services.Storage.BucketService.DeleteAsync(new DeleteBucketRequest { Name = bucketName }).WaitForCompletionAsync(sdk);
}
}
}
1 change: 1 addition & 0 deletions Yandex.Cloud.Protos/Yandex.Cloud.Protos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReleaseNotes>Yandex Cloud SDK</PackageReleaseNotes>
<Copyright>Copyright 2024, YANDEX LLC.</Copyright>
<PackageTags>yandex-cloud sdk proto proto3 protobuf grpc</PackageTags>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions Yandex.Cloud.SDK.Tests/Yandex.Cloud.SDK.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>

<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
3 changes: 1 addition & 2 deletions Yandex.Cloud.SDK/Generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ public class Services_Storage : Yandex.Cloud.ServiceRegistry
{

public Services_Storage(Yandex.Cloud.Sdk sdk) :
base(sdk, "unknown")
base(sdk, "storage-api")
{
throw new System.Exception("service Services_Storage is not supported at this moment");
}

public virtual Yandex.Cloud.Storage.V1.BucketService.BucketServiceClient BucketService
Expand Down
67 changes: 67 additions & 0 deletions Yandex.Cloud.SDK/SdkExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
using Yandex.Cloud.Operation;

namespace Yandex.Cloud;

public static class SdkExtensions
{
public static Task<Operation.Operation> WaitForCompletionAsync(this Sdk sdk, Operation.Operation operation,
CancellationToken cancellationToken = default)
{
return sdk.WaitForCompletionAsync(operation, TimeSpan.FromSeconds(1), cancellationToken);
}

public static async Task<Operation.Operation> WaitForCompletionAsync(this Sdk sdk, Operation.Operation operation, TimeSpan pollingInterval, CancellationToken cancellationToken = default)
{
if (operation.Done)
{
return operation;
}

var operationId = operation.Id;
var retryableCount = 0;
while (true)
{
try
{
operation = await sdk.Services.Operation.OperationService.GetAsync(
new GetOperationRequest
{
OperationId = operationId
},
cancellationToken: cancellationToken);

if (operation.Done)
{
return operation;
}
}
catch (RpcException ex) when(ex.StatusCode == StatusCode.NotFound)
{
retryableCount++;
if (retryableCount >= 3)
{
throw;
}
}

await Task.Delay(pollingInterval, cancellationToken);
}
}

public static Task<Operation.Operation> WaitForCompletionAsync(this Operation.Operation operation, Sdk sdk,
CancellationToken cancellationToken = default)
{
return sdk.WaitForCompletionAsync(operation, TimeSpan.FromSeconds(1), cancellationToken);
}

public static async Task<Operation.Operation> WaitForCompletionAsync(this AsyncUnaryCall<Operation.Operation> call, Sdk sdk,
CancellationToken cancellationToken = default)
{
var operation = await call.ResponseAsync;
return await sdk.WaitForCompletionAsync(operation, TimeSpan.FromSeconds(1), cancellationToken);
}
}
1 change: 1 addition & 0 deletions Yandex.Cloud.SDK/Yandex.Cloud.SDK.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageReleaseNotes>Yandex Cloud SDK</PackageReleaseNotes>
<Copyright>Copyright 2024, YANDEX LLC.</Copyright>
<PackageTags>yandex-cloud sdk proto proto3 protobuf grpc</PackageTags>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions tools/GeneratorConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class GeneratorConfig
{"Services_Serverless_Functions", new EndpointConfig("serverless-functions")},
{"Services_Serverless_Mdbproxy", new EndpointConfig("mdbproxy")},
{"Services_Serverless_Triggers", new EndpointConfig("serverless-triggers")},
{"Services_Storage", new EndpointConfig("storage-api")},
{"Services_Vpc", new EndpointConfig("vpc")},
{"Services_Ydb", new EndpointConfig("ydb")},
{
Expand Down
1 change: 1 addition & 0 deletions tools/tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
<LangVersion>default</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 7634d83

Please sign in to comment.