Skip to content

Commit

Permalink
Finalize "restore backup" feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wildman9x committed Apr 13, 2024
1 parent 8a55d90 commit 5614ccf
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 20 deletions.
41 changes: 22 additions & 19 deletions DotnetGRPC/DotnetGRPC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
<Protobuf Include="Protos\filetransfer.proto" GrpcServices="Server" />
<Protobuf Include="Protos\notification.proto" GrpcServices="Server" />
<Protobuf Include="Protos\database.proto" GrpcServices="Server" />
<Protobuf Include="Protos\greet.proto" GrpcServices="Server"/>
<Protobuf Include="Protos\filetransfer.proto" GrpcServices="Server"/>
<Protobuf Include="Protos\notification.proto" GrpcServices="Server"/>
<Protobuf Include="Protos\database.proto" GrpcServices="Server"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="AWSSDK.S3" Version="3.7.307.4" />
<PackageReference Include="Azure.Identity" Version="1.10.4" />
<PackageReference Include="Azure.ResourceManager" Version="1.11.0" />
<PackageReference Include="Azure.ResourceManager.PostgreSql" Version="1.1.3" />
<PackageReference Include="Grpc.AspNetCore" Version="2.61.0" />
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.61.0" />
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" Version="2.61.0" />
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.12" />
<PackageReference Include="Hangfire.PostgreSql" Version="1.20.8" />
<PackageReference Include="Microsoft.AspNetCore.Grpc.Swagger" Version="0.8.3" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
<PackageReference Include="protobuf-net" Version="3.2.30" />
<PackageReference Include="AWSSDK.S3" Version="3.7.307.4"/>
<PackageReference Include="Azure.Identity" Version="1.10.4"/>
<PackageReference Include="Azure.ResourceManager" Version="1.11.0"/>
<PackageReference Include="Azure.ResourceManager.PostgreSql" Version="1.1.3"/>
<PackageReference Include="Grpc.AspNetCore" Version="2.61.0"/>
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.61.0"/>
<PackageReference Include="Grpc.AspNetCore.Server.Reflection" Version="2.61.0"/>
<PackageReference Include="Hangfire.AspNetCore" Version="1.8.12"/>
<PackageReference Include="Hangfire.PostgreSql" Version="1.20.8"/>
<PackageReference Include="Microsoft.AspNetCore.Grpc.Swagger" Version="0.8.3"/>
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.7.2"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2"/>
<PackageReference Include="protobuf-net" Version="3.2.30"/>
<PackageReference Include="Microsoft.Azure.Management.PostgreSQL" Version="2.0.0"/>
<PackageReference Include="Microsoft.Azure.Management.Fluent" Version="1.38.1"/>
<PackageReference Include="Microsoft.Azure.Management.ResourceManager.Fluent" Version="1.38.1"/>
</ItemGroup>
</Project>
</Project>
2 changes: 1 addition & 1 deletion DotnetGRPC/Protos
Submodule Protos updated 1 files
+28 −0 database.proto
124 changes: 124 additions & 0 deletions DotnetGRPC/Services/DatabaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
using ProtoBuf;
using Newtonsoft.Json;
using static DotnetGRPC.RecoveryPoint;
using Microsoft.Azure.Management.PostgreSQL.FlexibleServers.Models;
using Microsoft.Azure.Management.PostgreSQL.FlexibleServers;
using Microsoft.Azure.Management.PostgreSQL.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Rest;
using Newtonsoft.Json.Linq;

