Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[Spanner]: Add Multi Region Encryption samples #2832

Merged

Conversation

amanda-tarafa
Copy link
Member

No description provided.

@amanda-tarafa amanda-tarafa requested a review from jskeet October 29, 2024 21:54
@amanda-tarafa amanda-tarafa requested review from a team as code owners October 29, 2024 21:54
Copy link

snippet-bot bot commented Oct 29, 2024

Here is the summary of changes.

You are about to add 4 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Oct 29, 2024
@amanda-tarafa
Copy link
Member Author

@panerorenn9541 FYI

public async Task TestCopyBackupWithMultiRegionEncryptionAsync()
{
CopyBackupWithMultiRegionEncryptionAsyncSample copyBackupSample = new CopyBackupWithMultiRegionEncryptionAsyncSample();
string source_Project_id = _fixture.ProjectId;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason these are using underscores instead of sourceProjectId etc?

You could potentially introduce a record type to make this simpler:

var source = new BackupName(_fixture.ProjectId, _fixture.InstanceId, _fixture.BackupId);
var target = new BackupName(_fixture.ProjectId, _fixture.InstanceId, SpannerFixture.GenerateId("test_", 16));
...
var backup = await copyBackupSample.CopyBackupWithMultiRegionEncryptionAsync(
    source.InstanceId, source.ProjectId, source.BackupId,
    target.InstanceId, target.ProjectId, target.BackupId,
    expireTime, kmsKeyNames);
...
}

// As a nested type
public record BackupName(string ProjectId, string InstanceId, string BackupId)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is assuming there isn't a good BackupName class already, of course - I'd really have hoped there would be...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we do have BackupName, because it's used later. Just use that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simpliefied this. I used named parameters when calling the method and pass the fixture values directly.

Note that we don't pass the BackupName to the sample as parameter because we want to show how to use BackupName.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also fixed the code from where this was copied.

