From 375f1e011f7447be278345dbdf78d7e842dee696 Mon Sep 17 00:00:00 2001 From: mahendra-google Date: Thu, 31 Oct 2024 09:28:27 +0000 Subject: [PATCH] addition of sample and test case for transfer from posix --- .../TransferFromPosixTest.cs | 65 ++++++++++++++++ .../TransferFromPosixSample.cs | 78 +++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 storagetransfer/api/StorageTransfer.Samples.Tests/TransferFromPosixTest.cs create mode 100644 storagetransfer/api/StorageTransfer.Samples/TransferFromPosixSample.cs diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/TransferFromPosixTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferFromPosixTest.cs new file mode 100644 index 00000000000..6f512b11fc2 --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferFromPosixTest.cs @@ -0,0 +1,65 @@ +// 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.Abstractions; +using Xunit; + + +namespace StorageTransfer.Samples.Tests; +[Collection(nameof(StorageFixture))] +public class TransferFromPosixTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _transferJobName; + private readonly ITestOutputHelper _outputHelper; + public TransferFromPosixTest(StorageFixture fixture, ITestOutputHelper outputHelper) + { + _fixture = fixture; + _outputHelper = outputHelper; + } + + [Fact] + public void TransferFromPosix() + { + TransferFromPosixSample transferFromPosixSample = new TransferFromPosixSample(_outputHelper); + var transferJob = transferFromPosixSample.TransferFromPosix(_fixture.ProjectId, _fixture.SourceAgentPoolName, _fixture.RootDirectory,_fixture.BucketNameSink); + var storage = StorageClient.Create(); + 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. + } + } +} diff --git a/storagetransfer/api/StorageTransfer.Samples/TransferFromPosixSample.cs b/storagetransfer/api/StorageTransfer.Samples/TransferFromPosixSample.cs new file mode 100644 index 00000000000..6bf91095722 --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/TransferFromPosixSample.cs @@ -0,0 +1,78 @@ +// 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_from_posix] +using Google.Cloud.StorageTransfer.V1; +using Xunit.Abstractions; + + +namespace StorageTransfer.Samples +{ + public class TransferFromPosixSample + { + /*Create a transfer from a POSIX file system to a GCS sink bucket*/ + private readonly ITestOutputHelper _output; + public TransferFromPosixSample(ITestOutputHelper output) + { + _output = output; + } + public TransferJob TransferFromPosix( + // Your Google Cloud Project ID + string projectId = "my-project-id", + // The agent pool associated with the POSIX data source. If not provided, defaults to the default agent + string sourceAgentPoolName = "projects/my-project-id/agentPools/transfer_service_default", + // The root directory path on the source filesystem + string rootDirectory = "/tmp/uploads", + // The GCS bucket to transfer data to + string sinkBucket = "my-sink-bucket") + { + + // # A useful description for your transfer job + string jobDescription = $"Transfers objects from a POSIX file system to a sink bucket ({sinkBucket})"; + + TransferJob transferJob = new TransferJob + { + ProjectId = projectId, + Description = jobDescription, + TransferSpec = new TransferSpec + { + GcsDataSink = new GcsData { BucketName = sinkBucket }, + SourceAgentPoolName = sourceAgentPoolName, + PosixDataSource = new PosixFilesystem { RootDirectory = rootDirectory } + }, + Status = TransferJob.Types.Status.Enabled, + }; + + + // 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 and ran transfer job from {rootDirectory} to {sinkBucket} with the name {response.Name}"); + return response; + + + } + } +} +// [END storagetransfer_transfer_from_posix] + +