namespace DotnetGRPC.Services
{
Expand Down Expand Up @@ -173,5 +180,122 @@ public override async Task<Empty> RestoreDatabase(RestoreDatabaseRequest restore

return new Empty();
}

public override async Task<Empty> RestoreDatabaseToAnother(RestoreDatabaseToAnotherRequest restoreDatabaseToAnotherRequest, ServerCallContext context)
{
// Create a PostgreSQL management client
var credentials = new Microsoft.Rest.TokenCredentials(GlobalVariables.Database.BackupToken);
var postgresqlManagementClient = new PostgreSQLManagementClient(credentials)
{
SubscriptionId = "5f459f53-780f-4ffc-8604-0e47bbbfb746"
};

// Create a new server instance
var server = new Microsoft.Azure.Management.PostgreSQL.FlexibleServers.Models.Server
{
Location = "southeastasia",

// Properties = new ServerPropertiesForDefaultCreate
// {
// CreateMode = CreateMode.PointInTimeRestore,
// SourceServerId = sourceServerId,
// RestorePointInTime = restorePointInTime
// }
CreateMode = "PointInTimeRestore",
PointInTimeUTC = DateTime.Parse(restoreDatabaseToAnotherRequest.RestoreDate),
SourceServerResourceId = $"/subscriptions/5f459f53-780f-4ffc-8604-0e47bbbfb746/resourceGroups/Comp-1640/providers/Microsoft.DBforPostgreSQL/flexibleServers/{restoreDatabaseToAnotherRequest.SourceName}",

Sku = new Microsoft.Azure.Management.PostgreSQL.FlexibleServers.Models.Sku
{
Name = "Standard_B1ms",
Tier = "Burstable"
},

};

// Start the create operation
var operation = await postgresqlManagementClient.Servers.CreateAsync("Comp-1640", restoreDatabaseToAnotherRequest.ServerName, server);

// Wait for the operation to complete
// var result = await operation.WaitForCompletionAsync();

Console.WriteLine($"Done create the {operation.Id}");

// Stop the source server
// await postgresqlManagementClient.Servers.StopAsync("Comp-1640", restoreDatabaseToAnotherRequest.SourceName);


return new Empty();
}

public override async Task<Empty> StopDatabase(StopDatabaseRequest stopDatabaseRequest, ServerCallContext context)
{
// Create a PostgreSQL management client
var credentials = new Microsoft.Rest.TokenCredentials(GlobalVariables.Database.BackupToken);
var postgresqlManagementClient = new PostgreSQLManagementClient(credentials)
{
SubscriptionId = "5f459f53-780f-4ffc-8604-0e47bbbfb746"
};

// Stop the server
await postgresqlManagementClient.Servers.StopAsync("Comp-1640", stopDatabaseRequest.ServerName);

return new Empty();
}

public override async Task<Empty> StartDatabase(StartDatabaseRequest startDatabaseRequest, ServerCallContext context)
{
// Create a PostgreSQL management client
var credentials = new Microsoft.Rest.TokenCredentials(GlobalVariables.Database.BackupToken);
var postgresqlManagementClient = new PostgreSQLManagementClient(credentials)
{
SubscriptionId = "5f459f53-780f-4ffc-8604-0e47bbbfb746"
};

// Stop the server
await postgresqlManagementClient.Servers.StartAsync("Comp-1640", startDatabaseRequest.ServerName);

return new Empty();
}

public override async Task<DatabaseServersResponse> GetDatabaseServers(Empty request, ServerCallContext context)
{
var token = GlobalVariables.Database.BackupToken; // Replace with your JWT token

using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

var url = "https://management.azure.com/subscriptions/5f459f53-780f-4ffc-8604-0e47bbbfb746/resourceGroups/Comp-1640/providers/Microsoft.DBforPostgreSQL/flexibleServers?api-version=2023-12-01-preview";
var response = await client.GetAsync(url);

var databaseServersResponse = new DatabaseServersResponse();
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
// Console.WriteLine(content);
var resources = JObject.Parse(content)["value"].ToObject<JArray>();
Console.WriteLine(resources);

foreach (var resource in resources)
{
var databaseServer = new DatabaseServer
{
Name = resource["name"].ToString(),
CreatedAt = resource["systemData"]["createdAt"].ToString(),
State = resource["properties"]["state"].ToString(),
};

databaseServersResponse.DatabaseServers.Add(databaseServer);
}
}
else
{
// Handle the error...
}

return databaseServersResponse;
}
}
}
}

0 comments on commit 5614ccf

Please sign in to comment.