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

samples (storage transfer) : add samples and test cases for storage transfer services i.e transfer to nearline,check latest transfer operation and quickstart #1

Merged
merged 9 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License").
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
using Xunit;
using Xunit.Abstractions;

namespace StorageTransfer.Samples.Tests;

[Collection(nameof(StorageFixture))]
public class CheckLatestTransferOperationTest
{

private readonly StorageFixture _fixture;
private string _jobName;
private readonly ITestOutputHelper _outputHelper;
public CheckLatestTransferOperationTest(StorageFixture fixture, ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
_fixture = fixture;
}

[Fact]
public void CheckLatestTransferOperation()
{
CheckLatestTransferOperationSample checkLatestTransferOperationSample = new CheckLatestTransferOperationSample(_outputHelper);
var transferJob = checkLatestTransferOperationSample.CheckLatestTransferOperation(_fixture.ProjectId,_fixture.JobName);
Assert.Contains("transferJobs/", transferJob.Name);
_jobName = transferJob.Name;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2021 Google Inc.
/**
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
using System;
using Google.Cloud.StorageTransfer.V1;
using Xunit;
using Xunit.Abstractions;

namespace StorageTransfer.Samples.Tests
{
Expand All @@ -25,16 +26,18 @@ public class QuickstartTest : IDisposable
{
private readonly StorageFixture _fixture;
private string _transferJobName;
private readonly ITestOutputHelper _outputHelper;

public QuickstartTest(StorageFixture fixture)
public QuickstartTest(StorageFixture fixture , ITestOutputHelper outputHelper)
{
_fixture = fixture;
_outputHelper = outputHelper;
}

[Fact]
public void TestQuickstart()
{
QuickstartSample quickstartSample = new QuickstartSample();
QuickstartSample quickstartSample = new QuickstartSample(_outputHelper);
var transferJob = quickstartSample.Quickstart(_fixture.ProjectId, _fixture.BucketNameSource, _fixture.BucketNameSink);
Assert.Contains("transferJobs/", transferJob.Name);
_transferJobName = transferJob.Name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/**
* Copyright 2021 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -28,11 +28,16 @@ public class StorageFixture : IDisposable, ICollectionFixture<StorageFixture>
public string ProjectId { get; }
public string BucketNameSource { get; } = Guid.NewGuid().ToString();
public string BucketNameSink { get; } = Guid.NewGuid().ToString();
public string JobName { get; }
public StorageClient Storage { get; } = StorageClient.Create();
public StorageTransferServiceClient Sts { get; } = StorageTransferServiceClient.Create();

public StorageFixture()
{
// Instantiate random number generator
Random random = new Random();
JobName = "transferJobs/" + random.NextInt64(1000000000000000, 9223372036854775807) + " ";

ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
if (string.IsNullOrWhiteSpace(ProjectId))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License").
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


using Google.Cloud.Storage.V1;
using Google.Cloud.StorageTransfer.V1;
using System;
using Xunit;
using Xunit.Abstractions;

namespace StorageTransfer.Samples.Tests;

[Collection(nameof(StorageFixture))]
public class TransferToNearlineTest : IDisposable
{
private readonly StorageFixture _fixture;
private string _transferJobName;
private readonly ITestOutputHelper _outputHelper;
public TransferToNearlineTest(StorageFixture fixture, ITestOutputHelper outputHelper)
{
_fixture = fixture;
_outputHelper = outputHelper;
}

[Fact]
public void TestTransferToNearline()
{
TransferToNearlineSample transferToNearlineSample = new TransferToNearlineSample(_outputHelper);
var storage = StorageClient.Create();
var bucket = storage.GetBucket(_fixture.BucketNameSink);
string storageClass = StorageClasses.Nearline;
bucket.StorageClass = storageClass;
bucket = storage.UpdateBucket(bucket);
var transferJob = transferToNearlineSample.TransferToNearline(_fixture.ProjectId, _fixture.BucketNameSource, _fixture.BucketNameSink);
Assert.Contains("transferJobs/", transferJob.Name);
_transferJobName = transferJob.Name;
}

public void Dispose()
{
try
{
_fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest()
{
ProjectId = _fixture.ProjectId,
JobName = _transferJobName,
TransferJob = new TransferJob()
{
Name = _transferJobName,
Status = TransferJob.Types.Status.Deleted
}
});
}
catch (Exception)
{
// Do nothing, we delete on a best effort basis.
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License").
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START storagetransfer_get_latest_transfer_operation]

using Google.Cloud.StorageTransfer.V1;
using System;
using Xunit.Abstractions;

namespace StorageTransfer.Samples
{
public class CheckLatestTransferOperationSample
{
private readonly ITestOutputHelper _output;
public CheckLatestTransferOperationSample(ITestOutputHelper output)
{
_output = output;

}
//Checks the latest transfer operation for a given transfer job.
public TransferJob CheckLatestTransferOperation(
// Your Google Cloud Project ID
string projectId = "my-project-id",
// The name of the job to check
string jobName = "transferJobs/1234567890")
{
if(string.IsNullOrEmpty(jobName))
{
throw new Exception("JobName can not be null or empty");
}
// Create a Transfer Service client
StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.Create();

GetTransferJobRequest getTransferJobRequest = new GetTransferJobRequest { ProjectId = projectId, JobName = jobName };
try
{
// Get Transfer job
TransferJob transferJob = storageTransfer.GetTransferJob(getTransferJobRequest);
// Get Latest operation name from tranfer job
string latestOperationName = transferJob.LatestOperationName;


if (!string.IsNullOrEmpty(latestOperationName))
{
_output.WriteLine("The latest operation for transfer job " +jobName+ " is: " +latestOperationName+ "");
}
else
{
_output.WriteLine("Transfer job "+ jobName +" hasn't run yet, try again once after job started running.");
}
return transferJob;
}
catch (Exception)
{
throw new Exception("Failed to get transfer job "+ jobName + "");
}
}
}
}
// [END storagetransfer_get_latest_transfer_operation]
18 changes: 11 additions & 7 deletions storagetransfer/api/StorageTransfer.Samples/QuickstartSample.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2021 Google Inc.
/**
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,15 +14,19 @@
* limitations under the License.
*/


// [START storagetransfer_quickstart]
using System;
using Google.Cloud.StorageTransfer.V1;
using Xunit.Abstractions;

namespace StorageTransfer.Samples
{
public class QuickstartSample
{
private readonly ITestOutputHelper _output;
public QuickstartSample(ITestOutputHelper output)
{
_output = output;
}
public TransferJob Quickstart(
// Your Google Cloud Project ID
string projectId = "my-project-id",
Expand All @@ -36,8 +40,8 @@ public TransferJob Quickstart(
ProjectId = projectId,
TransferSpec = new TransferSpec
{
GcsDataSink = new GcsData { BucketName = sourceBucket },
GcsDataSource = new GcsData { BucketName = sinkBucket }
GcsDataSink = new GcsData { BucketName = sinkBucket },
GcsDataSource = new GcsData { BucketName = sourceBucket }
},
Status = TransferJob.Types.Status.Enabled
};
Expand All @@ -50,7 +54,7 @@ public TransferJob Quickstart(
ProjectId = projectId
});

Console.WriteLine($"Created and ran transfer job from {sourceBucket} to {sinkBucket} with name {response.Name}");
_output.WriteLine($"Created and ran transfer job from {sourceBucket} to {sinkBucket} with name {response.Name}");

return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

<ItemGroup>
<PackageReference Include="Google.Cloud.StorageTransfer.V1" Version="2.7.0" />
<PackageReference Include="xunit.abstractions" Version="2.0.3" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License").
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [START storagetransfer_transfer_to_nearline]

using Google.Cloud.StorageTransfer.V1;
using Google.Protobuf.WellKnownTypes;
using System;
using Xunit.Abstractions;


namespace StorageTransfer.Samples
{

public class TransferToNearlineSample
{
/*Creates a one-off transfer job that transfers objects from a standard GCS bucket that are more
than 30 days old to a Nearline GCS bucket.*/
private readonly ITestOutputHelper _output;
public TransferToNearlineSample(ITestOutputHelper output)
{
_output = output;
}
public TransferJob TransferToNearline(
// Your Google Cloud Project ID
string projectId = "my-project-id",
// The GCS bucket to transfer objects from
string sourceBucket = "my-source-bucket",
// The GCS Nearline bucket to transfer old objects to
string sinkBucket = "my-sink-bucket")
{
// A description of this job
string jobDescription = $"Transfers old objects from standard bucket ({sourceBucket}) that haven't been modified in the last 30 days to a Nearline bucket ({sinkBucket})";

TransferJob transferJob = new TransferJob
{
ProjectId = projectId,
Description = jobDescription,
TransferSpec = new TransferSpec
{
GcsDataSink = new GcsData { BucketName = sinkBucket },
GcsDataSource = new GcsData { BucketName = sourceBucket },
ObjectConditions = new ObjectConditions { MinTimeElapsedSinceLastModification = Duration.FromTimeSpan(TimeSpan.FromSeconds(2592000)) },
TransferOptions = new TransferOptions { DeleteObjectsFromSourceAfterTransfer = true },
},
Status = TransferJob.Types.Status.Enabled,
Schedule = new Schedule { ScheduleStartDate = Google.Type.Date.FromDateTime(System.DateTime.UtcNow.Date.AddMonths(1)), ScheduleEndDate = Google.Type.Date.FromDateTime(System.DateTime.UtcNow.Date.AddMonths(1)) }
};
// Create a Transfer Service client
StorageTransferServiceClient client = StorageTransferServiceClient.Create();
// Create a Transfer job
TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob });
client.RunTransferJob(new RunTransferJobRequest
{
JobName = response.Name,
ProjectId = projectId
});

_output.WriteLine($"Created one-off transfer job from standard bucket {sourceBucket} to Nearline bucket {sinkBucket} with the name {response.Name}");
return response;
}
}
}
// [END storagetransfer_transfer_to_nearline]