diff --git a/.github/blunderbuss.yml b/.github/blunderbuss.yml index 08be36a46ee..84164bfc5a6 100644 --- a/.github/blunderbuss.yml +++ b/.github/blunderbuss.yml @@ -49,6 +49,10 @@ assign_issues_by: - 'api: parametermanager' to: - GoogleCloudPlatform/cloud-parameters-team +- labels: + - "api: modelarmor" + to: + - GoogleCloudPlatform/cloud-modelarmor-team assign_issues: - GoogleCloudPlatform/dotnet-samples-reviewers @@ -110,4 +114,8 @@ assign_prs_by: - labels: - 'api: parametermanager' to: - - GoogleCloudPlatform/cloud-parameters-team \ No newline at end of file + - GoogleCloudPlatform/cloud-parameters-team +- labels: + - "api: modelarmor" + to: + - GoogleCloudPlatform/cloud-modelarmor-team diff --git a/CODEOWNERS b/CODEOWNERS index d1ebe9e6c4c..7dc6468465e 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -36,6 +36,8 @@ applications/googlehome-meets-dotnetcontainers/ @meteatamel @GoogleCloudPlatfor run/ @meteatamel @GoogleCloudPlatform/dotnet-samples-reviewers aiplatform/ @meteatamel @GoogleCloudPlatform/dotnet-samples-reviewers +modelarmor/ @GoogleCloudPlatform/dotnet-samples-reviewers @GoogleCloudPlatform/cloud-modelarmor-team + retail/ @chenlei1216 @GoogleCloudPlatform/dotnet-samples-reviewers logging/ @GoogleCloudPlatform/dee-platform-ops @GoogleCloudPlatform/dotnet-samples-reviewers diff --git a/aiplatform/api/AIPlatform.Samples.Tests/AIPlatform.Samples.Tests.csproj b/aiplatform/api/AIPlatform.Samples.Tests/AIPlatform.Samples.Tests.csproj index 9e6ce533a02..b2633564cf3 100644 --- a/aiplatform/api/AIPlatform.Samples.Tests/AIPlatform.Samples.Tests.csproj +++ b/aiplatform/api/AIPlatform.Samples.Tests/AIPlatform.Samples.Tests.csproj @@ -6,7 +6,7 @@ - + diff --git a/appengine/flexible/Analytics/Analytics.Tests/Analytics.Tests.csproj b/appengine/flexible/Analytics/Analytics.Tests/Analytics.Tests.csproj index e7917358757..ce75b2c38f5 100644 --- a/appengine/flexible/Analytics/Analytics.Tests/Analytics.Tests.csproj +++ b/appengine/flexible/Analytics/Analytics.Tests/Analytics.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/appengine/flexible/CloudStorage/CloudStorage.Tests/CloudStorage.Tests.csproj b/appengine/flexible/CloudStorage/CloudStorage.Tests/CloudStorage.Tests.csproj index 586887fa83d..4c4c476bffc 100644 --- a/appengine/flexible/CloudStorage/CloudStorage.Tests/CloudStorage.Tests.csproj +++ b/appengine/flexible/CloudStorage/CloudStorage.Tests/CloudStorage.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/appengine/flexible/Datastore/Datastore.Tests/Datastore.Tests.csproj b/appengine/flexible/Datastore/Datastore.Tests/Datastore.Tests.csproj index ecb4783768b..220067f8ff3 100644 --- a/appengine/flexible/Datastore/Datastore.Tests/Datastore.Tests.csproj +++ b/appengine/flexible/Datastore/Datastore.Tests/Datastore.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/appengine/flexible/HelloWorld/HelloWorld.Tests/HelloWorld.Tests.csproj b/appengine/flexible/HelloWorld/HelloWorld.Tests/HelloWorld.Tests.csproj index 524883dab39..641e5b3263a 100644 --- a/appengine/flexible/HelloWorld/HelloWorld.Tests/HelloWorld.Tests.csproj +++ b/appengine/flexible/HelloWorld/HelloWorld.Tests/HelloWorld.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/appengine/flexible/Metadata/Metadata.Tests/Metadata.Tests.csproj b/appengine/flexible/Metadata/Metadata.Tests/Metadata.Tests.csproj index 7f018255c6b..728e3164cb7 100644 --- a/appengine/flexible/Metadata/Metadata.Tests/Metadata.Tests.csproj +++ b/appengine/flexible/Metadata/Metadata.Tests/Metadata.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/appengine/flexible/Pubsub/Pubsub.Tests/Pubsub.Tests.csproj b/appengine/flexible/Pubsub/Pubsub.Tests/Pubsub.Tests.csproj index 1e9296a0589..759c19a3417 100644 --- a/appengine/flexible/Pubsub/Pubsub.Tests/Pubsub.Tests.csproj +++ b/appengine/flexible/Pubsub/Pubsub.Tests/Pubsub.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/appengine/flexible/StaticContent/StaticContent.Tests/StaticContent.Tests.csproj b/appengine/flexible/StaticContent/StaticContent.Tests/StaticContent.Tests.csproj index d9a1079b316..2bd3366fe47 100644 --- a/appengine/flexible/StaticContent/StaticContent.Tests/StaticContent.Tests.csproj +++ b/appengine/flexible/StaticContent/StaticContent.Tests/StaticContent.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/appengine/flexible/WebSocket/WebSocket.Tests/WebSocket.Tests.csproj b/appengine/flexible/WebSocket/WebSocket.Tests/WebSocket.Tests.csproj index 8490962425f..a57d8482ef5 100644 --- a/appengine/flexible/WebSocket/WebSocket.Tests/WebSocket.Tests.csproj +++ b/appengine/flexible/WebSocket/WebSocket.Tests/WebSocket.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/applications/leaderboard/LeaderboardTest/LeaderboardTest.csproj b/applications/leaderboard/LeaderboardTest/LeaderboardTest.csproj index fd682d16419..ecab5eb0665 100644 --- a/applications/leaderboard/LeaderboardTest/LeaderboardTest.csproj +++ b/applications/leaderboard/LeaderboardTest/LeaderboardTest.csproj @@ -8,7 +8,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/asset/api/Asset.Samples.Tests/Asset.Samples.Tests.csproj b/asset/api/Asset.Samples.Tests/Asset.Samples.Tests.csproj index 7fc8e786a3c..236cc9d0bca 100644 --- a/asset/api/Asset.Samples.Tests/Asset.Samples.Tests.csproj +++ b/asset/api/Asset.Samples.Tests/Asset.Samples.Tests.csproj @@ -1,14 +1,14 @@  - net6.0 + net8.0 false - + diff --git a/asset/api/Asset.Samples.Tests/SearchAllIamPoliciesTest.cs b/asset/api/Asset.Samples.Tests/SearchAllIamPoliciesTest.cs index 279fe72711e..1a9b283bbd8 100644 --- a/asset/api/Asset.Samples.Tests/SearchAllIamPoliciesTest.cs +++ b/asset/api/Asset.Samples.Tests/SearchAllIamPoliciesTest.cs @@ -34,7 +34,8 @@ public void TestSearchAllIamPolicies() { // Run the sample code. string scope = $"projects/{_fixture.ProjectId}"; - var result = _sample.SearchAllIamPolicies(scope, query: "policy:roles/owner"); + // On the test we use an empty query to make sure we find some policy. + var result = _sample.SearchAllIamPolicies(scope, query: ""); Assert.NotEmpty(result.Results); } diff --git a/asset/api/Asset.sln b/asset/api/Asset.sln index 095021856c8..2225b86634f 100644 --- a/asset/api/Asset.sln +++ b/asset/api/Asset.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31229.75 +# Visual Studio Version 17 +VisualStudioVersion = 17.13.35931.197 d17.13 MinimumVisualStudioVersion = 15.0.26124.0 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Asset.Samples", "Asset.Samples\Asset.Samples.csproj", "{9E6D0AED-B21C-4A20-90EA-F17875346C8D}" EndProject diff --git a/auth/AuthSample/AuthSample.csproj b/auth/AuthSample/AuthSample.csproj index 1429918703a..0b6fc092bc9 100644 --- a/auth/AuthSample/AuthSample.csproj +++ b/auth/AuthSample/AuthSample.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 diff --git a/auth/AuthTest/AuthTest.csproj b/auth/AuthTest/AuthTest.csproj index 9821e8f6b3c..d370c59652e 100644 --- a/auth/AuthTest/AuthTest.csproj +++ b/auth/AuthTest/AuthTest.csproj @@ -1,11 +1,11 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/bigquery-storage/api/BigQueryStorage.Samples.Tests/BigQueryStorage.Samples.Tests.csproj b/bigquery-storage/api/BigQueryStorage.Samples.Tests/BigQueryStorage.Samples.Tests.csproj index d9797b85a13..dc260170f77 100644 --- a/bigquery-storage/api/BigQueryStorage.Samples.Tests/BigQueryStorage.Samples.Tests.csproj +++ b/bigquery-storage/api/BigQueryStorage.Samples.Tests/BigQueryStorage.Samples.Tests.csproj @@ -6,7 +6,7 @@ - + diff --git a/bigquery/api/SimpleApp/SimpleApp.csproj b/bigquery/api/SimpleApp/SimpleApp.csproj index 6ecb4e1a1ca..7bfa2055c83 100644 --- a/bigquery/api/SimpleApp/SimpleApp.csproj +++ b/bigquery/api/SimpleApp/SimpleApp.csproj @@ -2,14 +2,14 @@ Exe - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/bigquery/api/Snippets/Snippets.csproj b/bigquery/api/Snippets/Snippets.csproj index 3a3da9f4387..a95c527927c 100644 --- a/bigquery/api/Snippets/Snippets.csproj +++ b/bigquery/api/Snippets/Snippets.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 - + runtime; build; native; contentfiles; analyzers all diff --git a/bigquery/data-transfer/api/QuickStart/QuickStart.csproj b/bigquery/data-transfer/api/QuickStart/QuickStart.csproj index b19b0793a3b..e6f48768273 100644 --- a/bigquery/data-transfer/api/QuickStart/QuickStart.csproj +++ b/bigquery/data-transfer/api/QuickStart/QuickStart.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 diff --git a/bigquery/data-transfer/api/QuickStartTest/QuickStartTest.csproj b/bigquery/data-transfer/api/QuickStartTest/QuickStartTest.csproj index c1e4d3d3815..6c964cbd0b4 100644 --- a/bigquery/data-transfer/api/QuickStartTest/QuickStartTest.csproj +++ b/bigquery/data-transfer/api/QuickStartTest/QuickStartTest.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers diff --git a/bigtable/api/BigtableTest/BigtableTest.csproj b/bigtable/api/BigtableTest/BigtableTest.csproj index 99678b6efe6..17eb7787fa1 100644 --- a/bigtable/api/BigtableTest/BigtableTest.csproj +++ b/bigtable/api/BigtableTest/BigtableTest.csproj @@ -8,7 +8,7 @@ - + runtime; build; native; contentfiles; analyzers all diff --git a/bigtable/api/SnippetsTest/SnippetsTest.csproj b/bigtable/api/SnippetsTest/SnippetsTest.csproj index 603bf47829f..5f9eb8a5728 100644 --- a/bigtable/api/SnippetsTest/SnippetsTest.csproj +++ b/bigtable/api/SnippetsTest/SnippetsTest.csproj @@ -9,7 +9,7 @@ - + diff --git a/cdn/signUrl/SignUrlSample.Tests/SignUrlSample.Tests.csproj b/cdn/signUrl/SignUrlSample.Tests/SignUrlSample.Tests.csproj index 69a14940ab5..af1e51d36d6 100644 --- a/cdn/signUrl/SignUrlSample.Tests/SignUrlSample.Tests.csproj +++ b/cdn/signUrl/SignUrlSample.Tests/SignUrlSample.Tests.csproj @@ -8,7 +8,7 @@ - + runtime; build; native; contentfiles; analyzers diff --git a/cloudtasks/api/TasksSample/TasksSample.csproj b/cloudtasks/api/TasksSample/TasksSample.csproj index 893abcebf02..67152b284f5 100644 --- a/cloudtasks/api/TasksSample/TasksSample.csproj +++ b/cloudtasks/api/TasksSample/TasksSample.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 - + diff --git a/commandlineutil/Sample/CommandLineUtilSample.csproj b/commandlineutil/Sample/CommandLineUtilSample.csproj index 31e669a88c9..c676f76ecfc 100644 --- a/commandlineutil/Sample/CommandLineUtilSample.csproj +++ b/commandlineutil/Sample/CommandLineUtilSample.csproj @@ -6,7 +6,7 @@ Exe - net6.0 + net8.0 diff --git a/commandlineutil/SampleTest/SampleTest.csproj b/commandlineutil/SampleTest/SampleTest.csproj index d20bb9fe7f8..c86706559d2 100644 --- a/commandlineutil/SampleTest/SampleTest.csproj +++ b/commandlineutil/SampleTest/SampleTest.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 false - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/compute/api/Compute.Samples.Tests/Compute.Samples.Tests.csproj b/compute/api/Compute.Samples.Tests/Compute.Samples.Tests.csproj index c1fa7789b91..fc61d9bc9c0 100644 --- a/compute/api/Compute.Samples.Tests/Compute.Samples.Tests.csproj +++ b/compute/api/Compute.Samples.Tests/Compute.Samples.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/datastore/admin-api/DatastoreAdmin.Samples.Tests/DatastoreAdmin.Samples.Tests.csproj b/datastore/admin-api/DatastoreAdmin.Samples.Tests/DatastoreAdmin.Samples.Tests.csproj index 9fc045f5609..2f2d1e661d9 100644 --- a/datastore/admin-api/DatastoreAdmin.Samples.Tests/DatastoreAdmin.Samples.Tests.csproj +++ b/datastore/admin-api/DatastoreAdmin.Samples.Tests/DatastoreAdmin.Samples.Tests.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 false - + diff --git a/datastore/api/QuickStart/QuickStart.csproj b/datastore/api/QuickStart/QuickStart.csproj index 06a8c793ef4..3bdff3e2b7b 100644 --- a/datastore/api/QuickStart/QuickStart.csproj +++ b/datastore/api/QuickStart/QuickStart.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 diff --git a/datastore/api/QuickStartTest/QuickStartTest.csproj b/datastore/api/QuickStartTest/QuickStartTest.csproj index eadae3239b6..c5f037f5bbe 100644 --- a/datastore/api/QuickStartTest/QuickStartTest.csproj +++ b/datastore/api/QuickStartTest/QuickStartTest.csproj @@ -1,14 +1,14 @@  - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/datastore/api/TaskList/TaskList.csproj b/datastore/api/TaskList/TaskList.csproj index 61e6341cf28..62d32836bcf 100644 --- a/datastore/api/TaskList/TaskList.csproj +++ b/datastore/api/TaskList/TaskList.csproj @@ -1,7 +1,7 @@  Exe - net6.0 + net8.0 diff --git a/datastore/api/TaskListTest/DatastoreTest.csproj b/datastore/api/TaskListTest/DatastoreTest.csproj index 09414202f3d..f122f7b083a 100644 --- a/datastore/api/TaskListTest/DatastoreTest.csproj +++ b/datastore/api/TaskListTest/DatastoreTest.csproj @@ -1,11 +1,11 @@  Library - net6.0 + net8.0 - + runtime; build; native; contentfiles; analyzers all diff --git a/dlp/api/Snippets.Tests/Snippets.Tests.csproj b/dlp/api/Snippets.Tests/Snippets.Tests.csproj index 63d61547253..3347f89d8f6 100644 --- a/dlp/api/Snippets.Tests/Snippets.Tests.csproj +++ b/dlp/api/Snippets.Tests/Snippets.Tests.csproj @@ -1,11 +1,11 @@  - net6.0 + net8.0 false - + all diff --git a/documentai/api/DocumentAI.Samples.Tests/DocumentAI.Samples.Tests.csproj b/documentai/api/DocumentAI.Samples.Tests/DocumentAI.Samples.Tests.csproj index 09fa5fe15ec..54a8eb5a926 100644 --- a/documentai/api/DocumentAI.Samples.Tests/DocumentAI.Samples.Tests.csproj +++ b/documentai/api/DocumentAI.Samples.Tests/DocumentAI.Samples.Tests.csproj @@ -1,12 +1,12 @@ - net6.0 + net8.0 false - + diff --git a/documentai/api/DocumentAI.Samples/DocumentAI.Samples.csproj b/documentai/api/DocumentAI.Samples/DocumentAI.Samples.csproj index 90dba123732..28f7763c480 100644 --- a/documentai/api/DocumentAI.Samples/DocumentAI.Samples.csproj +++ b/documentai/api/DocumentAI.Samples/DocumentAI.Samples.csproj @@ -1,6 +1,6 @@ - net6.0 + net8.0 false diff --git a/error-reporting/api/ErrorReportingSampleTests/ErrorReportingSampleTests.csproj b/error-reporting/api/ErrorReportingSampleTests/ErrorReportingSampleTests.csproj index e917e2d381a..32f503c61e8 100644 --- a/error-reporting/api/ErrorReportingSampleTests/ErrorReportingSampleTests.csproj +++ b/error-reporting/api/ErrorReportingSampleTests/ErrorReportingSampleTests.csproj @@ -8,7 +8,7 @@ - + runtime; build; native; contentfiles; analyzers diff --git a/firestore/api/AddData/AddData.csproj b/firestore/api/AddData/AddData.csproj index 97cd2ead27f..e24a8cc037c 100644 --- a/firestore/api/AddData/AddData.csproj +++ b/firestore/api/AddData/AddData.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.AddData diff --git a/firestore/api/DataModel/DataModel.csproj b/firestore/api/DataModel/DataModel.csproj index 8f19930ee38..d4d70c82f7e 100644 --- a/firestore/api/DataModel/DataModel.csproj +++ b/firestore/api/DataModel/DataModel.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.DataModel diff --git a/firestore/api/DeleteData/DeleteData.csproj b/firestore/api/DeleteData/DeleteData.csproj index b743197d2b8..6f34bfb3604 100644 --- a/firestore/api/DeleteData/DeleteData.csproj +++ b/firestore/api/DeleteData/DeleteData.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.DeleteData diff --git a/firestore/api/FirestoreTest/FirestoreTest.csproj b/firestore/api/FirestoreTest/FirestoreTest.csproj index c4cda8c439e..a3c11859f6c 100644 --- a/firestore/api/FirestoreTest/FirestoreTest.csproj +++ b/firestore/api/FirestoreTest/FirestoreTest.csproj @@ -1,12 +1,12 @@ Library - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/firestore/api/GetData/GetData.csproj b/firestore/api/GetData/GetData.csproj index 9d14cea87e7..f51180ce14d 100644 --- a/firestore/api/GetData/GetData.csproj +++ b/firestore/api/GetData/GetData.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.GetData latest diff --git a/firestore/api/ListenData/ListenData.csproj b/firestore/api/ListenData/ListenData.csproj index af6d4603c61..b9085f1e064 100644 --- a/firestore/api/ListenData/ListenData.csproj +++ b/firestore/api/ListenData/ListenData.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.ListenData diff --git a/firestore/api/ManageIndexes/ManageIndexes.csproj b/firestore/api/ManageIndexes/ManageIndexes.csproj index 0614b7cf15a..b7dcd47a2eb 100644 --- a/firestore/api/ManageIndexes/ManageIndexes.csproj +++ b/firestore/api/ManageIndexes/ManageIndexes.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.ManageIndexes diff --git a/firestore/api/OrderLimitData/OrderLimitData.csproj b/firestore/api/OrderLimitData/OrderLimitData.csproj index 89d2b98382c..14b1660109c 100644 --- a/firestore/api/OrderLimitData/OrderLimitData.csproj +++ b/firestore/api/OrderLimitData/OrderLimitData.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.OrderLimitData diff --git a/firestore/api/PaginateData/PaginateData.csproj b/firestore/api/PaginateData/PaginateData.csproj index 8b3c270fe5b..57a8ea61104 100644 --- a/firestore/api/PaginateData/PaginateData.csproj +++ b/firestore/api/PaginateData/PaginateData.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.PaginateData diff --git a/firestore/api/QueryData/QueryData.csproj b/firestore/api/QueryData/QueryData.csproj index 9269c3b6b1e..9a3618a5696 100644 --- a/firestore/api/QueryData/QueryData.csproj +++ b/firestore/api/QueryData/QueryData.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.QueryData diff --git a/firestore/api/Quickstart/Quickstart.csproj b/firestore/api/Quickstart/Quickstart.csproj index 596215aaf14..51cae027f90 100644 --- a/firestore/api/Quickstart/Quickstart.csproj +++ b/firestore/api/Quickstart/Quickstart.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.Quickstart diff --git a/firestore/api/SolutionCounter/SolutionCounter.csproj b/firestore/api/SolutionCounter/SolutionCounter.csproj index ac419949445..fb3d19d068a 100644 --- a/firestore/api/SolutionCounter/SolutionCounter.csproj +++ b/firestore/api/SolutionCounter/SolutionCounter.csproj @@ -1,7 +1,7 @@  Exe - net6.0 + net8.0 GoogleCloudSamples.DistributedCounter diff --git a/firestore/api/TransactionsAndBatchedWrites/TransactionsAndBatchedWrites.csproj b/firestore/api/TransactionsAndBatchedWrites/TransactionsAndBatchedWrites.csproj index 0f5722c8e92..8b42b971e87 100644 --- a/firestore/api/TransactionsAndBatchedWrites/TransactionsAndBatchedWrites.csproj +++ b/firestore/api/TransactionsAndBatchedWrites/TransactionsAndBatchedWrites.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 GoogleCloudSamples.TransactionsAndBatchedWrites diff --git a/functions/concepts/Concepts.Tests/Concepts.Tests.csproj b/functions/concepts/Concepts.Tests/Concepts.Tests.csproj index 3310ab767da..4220336ce34 100644 --- a/functions/concepts/Concepts.Tests/Concepts.Tests.csproj +++ b/functions/concepts/Concepts.Tests/Concepts.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/functions/firebase/Firebase.Tests/Firebase.Tests.csproj b/functions/firebase/Firebase.Tests/Firebase.Tests.csproj index 8a3a00dee04..fc8f00f9fff 100644 --- a/functions/firebase/Firebase.Tests/Firebase.Tests.csproj +++ b/functions/firebase/Firebase.Tests/Firebase.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/functions/helloworld/HelloWorld.Tests/HelloWorld.Tests.csproj b/functions/helloworld/HelloWorld.Tests/HelloWorld.Tests.csproj index 1273a5ed38c..68e3bf3f02c 100644 --- a/functions/helloworld/HelloWorld.Tests/HelloWorld.Tests.csproj +++ b/functions/helloworld/HelloWorld.Tests/HelloWorld.Tests.csproj @@ -9,7 +9,7 @@ - + diff --git a/functions/http/Http.Tests/Http.Tests.csproj b/functions/http/Http.Tests/Http.Tests.csproj index ff06f627f42..706334569cb 100644 --- a/functions/http/Http.Tests/Http.Tests.csproj +++ b/functions/http/Http.Tests/Http.Tests.csproj @@ -8,7 +8,7 @@ - + diff --git a/functions/logging/Logging.Tests/Logging.Tests.csproj b/functions/logging/Logging.Tests/Logging.Tests.csproj index 6a34cc08056..7930baa0ec1 100644 --- a/functions/logging/Logging.Tests/Logging.Tests.csproj +++ b/functions/logging/Logging.Tests/Logging.Tests.csproj @@ -8,7 +8,7 @@ - + diff --git a/functions/responsestreaming/StreamBigQuery.Tests/StreamBigQuery.Tests.csproj b/functions/responsestreaming/StreamBigQuery.Tests/StreamBigQuery.Tests.csproj index e9014785286..37d7e2c0b8b 100644 --- a/functions/responsestreaming/StreamBigQuery.Tests/StreamBigQuery.Tests.csproj +++ b/functions/responsestreaming/StreamBigQuery.Tests/StreamBigQuery.Tests.csproj @@ -8,7 +8,7 @@ - + diff --git a/functions/typed/Typed.Tests/Typed.Tests.csproj b/functions/typed/Typed.Tests/Typed.Tests.csproj index 4742c6c8beb..fe1c1a9ddbc 100644 --- a/functions/typed/Typed.Tests/Typed.Tests.csproj +++ b/functions/typed/Typed.Tests/Typed.Tests.csproj @@ -8,7 +8,7 @@ - + diff --git a/iam/api/AccessTest/AccessTest.csproj b/iam/api/AccessTest/AccessTest.csproj index dcd300b3d1b..f0f982c6b6a 100644 --- a/iam/api/AccessTest/AccessTest.csproj +++ b/iam/api/AccessTest/AccessTest.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/iam/api/CustomRoles/CustomRoles.csproj b/iam/api/CustomRoles/CustomRoles.csproj index 941b8749c0f..3121ab15fb7 100644 --- a/iam/api/CustomRoles/CustomRoles.csproj +++ b/iam/api/CustomRoles/CustomRoles.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 diff --git a/iam/api/CustomRolesTest/CustomRolesTest.csproj b/iam/api/CustomRolesTest/CustomRolesTest.csproj index 87e0faf12ac..67092cec778 100644 --- a/iam/api/CustomRolesTest/CustomRolesTest.csproj +++ b/iam/api/CustomRolesTest/CustomRolesTest.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/iam/api/QuickStart/QuickStart.csproj b/iam/api/QuickStart/QuickStart.csproj index f9bb4d218c6..74ea8602217 100644 --- a/iam/api/QuickStart/QuickStart.csproj +++ b/iam/api/QuickStart/QuickStart.csproj @@ -1,7 +1,7 @@ Exe - net6.0 + net8.0 diff --git a/iam/api/QuickStartTest/QuickStartTest.csproj b/iam/api/QuickStartTest/QuickStartTest.csproj index 4213223895d..c6d1373c3e3 100644 --- a/iam/api/QuickStartTest/QuickStartTest.csproj +++ b/iam/api/QuickStartTest/QuickStartTest.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers diff --git a/iam/api/ServiceAccountKeys/ServiceAccountKeys.csproj b/iam/api/ServiceAccountKeys/ServiceAccountKeys.csproj index 3f33039976a..5af75454772 100644 --- a/iam/api/ServiceAccountKeys/ServiceAccountKeys.csproj +++ b/iam/api/ServiceAccountKeys/ServiceAccountKeys.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 diff --git a/iam/api/ServiceAccountTests/ServiceAccountTests.csproj b/iam/api/ServiceAccountTests/ServiceAccountTests.csproj index 46ad5f91d2e..df694208bee 100644 --- a/iam/api/ServiceAccountTests/ServiceAccountTests.csproj +++ b/iam/api/ServiceAccountTests/ServiceAccountTests.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/iam/api/ServiceAccounts/ServiceAccounts.csproj b/iam/api/ServiceAccounts/ServiceAccounts.csproj index 367512fc982..d2e589674c0 100644 --- a/iam/api/ServiceAccounts/ServiceAccounts.csproj +++ b/iam/api/ServiceAccounts/ServiceAccounts.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 diff --git a/identity-aware-proxy/IAPClientTest/IAPClientTest.csproj b/identity-aware-proxy/IAPClientTest/IAPClientTest.csproj index 2ca73213396..b6bc77624cd 100644 --- a/identity-aware-proxy/IAPClientTest/IAPClientTest.csproj +++ b/identity-aware-proxy/IAPClientTest/IAPClientTest.csproj @@ -9,7 +9,7 @@ - + runtime; build; native; contentfiles; analyzers diff --git a/kms/api/Kms.Samples.Tests/Kms.Samples.Tests.csproj b/kms/api/Kms.Samples.Tests/Kms.Samples.Tests.csproj index 86ea65067e3..a3b31cfa553 100644 --- a/kms/api/Kms.Samples.Tests/Kms.Samples.Tests.csproj +++ b/kms/api/Kms.Samples.Tests/Kms.Samples.Tests.csproj @@ -6,7 +6,7 @@ - + diff --git a/logging/api/LoggingTest/LoggingTest.csproj b/logging/api/LoggingTest/LoggingTest.csproj index bd8d663fa2c..63933b6cade 100644 --- a/logging/api/LoggingTest/LoggingTest.csproj +++ b/logging/api/LoggingTest/LoggingTest.csproj @@ -8,7 +8,7 @@ - + runtime; build; native; contentfiles; analyzers all diff --git a/media/livestream/api/LiveStream.Samples.Tests/LiveStream.Samples.Tests.csproj b/media/livestream/api/LiveStream.Samples.Tests/LiveStream.Samples.Tests.csproj index e699a684378..b965b766644 100644 --- a/media/livestream/api/LiveStream.Samples.Tests/LiveStream.Samples.Tests.csproj +++ b/media/livestream/api/LiveStream.Samples.Tests/LiveStream.Samples.Tests.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/media/stitcher/api/Stitcher.Samples.Tests/Stitcher.Samples.Tests.csproj b/media/stitcher/api/Stitcher.Samples.Tests/Stitcher.Samples.Tests.csproj index f4a7678d690..ab16640463e 100644 --- a/media/stitcher/api/Stitcher.Samples.Tests/Stitcher.Samples.Tests.csproj +++ b/media/stitcher/api/Stitcher.Samples.Tests/Stitcher.Samples.Tests.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/media/transcoder/api/Transcoder.Samples.Tests/Transcoder.Samples.Tests.csproj b/media/transcoder/api/Transcoder.Samples.Tests/Transcoder.Samples.Tests.csproj index 38357827e2f..10a4f17d3e6 100644 --- a/media/transcoder/api/Transcoder.Samples.Tests/Transcoder.Samples.Tests.csproj +++ b/media/transcoder/api/Transcoder.Samples.Tests/Transcoder.Samples.Tests.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/modelarmor/api/ModelArmor.Samples.Tests/ModelArmor.Samples.Tests.csproj b/modelarmor/api/ModelArmor.Samples.Tests/ModelArmor.Samples.Tests.csproj new file mode 100644 index 00000000000..dab25c9205d --- /dev/null +++ b/modelarmor/api/ModelArmor.Samples.Tests/ModelArmor.Samples.Tests.csproj @@ -0,0 +1,16 @@ + + + net8.0 + false + + + + + + + + + + + + diff --git a/modelarmor/api/ModelArmor.Samples.Tests/ModelArmorFixture.cs b/modelarmor/api/ModelArmor.Samples.Tests/ModelArmorFixture.cs new file mode 100644 index 00000000000..96c36d35274 --- /dev/null +++ b/modelarmor/api/ModelArmor.Samples.Tests/ModelArmorFixture.cs @@ -0,0 +1,67 @@ +/* + * Copyright 2025 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 System; +using Google.Cloud.ModelArmor.V1; +using Xunit; + +[CollectionDefinition(nameof(ModelArmorFixture))] +public class ModelArmorFixture : IDisposable, ICollectionFixture +{ + public ModelArmorClient Client { get; } + public string ProjectId { get; } + public string LocationId { get; } + public TemplateName TemplateForQuickstartName { get; } + + public ModelArmorFixture() + { + // Get the Google Cloud Project ID. + ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); + if (string.IsNullOrEmpty(ProjectId)) + { + throw new Exception("Missing GOOGLE_PROJECT_ID environment variable"); + } + + // Get location ID from environment variable or use default. + LocationId = Environment.GetEnvironmentVariable("GOOGLE_CLOUD_LOCATION") ?? "us-central1"; + + // Create client. + Client = ModelArmorClient.Create(); + + // Create a template name for quickstart test. + string templateId = $"test-csharp-{Guid.NewGuid().ToString("N").Substring(0, 8)}"; + TemplateForQuickstartName = TemplateName.FromProjectLocationTemplate( + ProjectId, + LocationId, + templateId + ); + } + + public void Dispose() + { + // Clean up resources after tests. + try + { + Client.DeleteTemplate( + new DeleteTemplateRequest { Name = TemplateForQuickstartName.ToString() } + ); + } + catch (Exception) + { + // Ignore errors during cleanup. + } + } +} diff --git a/modelarmor/api/ModelArmor.Samples.Tests/QuickStartTest.cs b/modelarmor/api/ModelArmor.Samples.Tests/QuickStartTest.cs new file mode 100644 index 00000000000..b9a6bffb5e9 --- /dev/null +++ b/modelarmor/api/ModelArmor.Samples.Tests/QuickStartTest.cs @@ -0,0 +1,46 @@ +/* + * Copyright 2025 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.ModelArmor.V1; +using Xunit; +using Xunit.Abstractions; + +[Collection(nameof(ModelArmorFixture))] +public class QuickstartTests +{ + private readonly ModelArmorFixture _fixture; + private readonly QuickstartSample _sample; + + public QuickstartTests(ModelArmorFixture fixture, ITestOutputHelper output) + { + _fixture = fixture; + _sample = new QuickstartSample(); + } + + [Fact] + public void Runs() + { + // Get template name from fixture + TemplateName templateName = _fixture.TemplateForQuickstartName; + + // Run the quickstart sample + _sample.Quickstart( + projectId: templateName.ProjectId, + locationId: templateName.LocationId, + templateId: templateName.TemplateId + ); + } +} diff --git a/modelarmor/api/ModelArmor.Samples/ModelArmor.Samples.csproj b/modelarmor/api/ModelArmor.Samples/ModelArmor.Samples.csproj new file mode 100644 index 00000000000..2327504e74e --- /dev/null +++ b/modelarmor/api/ModelArmor.Samples/ModelArmor.Samples.csproj @@ -0,0 +1,9 @@ + + + net8.0 + false + + + + + diff --git a/modelarmor/api/ModelArmor.Samples/QuickStart.cs b/modelarmor/api/ModelArmor.Samples/QuickStart.cs new file mode 100644 index 00000000000..44699901f29 --- /dev/null +++ b/modelarmor/api/ModelArmor.Samples/QuickStart.cs @@ -0,0 +1,128 @@ +/* + * Copyright 2025 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 modelarmor_quickstart] +using System; +using System.Text; +using Google.Api.Gax.ResourceNames; +using Google.Cloud.ModelArmor.V1; + +public class QuickstartSample +{ + public void Quickstart( + string projectId = "my-project", + string locationId = "us-central1", + string templateId = "my-template" + ) + { + ModelArmorClientBuilder clientBuilder = new ModelArmorClientBuilder + { + Endpoint = $"modelarmor.{locationId}.rep.googleapis.com", + }; + + // Create the client. + Google.Cloud.ModelArmor.V1.ModelArmorClient client = clientBuilder.Build(); + + LocationName parent = LocationName.FromProjectLocation(projectId, locationId); + + // Build the Model Armor template with preferred filters + RaiFilterSettings raiFilterSettings = new RaiFilterSettings(); + raiFilterSettings.RaiFilters.Add( + new RaiFilterSettings.Types.RaiFilter + { + FilterType = RaiFilterType.Dangerous, + ConfidenceLevel = DetectionConfidenceLevel.High, + } + ); + raiFilterSettings.RaiFilters.Add( + new RaiFilterSettings.Types.RaiFilter + { + FilterType = RaiFilterType.HateSpeech, + ConfidenceLevel = DetectionConfidenceLevel.MediumAndAbove, + } + ); + raiFilterSettings.RaiFilters.Add( + new RaiFilterSettings.Types.RaiFilter + { + FilterType = RaiFilterType.SexuallyExplicit, + ConfidenceLevel = DetectionConfidenceLevel.MediumAndAbove, + } + ); + raiFilterSettings.RaiFilters.Add( + new RaiFilterSettings.Types.RaiFilter + { + FilterType = RaiFilterType.Harassment, + ConfidenceLevel = DetectionConfidenceLevel.MediumAndAbove, + } + ); + + // Create the filter config with RAI settings + FilterConfig modelArmorFilter = new FilterConfig { RaiSettings = raiFilterSettings }; + + // Create the template + Template template = new Template { FilterConfig = modelArmorFilter }; + + // Create the request + CreateTemplateRequest request = new CreateTemplateRequest + { + ParentAsLocationName = parent, + TemplateId = templateId, + Template = template, + }; + + // Send the request + Template createdTemplate = client.CreateTemplate(request); + + Console.WriteLine($"Created template: {createdTemplate.Name}"); + + // Sanitize a user prompt using the created template + string userPrompt = "Unsafe user prompt"; + + TemplateName templateName = TemplateName.FromProjectLocationTemplate( + projectId, + locationId, + templateId + ); + + SanitizeUserPromptRequest userPromptSanitizeRequest = new SanitizeUserPromptRequest + { + TemplateName = templateName, + UserPromptData = new DataItem { Text = userPrompt }, + }; + + SanitizeUserPromptResponse userPromptSanitizeResponse = client.SanitizeUserPrompt( + userPromptSanitizeRequest + ); + + Console.WriteLine($"Result for User Prompt Sanitization: {userPromptSanitizeResponse}"); + + // Sanitize a model response using the created template + string modelResponse = "Unsanitized model output"; + + SanitizeModelResponseRequest modelSanitizeRequest = new SanitizeModelResponseRequest + { + TemplateName = templateName, + ModelResponseData = new DataItem { Text = modelResponse }, + }; + + SanitizeModelResponseResponse modelSanitizeResponse = client.SanitizeModelResponse( + modelSanitizeRequest + ); + + Console.WriteLine($"Result for Model Response Sanitization: {modelSanitizeResponse}"); + } +} +// [END modelarmor_quickstart] diff --git a/modelarmor/api/ModelArmor.sln b/modelarmor/api/ModelArmor.sln new file mode 100644 index 00000000000..b541616a768 --- /dev/null +++ b/modelarmor/api/ModelArmor.sln @@ -0,0 +1,48 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelArmor.Samples", "ModelArmor.Samples\ModelArmor.Samples.csproj", "{BA205D98-AC20-4BE1-92D0-4C2081378EBC}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ModelArmor.Samples.Tests", "ModelArmor.Samples.Tests\ModelArmor.Samples.Tests.csproj", "{EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Debug|x64.ActiveCfg = Debug|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Debug|x64.Build.0 = Debug|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Debug|x86.ActiveCfg = Debug|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Debug|x86.Build.0 = Debug|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Release|Any CPU.Build.0 = Release|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Release|x64.ActiveCfg = Release|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Release|x64.Build.0 = Release|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Release|x86.ActiveCfg = Release|Any CPU + {BA205D98-AC20-4BE1-92D0-4C2081378EBC}.Release|x86.Build.0 = Release|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Debug|x64.ActiveCfg = Debug|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Debug|x64.Build.0 = Debug|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Debug|x86.ActiveCfg = Debug|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Debug|x86.Build.0 = Debug|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Release|Any CPU.Build.0 = Release|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Release|x64.ActiveCfg = Release|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Release|x64.Build.0 = Release|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Release|x86.ActiveCfg = Release|Any CPU + {EAD16385-9F71-47CA-A8E9-91B20C5A9BBE}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/modelarmor/api/runTest.ps1 b/modelarmor/api/runTest.ps1 new file mode 100755 index 00000000000..05d154e7749 --- /dev/null +++ b/modelarmor/api/runTest.ps1 @@ -0,0 +1,16 @@ +# Copyright 2025 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. + +dotnet restore --force +dotnet test --no-restore --test-adapter-path:. --logger:junit 2>&1 | %{ "$_" } diff --git a/monitoring/api/AlertSample/AlertSample.csproj b/monitoring/api/AlertSample/AlertSample.csproj index 22b005b0fac..2e67e8208e0 100644 --- a/monitoring/api/AlertSample/AlertSample.csproj +++ b/monitoring/api/AlertSample/AlertSample.csproj @@ -8,6 +8,6 @@ Exe - net6.0 + net8.0 \ No newline at end of file diff --git a/monitoring/api/AlertSnippets/AlertSnippets.csproj b/monitoring/api/AlertSnippets/AlertSnippets.csproj index 7189c62de0f..0185e367c3d 100644 --- a/monitoring/api/AlertSnippets/AlertSnippets.csproj +++ b/monitoring/api/AlertSnippets/AlertSnippets.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers diff --git a/monitoring/api/AlertTest/AlertTest.csproj b/monitoring/api/AlertTest/AlertTest.csproj index 2ba2f66c2c1..e963d48fad5 100644 --- a/monitoring/api/AlertTest/AlertTest.csproj +++ b/monitoring/api/AlertTest/AlertTest.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/monitoring/api/MonitoringSample/Monitoring.csproj b/monitoring/api/MonitoringSample/Monitoring.csproj index 245d16806a6..d2bfe5261f8 100644 --- a/monitoring/api/MonitoringSample/Monitoring.csproj +++ b/monitoring/api/MonitoringSample/Monitoring.csproj @@ -1,7 +1,7 @@  Exe - net6.0 + net8.0 GoogleCloudSamples.Monitoring diff --git a/monitoring/api/MonitoringTest/MonitoringTest.csproj b/monitoring/api/MonitoringTest/MonitoringTest.csproj index 48d0ad95b3c..4e1f21b49ff 100644 --- a/monitoring/api/MonitoringTest/MonitoringTest.csproj +++ b/monitoring/api/MonitoringTest/MonitoringTest.csproj @@ -1,11 +1,11 @@  Library - net6.0 + net8.0 - + runtime; build; native; contentfiles; analyzers all diff --git a/monitoring/api/QuickStart/QuickStart.csproj b/monitoring/api/QuickStart/QuickStart.csproj index aecef2ae9c0..173bbc6efeb 100644 --- a/monitoring/api/QuickStart/QuickStart.csproj +++ b/monitoring/api/QuickStart/QuickStart.csproj @@ -1,7 +1,7 @@  Exe - net6.0 + net8.0 diff --git a/monitoring/api/UptimeCheckSample/UptimeCheck.csproj b/monitoring/api/UptimeCheckSample/UptimeCheck.csproj index 78d4160a3f0..cb85229cf87 100644 --- a/monitoring/api/UptimeCheckSample/UptimeCheck.csproj +++ b/monitoring/api/UptimeCheckSample/UptimeCheck.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net8.0 diff --git a/monitoring/api/UptimeCheckTest/UptimeCheckTest.csproj b/monitoring/api/UptimeCheckTest/UptimeCheckTest.csproj index 962d4abb110..ca09d2109d0 100644 --- a/monitoring/api/UptimeCheckTest/UptimeCheckTest.csproj +++ b/monitoring/api/UptimeCheckTest/UptimeCheckTest.csproj @@ -1,13 +1,13 @@ - net6.0 + net8.0 false - + runtime; build; native; contentfiles; analyzers all diff --git a/parametermanager/api/ParameterManager.RegionalSamples.Tests/ParameterManager.RegionalSamples.Tests.csproj b/parametermanager/api/ParameterManager.RegionalSamples.Tests/ParameterManager.RegionalSamples.Tests.csproj index dd0daee8b2c..570e5e0caf2 100644 --- a/parametermanager/api/ParameterManager.RegionalSamples.Tests/ParameterManager.RegionalSamples.Tests.csproj +++ b/parametermanager/api/ParameterManager.RegionalSamples.Tests/ParameterManager.RegionalSamples.Tests.csproj @@ -12,9 +12,9 @@ - + - + diff --git a/parametermanager/api/ParameterManager.RegionalSamples.Tests/RegionalQuickstartTests.cs b/parametermanager/api/ParameterManager.RegionalSamples.Tests/RegionalQuickstartTests.cs new file mode 100644 index 00000000000..8f10944d75f --- /dev/null +++ b/parametermanager/api/ParameterManager.RegionalSamples.Tests/RegionalQuickstartTests.cs @@ -0,0 +1,50 @@ +/* + * Copyright 2025 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.ParameterManager.V1; + +[Collection(nameof(ParameterManagerRegionalFixture))] +public class RegionalQuickstartTests +{ + private readonly ParameterManagerRegionalFixture _fixture; + private readonly RegionalQuickstartSample _sample; + + public RegionalQuickstartTests(ParameterManagerRegionalFixture fixture) + { + _fixture = fixture; + _sample = new RegionalQuickstartSample(); + } + + [Fact] + public void RegionalQuickstart() + { + string parameterId = _fixture.RandomId(); + ParameterName parameterName = new ParameterName(_fixture.ProjectId, ParameterManagerRegionalFixture.LocationId, parameterId); + ParameterVersionName parameterVersionName = new ParameterVersionName(_fixture.ProjectId, ParameterManagerRegionalFixture.LocationId, parameterId, _fixture.RandomId()); + + _sample.RegionalQuickstart( + projectId: parameterVersionName.ProjectId, parameterId: parameterVersionName.ParameterId, locationId: ParameterManagerRegionalFixture.LocationId, versionId: parameterVersionName.ParameterVersionId); + _fixture.ParametersToDelete.Add(parameterName); + _fixture.ParameterVersionsToDelete.Add(parameterVersionName); + + // Create the client with the regional endpoint + ParameterManagerClient client = _fixture.Client; + ParameterVersion result = client.GetParameterVersion(parameterVersionName); + + Assert.NotNull(result); + Assert.Equal(parameterVersionName.ParameterVersionId, result.ParameterVersionName.ParameterVersionId); + } +} diff --git a/parametermanager/api/ParameterManager.RegionalSamples/RegionalQuickstart.cs b/parametermanager/api/ParameterManager.RegionalSamples/RegionalQuickstart.cs new file mode 100644 index 00000000000..1aa1315891e --- /dev/null +++ b/parametermanager/api/ParameterManager.RegionalSamples/RegionalQuickstart.cs @@ -0,0 +1,87 @@ +/* + * Copyright 2025 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 parametermanager_regional_quickstart] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.ParameterManager.V1; +using Google.Protobuf; +using System.Text; + +public class RegionalQuickstartSample +{ + /// + /// This function demonstrates how to use the Parameter Manager API with the Parameter Manager SDK for Google Cloud. + /// It covers structured parameter creation and creation of parameter version with JSON format payload. + /// Finally, it fetches the decoded payload and prints them. + /// + /// The ID of the project where the parameter is located. + /// The ID of the region where the parameter is located. + /// The ID of the parameter for which the version is to be created. + /// The ID of the version to be created. + public void RegionalQuickstart( + string projectId, + string locationId, + string parameterId, + string versionId) + { + // Define the regional endpoint + string regionalEndpoint = $"parametermanager.{locationId}.rep.googleapis.com"; + + // Create the client with the regional endpoint + ParameterManagerClient client = new ParameterManagerClientBuilder + { + Endpoint = regionalEndpoint + }.Build(); + + // Build the parent resource name for the regional locationId + LocationName parent = new LocationName(projectId, locationId); + + // Create a parameter + Parameter parameter = new Parameter + { + Format = ParameterFormat.Json + }; + + // Call the API to create the parameter. + Parameter createdParameter = client.CreateParameter(parent, parameter, parameterId); + Console.WriteLine($"Created regional parameter {createdParameter.Name} with format {createdParameter.Format}"); + + // Build the parent resource name using ParameterName. + ParameterName parameterName = new ParameterName(projectId, locationId, parameterId); + + // Create a parameter version with a JSON payload + string payload = "{\"username\": \"test-user\", \"host\": \"localhost\"}"; + ByteString data = ByteString.CopyFrom(payload, Encoding.UTF8); + ParameterVersion parameterVersion = new ParameterVersion + { + Payload = new ParameterVersionPayload + { + Data = data + } + }; + + // Call the API to create the parameter version. + ParameterVersion createdParameterVersion = client.CreateParameterVersion(parameterName, parameterVersion, versionId); + Console.WriteLine($"Created regional parameter version: {createdParameterVersion.Name}"); + + // Fetch the parameter version data. + ParameterVersion getParameterVersion = client.GetParameterVersion(createdParameterVersion.Name); + string decodedData = Encoding.UTF8.GetString(getParameterVersion.Payload.Data.ToByteArray()); + Console.WriteLine($"Payload: {decodedData}"); + } +} +// [END parametermanager_regional_quickstart] diff --git a/parametermanager/api/ParameterManager.Samples.Tests/GetParameterTests.cs b/parametermanager/api/ParameterManager.Samples.Tests/GetParameterTests.cs new file mode 100644 index 00000000000..570e3974a72 --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples.Tests/GetParameterTests.cs @@ -0,0 +1,41 @@ +/* + * Copyright 2025 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.ParameterManager.V1; + +[Collection(nameof(ParameterManagerFixture))] +public class GetParameterTests +{ + private readonly ParameterManagerFixture _fixture; + private readonly GetParameterSample _sample; + + public GetParameterTests(ParameterManagerFixture fixture) + { + _fixture = fixture; + _sample = new GetParameterSample(); + } + + [Fact] + public void GetParameter() + { + string parameterId = _fixture.RandomId(); + Parameter parameter = _fixture.CreateParameter(parameterId, ParameterFormat.Json); + Parameter result = _sample.GetParameter(projectId: _fixture.ProjectId, parameterId: parameterId); + + Assert.NotNull(result); + Assert.Equal(parameterId, result.ParameterName.ParameterId); + } +} diff --git a/parametermanager/api/ParameterManager.Samples.Tests/GetParameterVersionTests.cs b/parametermanager/api/ParameterManager.Samples.Tests/GetParameterVersionTests.cs new file mode 100644 index 00000000000..ba7134049b1 --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples.Tests/GetParameterVersionTests.cs @@ -0,0 +1,44 @@ +/* + * Copyright 2025 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.ParameterManager.V1; + +[Collection(nameof(ParameterManagerFixture))] +public class GetParameterVersionTests +{ + private readonly ParameterManagerFixture _fixture; + private readonly GetParameterVersionSample _sample; + + public GetParameterVersionTests(ParameterManagerFixture fixture) + { + _fixture = fixture; + _sample = new GetParameterVersionSample(); + } + + [Fact] + public void GetParameterVersion() + { + string parameterId = _fixture.RandomId(); + string versionId = _fixture.RandomId(); + Parameter parameter = _fixture.CreateParameter(parameterId, ParameterFormat.Unformatted); + string payload = "test123"; + ParameterVersion parameterVersion = _fixture.CreateParameterVersion(parameterId, versionId, payload); + ParameterVersion result = _sample.GetParameterVersion(projectId: _fixture.ProjectId, parameterId: parameterId, versionId: versionId); + + Assert.NotNull(result); + Assert.Equal(versionId, result.ParameterVersionName.ParameterVersionId); + } +} diff --git a/parametermanager/api/ParameterManager.Samples.Tests/ListParameterVersionsTests.cs b/parametermanager/api/ParameterManager.Samples.Tests/ListParameterVersionsTests.cs new file mode 100644 index 00000000000..d1522604a99 --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples.Tests/ListParameterVersionsTests.cs @@ -0,0 +1,43 @@ +/* + * Copyright 2025 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.ParameterManager.V1; + +[Collection(nameof(ParameterManagerFixture))] +public class ListParameterVersionsTests +{ + private readonly ParameterManagerFixture _fixture; + private readonly ListParameterVersionsSample _sample; + + public ListParameterVersionsTests(ParameterManagerFixture fixture) + { + _fixture = fixture; + _sample = new ListParameterVersionsSample(); + } + + [Fact] + public void ListParameterVersions() + { + string parameterId = _fixture.RandomId(); + Parameter parameter = _fixture.CreateParameter(parameterId, ParameterFormat.Unformatted); + string payload = "test123"; + ParameterVersion parameterVersion = _fixture.CreateParameterVersion(parameterId, _fixture.RandomId(), payload); + ParameterVersion parameterVersion1 = _fixture.CreateParameterVersion(parameterId, _fixture.RandomId(), payload); + + IEnumerable result = _sample.ListParameterVersions(projectId: _fixture.ProjectId, parameterId: parameterId); + Assert.NotNull(result); + } +} diff --git a/parametermanager/api/ParameterManager.Samples.Tests/ListParametersTests.cs b/parametermanager/api/ParameterManager.Samples.Tests/ListParametersTests.cs new file mode 100644 index 00000000000..fb3b6633520 --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples.Tests/ListParametersTests.cs @@ -0,0 +1,39 @@ +/* + * Copyright 2025 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.ParameterManager.V1; + +[Collection(nameof(ParameterManagerFixture))] +public class ListParametersTests +{ + private readonly ParameterManagerFixture _fixture; + private readonly ListParametersSample _sample; + + public ListParametersTests(ParameterManagerFixture fixture) + { + _fixture = fixture; + _sample = new ListParametersSample(); + } + + [Fact] + public void ListParameters() + { + Parameter parameter = _fixture.CreateParameter(_fixture.RandomId(), ParameterFormat.Json); + Parameter parameter1 = _fixture.CreateParameter(_fixture.RandomId(), ParameterFormat.Json); + IEnumerable result = _sample.ListParameters(projectId: _fixture.ProjectId); + Assert.NotNull(result); + } +} diff --git a/parametermanager/api/ParameterManager.Samples.Tests/ParameterManager.Samples.Tests.csproj b/parametermanager/api/ParameterManager.Samples.Tests/ParameterManager.Samples.Tests.csproj index a4587a1e042..1dfd2d2efe0 100644 --- a/parametermanager/api/ParameterManager.Samples.Tests/ParameterManager.Samples.Tests.csproj +++ b/parametermanager/api/ParameterManager.Samples.Tests/ParameterManager.Samples.Tests.csproj @@ -12,7 +12,7 @@ - + diff --git a/parametermanager/api/ParameterManager.Samples.Tests/ParameterManagerFixture.cs b/parametermanager/api/ParameterManager.Samples.Tests/ParameterManagerFixture.cs index 8b01f16472b..12791423862 100644 --- a/parametermanager/api/ParameterManager.Samples.Tests/ParameterManagerFixture.cs +++ b/parametermanager/api/ParameterManager.Samples.Tests/ParameterManagerFixture.cs @@ -26,7 +26,9 @@ public class ParameterManagerFixture : IDisposable, ICollectionFixture ParametersToDelete { get; } = new List(); + internal List SecretsToDelete { get; } = new List(); internal List ParameterVersionsToDelete { get; } = new List(); public ParameterManagerFixture() @@ -38,6 +40,7 @@ public ParameterManagerFixture() } Client = ParameterManagerClient.Create(); + SecretClient = SecretManagerServiceClient.Create(); } public void Dispose() @@ -50,6 +53,10 @@ public void Dispose() { DeleteParameter(parameter); } + foreach (var secret in SecretsToDelete) + { + DeleteSecret(secret); + } } public string RandomId() @@ -71,6 +78,22 @@ public Parameter CreateParameter(string parameterId, ParameterFormat format) return Parameter; } + public ParameterVersion CreateParameterVersion(string parameterId, string versionId, string payload) + { + ParameterName parameterName = new ParameterName(ProjectId, LocationId, parameterId); + ParameterVersion parameterVersion = new ParameterVersion + { + Payload = new ParameterVersionPayload + { + Data = ByteString.CopyFrom(payload, Encoding.UTF8) + } + }; + + ParameterVersion ParameterVersion = Client.CreateParameterVersion(parameterName, parameterVersion, versionId); + ParameterVersionsToDelete.Add(ParameterVersion.ParameterVersionName); + return ParameterVersion; + } + private void DeleteParameter(ParameterName name) { try @@ -94,4 +117,62 @@ private void DeleteParameterVersion(ParameterVersionName name) // Ignore error - Parameter version was already deleted } } + + public Secret CreateSecret(string secretId) + { + ProjectName projectName = new ProjectName(ProjectId); + + Secret secret = new Secret + { + Replication = new Replication + { + Automatic = new Replication.Types.Automatic(), + }, + }; + Secret Secret = SecretClient.CreateSecret(projectName, secretId, secret); + SecretsToDelete.Add(Secret.SecretName); + return Secret; + } + + public Policy GrantIAMAccess(SecretName secretName, string member) + { + // Get current policy. + Policy policy = SecretClient.GetIamPolicy(new GetIamPolicyRequest + { + ResourceAsResourceName = secretName, + }); + + // Add the user to the list of bindings. + policy.AddRoleMember("roles/secretmanager.secretAccessor", member); + + // Save the updated policy. + policy = SecretClient.SetIamPolicy(new SetIamPolicyRequest + { + ResourceAsResourceName = secretName, + Policy = policy, + }); + return policy; + } + + public SecretVersion AddSecretVersion(Secret secret) + { + SecretPayload payload = new SecretPayload + { + Data = ByteString.CopyFrom("my super secret data", Encoding.UTF8), + }; + + return SecretClient.AddSecretVersion(secret.SecretName, payload); + } + + private void DeleteSecret(SecretName name) + { + try + { + SecretClient.DeleteSecret(name); + } + catch (Grpc.Core.RpcException e) when (e.StatusCode == Grpc.Core.StatusCode.NotFound) + { + // Ignore error - secret was already deleted + } + } } diff --git a/parametermanager/api/ParameterManager.Samples.Tests/QuickstartTests.cs b/parametermanager/api/ParameterManager.Samples.Tests/QuickstartTests.cs new file mode 100644 index 00000000000..4f2b6238f87 --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples.Tests/QuickstartTests.cs @@ -0,0 +1,48 @@ +/* + * Copyright 2025 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.ParameterManager.V1; + +[Collection(nameof(ParameterManagerFixture))] +public class QuickstartTests +{ + private readonly ParameterManagerFixture _fixture; + private readonly QuickstartSample _sample; + + public QuickstartTests(ParameterManagerFixture fixture) + { + _fixture = fixture; + _sample = new QuickstartSample(); + } + + [Fact] + public void Quickstart() + { + string parameterId = _fixture.RandomId(); + ParameterName parameterName = new ParameterName(_fixture.ProjectId, ParameterManagerFixture.LocationId, parameterId); + ParameterVersionName parameterVersionName = new ParameterVersionName(_fixture.ProjectId, ParameterManagerFixture.LocationId, parameterId, _fixture.RandomId()); + _sample.Quickstart( + projectId: parameterVersionName.ProjectId, parameterId: parameterVersionName.ParameterId, versionId: parameterVersionName.ParameterVersionId); + _fixture.ParametersToDelete.Add(parameterName); + _fixture.ParameterVersionsToDelete.Add(parameterVersionName); + + ParameterManagerClient client = _fixture.Client; + ParameterVersion result = client.GetParameterVersion(parameterVersionName); + + Assert.NotNull(result); + Assert.Equal(parameterVersionName.ParameterVersionId, result.ParameterVersionName.ParameterVersionId); + } +} diff --git a/parametermanager/api/ParameterManager.Samples.Tests/RenderParameterVersionTests.cs b/parametermanager/api/ParameterManager.Samples.Tests/RenderParameterVersionTests.cs new file mode 100644 index 00000000000..553a452e06c --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples.Tests/RenderParameterVersionTests.cs @@ -0,0 +1,51 @@ +/* + * Copyright 2025 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.Iam.V1; +using Google.Cloud.ParameterManager.V1; +using Google.Cloud.SecretManager.V1; + +[Collection(nameof(ParameterManagerFixture))] +public class RenderParameterVersionTests +{ + private readonly ParameterManagerFixture _fixture; + private readonly RenderParameterVersionSample _sample; + + public RenderParameterVersionTests(ParameterManagerFixture fixture) + { + _fixture = fixture; + _sample = new RenderParameterVersionSample(); + } + + [Fact] + public void RenderParameterVersion() + { + string parameterId = _fixture.RandomId(); + string versionId = _fixture.RandomId(); + Parameter parameter = _fixture.CreateParameter(parameterId, ParameterFormat.Unformatted); + + Secret secret = _fixture.CreateSecret(_fixture.RandomId()); + _fixture.AddSecretVersion(secret); + string payload = $"{{\"username\": \"test-user\", \"password\": \"__REF__(//secretmanager.googleapis.com/{secret.SecretName}/versions/latest)\"}}"; + _fixture.GrantIAMAccess(secret.SecretName, parameter.PolicyMember.IamPolicyUidPrincipal); + + ParameterVersion parameterVersion = _fixture.CreateParameterVersion(parameterId, versionId, payload); + + string result = _sample.RenderParameterVersion(projectId: _fixture.ProjectId, parameterId: parameterId, versionId); + + Assert.NotNull(result); + } +} diff --git a/parametermanager/api/ParameterManager.Samples/CreateParameterVersion.cs b/parametermanager/api/ParameterManager.Samples/CreateParameterVersion.cs index 1d56fd60b79..2a6d7a15575 100644 --- a/parametermanager/api/ParameterManager.Samples/CreateParameterVersion.cs +++ b/parametermanager/api/ParameterManager.Samples/CreateParameterVersion.cs @@ -54,7 +54,7 @@ public ParameterVersion CreateParameterVersion( }; // Call the API to create the parameter version. - ParameterVersion createdParameterVersion = client.CreateParameterVersion(parent.ToString(), parameterVersion, versionId); + ParameterVersion createdParameterVersion = client.CreateParameterVersion(parent, parameterVersion, versionId); // Print the created parameter version name. Console.WriteLine($"Created parameter version: {createdParameterVersion.Name}"); diff --git a/parametermanager/api/ParameterManager.Samples/CreateParameterVersionWithSecret.cs b/parametermanager/api/ParameterManager.Samples/CreateParameterVersionWithSecret.cs index 729a71d1969..6c7557dbf15 100644 --- a/parametermanager/api/ParameterManager.Samples/CreateParameterVersionWithSecret.cs +++ b/parametermanager/api/ParameterManager.Samples/CreateParameterVersionWithSecret.cs @@ -56,7 +56,7 @@ public ParameterVersion CreateParameterVersionWithSecret( }; // Call the API to create the parameter version. - ParameterVersion createdParameterVersion = client.CreateParameterVersion(parent.ToString(), parameterVersion, versionId); + ParameterVersion createdParameterVersion = client.CreateParameterVersion(parent, parameterVersion, versionId); // Print the created parameter version name. Console.WriteLine($"Created parameter version: {createdParameterVersion.Name}"); diff --git a/parametermanager/api/ParameterManager.Samples/CreateStructuredParameterVersion.cs b/parametermanager/api/ParameterManager.Samples/CreateStructuredParameterVersion.cs index 76f914ca837..b5fda0b2e34 100644 --- a/parametermanager/api/ParameterManager.Samples/CreateStructuredParameterVersion.cs +++ b/parametermanager/api/ParameterManager.Samples/CreateStructuredParameterVersion.cs @@ -54,7 +54,7 @@ public ParameterVersion CreateStructuredParameterVersion( }; // Call the API to create the parameter version. - ParameterVersion createdParameterVersion = client.CreateParameterVersion(parent.ToString(), parameterVersion, versionId); + ParameterVersion createdParameterVersion = client.CreateParameterVersion(parent, parameterVersion, versionId); // Print the created parameter version name. Console.WriteLine($"Created parameter version: {createdParameterVersion.Name}"); diff --git a/parametermanager/api/ParameterManager.Samples/GetParameter.cs b/parametermanager/api/ParameterManager.Samples/GetParameter.cs new file mode 100644 index 00000000000..1f3390d3d39 --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples/GetParameter.cs @@ -0,0 +1,49 @@ +/* + * Copyright 2025 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 parametermanager_get_param] + +using Google.Cloud.ParameterManager.V1; + +public class GetParameterSample +{ + /// + /// This function retrieves a parameter using the Parameter Manager SDK for GCP. + /// + /// The ID of the project where the parameter is located. + /// The ID of the parameter to be retrieved. + /// The retrieved Parameter object. + public Parameter GetParameter( + string projectId, + string parameterId) + { + // Create the client. + ParameterManagerClient client = ParameterManagerClient.Create(); + + // Build the resource name for the parameter. + ParameterName parameterName = new ParameterName(projectId, "global", parameterId); + + // Call the API to get the parameter. + Parameter parameter = client.GetParameter(parameterName); + + // Print the retrieved parameter name. + Console.WriteLine($"Found the parameter {parameter.Name} with format {parameter.Format}"); + + // Return the retrieved parameter. + return parameter; + } +} +// [END parametermanager_get_param] diff --git a/parametermanager/api/ParameterManager.Samples/GetParameterVersion.cs b/parametermanager/api/ParameterManager.Samples/GetParameterVersion.cs new file mode 100644 index 00000000000..4c227ebb492 --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples/GetParameterVersion.cs @@ -0,0 +1,57 @@ +/* + * Copyright 2025 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 parametermanager_get_param_version] + +using Google.Cloud.ParameterManager.V1; +using System.Text; + +public class GetParameterVersionSample +{ + /// + /// This function retrieves a parameter version using the Parameter Manager SDK for GCP. + /// + /// The ID of the project where the parameter is located. + /// The ID of the parameter for which the version is to be retrieved. + /// The ID of the version to be retrieved. + /// The retrieved ParameterVersion object. + public ParameterVersion GetParameterVersion( + string projectId, + string parameterId, + string versionId) + { + // Create the client. + ParameterManagerClient client = ParameterManagerClient.Create(); + + // Build the resource name for the parameter version. + ParameterVersionName parameterVersionName = new ParameterVersionName(projectId, "global", parameterId, versionId); + + // Call the API to get the parameter version. + ParameterVersion parameterVersion = client.GetParameterVersion(parameterVersionName); + + // Print the retrieved parameter version name. + Console.WriteLine($"Found regional parameter version {parameterVersion.Name} with state {(parameterVersion.Disabled ? "disabled" : "enabled")}"); + + if (!parameterVersion.Disabled) + { + Console.WriteLine($"Payload: {Encoding.UTF8.GetString(parameterVersion.Payload.Data.ToByteArray())}"); + } + + // Return the retrieved parameter version. + return parameterVersion; + } +} +// [END parametermanager_get_param_version] diff --git a/parametermanager/api/ParameterManager.Samples/ListParameterVersions.cs b/parametermanager/api/ParameterManager.Samples/ListParameterVersions.cs new file mode 100644 index 00000000000..a87009a2fec --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples/ListParameterVersions.cs @@ -0,0 +1,53 @@ +/* + * Copyright 2025 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 parametermanager_list_param_versions] + +using Google.Api.Gax; +using Google.Cloud.ParameterManager.V1; + +public class ListParameterVersionsSample +{ + /// + /// This function lists parameter version using the Parameter Manager SDK for GCP. + /// + /// The ID of the project where the parameter is located. + /// The ID of the parameter for which the version is to be listed. + /// A list of ParameterVersion objects. + public IEnumerable ListParameterVersions( + string projectId, + string parameterId) + { + // Create the client. + ParameterManagerClient client = ParameterManagerClient.Create(); + + // Build the parent resource name for the parameter. + ParameterName parent = new ParameterName(projectId, "global", parameterId); + + // Call the API to list the parameter versions. + PagedEnumerable response = client.ListParameterVersions(parent); + + // Print each parameter version name. + foreach (ParameterVersion parameterVersion in response) + { + Console.WriteLine($"Found parameter version: {parameterVersion.Name}"); + } + + // Return the list of parameter versions. + return response; + } +} +// [END parametermanager_list_param_versions] diff --git a/parametermanager/api/ParameterManager.Samples/ListParameters.cs b/parametermanager/api/ParameterManager.Samples/ListParameters.cs new file mode 100644 index 00000000000..3dc4925705a --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples/ListParameters.cs @@ -0,0 +1,51 @@ +/* + * Copyright 2025 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 parametermanager_list_params] + +using Google.Api.Gax; +using Google.Api.Gax.ResourceNames; +using Google.Cloud.ParameterManager.V1; + +public class ListParametersSample +{ + /// + /// This function lists parameter using the Parameter Manager SDK for GCP. + /// + /// The ID of the project where the parameter is located. + /// A list of Parameter objects. + public IEnumerable ListParameters(string projectId) + { + // Create the client. + ParameterManagerClient client = ParameterManagerClient.Create(); + + // Build the parent resource name. + LocationName parent = new LocationName(projectId, "global"); + + // Call the API to list the parameters. + PagedEnumerable response = client.ListParameters(parent); + + // Print each parameter name. + foreach (Parameter parameter in response) + { + Console.WriteLine($"Found parameter {parameter.Name} with format {parameter.Format}"); + } + + // Return the list of parameters. + return response; + } +} +// [END parametermanager_list_params] diff --git a/parametermanager/api/ParameterManager.Samples/Quickstart.cs b/parametermanager/api/ParameterManager.Samples/Quickstart.cs new file mode 100644 index 00000000000..d99a5e52cee --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples/Quickstart.cs @@ -0,0 +1,79 @@ +/* + * Copyright 2025 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 parametermanager_quickstart] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.ParameterManager.V1; +using Google.Protobuf; +using System.Text; + +public class QuickstartSample +{ + /// + /// This function demonstrates how to use the Parameter Manager API with the Parameter Manager SDK for Google Cloud. + /// It covers structured parameter creation and creation of parameter version with JSON format payload. + /// Finally, it fetches the decoded payload and prints them. + /// + /// The ID of the project where the parameter is located. + /// The ID of the parameter for which the version is to be created. + /// The ID of the version to be created. + public void Quickstart( + string projectId, + string parameterId, + string versionId) + { + // Create the client. + ParameterManagerClient client = ParameterManagerClient.Create(); + + // Build the parent resource name. + LocationName parent = new LocationName(projectId, "global"); + + // Create a structured parameter. + Parameter parameter = new Parameter + { + Format = ParameterFormat.Json + }; + + // Call the API to create the parameter. + Parameter createdParameter = client.CreateParameter(parent, parameter, parameterId); + Console.WriteLine($"Created parameter {createdParameter.Name} with format {createdParameter.Format}"); + + // Build the parent resource name using ParameterName. + ParameterName parameterName = new ParameterName(projectId, "global", parameterId); + + // Define the JSON payload + string payload = "{\"username\": \"test-user\", \"host\": \"localhost\"}"; + ByteString data = ByteString.CopyFrom(payload, Encoding.UTF8); + ParameterVersion parameterVersion = new ParameterVersion + { + Payload = new ParameterVersionPayload + { + Data = data + } + }; + + // Call the API to create the parameter version. + ParameterVersion createdParameterVersion = client.CreateParameterVersion(parameterName, parameterVersion, versionId); + Console.WriteLine($"Created parameter version: {createdParameterVersion.Name}"); + + // Fetch the parameter version data. + ParameterVersion getParameterVersion = client.GetParameterVersion(createdParameterVersion.Name); + string decodedData = Encoding.UTF8.GetString(getParameterVersion.Payload.Data.ToByteArray()); + Console.WriteLine($"Payload: {decodedData}"); + } +} +// [END parametermanager_quickstart] diff --git a/parametermanager/api/ParameterManager.Samples/RenderParameterVersion.cs b/parametermanager/api/ParameterManager.Samples/RenderParameterVersion.cs new file mode 100644 index 00000000000..cc632aad283 --- /dev/null +++ b/parametermanager/api/ParameterManager.Samples/RenderParameterVersion.cs @@ -0,0 +1,55 @@ +/* + * Copyright 2025 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 parametermanager_render_param_version] + +using Google.Cloud.ParameterManager.V1; +using System.Text; + +public class RenderParameterVersionSample +{ + /// + /// This function renders a parameter version using the Parameter Manager SDK for GCP. + /// + /// The ID of the project where the parameter is located. + /// The ID of the parameter for which the version is to be rendered. + /// The ID of the version to be rendered. + /// The rendered parameter version data as a string. + public string RenderParameterVersion( + string projectId, + string parameterId, + string versionId) + { + // Create the client. + ParameterManagerClient client = ParameterManagerClient.Create(); + + // Build the resource name for the parameter version. + ParameterVersionName parameterVersionName = new ParameterVersionName(projectId, "global", parameterId, versionId); + + // Call the API to render the parameter version. + RenderParameterVersionResponse renderParameterVersionResponse = client.RenderParameterVersion(parameterVersionName); + + // Decode the base64 encoded data. + string decodedData = Encoding.UTF8.GetString(renderParameterVersionResponse.RenderedPayload.ToByteArray()); + + // Print the rendered parameter version data. + Console.WriteLine($"Rendered parameter version payload: {decodedData}"); + + // Return the rendered parameter version data. + return decodedData; + } +} +// [END parametermanager_render_param_version] diff --git a/parametermanager/api/ParameterManager.sln b/parametermanager/api/ParameterManager.sln index b0a12d2134f..6a90829d1c2 100644 --- a/parametermanager/api/ParameterManager.sln +++ b/parametermanager/api/ParameterManager.sln @@ -7,9 +7,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParameterManager.Samples", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParameterManager.Samples.Tests", "ParameterManager.Samples.Tests\ParameterManager.Samples.Tests.csproj", "{2ED39537-73EA-4186-A92D-82F59005FE14}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParameterManager.RegionalSamples.Tests", "ParameterManager.RegionalSamples.Tests\ParameterManager.RegionalSamples.Tests.csproj", "{2688B889-A2A8-4A31-B903-65A4DAC566D5}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParameterManager.RegionalSamples", "ParameterManager.RegionalSamples\ParameterManager.RegionalSamples.csproj", "{5EF4297D-DE95-4CF1-8271-B3232CA00705}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParameterManager.RegionalSamples", "ParameterManager.RegionalSamples\ParameterManager.RegionalSamples.csproj", "{9230440A-5059-4C58-B853-F443A92516C1}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParameterManager.RegionalSamples.Tests", "ParameterManager.RegionalSamples.Tests\ParameterManager.RegionalSamples.Tests.csproj", "{121A82D9-C771-4A9F-8FE7-DFED986B909B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,30 +45,30 @@ Global {2ED39537-73EA-4186-A92D-82F59005FE14}.Release|x64.Build.0 = Release|Any CPU {2ED39537-73EA-4186-A92D-82F59005FE14}.Release|x86.ActiveCfg = Release|Any CPU {2ED39537-73EA-4186-A92D-82F59005FE14}.Release|x86.Build.0 = Release|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Debug|x64.ActiveCfg = Debug|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Debug|x64.Build.0 = Debug|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Debug|x86.ActiveCfg = Debug|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Debug|x86.Build.0 = Debug|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Release|Any CPU.Build.0 = Release|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Release|x64.ActiveCfg = Release|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Release|x64.Build.0 = Release|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Release|x86.ActiveCfg = Release|Any CPU - {2688B889-A2A8-4A31-B903-65A4DAC566D5}.Release|x86.Build.0 = Release|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Debug|x64.ActiveCfg = Debug|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Debug|x64.Build.0 = Debug|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Debug|x86.ActiveCfg = Debug|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Debug|x86.Build.0 = Debug|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Release|Any CPU.Build.0 = Release|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Release|x64.ActiveCfg = Release|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Release|x64.Build.0 = Release|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Release|x86.ActiveCfg = Release|Any CPU - {9230440A-5059-4C58-B853-F443A92516C1}.Release|x86.Build.0 = Release|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Debug|x64.ActiveCfg = Debug|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Debug|x64.Build.0 = Debug|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Debug|x86.ActiveCfg = Debug|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Debug|x86.Build.0 = Debug|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Release|Any CPU.Build.0 = Release|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Release|x64.ActiveCfg = Release|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Release|x64.Build.0 = Release|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Release|x86.ActiveCfg = Release|Any CPU + {5EF4297D-DE95-4CF1-8271-B3232CA00705}.Release|x86.Build.0 = Release|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Debug|x64.ActiveCfg = Debug|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Debug|x64.Build.0 = Debug|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Debug|x86.ActiveCfg = Debug|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Debug|x86.Build.0 = Debug|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Release|Any CPU.Build.0 = Release|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Release|x64.ActiveCfg = Release|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Release|x64.Build.0 = Release|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Release|x86.ActiveCfg = Release|Any CPU + {121A82D9-C771-4A9F-8FE7-DFED986B909B}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/parametermanager/api/README.md b/parametermanager/api/README.md new file mode 100644 index 00000000000..ccf54a1be45 --- /dev/null +++ b/parametermanager/api/README.md @@ -0,0 +1,32 @@ +# .NET Parameter Manager Regional Samples + +These samples demonstrate how to interact with the [Parameter Manager][parametermanager] using C# and +the .NET client libraries to call the Parameter Manager API. + +The samples require [.NET Core 8.0][net-core] or later. + +## Setup + +1. Set up a [.NET development environment](https://cloud.google.com/dotnet/docs/setup). + +1. [Enable the Parameter Manager API][enable-api] in your Google Cloud project. + + +## Contributing changes + +* See [CONTRIBUTING.md](../../CONTRIBUTING.md) + + +## Licensing + +* See [LICENSE](../../LICENSE) + + +## Testing + +* See [TESTING.md](../../TESTING.md) + + +[parametermanager]: https://cloud.google.com/secret-manager/parameter-manager/docs/overview +[enable-api]: https://console.cloud.google.com/apis/enableflow?apiid=parametermanager.googleapis.com +[net-core]: https://www.microsoft.com/net/core diff --git a/pubsub/api/Pubsub.Samples.Tests/CreateTopicWithCloudStorageIngestionTest.cs b/pubsub/api/Pubsub.Samples.Tests/CreateTopicWithCloudStorageIngestionTest.cs index 18ae6a5a640..5339f5bba85 100644 --- a/pubsub/api/Pubsub.Samples.Tests/CreateTopicWithCloudStorageIngestionTest.cs +++ b/pubsub/api/Pubsub.Samples.Tests/CreateTopicWithCloudStorageIngestionTest.cs @@ -37,7 +37,7 @@ public void CreateTopicWithCloudStorageIngestion() string inputFormat = "text"; string textDelimiter = "\n"; string matchGlob = "**.txt"; - string minimumObjectCreateTime = "1970-01-01T00:00:00Z"; + DateTimeOffset minimumObjectCreateTime = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero); Topic createdTopic = _createTopicWithCloudStorageIngestionSample.CreateTopicWithCloudStorageIngestion(_pubsubFixture.ProjectId, topicId, bucket, inputFormat, textDelimiter, matchGlob, minimumObjectCreateTime); // Confirm that the created topic and topic retrieved by ID are equal @@ -49,6 +49,6 @@ public void CreateTopicWithCloudStorageIngestion() Assert.NotNull(createdTopic.IngestionDataSourceSettings.CloudStorage.TextFormat); Assert.Equal(textDelimiter, createdTopic.IngestionDataSourceSettings.CloudStorage.TextFormat.Delimiter); Assert.Equal(matchGlob, createdTopic.IngestionDataSourceSettings.CloudStorage.MatchGlob); - Assert.Equal(Timestamp.FromDateTime(DateTime.Parse(minimumObjectCreateTime)), createdTopic.IngestionDataSourceSettings.CloudStorage.MinimumObjectCreateTime); + Assert.Equal(Timestamp.FromDateTimeOffset(minimumObjectCreateTime), createdTopic.IngestionDataSourceSettings.CloudStorage.MinimumObjectCreateTime); } } diff --git a/pubsub/api/Pubsub.Samples.Tests/Pubsub.Samples.Tests.csproj b/pubsub/api/Pubsub.Samples.Tests/Pubsub.Samples.Tests.csproj index 7fb739e083f..11bb9bd0aad 100644 --- a/pubsub/api/Pubsub.Samples.Tests/Pubsub.Samples.Tests.csproj +++ b/pubsub/api/Pubsub.Samples.Tests/Pubsub.Samples.Tests.csproj @@ -1,7 +1,7 @@  - net6.0 + net8.0 false @@ -9,7 +9,7 @@ - + diff --git a/pubsub/api/Pubsub.Samples/CreateTopicWithCloudStorageIngestion.cs b/pubsub/api/Pubsub.Samples/CreateTopicWithCloudStorageIngestion.cs index 0a0c1f1d3e1..bf12c730f8a 100644 --- a/pubsub/api/Pubsub.Samples/CreateTopicWithCloudStorageIngestion.cs +++ b/pubsub/api/Pubsub.Samples/CreateTopicWithCloudStorageIngestion.cs @@ -21,10 +21,14 @@ public class CreateTopicWithCloudStorageIngestionSample { - public Topic CreateTopicWithCloudStorageIngestion(string projectId, string topicId, string bucket, string inputFormat, string textDelimiter, string matchGlob, string minimumObjectCreateTime) + public Topic CreateTopicWithCloudStorageIngestion(string projectId, string topicId, string bucket, string inputFormat, string textDelimiter, string matchGlob, DateTimeOffset minimumObjectCreateTime) { - IngestionDataSourceSettings.Types.CloudStorage cloudStorageSettings = new IngestionDataSourceSettings.Types.CloudStorage { Bucket = bucket }; + IngestionDataSourceSettings.Types.CloudStorage cloudStorageSettings = new IngestionDataSourceSettings.Types.CloudStorage + { + Bucket = bucket, + MinimumObjectCreateTime = Timestamp.FromDateTimeOffset(minimumObjectCreateTime), + }; switch (inputFormat) { @@ -49,11 +53,6 @@ public Topic CreateTopicWithCloudStorageIngestion(string projectId, string topic cloudStorageSettings.MatchGlob = matchGlob; } - if (!string.IsNullOrEmpty(minimumObjectCreateTime)) - { - cloudStorageSettings.MinimumObjectCreateTime = Timestamp.FromDateTime(DateTime.Parse(minimumObjectCreateTime)); - } - PublisherServiceApiClient publisher = PublisherServiceApiClient.Create(); Topic topic = new Topic() { diff --git a/retail/interactive-tutorial/RetailEvents.Samples.Tests/RetailEvents.Samples.Tests.csproj b/retail/interactive-tutorial/RetailEvents.Samples.Tests/RetailEvents.Samples.Tests.csproj index bad5b5b66f2..67577846205 100644 --- a/retail/interactive-tutorial/RetailEvents.Samples.Tests/RetailEvents.Samples.Tests.csproj +++ b/retail/interactive-tutorial/RetailEvents.Samples.Tests/RetailEvents.Samples.Tests.csproj @@ -7,7 +7,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/retail/interactive-tutorial/RetailProducts.Samples.Tests/RetailProducts.Samples.Tests.csproj b/retail/interactive-tutorial/RetailProducts.Samples.Tests/RetailProducts.Samples.Tests.csproj index e441a64dd67..95480efa9c4 100644 --- a/retail/interactive-tutorial/RetailProducts.Samples.Tests/RetailProducts.Samples.Tests.csproj +++ b/retail/interactive-tutorial/RetailProducts.Samples.Tests/RetailProducts.Samples.Tests.csproj @@ -8,7 +8,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/retail/interactive-tutorial/RetailSearch.Samples.Tests/RetailSearch.Samples.Tests.csproj b/retail/interactive-tutorial/RetailSearch.Samples.Tests/RetailSearch.Samples.Tests.csproj index 9de2373fa47..ada710edaee 100644 --- a/retail/interactive-tutorial/RetailSearch.Samples.Tests/RetailSearch.Samples.Tests.csproj +++ b/retail/interactive-tutorial/RetailSearch.Samples.Tests/RetailSearch.Samples.Tests.csproj @@ -7,7 +7,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/run/markdown-preview/Samples.Run.MarkdownPreview.Editor.Tests/Samples.Run.MarkdownPreview.Editor.Tests.csproj b/run/markdown-preview/Samples.Run.MarkdownPreview.Editor.Tests/Samples.Run.MarkdownPreview.Editor.Tests.csproj index fab7493f865..7f1ad00a41a 100644 --- a/run/markdown-preview/Samples.Run.MarkdownPreview.Editor.Tests/Samples.Run.MarkdownPreview.Editor.Tests.csproj +++ b/run/markdown-preview/Samples.Run.MarkdownPreview.Editor.Tests/Samples.Run.MarkdownPreview.Editor.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable false @@ -13,7 +13,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/run/markdown-preview/Samples.Run.MarkdownPreview.Editor/Samples.Run.MarkdownPreview.Editor.csproj b/run/markdown-preview/Samples.Run.MarkdownPreview.Editor/Samples.Run.MarkdownPreview.Editor.csproj index 0a109f23e10..ebc9525b85f 100644 --- a/run/markdown-preview/Samples.Run.MarkdownPreview.Editor/Samples.Run.MarkdownPreview.Editor.csproj +++ b/run/markdown-preview/Samples.Run.MarkdownPreview.Editor/Samples.Run.MarkdownPreview.Editor.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable editor diff --git a/run/markdown-preview/Samples.Run.MarkdownPreview.Renderer.Tests/Samples.Run.MarkdownPreview.Renderer.Tests.csproj b/run/markdown-preview/Samples.Run.MarkdownPreview.Renderer.Tests/Samples.Run.MarkdownPreview.Renderer.Tests.csproj index c8e7b368525..25216596879 100644 --- a/run/markdown-preview/Samples.Run.MarkdownPreview.Renderer.Tests/Samples.Run.MarkdownPreview.Renderer.Tests.csproj +++ b/run/markdown-preview/Samples.Run.MarkdownPreview.Renderer.Tests/Samples.Run.MarkdownPreview.Renderer.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable false @@ -12,7 +12,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/run/markdown-preview/Samples.Run.MarkdownPreview.Renderer/Samples.Run.MarkdownPreview.Renderer.csproj b/run/markdown-preview/Samples.Run.MarkdownPreview.Renderer/Samples.Run.MarkdownPreview.Renderer.csproj index 36380216e0b..352dbc6f1b6 100644 --- a/run/markdown-preview/Samples.Run.MarkdownPreview.Renderer/Samples.Run.MarkdownPreview.Renderer.csproj +++ b/run/markdown-preview/Samples.Run.MarkdownPreview.Renderer/Samples.Run.MarkdownPreview.Renderer.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable renderer diff --git a/run/pubsub/Run.Samples.Pubsub.MinimalApi.Tests/Run.Samples.Pubsub.MinimalApi.Tests.csproj b/run/pubsub/Run.Samples.Pubsub.MinimalApi.Tests/Run.Samples.Pubsub.MinimalApi.Tests.csproj index 704d499e575..b171d46cf79 100644 --- a/run/pubsub/Run.Samples.Pubsub.MinimalApi.Tests/Run.Samples.Pubsub.MinimalApi.Tests.csproj +++ b/run/pubsub/Run.Samples.Pubsub.MinimalApi.Tests/Run.Samples.Pubsub.MinimalApi.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable false @@ -10,7 +10,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/run/pubsub/Run.Samples.Pubsub.MinimalApi/Run.Samples.Pubsub.MinimalApi.csproj b/run/pubsub/Run.Samples.Pubsub.MinimalApi/Run.Samples.Pubsub.MinimalApi.csproj index b775b0957d0..13d81270429 100644 --- a/run/pubsub/Run.Samples.Pubsub.MinimalApi/Run.Samples.Pubsub.MinimalApi.csproj +++ b/run/pubsub/Run.Samples.Pubsub.MinimalApi/Run.Samples.Pubsub.MinimalApi.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 enable enable diff --git a/secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj b/secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj index 1330ff7dadc..6076917272b 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj +++ b/secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj @@ -6,7 +6,7 @@ - + diff --git a/securitycenter/api/SecurityCenter.Samples.Tests/SecurityCenter.Samples.Tests.csproj b/securitycenter/api/SecurityCenter.Samples.Tests/SecurityCenter.Samples.Tests.csproj index ed6518ea43c..c880ecb881e 100644 --- a/securitycenter/api/SecurityCenter.Samples.Tests/SecurityCenter.Samples.Tests.csproj +++ b/securitycenter/api/SecurityCenter.Samples.Tests/SecurityCenter.Samples.Tests.csproj @@ -4,7 +4,7 @@ false - + diff --git a/servicedirectory/api/ServiceDirectory.Samples.Tests/ServiceDirectory.Samples.Tests.csproj b/servicedirectory/api/ServiceDirectory.Samples.Tests/ServiceDirectory.Samples.Tests.csproj index 5cb701c45af..97f7a518e3e 100644 --- a/servicedirectory/api/ServiceDirectory.Samples.Tests/ServiceDirectory.Samples.Tests.csproj +++ b/servicedirectory/api/ServiceDirectory.Samples.Tests/ServiceDirectory.Samples.Tests.csproj @@ -6,7 +6,7 @@ - + diff --git a/spanner/api/Spanner.Samples.Tests/Spanner.Samples.Tests.csproj b/spanner/api/Spanner.Samples.Tests/Spanner.Samples.Tests.csproj index 491ba6806d2..a926ad06b4b 100644 --- a/spanner/api/Spanner.Samples.Tests/Spanner.Samples.Tests.csproj +++ b/spanner/api/Spanner.Samples.Tests/Spanner.Samples.Tests.csproj @@ -14,7 +14,7 @@ - + diff --git a/storage/api/Storage.Samples.Tests/Storage.Samples.Tests.csproj b/storage/api/Storage.Samples.Tests/Storage.Samples.Tests.csproj index 1df5bdd5d39..d668856b4f3 100644 --- a/storage/api/Storage.Samples.Tests/Storage.Samples.Tests.csproj +++ b/storage/api/Storage.Samples.Tests/Storage.Samples.Tests.csproj @@ -1,14 +1,14 @@  - net6.0 + net8.0 false - + diff --git a/storageinsights/api/StorageInsights.Samples.Tests/StorageInsights.Samples.Tests.csproj b/storageinsights/api/StorageInsights.Samples.Tests/StorageInsights.Samples.Tests.csproj index 66613d3a449..4eca63ac62e 100644 --- a/storageinsights/api/StorageInsights.Samples.Tests/StorageInsights.Samples.Tests.csproj +++ b/storageinsights/api/StorageInsights.Samples.Tests/StorageInsights.Samples.Tests.csproj @@ -2,7 +2,7 @@ - net6.0 + net8.0 false @@ -14,7 +14,7 @@ - + diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/CheckLatestTransferOperationTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/CheckLatestTransferOperationTest.cs new file mode 100644 index 00000000000..adaf2f838da --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/CheckLatestTransferOperationTest.cs @@ -0,0 +1,92 @@ +// 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.StorageTransfer.V1; +using System; +using Xunit; + +namespace StorageTransfer.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class CheckLatestTransferOperationTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _jobName; + private readonly string _transferJobName; + private readonly string _sourceBucket; + private readonly string _sinkBucket; + + public CheckLatestTransferOperationTest(StorageFixture fixture) + { + _fixture = fixture; + _sourceBucket = _fixture.GenerateBucketName(); + _sinkBucket = _fixture.GenerateBucketName(); + _fixture.CreateBucketAndGrantStsPermissions(_sourceBucket); + _fixture.CreateBucketAndGrantStsPermissions(_sinkBucket); + _transferJobName = CreateTransferJob(); + } + + [Fact] + public void CheckLatestTransferOperation() + { + CheckLatestTransferOperationSample checkLatestTransferOperationSample = new CheckLatestTransferOperationSample(); + var transferJob = checkLatestTransferOperationSample.CheckLatestTransferOperation(_fixture.ProjectId, _transferJobName); + Assert.Contains("transferJobs/", transferJob.Name); + _jobName = transferJob.Name; + } + + private string CreateTransferJob() + { + TransferJob transferJob = new TransferJob + { + ProjectId = _fixture.ProjectId, + TransferSpec = new TransferSpec + { + GcsDataSink = new GcsData { BucketName = _sinkBucket }, + GcsDataSource = new GcsData { BucketName = _sourceBucket } + }, + Status = TransferJob.Types.Status.Enabled + }; + CreateTransferJobRequest request = new CreateTransferJobRequest + { + TransferJob = transferJob + }; + + TransferJob response = _fixture.Sts.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); + return response.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 + } + }); + _fixture.Storage.DeleteBucket(_sourceBucket); + _fixture.Storage.DeleteBucket(_sinkBucket); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/CreateEventDrivenGcsTransferTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/CreateEventDrivenGcsTransferTest.cs new file mode 100644 index 00000000000..8815afa51df --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/CreateEventDrivenGcsTransferTest.cs @@ -0,0 +1,114 @@ +/** + * 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. + * You may obtain a copy of the License at + * + * http://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.PubSub.V1; +using Google.Cloud.StorageTransfer.V1; +using System; +using Xunit; + +namespace StorageTransfer.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class CreateEventDrivenGcsTransferTest : IDisposable +{ + private readonly StorageFixture _fixture; + private readonly string _pubSubId; + private string _transferJobName; + private readonly string _sourceBucket; + private readonly string _sinkBucket; + private string TopicId { get; } = $"Topic-{Guid.NewGuid().ToString()}"; + private string SubscriptionId { get; } = $"Subscription-{Guid.NewGuid().ToString()}"; + private SubscriberServiceApiClient SubscriberClient { get; } = SubscriberServiceApiClient.Create(); + private PublisherServiceApiClient PublisherClient { get; } = PublisherServiceApiClient.Create(); + + public CreateEventDrivenGcsTransferTest(StorageFixture fixture) + { + _fixture = fixture; + _sourceBucket = _fixture.GenerateBucketName(); + _sinkBucket = _fixture.GenerateBucketName(); + _fixture.CreateBucketAndGrantStsPermissions(_sourceBucket); + _fixture.CreateBucketAndGrantStsPermissions(_sinkBucket); + _pubSubId = $"projects/{_fixture.ProjectId}/subscriptions/{SubscriptionId}"; + CreatePubSubResourcesAndGrantStsPermissions(); + } + + [Fact] + public void CreateEventDrivenGcsTransfer() + { + CreateEventDrivenGcsTransferSample createEventDrivenGcsTransferSample = new CreateEventDrivenGcsTransferSample(); + var transferJob = createEventDrivenGcsTransferSample.CreateEventDrivenGcsTransfer(_fixture.ProjectId, _sourceBucket, _sinkBucket, _pubSubId); + Assert.Contains("transferJobs/", transferJob.Name); + _transferJobName = transferJob.Name; + } + + private void CreatePubSubResourcesAndGrantStsPermissions() + { + string email = _fixture.Sts.GetGoogleServiceAccount(new GetGoogleServiceAccountRequest() + { + ProjectId = _fixture.ProjectId + }).AccountEmail; + + string memberServiceAccount = "serviceAccount:" + email; + SubscriptionName subscriptionName = new SubscriptionName(_fixture.ProjectId, SubscriptionId); + TopicName topicName = new TopicName(_fixture.ProjectId, TopicId); + PublisherClient.CreateTopic(topicName); + SubscriberClient.CreateSubscription(subscriptionName, topicName, pushConfig: null, ackDeadlineSeconds: 500); + + var policyIamPolicyTopic = new Google.Cloud.Iam.V1.Policy(); + policyIamPolicyTopic.AddRoleMember("roles/pubsub.publisher", memberServiceAccount); + PublisherClient.IAMPolicyClient.SetIamPolicy(new Google.Cloud.Iam.V1.SetIamPolicyRequest + { + ResourceAsResourceName = topicName, + Policy = policyIamPolicyTopic + }); + + var policyIamPolicySubscriber = new Google.Cloud.Iam.V1.Policy(); + policyIamPolicySubscriber.AddRoleMember("roles/pubsub.subscriber", memberServiceAccount); + PublisherClient.IAMPolicyClient.SetIamPolicy(new Google.Cloud.Iam.V1.SetIamPolicyRequest + { + ResourceAsResourceName = subscriptionName, + Policy = policyIamPolicySubscriber + }); + } + + public void Dispose() + { + try + { + _fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest() + { + ProjectId = _fixture.ProjectId, + JobName = _transferJobName, + TransferJob = new TransferJob() + { + Name = _transferJobName, + Status = TransferJob.Types.Status.Deleted + } + }); + + TopicName topicName = TopicName.FromProjectTopic(_fixture.ProjectId, TopicId); + PublisherClient.DeleteTopic(topicName); + SubscriptionName subscriptionName = SubscriptionName.FromProjectSubscription(_fixture.ProjectId, SubscriptionId); + SubscriberClient.DeleteSubscription(subscriptionName); + _fixture.Storage.DeleteBucket(_sourceBucket); + _fixture.Storage.DeleteBucket(_sinkBucket); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/DownloadToPosixTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/DownloadToPosixTest.cs new file mode 100644 index 00000000000..0ee2a072e2f --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/DownloadToPosixTest.cs @@ -0,0 +1,83 @@ +// 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 System.IO; +using Xunit; + +namespace StorageTransfer.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class DownloadToPosixTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _transferJobName; + private readonly string _tempDirectory; + private readonly string _gcsSourcePath; + private readonly string _sourceBucket; + + public DownloadToPosixTest(StorageFixture fixture) + { + _fixture = fixture; + _sourceBucket = _fixture.GenerateBucketName(); + _fixture.CreateBucketAndGrantStsPermissions(_sourceBucket); + _tempDirectory = _fixture.GenerateTempFolderPath(); + _gcsSourcePath = $"{Guid.NewGuid()}/{Guid.NewGuid()}/"; + UploadObjectToBucket(_sourceBucket); + } + + [Fact] + public void DownloadToPosix() + { + DownloadToPosixSample downloadToPosixSample = new DownloadToPosixSample(); + Directory.CreateDirectory(_tempDirectory); + var transferJob = downloadToPosixSample.DownloadToPosix(_fixture.ProjectId, _fixture.SinkAgentPoolName, _sourceBucket, _gcsSourcePath, _tempDirectory); + Assert.Contains("transferJobs/", transferJob.Name); + Assert.True(Directory.Exists(_tempDirectory)); + _transferJobName = transferJob.Name; + } + + private void UploadObjectToBucket(string bucketName) + { + byte[] byteArray = System.Text.Encoding.UTF8.GetBytes($@"{Guid.NewGuid()}.jpeg"); + MemoryStream stream = new MemoryStream(byteArray); + string fileName = $"{_gcsSourcePath}{DateTime.Now.ToString("yyyyMMddHHmmss")}.txt"; + _fixture.Storage.UploadObject(bucketName, fileName, "application/octet-stream", stream); + } + + public void Dispose() + { + try + { + _fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest() + { + ProjectId = _fixture.ProjectId, + JobName = _transferJobName, + TransferJob = new TransferJob() + { + Name = _transferJobName, + Status = TransferJob.Types.Status.Deleted + } + }); + Directory.Delete(_tempDirectory, true); + _fixture.Storage.DeleteBucket(_sourceBucket, new DeleteBucketOptions { DeleteObjects = true }); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/QuickstartTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/QuickStartTest.cs similarity index 66% rename from storagetransfer/api/StorageTransfer.Samples.Tests/QuickstartTest.cs rename to storagetransfer/api/StorageTransfer.Samples.Tests/QuickStartTest.cs index 30cac132e63..53048c0d578 100644 --- a/storagetransfer/api/StorageTransfer.Samples.Tests/QuickstartTest.cs +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/QuickStartTest.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright 2021 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,28 +14,34 @@ * limitations under the License. */ -using System; using Google.Cloud.StorageTransfer.V1; +using System; using Xunit; namespace StorageTransfer.Samples.Tests { [Collection(nameof(StorageFixture))] - public class QuickstartTest : IDisposable + public class QuickStartTest : IDisposable { private readonly StorageFixture _fixture; private string _transferJobName; + private readonly string _sourceBucket; + private readonly string _sinkBucket; - public QuickstartTest(StorageFixture fixture) + public QuickStartTest(StorageFixture fixture) { _fixture = fixture; + _sourceBucket = _fixture.GenerateBucketName(); + _sinkBucket = _fixture.GenerateBucketName(); + _fixture.CreateBucketAndGrantStsPermissions(_sourceBucket); + _fixture.CreateBucketAndGrantStsPermissions(_sinkBucket); } [Fact] - public void TestQuickstart() + public void QuickStart() { - QuickstartSample quickstartSample = new QuickstartSample(); - var transferJob = quickstartSample.Quickstart(_fixture.ProjectId, _fixture.BucketNameSource, _fixture.BucketNameSink); + QuickStartSample quickStartSample = new QuickStartSample(); + var transferJob = quickStartSample.QuickStart(_fixture.ProjectId, _sourceBucket, _sinkBucket); Assert.Contains("transferJobs/", transferJob.Name); _transferJobName = transferJob.Name; } @@ -54,6 +60,8 @@ public void Dispose() Status = TransferJob.Types.Status.Deleted } }); + _fixture.Storage.DeleteBucket(_sourceBucket); + _fixture.Storage.DeleteBucket(_sinkBucket); } catch (Exception) { diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/StorageFixture.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/StorageFixture.cs index 2938516a605..7c3e0588386 100644 --- a/storagetransfer/api/StorageTransfer.Samples.Tests/StorageFixture.cs +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/StorageFixture.cs @@ -1,4 +1,4 @@ -/** +/** * Copyright 2021 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -14,36 +14,41 @@ * limitations under the License. */ -using System; -using System.Collections.Generic; +using Google; using Google.Apis.Storage.v1.Data; using Google.Cloud.Storage.V1; using Google.Cloud.StorageTransfer.V1; +using System; +using System.Collections.Generic; +using System.IO; +using System.Threading; +using System.Xml.Linq; using Xunit; + namespace StorageTransfer.Samples.Tests { [CollectionDefinition(nameof(StorageFixture))] public class StorageFixture : IDisposable, ICollectionFixture { public string ProjectId { get; } - public string BucketNameSource { get; } = Guid.NewGuid().ToString(); - public string BucketNameSink { get; } = Guid.NewGuid().ToString(); + public string SourceAgentPoolName { get; } + public string SinkAgentPoolName { get; } public StorageClient Storage { get; } = StorageClient.Create(); public StorageTransferServiceClient Sts { get; } = StorageTransferServiceClient.Create(); + private readonly List _bucketsToDelete = []; public StorageFixture() { ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); + SourceAgentPoolName = $"projects/{ProjectId}/agentPools/transfer_service_default"; + SinkAgentPoolName = $"projects/{ProjectId}/agentPools/transfer_service_default"; if (string.IsNullOrWhiteSpace(ProjectId)) { throw new Exception("You need to set the Environment variable 'GOOGLE_PROJECT_ID' with your Google Cloud Project's project id."); } - - CreateBucketAndGrantStsPermissions(BucketNameSink); - CreateBucketAndGrantStsPermissions(BucketNameSource); } - private void CreateBucketAndGrantStsPermissions(string bucketName) + internal void CreateBucketAndGrantStsPermissions(string bucketName, bool registerForDeletion = true) { var bucket = Storage.CreateBucket(ProjectId, new Bucket { @@ -81,32 +86,48 @@ private void CreateBucketAndGrantStsPermissions(string bucketName) Role = bucketWriter, Members = new List { member } }; - policy.Bindings.Add(objectViewerBinding); policy.Bindings.Add(bucketReaderBinding); policy.Bindings.Add(bucketWriterBinding); - Storage.SetBucketIamPolicy(bucketName, policy); + SleepAfterBucketCreateDelete(); + if (registerForDeletion) + { + RegisterBucketToDelete(bucketName); + } } + internal string GenerateBucketName() => Guid.NewGuid().ToString(); + internal string GetCurrentUserTempFolderPath() => System.IO.Path.GetTempPath(); + internal string GenerateTempFolderPath() => Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + + /// + /// Bucket creation/deletion is rate-limited. To avoid making the tests flaky, we sleep after each operation. + /// + internal static void SleepAfterBucketCreateDelete() => Thread.Sleep(2000); + + internal void RegisterBucketToDelete(string bucket) => _bucketsToDelete.Add(bucket); + public void Dispose() { - try + foreach (var bucket in _bucketsToDelete) { - Storage.DeleteBucket(BucketNameSink); - } - catch (Exception) - { - // Do nothing, we delete on a best effort basis. + DeleteBucket(Storage, bucket, null); } + } + + private void DeleteBucket(StorageClient client, string bucket, string userProject) + { try { - Storage.DeleteBucket(BucketNameSource); + client.DeleteBucket(bucket, new DeleteBucketOptions { UserProject = userProject, DeleteObjects = true }); } - catch (Exception) + catch (GoogleApiException) { - // Do nothing, we delete on a best effort basis. + // Some tests fail to delete buckets due to object retention locks etc. + // They can be cleaned up later. } + SleepAfterBucketCreateDelete(); } } } diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/StorageTransfer.Samples.Tests.csproj b/storagetransfer/api/StorageTransfer.Samples.Tests/StorageTransfer.Samples.Tests.csproj index 88979dd6906..0dc66228b21 100644 --- a/storagetransfer/api/StorageTransfer.Samples.Tests/StorageTransfer.Samples.Tests.csproj +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/StorageTransfer.Samples.Tests.csproj @@ -11,11 +11,13 @@ - + - + + + - \ No newline at end of file + \ No newline at end of file diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/TransferBetweenPosixTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferBetweenPosixTest.cs new file mode 100644 index 00000000000..42f39d043be --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferBetweenPosixTest.cs @@ -0,0 +1,86 @@ +// 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.StorageTransfer.V1; +using System; +using System.IO; +using Xunit; + +namespace StorageTransfer.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class TransferBetweenPosixTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _transferJobName; + private readonly string _tempDirectory; + private readonly string _tempDestinationDirectory; + private readonly string _bucket; + + public TransferBetweenPosixTest(StorageFixture fixture) + { + _fixture = fixture; + _bucket = _fixture.GenerateBucketName(); + _fixture.CreateBucketAndGrantStsPermissions(_bucket); + _tempDirectory = fixture.GenerateTempFolderPath(); + _tempDestinationDirectory = fixture.GenerateTempFolderPath(); + } + + [Fact] + public void TransferBetweenPosix() + { + TransferBetweenPosixSample transferBetweenPosixSample = new TransferBetweenPosixSample(); + Directory.CreateDirectory(_tempDirectory); + Directory.CreateDirectory(_tempDestinationDirectory); + string fileName = Path.Combine(_tempDirectory, $"{Guid.NewGuid().ToString()}.txt"); + if (File.Exists(fileName)) + { + File.Delete(fileName); + } + using (StreamWriter sw = new StreamWriter(fileName)) + { + sw.WriteLine(Guid.NewGuid().ToString()); + } + var transferJob = transferBetweenPosixSample.TransferBetweenPosix(_fixture.ProjectId, _fixture.SourceAgentPoolName, _fixture.SinkAgentPoolName, _tempDirectory, _tempDestinationDirectory, _bucket); + Assert.Contains("transferJobs/", transferJob.Name); + _transferJobName = transferJob.Name; + Assert.True(Directory.Exists(_tempDirectory)); + Assert.True(Directory.Exists(_tempDestinationDirectory)); + Assert.True(File.Exists(fileName)); + } + + public void Dispose() + { + try + { + _fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest() + { + ProjectId = _fixture.ProjectId, + JobName = _transferJobName, + TransferJob = new TransferJob() + { + Name = _transferJobName, + Status = TransferJob.Types.Status.Deleted + } + }); + Directory.Delete(_tempDirectory, true); + Directory.Delete(_tempDestinationDirectory, true); + _fixture.Storage.DeleteBucket(_bucket); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/TransferFromPosixTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferFromPosixTest.cs new file mode 100644 index 00000000000..3f72ac58dc1 --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferFromPosixTest.cs @@ -0,0 +1,67 @@ +// 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.StorageTransfer.V1; +using System; +using Xunit; + +namespace StorageTransfer.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class TransferFromPosixTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _transferJobName; + private readonly string _rootDirectory; + private readonly string _bucket; + + public TransferFromPosixTest(StorageFixture fixture) + { + _fixture = fixture; + _bucket = _fixture.GenerateBucketName(); + _fixture.CreateBucketAndGrantStsPermissions(_bucket); + _rootDirectory = fixture.GetCurrentUserTempFolderPath(); + } + + [Fact] + public void TransferFromPosix() + { + TransferFromPosixSample transferFromPosixSample = new TransferFromPosixSample(); + var transferJob = transferFromPosixSample.TransferFromPosix(_fixture.ProjectId, _fixture.SourceAgentPoolName, _rootDirectory, _bucket); + 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 + } + }); + _fixture.Storage.DeleteBucket(_bucket); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/TransferToNearlineTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferToNearlineTest.cs new file mode 100644 index 00000000000..2ceddc570b3 --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferToNearlineTest.cs @@ -0,0 +1,75 @@ +// 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; + +namespace StorageTransfer.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class TransferToNearlineTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _transferJobName; + private readonly string _sourceBucket; + private readonly string _sinkBucket; + + public TransferToNearlineTest(StorageFixture fixture) + { + _fixture = fixture; + _sourceBucket = _fixture.GenerateBucketName(); + _sinkBucket = _fixture.GenerateBucketName(); + _fixture.CreateBucketAndGrantStsPermissions(_sourceBucket); + _fixture.CreateBucketAndGrantStsPermissions(_sinkBucket); + } + + [Fact] + public void TransferToNearline() + { + TransferToNearlineSample transferToNearlineSample = new TransferToNearlineSample(); + var storage = _fixture.Storage; + var bucket = storage.GetBucket(_sinkBucket); + string storageClass = StorageClasses.Nearline; + bucket.StorageClass = storageClass; + storage.UpdateBucket(bucket); + var transferJob = transferToNearlineSample.TransferToNearline(_fixture.ProjectId, _sourceBucket, _sinkBucket); + 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 + } + }); + _fixture.Storage.DeleteBucket(_sourceBucket); + _fixture.Storage.DeleteBucket(_sinkBucket); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storagetransfer/api/StorageTransfer.Samples.Tests/TransferUsingManifestTest.cs b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferUsingManifestTest.cs new file mode 100644 index 00000000000..811acf71385 --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples.Tests/TransferUsingManifestTest.cs @@ -0,0 +1,83 @@ +// 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 System.IO; +using Xunit; + +namespace StorageTransfer.Samples.Tests; + +[Collection(nameof(StorageFixture))] +public class TransferUsingManifestTest : IDisposable +{ + private readonly StorageFixture _fixture; + private string _transferJobName; + private readonly string _rootDirectory; + private readonly string _manifestObjectName; + private readonly string _manifestBucket; + private readonly string _sinkBucket; + + public TransferUsingManifestTest(StorageFixture fixture) + { + _fixture = fixture; + _manifestBucket = _fixture.GenerateBucketName(); + _sinkBucket = _fixture.GenerateBucketName(); + _fixture.CreateBucketAndGrantStsPermissions(_manifestBucket); + _fixture.CreateBucketAndGrantStsPermissions(_sinkBucket); + _rootDirectory = fixture.GetCurrentUserTempFolderPath(); + _manifestObjectName = $@"{Guid.NewGuid()}.csv"; + UploadObjectToManifestBucket(_manifestBucket); + } + + [Fact] + public void TransferUsingManifest() + { + TransferUsingManifestSample transferUsingManifestSample = new TransferUsingManifestSample(); + var transferJob = transferUsingManifestSample.TransferUsingManifest(_fixture.ProjectId, _fixture.SourceAgentPoolName, _rootDirectory, _manifestBucket, _sinkBucket, _manifestObjectName); + Assert.Contains("transferJobs/", transferJob.Name); + _transferJobName = transferJob.Name; + } + + private void UploadObjectToManifestBucket(string bucketName) + { + byte[] byteArray = System.Text.Encoding.UTF8.GetBytes($@"{Guid.NewGuid()}.jpeg"); + MemoryStream stream = new MemoryStream(byteArray); + _fixture.Storage.UploadObject(bucketName, _manifestObjectName, "application/octet-stream", stream); + } + + public void Dispose() + { + try + { + _fixture.Sts.UpdateTransferJob(new UpdateTransferJobRequest() + { + ProjectId = _fixture.ProjectId, + JobName = _transferJobName, + TransferJob = new TransferJob() + { + Name = _transferJobName, + Status = TransferJob.Types.Status.Deleted + } + }); + _fixture.Storage.DeleteBucket(_manifestBucket, new DeleteBucketOptions { DeleteObjects = true }); + _fixture.Storage.DeleteBucket(_sinkBucket, new DeleteBucketOptions { DeleteObjects = true }); + } + catch (Exception) + { + // Do nothing, we delete on a best effort basis. + } + } +} diff --git a/storagetransfer/api/StorageTransfer.Samples/CheckLatestTransferOperationSample.cs b/storagetransfer/api/StorageTransfer.Samples/CheckLatestTransferOperationSample.cs new file mode 100644 index 00000000000..da9a1bdeaaa --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/CheckLatestTransferOperationSample.cs @@ -0,0 +1,48 @@ +// 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; + +public class CheckLatestTransferOperationSample +{ + /// + /// Checks the latest transfer operation for a given transfer job. + /// + /// The ID of the Google Cloud project. + /// The name of the transfer job. + public TransferJob CheckLatestTransferOperation( + string projectId = "my-project-id", + string jobName = "transferJobs/1234567890") + { + StorageTransferServiceClient storageTransfer = StorageTransferServiceClient.Create(); + GetTransferJobRequest getTransferJobRequest = new GetTransferJobRequest { ProjectId = projectId, JobName = jobName }; + + TransferJob transferJob = storageTransfer.GetTransferJob(getTransferJobRequest); + string latestOperationName = transferJob.LatestOperationName; + + if (!string.IsNullOrEmpty(latestOperationName)) + { + Console.WriteLine("The latest operation for transfer job " + jobName + " is: " + latestOperationName + ""); + } + else + { + Console.WriteLine("Transfer job " + jobName + " hasn't run yet, try again after the job has started running."); + } + return transferJob; + } +} +// [END storagetransfer_get_latest_transfer_operation] diff --git a/storagetransfer/api/StorageTransfer.Samples/CreateEventDrivenGcsTransferSample.cs b/storagetransfer/api/StorageTransfer.Samples/CreateEventDrivenGcsTransferSample.cs new file mode 100644 index 00000000000..34c57b3afbf --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/CreateEventDrivenGcsTransferSample.cs @@ -0,0 +1,58 @@ +/** + * 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. + * You may obtain a copy of the License at + * + * http://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_create_event_driven_gcs_transfer] + +using Google.Cloud.StorageTransfer.V1; +using System; + +public class CreateEventDrivenGcsTransferSample +{ + /// + /// Creates an event driven gcs data transfer from source gcs bucket to sink gcs bucket subscribed to pub/sub subscription id. + /// + /// The ID of the Google Cloud project. + /// The GCS bucket to transfer data from. + /// The GCS bucket to transfer data to. + /// The subscription ID to a PubSub queue to track. + public TransferJob CreateEventDrivenGcsTransfer( + string projectId = "my-project-id", + string sourceBucket = "my-source-bucket", + string sinkBucket = "my-sink-bucket", + string pubSubId = "projects/PROJECT_NAME/subscriptions/SUBSCRIPTION_ID") + { + string jobDescription = $"Event driven gcs data transfer from {sourceBucket} to {sinkBucket} subscribed to {pubSubId} "; + + TransferJob transferJob = new TransferJob + { + ProjectId = projectId, + Description = jobDescription, + TransferSpec = new TransferSpec + { + GcsDataSink = new GcsData { BucketName = sinkBucket }, + GcsDataSource = new GcsData { BucketName = sourceBucket }, + }, + Status = TransferJob.Types.Status.Enabled, + EventStream = new EventStream { Name = pubSubId } + }; + + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); + Console.WriteLine($"Created an event driven transfer job from {sourceBucket} to {sinkBucket} subscribed to {pubSubId} with name {response.Name}"); + return response; + } +} +// [END storagetransfer_create_event_driven_gcs_transfer] diff --git a/storagetransfer/api/StorageTransfer.Samples/DownloadToPosixSample.cs b/storagetransfer/api/StorageTransfer.Samples/DownloadToPosixSample.cs new file mode 100644 index 00000000000..e0ddd6fa376 --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/DownloadToPosixSample.cs @@ -0,0 +1,64 @@ +// 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_download_to_posix] + +using Google.Cloud.StorageTransfer.V1; +using System; + +public class DownloadToPosixSample +{ + /// + /// Creates a transfer for downloading objects from a gcs bucket to the root directory of POSIX file system. + /// + /// The ID of the Google Cloud project. + /// The agent pool associated with the POSIX data sink. If not provided, defaults to the default agent. + /// Your GCS source bucket name. + /// An optional path on the Google Cloud Storage bucket to download from. + /// The root directory path on the source filesystem. + public TransferJob DownloadToPosix( + string projectId = "my-project-id", + string sinkAgentPoolName = "projects/my-project-id/agentPools/transfer_service_default", + string sourceBucket = "my-source-bucket", + string gcsSourcePath = "foo/bar/", + string rootDirectory = "/tmp/uploads") + { + string jobDescription = $"Download objects from a GCS source bucket ({sourceBucket}/{gcsSourcePath}) to the root directory of POSIX file system"; + + TransferJob transferJob = new TransferJob + { + ProjectId = projectId, + Description = jobDescription, + TransferSpec = new TransferSpec + { + GcsDataSource = new GcsData { BucketName = sourceBucket, Path = gcsSourcePath }, + SinkAgentPoolName = sinkAgentPoolName, + PosixDataSink = new PosixFilesystem { RootDirectory = rootDirectory } + }, + Status = TransferJob.Types.Status.Enabled, + }; + + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); + client.RunTransferJob(new RunTransferJobRequest + { + JobName = response.Name, + ProjectId = projectId + }); + + Console.WriteLine($"Created and ran transfer job from ({sourceBucket}/{gcsSourcePath}) to {rootDirectory} with the name {response.Name}"); + return response; + } +} +// [END storagetransfer_download_to_posix] diff --git a/storagetransfer/api/StorageTransfer.Samples/QuickStartSample.cs b/storagetransfer/api/StorageTransfer.Samples/QuickStartSample.cs new file mode 100644 index 00000000000..115e71231da --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/QuickStartSample.cs @@ -0,0 +1,58 @@ +/** + * Copyright 2021 Google Inc. + * + * 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 + * + * http://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_quickstart] + +using Google.Cloud.StorageTransfer.V1; +using System; + +public class QuickStartSample +{ + /// + /// Creates an one-time transfer job from a Google Cloud storage bucket to another bucket. + /// + /// The ID of the Google Cloud project. + /// The GCS bucket to transfer data from. + /// The GCS bucket to transfer data to. + public TransferJob QuickStart( + string projectId = "my-project-id", + string sourceBucket = "my-source-bucket", + string sinkBucket = "my-sink-bucket") + { + TransferJob transferJob = new TransferJob + { + ProjectId = projectId, + TransferSpec = new TransferSpec + { + GcsDataSink = new GcsData { BucketName = sinkBucket }, + GcsDataSource = new GcsData { BucketName = sourceBucket } + }, + Status = TransferJob.Types.Status.Enabled + }; + + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); + client.RunTransferJob(new RunTransferJobRequest + { + JobName = response.Name, + ProjectId = projectId + }); + + Console.WriteLine($"Created and ran transfer job from {sourceBucket} to {sinkBucket} with name {response.Name}"); + return response; + } +} +// [END storagetransfer_quickstart] diff --git a/storagetransfer/api/StorageTransfer.Samples/QuickstartSample.cs b/storagetransfer/api/StorageTransfer.Samples/QuickstartSample.cs deleted file mode 100644 index cc84bfe0620..00000000000 --- a/storagetransfer/api/StorageTransfer.Samples/QuickstartSample.cs +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright 2021 Google Inc. - * - * 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 - * - * http://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_quickstart] -using System; -using Google.Cloud.StorageTransfer.V1; - -namespace StorageTransfer.Samples -{ - public class QuickstartSample - { - public TransferJob Quickstart( - // Your Google Cloud Project ID - string projectId = "my-project-id", - // The GCS bucket to transfer data from - string sourceBucket = "my-source-bucket", - // The GCS bucket to transfer data to - string sinkBucket = "my-sink-bucket") - { - TransferJob transferJob = new TransferJob - { - ProjectId = projectId, - TransferSpec = new TransferSpec - { - GcsDataSink = new GcsData { BucketName = sourceBucket }, - GcsDataSource = new GcsData { BucketName = sinkBucket } - }, - Status = TransferJob.Types.Status.Enabled - }; - - StorageTransferServiceClient client = StorageTransferServiceClient.Create(); - TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); - client.RunTransferJob(new RunTransferJobRequest - { - JobName = response.Name, - ProjectId = projectId - }); - - Console.WriteLine($"Created and ran transfer job from {sourceBucket} to {sinkBucket} with name {response.Name}"); - - return response; - } - } -} -// [END storagetransfer_quickstart] diff --git a/storagetransfer/api/StorageTransfer.Samples/TransferBetweenPosixSample.cs b/storagetransfer/api/StorageTransfer.Samples/TransferBetweenPosixSample.cs new file mode 100644 index 00000000000..b1410a2a92f --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/TransferBetweenPosixSample.cs @@ -0,0 +1,68 @@ +// 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_posix_to_posix] + +using Google.Cloud.StorageTransfer.V1; +using System; + +public class TransferBetweenPosixSample +{ + /// + /// Creates a transfer that transfer objects from root directory to the destination directory between POSIX file systems. + /// + /// The ID of the Google Cloud project. + /// The agent pool associated with the POSIX data source. If not provided, defaults to the default agent. + /// The agent pool associated with the POSIX data sink. If not provided, defaults to the default agent. + /// The root directory path on the source filesystem. + /// The root directory path on the sink filesystem. + /// The name of GCS bucket for intermediate storage. + public TransferJob TransferBetweenPosix( + string projectId = "my-project-id", + string sourceAgentPoolName = "projects/my-project-id/agentPools/transfer_service_default", + string sinkAgentPoolName = "projects/my-project-id/agentPools/transfer_service_default", + string rootDirectory = "/tmp/uploads", + string destinationDirectory = "/directory/to/transfer/sink", + string intermediateBucket = "my-intermediate-bucket") + { + string jobDescription = $"Transfer objects from {rootDirectory} to the {destinationDirectory} between POSIX file system"; + + TransferJob transferJob = new TransferJob + { + ProjectId = projectId, + Description = jobDescription, + TransferSpec = new TransferSpec + { + SourceAgentPoolName = sourceAgentPoolName, + SinkAgentPoolName = sinkAgentPoolName, + PosixDataSource = new PosixFilesystem { RootDirectory = rootDirectory }, + PosixDataSink = new PosixFilesystem { RootDirectory = destinationDirectory }, + GcsIntermediateDataLocation = new GcsData { BucketName = intermediateBucket } + }, + Status = TransferJob.Types.Status.Enabled, + }; + + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); + client.RunTransferJob(new RunTransferJobRequest + { + JobName = response.Name, + ProjectId = projectId + }); + + Console.WriteLine($"Created and ran transfer job from {rootDirectory} to {destinationDirectory} with the name {response.Name}"); + return response; + } +} +// [END storagetransfer_transfer_posix_to_posix] diff --git a/storagetransfer/api/StorageTransfer.Samples/TransferFromPosixSample.cs b/storagetransfer/api/StorageTransfer.Samples/TransferFromPosixSample.cs new file mode 100644 index 00000000000..8d03df85a69 --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/TransferFromPosixSample.cs @@ -0,0 +1,62 @@ +// 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 System; + +public class TransferFromPosixSample +{ + /// + /// Creates a transfer job to transfer objects from a POSIX file system to a gcs sink bucket. + /// + /// The ID of the Google Cloud project. + /// The agent pool associated with the POSIX data source. If not provided, defaults to the default agent. + /// The root directory path on the source filesystem. + /// The GCS bucket to transfer data to. + public TransferJob TransferFromPosix( + string projectId = "my-project-id", + string sourceAgentPoolName = "projects/my-project-id/agentPools/transfer_service_default", + string rootDirectory = "/tmp/uploads", + string sinkBucket = "my-sink-bucket") + { + 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, + }; + + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); + client.RunTransferJob(new RunTransferJobRequest + { + JobName = response.Name, + ProjectId = projectId + }); + + Console.WriteLine($"Created and ran transfer job from {rootDirectory} to {sinkBucket} with the name {response.Name}"); + return response; + } +} +// [END storagetransfer_transfer_from_posix] diff --git a/storagetransfer/api/StorageTransfer.Samples/TransferToNearlineSample.cs b/storagetransfer/api/StorageTransfer.Samples/TransferToNearlineSample.cs new file mode 100644 index 00000000000..0e32ed5f087 --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/TransferToNearlineSample.cs @@ -0,0 +1,64 @@ +// 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; + +public class TransferToNearlineSample +{ + /// + /// Creates an one-off transfer job that transfers objects from a standard gcs bucket that are more + /// than 30 days old to a nearline gcs bucket. + /// + /// The ID of the Google Cloud project. + /// The GCS bucket to transfer objects from. + /// The GCS Nearline bucket to transfer old objects to. + public TransferJob TransferToNearline( + string projectId = "my-project-id", + string sourceBucket = "my-source-bucket", + string sinkBucket = "my-sink-bucket") + { + 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)) } + }; + + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); + client.RunTransferJob(new RunTransferJobRequest + { + JobName = response.Name, + ProjectId = projectId + }); + + Console.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] diff --git a/storagetransfer/api/StorageTransfer.Samples/TransferUsingManifestSample.cs b/storagetransfer/api/StorageTransfer.Samples/TransferUsingManifestSample.cs new file mode 100644 index 00000000000..f21fb6d6faa --- /dev/null +++ b/storagetransfer/api/StorageTransfer.Samples/TransferUsingManifestSample.cs @@ -0,0 +1,71 @@ +// 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_manifest_request] + +using Google.Cloud.StorageTransfer.V1; +using System; + +public class TransferUsingManifestSample +{ + /// + /// Creates a transfer job that transfer objects from a POSIX file system to a gcs sink bucket using a manifest file. + /// + /// The ID of the Google Cloud project. + /// The agent pool associated with the POSIX data source. If not provided, + /// defaults to the default agent + /// The root directory path on the source filesystem. + /// The GCS bucket which has your manifest file. + /// The GCS bucket to transfer data to. + /// The name of the manifest file in manifestBucket that specifies which objects to transfer. + public TransferJob TransferUsingManifest( + string projectId = "my-project-id", + string sourceAgentPoolName = "projects/my-project-id/agentPools/transfer_service_default", + string rootDirectory = "/tmp/uploads", + string manifestBucket = "my-source-bucket", + string sinkBucket = "my-sink-bucket", + string manifestObjectName = "path/to/manifest.csv") + { + string manifestLocation = $"gs://{manifestBucket}/{manifestObjectName}"; + + string jobDescription = $"Transfers objects from a POSIX file system to a sink bucket ({sinkBucket}) using manifest file"; + + TransferJob transferJob = new TransferJob + { + ProjectId = projectId, + Description = jobDescription, + TransferSpec = new TransferSpec + { + GcsDataSink = new GcsData { BucketName = sinkBucket }, + GcsDataSource = new GcsData { BucketName = manifestBucket }, + SourceAgentPoolName = sourceAgentPoolName, + PosixDataSource = new PosixFilesystem { RootDirectory = rootDirectory }, + TransferManifest = new TransferManifest { Location = manifestLocation } + }, + Status = TransferJob.Types.Status.Enabled, + }; + + StorageTransferServiceClient client = StorageTransferServiceClient.Create(); + TransferJob response = client.CreateTransferJob(new CreateTransferJobRequest { TransferJob = transferJob }); + client.RunTransferJob(new RunTransferJobRequest + { + JobName = response.Name, + ProjectId = projectId + }); + + Console.WriteLine($"Created and ran transfer job from {rootDirectory} to {sinkBucket} using manifest file {manifestLocation} with the name {response.Name}"); + return response; + } +} +// [END storagetransfer_manifest_request] diff --git a/workflows/api/Workflow.Samples.Test/Workflow.Samples.Test.csproj b/workflows/api/Workflow.Samples.Test/Workflow.Samples.Test.csproj index 3091a235bae..e185308a423 100644 --- a/workflows/api/Workflow.Samples.Test/Workflow.Samples.Test.csproj +++ b/workflows/api/Workflow.Samples.Test/Workflow.Samples.Test.csproj @@ -10,7 +10,7 @@ - +