[SkippableFact]
public async Task TestCreateBackupWithMultiRegionEncryptionAsync()
{
Skip.If(!_fixture.RunCmekBackupSampleTests, SpannerFixture.SkipCmekBackupSamplesMessage);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps change RunCmekBackupSampleTests to SkipCmekBackupSampleTests for consistency between the two properties, and to avoid the negation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! And changed everywhere.


Console.WriteLine($"Backup copied successfully.");
Console.WriteLine($"Backup with Id {sourceBackupId} has been copied from {sourceProjectId}/{sourceInstanceId} to {targetProjectId}/{targetInstanceId} Backup {targetBackupId}");
Console.WriteLine($"Backup {backup.Name} of size {backup.SizeBytes} bytes was created with encryption keys {string.Join(", ", (object[]) kmsKeyNames)} at {backup.CreateTime} from {backup.Database} and is in state {backup.State} and has version time {backup.VersionTime.ToDateTime()}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the cast to object[]? I wouldn't expect that to be necessary. (I assume I'm missing something, but I'd like to know what...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to call ToDateTime() either - I believe we'll end up with an ISO-8601-formatted timestamp if you just use {backup.VersionTime}. (You're doing that later with CreateTime, so I'd assume it would work the same?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the cast to object[]? I wouldn't expect that to be necessary.

This is an array of CryptoKeyNames so the call to string.Join is ambiguos between object[] and IEnumerable<T>. I'm changing this to be an IEnumerable<CryptoKeyNames> instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to call ToDateTime() either

Yes, removed the call to ToDateTime, here and in the cody from where this was copied.

}

var backup = completedResponse.Result;
Console.WriteLine($"Backup {backup.Name} of size {backup.SizeBytes} bytes was created with encryption keys {string.Join(", ", (object[]) kmsKeyNames)} at {backup.CreateTime}");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto on the cast here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(It's in later samples too.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turned the parameter to IEnumerable<CryptoKeyName> everywhere. And removed the cast.

DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.Create();
// Define create table statement for table #1.
var createSingersTable =
@"CREATE TABLE Singers (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: indent this line? (Ditto for createAlbumsTable)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here and in all the create database samples.

jskeet
jskeet previously approved these changes Oct 30, 2024
Copy link
Member

@jskeet jskeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving so that you can merge after addressing the comments, which are all nits really.

Copy link
Member Author

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jskeet all comments addressed and fixed elsewhere. Changes in the second commit. PTAL.

public async Task TestCopyBackupWithMultiRegionEncryptionAsync()
{
CopyBackupWithMultiRegionEncryptionAsyncSample copyBackupSample = new CopyBackupWithMultiRegionEncryptionAsyncSample();
string source_Project_id = _fixture.ProjectId;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I simpliefied this. I used named parameters when calling the method and pass the fixture values directly.

Note that we don't pass the BackupName to the sample as parameter because we want to show how to use BackupName.

[SkippableFact]
public async Task TestCreateBackupWithMultiRegionEncryptionAsync()
{
Skip.If(!_fixture.RunCmekBackupSampleTests, SpannerFixture.SkipCmekBackupSamplesMessage);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! And changed everywhere.

public async Task TestCopyBackupWithMultiRegionEncryptionAsync()
{
CopyBackupWithMultiRegionEncryptionAsyncSample copyBackupSample = new CopyBackupWithMultiRegionEncryptionAsyncSample();
string source_Project_id = _fixture.ProjectId;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also fixed the code from where this was copied.


Console.WriteLine($"Backup copied successfully.");
Console.WriteLine($"Backup with Id {sourceBackupId} has been copied from {sourceProjectId}/{sourceInstanceId} to {targetProjectId}/{targetInstanceId} Backup {targetBackupId}");
Console.WriteLine($"Backup {backup.Name} of size {backup.SizeBytes} bytes was created with encryption keys {string.Join(", ", (object[]) kmsKeyNames)} at {backup.CreateTime} from {backup.Database} and is in state {backup.State} and has version time {backup.VersionTime.ToDateTime()}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the cast to object[]? I wouldn't expect that to be necessary.

This is an array of CryptoKeyNames so the call to string.Join is ambiguos between object[] and IEnumerable<T>. I'm changing this to be an IEnumerable<CryptoKeyNames> instead.


Console.WriteLine($"Backup copied successfully.");
Console.WriteLine($"Backup with Id {sourceBackupId} has been copied from {sourceProjectId}/{sourceInstanceId} to {targetProjectId}/{targetInstanceId} Backup {targetBackupId}");
Console.WriteLine($"Backup {backup.Name} of size {backup.SizeBytes} bytes was created with encryption keys {string.Join(", ", (object[]) kmsKeyNames)} at {backup.CreateTime} from {backup.Database} and is in state {backup.State} and has version time {backup.VersionTime.ToDateTime()}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to call ToDateTime() either

Yes, removed the call to ToDateTime, here and in the cody from where this was copied.

}

var backup = completedResponse.Result;
Console.WriteLine($"Backup {backup.Name} of size {backup.SizeBytes} bytes was created with encryption keys {string.Join(", ", (object[]) kmsKeyNames)} at {backup.CreateTime}");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turned the parameter to IEnumerable<CryptoKeyName> everywhere. And removed the cast.

DatabaseAdminClient databaseAdminClient = DatabaseAdminClient.Create();
// Define create table statement for table #1.
var createSingersTable =
@"CREATE TABLE Singers (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here and in all the create database samples.

jskeet
jskeet previously approved these changes Oct 31, 2024
Copy link
Member

@jskeet jskeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One simple nit - rest looks good.

@@ -38,9 +38,11 @@ public void CopyBackup()
string target_backupId = SpannerFixture.GenerateId("test_", 16);
DateTimeOffset expireTime = DateTimeOffset.UtcNow.AddHours(12);

Backup backup = copyBackupSample.CopyBackup(source_Instance_id, source_Project_id, source_backupId,
target_Instance_id, target_Project_id, target_backupId, expireTime);
Backup backup = copyBackupSample.CopyBackup(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variables are still declared above, just not used any more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants