From 62f68acf3c4453b148f8a0dde96b8dd23c2874e0 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Thu, 22 Jan 2026 15:12:22 +0530 Subject: [PATCH 01/19] feat(secretmanager): Adding labels and annotations sample --- .../DeleteRegionalSecretAnnotationTests.cs | 72 +++++++++++++++++++ .../DeleteRegionalSecretLabelTests.cs | 68 ++++++++++++++++++ .../DeleteSecretAnnotationTests.cs | 62 ++++++++++++++++ .../DeleteSecretLabelTests.cs | 62 ++++++++++++++++ .../EditSecretLabelTests.cs | 59 +++++++++++++++ .../DeleteRegionalSecretAnnotation.cs | 55 ++++++++++++++ .../DeleteRegionalSecretLabel.cs | 55 ++++++++++++++ .../DeleteSecretAnnotation.cs | 50 +++++++++++++ .../DeleteSecretLabel.cs | 50 +++++++++++++ .../SecretManager.Samples/EditSecretLabel.cs | 55 ++++++++++++++ 10 files changed, 588 insertions(+) create mode 100644 secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs create mode 100644 secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs create mode 100644 secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs create mode 100644 secretmanager/api/SecretManager.Samples/DeleteSecretAnnotation.cs create mode 100644 secretmanager/api/SecretManager.Samples/DeleteSecretLabel.cs create mode 100644 secretmanager/api/SecretManager.Samples/EditSecretLabel.cs diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs new file mode 100644 index 00000000000..8e5277e51b5 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs @@ -0,0 +1,72 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class DeleteRegionalSecretAnnotationTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly DeleteRegionalSecretAnnotationSample _sample; + + public DeleteRegionalSecretAnnotationTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new DeleteRegionalSecretAnnotationSample(); + } + + [Fact] + public void DeleteRegionalSecretAnnotation() + { + // Create a secret with annotations + Secret secret = _fixture.CreateSecret(_fixture.RandomId()); + SecretName secretName = secret.SecretName; + + // Get a key from the existing annotations + string annotationKey = _fixture.AnnotationKey; + + // Verify the secret has annotations + Assert.NotEmpty(secret.Annotations); + Assert.True(secret.Annotations.ContainsKey(annotationKey)); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Run the sample code to delete the annotation + _sample.DeleteRegionalSecretAnnotation( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected messages + Assert.Contains($"Updated secret:", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs new file mode 100644 index 00000000000..010428401d0 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs @@ -0,0 +1,68 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class DeleteRegionalSecretLabelTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly DeleteRegionalSecretLabelSample _sample; + + public DeleteRegionalSecretLabelTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new DeleteRegionalSecretLabelSample(); + } + + [Fact] + public void DeleteRegionalSecretLabel() + { + Secret secret = _fixture.CreateSecret(_fixture.RandomId()); + SecretName secretName = secret.SecretName; + + // Verify the secret has labels + Assert.NotEmpty(secret.Labels); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Run the sample code to delete all labels + _sample.DeleteRegionalSecretLabel( + + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected messages + Assert.Contains($"Updated secret:", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs new file mode 100644 index 00000000000..a82f9cf0ee1 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs @@ -0,0 +1,62 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class DeleteSecretAnnotationTests +{ + private readonly SecretManagerFixture _fixture; + private readonly DeleteSecretAnnotationSample _sample; + + public DeleteSecretAnnotationTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new DeleteSecretAnnotationSample(); + } + + [Fact] + public void DeleteSecretAnnotation() + { + // Create the secret. + Secret secret = _fixture.CreateSecret(_fixture.RandomId()); + + // Get the secretName from the created secret. + SecretName secretName = secret.SecretName; + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Call the code sample function to delete the label + _sample.DeleteSecretAnnotation( + projectId: secretName.ProjectId, secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains("Updated secret:", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs new file mode 100644 index 00000000000..f2805cb536c --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs @@ -0,0 +1,62 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class DeleteSecretLabelTests +{ + private readonly SecretManagerFixture _fixture; + private readonly DeleteSecretLabelSample _sample; + + public DeleteSecretLabelTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new DeleteSecretLabelSample(); + } + + [Fact] + public void DeleteSecretLabel() + { + // Create the secret. + Secret secret = _fixture.CreateSecret(_fixture.RandomId()); + + // Get the secretName from the created secret. + SecretName secretName = secret.SecretName; + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Call the code sample function to delete the label + _sample.DeleteSecretLabel( + projectId: secretName.ProjectId, secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains("Updated secret:", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs new file mode 100644 index 00000000000..18b246d7216 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs @@ -0,0 +1,59 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class EditSecretLabelTests +{ + private readonly SecretManagerFixture _fixture; + private readonly EditSecretLabelSample _sample; + + public EditSecretLabelTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new EditSecretLabelSample(); + } + + [Fact] + public void EditSecretLabel() + { + // Get the SecretName to create Secret. + SecretName secretName = _fixture.Secret.SecretName; + + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Call the code sample function. + _sample.EditSecretLabel( + projectId: secretName.ProjectId, secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + Assert.Contains($"Updated secret: ", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + } +} diff --git a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs new file mode 100644 index 00000000000..4045f2c66ae --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs @@ -0,0 +1,55 @@ +/* + * Copyright 2026 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 secretmanager_delete_regional_secret_annotation] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class DeleteRegionalSecretAnnotationSample +{ + public void DeleteRegionalSecretAnnotation( + string projectId = "my-project", + string locationId = "my-location", + string secretId = "my-secret") + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the resource name of the secret. + string secretName = $"projects/{projectId}/locations/{locationId}/secrets/{secretId}"; + + // Get the secret. + Secret secret = client.GetSecret(secretName); + + // Delete the annotation + secret.Annotations.Clear(); + + // Build the field mask. + FieldMask updateMask = FieldMask.FromString("annotations"); + + // Update the secret. + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + // Print the new secret name. + Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + } +} +// [END secretmanager_delete_regional_secret_annotation] diff --git a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs new file mode 100644 index 00000000000..e11906c1395 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs @@ -0,0 +1,55 @@ +/* + * Copyright 2026 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 secretmanager_delete_regional_secret_label] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class DeleteRegionalSecretLabelSample +{ + public void DeleteRegionalSecretLabel( + string projectId = "my-project", + string locationId = "my-location", + string secretId = "my-secret") + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the resource name of the secret. + string secretName = $"projects/{projectId}/locations/{locationId}/secrets/{secretId}"; + + // Get the secret. + Secret secret = client.GetSecret(secretName); + + // Clear all labels + secret.Labels.Clear(); + + // Build the field mask. + FieldMask updateMask = FieldMask.FromString("labels"); + + // Update the secret. + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + // Print the new secret name. + Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + } +} +// [END secretmanager_delete_regional_secret_label] diff --git a/secretmanager/api/SecretManager.Samples/DeleteSecretAnnotation.cs b/secretmanager/api/SecretManager.Samples/DeleteSecretAnnotation.cs new file mode 100644 index 00000000000..165288d5e17 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/DeleteSecretAnnotation.cs @@ -0,0 +1,50 @@ +/* + * Copyright 2026 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 secretmanager_delete_secret_annotation] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class DeleteSecretAnnotationSample +{ + public void DeleteSecretAnnotation( + string projectId = "my-project", string secretId = "my-secret") + { + // Create the client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name of the secret. + SecretName secretName = new SecretName(projectId, secretId); + + // Get the secret. + Secret secret = client.GetSecret(secretName); + + // Clear all annotations + secret.Annotations.Clear(); + + // Build the field mask. + FieldMask updateMask = FieldMask.FromString("annotations"); + + // Update the secret. + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + // Print the new secret name. + Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + } +} +// [END secretmanager_delete_secret_annotation] diff --git a/secretmanager/api/SecretManager.Samples/DeleteSecretLabel.cs b/secretmanager/api/SecretManager.Samples/DeleteSecretLabel.cs new file mode 100644 index 00000000000..b25307f7edb --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/DeleteSecretLabel.cs @@ -0,0 +1,50 @@ +/* + * Copyright 2026 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 secretmanager_delete_secret_label] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class DeleteSecretLabelSample +{ + public void DeleteSecretLabel( + string projectId = "my-project", string secretId = "my-secret") + { + // Create the client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name of the secret. + SecretName secretName = new SecretName(projectId, secretId); + + // Get the secret. + Secret secret = client.GetSecret(secretName); + + // Clear all labels + secret.Labels.Clear(); + + // Build the field mask. + FieldMask updateMask = FieldMask.FromString("labels"); + + // Update the secret. + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + // Print the new secret name. + Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + } +} +// [END secretmanager_delete_secret_label] diff --git a/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs b/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs new file mode 100644 index 00000000000..5d0281949e3 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs @@ -0,0 +1,55 @@ +/* + * Copyright 2026 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 secretmanager_edit_secret_label] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; +using System.Collections.Generic; + +public class EditSecretLabelSample +{ + public void EditSecretLabel( + string projectId = "my-project", string secretId = "my-secret") + { + string labelKey = "my-label-key"; + string labelValue = "my-label-value"; + // Create the Secret Manager client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name of the secret. + SecretName name = new SecretName(projectId, secretId); + + // Get the secret. + Secret secret = client.GetSecret(name); + + Console.WriteLine($"Updated secret: {secret.Name}"); + + // Update the labels + secret.Labels[labelKey] = labelValue; + + // Update the secret. + FieldMask updateMask = FieldMask.FromString("labels"); + + // Call the API. + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + // Print the new secret name. + Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + } +} +// [END secretmanager_edit_secret_label] From 0b2048116720af2dfb116dc7367b0b76663b4374 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Thu, 22 Jan 2026 18:03:45 +0530 Subject: [PATCH 02/19] feat(secretmanager): Adding filter and cmek samples --- .../ListSecretsWithFilterTests.cs | 60 ++++++++++++++++ .../createRegionalSecretWithCmekTests.cs | 72 +++++++++++++++++++ .../createSecretWithCmekTests.cs | 71 ++++++++++++++++++ .../ListSecretsWithFilter.cs | 47 ++++++++++++ .../createRegionalSecretWithCmek.cs | 56 +++++++++++++++ .../createSecretWithCmek.cs | 58 +++++++++++++++ 6 files changed, 364 insertions(+) create mode 100644 secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs create mode 100644 secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs create mode 100644 secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs create mode 100644 secretmanager/api/SecretManager.Samples/createSecretWithCmek.cs diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs new file mode 100644 index 00000000000..317751fdcb2 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs @@ -0,0 +1,60 @@ +/* + * Copyright 2026 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 System.IO; +using System.Collections.Generic; +using Xunit; +using Google.Cloud.SecretManager.V1; + +[Collection(nameof(SecretManagerFixture))] +public class ListSecretsWithFilterTests +{ + private readonly SecretManagerFixture _fixture; + private readonly ListSecretsWithFilterSample _sample; + + public ListSecretsWithFilterTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new ListSecretsWithFilterSample(); + } + + [Fact] + public void ListsSecretsWithFilter() + { + Secret secret = _fixture.CreateSecret(_fixture.RandomId()); + SecretName secretName = secret.SecretName; + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Run the sample code + _sample.ListSecretsWithFilter( + projectId: _fixture.ProjectId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the created secret + Assert.Contains(secretName.ToString(), consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + _fixture.DeleteSecret(secretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs b/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs new file mode 100644 index 00000000000..c6eaa0822d2 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs @@ -0,0 +1,72 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class CreateRegionalSecretWithCmekTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly CreateRegionalSecretWithCmekSample _sample; + + public CreateRegionalSecretWithCmekTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new CreateRegionalSecretWithCmekSample(); + } + + [Fact] + public void CreatesRegionalSecretWithCmek() + { + // Skip the test if no regional KMS key is available + if (string.IsNullOrEmpty(_fixture.KmsKeyName)) + { + return; + } + + // Get the SecretName from the set ProjectId & LocationId. + SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Create the regional secret with CMEK. + _sample.CreateRegionalSecretWithCmek( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId, + kmsKeyName: _fixture.KmsKeyName); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Created secret ", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret. + _fixture.DeleteSecret(secretName); + + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs b/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs new file mode 100644 index 00000000000..9c5d0242f16 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs @@ -0,0 +1,71 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class CreateSecretWithCmekTests +{ + private readonly SecretManagerFixture _fixture; + private readonly CreateSecretWithCmekSample _sample; + + public CreateSecretWithCmekTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new CreateSecretWithCmekSample(); + } + + [Fact] + public void CreatesSecretWithCmek() + { + // Skip the test if no KMS key is available + if (string.IsNullOrEmpty(_fixture.KmsKeyName)) + { + return; + } + + // Get the SecretName to create Secret. + SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Create the secret with CMEK. + _sample.CreateSecretWithCmek( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + kmsKeyName: _fixture.KmsKeyName); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Created secret", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret. + _fixture.DeleteSecret(secretName); + + } +} diff --git a/secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs b/secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs new file mode 100644 index 00000000000..f7f4718ec61 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs @@ -0,0 +1,47 @@ +/* + * Copyright 2026 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 secretmanager_list_secrets_with_filter] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using System; + +public class ListSecretsWithFilterSample +{ + public void ListSecretsWithFilter(string projectId = "my-project") + { + string filterStr = "labels.my-label-key=my-label-value"; + // Create the Secret Manager client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name of the parent project. + ProjectName projectName = new ProjectName(projectId); + ListSecretsRequest request = new ListSecretsRequest + { + ParentAsProjectName = projectName, + Filter = filterStr + }; + + + // List all secrets with the provided filter. + foreach (Secret secret in client.ListSecrets(request)) + { + Console.WriteLine($"Found secret: {secret.Name}"); + } + } +} +// [END secretmanager_list_secrets_with_filter] diff --git a/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs b/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs new file mode 100644 index 00000000000..f756e43259f --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs @@ -0,0 +1,56 @@ +/* + * Copyright 2026 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 secretmanager_create_regional_secret_with_cmek] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using System; + +public class CreateRegionalSecretWithCmekSample +{ + public void CreateRegionalSecretWithCmek( + string projectId = "my-project", + string locationId = "us-central1", + string secretId = "my-secret", + string kmsKeyName = "projects/my-project/locations/us-central1/keyRings/my-keyring/cryptoKeys/my-key") + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the parent resource name. + LocationName location = new LocationName(projectId, locationId); + + // Build the secret with CMEK. + Secret secret = new Secret + { + CustomerManagedEncryption = new CustomerManagedEncryption + { + KmsKeyName = kmsKeyName + } + }; + + // Call the API. + Secret createdSecret = client.CreateSecret(location, secretId, secret); + + // Print information about the created secret. + Console.WriteLine($"Created secret {createdSecret.Name} with CMEK key {kmsKeyName}"); + } +} +// [END secretmanager_create_regional_secret_with_cmek] diff --git a/secretmanager/api/SecretManager.Samples/createSecretWithCmek.cs b/secretmanager/api/SecretManager.Samples/createSecretWithCmek.cs new file mode 100644 index 00000000000..74d2bed7a6e --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/createSecretWithCmek.cs @@ -0,0 +1,58 @@ +/* + * Copyright 2026 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 secretmanager_create_secret_with_cmek] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using System; + +public class CreateSecretWithCmekSample +{ + public void CreateSecretWithCmek( + string projectId = "my-project", + string secretId = "my-secret", + string kmsKeyName = "projects/my-project/locations/global/keyRings/my-keyring/cryptoKeys/my-key") + { + // Create the client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the parent resource name. + ProjectName projectName = new ProjectName(projectId); + + // Build the secret with automatic replication and CMEK. + Secret secret = new Secret + { + Replication = new Replication + { + Automatic = new Replication.Types.Automatic + { + CustomerManagedEncryption = new CustomerManagedEncryption + { + KmsKeyName = kmsKeyName + } + } + } + }; + + // Call the API. + Secret createdSecret = client.CreateSecret(projectName, secretId, secret); + + // Print information about the created secret. + Console.WriteLine($"Created secret {createdSecret.Name} with CMEK key {kmsKeyName}"); + } +} +// [END secretmanager_create_secret_with_cmek] From dee8bc187f7ef7f497d160c4710f0c89fca71295 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Thu, 22 Jan 2026 18:22:40 +0530 Subject: [PATCH 03/19] feat(secretmanager): Adding secret version filter samples --- .../ListSecretVersionsWithFilterTests.cs | 71 +++++++++++++++++++ .../ListSecretVersionsWithFilter.cs | 50 +++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs create mode 100644 secretmanager/api/SecretManager.Samples/ListSecretVersionsWithFilter.cs diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs new file mode 100644 index 00000000000..1de2d88cdde --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs @@ -0,0 +1,71 @@ +/* + * Copyright 2026 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 System.IO; +using System.Collections.Generic; +using Xunit; +using Google.Cloud.SecretManager.V1; + +[Collection(nameof(SecretManagerFixture))] +public class ListSecretVersionsWithFilterTests +{ + private readonly SecretManagerFixture _fixture; + private readonly ListSecretVersionsWithFilterSample _sample; + + public ListSecretVersionsWithFilterTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new ListSecretVersionsWithFilterSample(); + } + + [Fact] + public void ListSecretVersionsWithFilter() + { + // Create the secret resource. + Secret secret = _fixture.CreateSecret(_fixture.RandomId()); + + // Get the SecretName. + SecretName secretName = secret.SecretName; + + // Run the code sample. + SecretVersion secretVersion = _fixture.AddSecretVersion(secret); + _fixture.DisableSecretVersion(secretVersion); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Run the sample code + _sample.ListSecretVersionsWithFilter( + projectId: secretName.ProjectId, + secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Found secret version", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples/ListSecretVersionsWithFilter.cs b/secretmanager/api/SecretManager.Samples/ListSecretVersionsWithFilter.cs new file mode 100644 index 00000000000..0b30c19bb02 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/ListSecretVersionsWithFilter.cs @@ -0,0 +1,50 @@ +/* + * Copyright 2026 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 secretmanager_list_secret_versions_with_filter] + +using Google.Cloud.SecretManager.V1; +using System; + +public class ListSecretVersionsWithFilterSample +{ + public void ListSecretVersionsWithFilter( + string projectId = "my-project", + string secretId = "my-secret" + ) + { + string filterStr = "state:DISABLED"; + // Create the Secret Manager client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name of the parent secret. + SecretName secretName = new SecretName(projectId, secretId); + + // Create the request with filter. + ListSecretVersionsRequest request = new ListSecretVersionsRequest + { + ParentAsSecretName = secretName, + Filter = filterStr + }; + + // List all secret versions with the provided filter. + foreach (SecretVersion version in client.ListSecretVersions(request)) + { + Console.WriteLine($"Found secret version: {version.Name}"); + } + } +} +// [END secretmanager_list_secret_versions_with_filter] From a888e11a9bf3a21e592aa04ef45e3a7aff06a0b5 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Tue, 27 Jan 2026 11:38:22 +0530 Subject: [PATCH 04/19] feat(secretmanager): Adding secrets tags samples --- .../BindTagToRegionalSecretTests.cs | 175 ++++++++++++++++ .../BindTagToSecretTests.cs | 173 ++++++++++++++++ .../DetachRegionalTagBindingTests.cs | 171 ++++++++++++++++ .../DetachTagBindingTests.cs | 185 +++++++++++++++++ .../ListRegionalTagBindingsTests.cs | 169 +++++++++++++++ .../ListTagBindingsTests.cs | 192 ++++++++++++++++++ .../SecretManager.Samples.Tests.csproj | 2 +- .../BindTagToRegionalSecret.cs | 82 ++++++++ .../SecretManager.Samples/BindTagToSecret.cs | 74 +++++++ .../DetachRegionalTagBinding.cs | 85 ++++++++ .../SecretManager.Samples/DetachTagBinding.cs | 80 ++++++++ .../ListRegionalTagBindings.cs | 71 +++++++ .../SecretManager.Samples/ListTagBindings.cs | 61 ++++++ .../SecretManager.Samples.csproj | 1 + 14 files changed, 1520 insertions(+), 1 deletion(-) create mode 100644 secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs create mode 100644 secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs create mode 100644 secretmanager/api/SecretManager.Samples/BindTagToSecret.cs create mode 100644 secretmanager/api/SecretManager.Samples/DetachRegionalTagBinding.cs create mode 100644 secretmanager/api/SecretManager.Samples/DetachTagBinding.cs create mode 100644 secretmanager/api/SecretManager.Samples/ListRegionalTagBindings.cs create mode 100644 secretmanager/api/SecretManager.Samples/ListTagBindings.cs diff --git a/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs new file mode 100644 index 00000000000..8e88c631876 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs @@ -0,0 +1,175 @@ +/* + * Copyright 2026 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.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.IO; +using System.Threading.Tasks; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class BindTagsToRegionalSecretTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly BindTagsToRegionalSecretSample _sample; + private readonly TagKeysClient _tagKeysClient; + private readonly TagValuesClient _tagValuesClient; + private readonly TagBindingsClient _tagBindingsClient; + private string _tagKeyName; + private string _tagValueName; + private string _tagBindingName; + private string _secretId; + + public BindTagsToRegionalSecretTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new BindTagsToRegionalSecretSample(); + _tagKeysClient = TagKeysClient.Create(); + _tagValuesClient = TagValuesClient.Create(); + _tagBindingsClient = TagBindingsClient.Create(); + } + + private void CreateTagKeyAndValue(string projectId) + { + // Generate unique names for tag key and value + string tagKeyShortName = $"test-key-{_fixture.RandomId()}"; + string tagValueShortName = $"test-value-{_fixture.RandomId()}"; + + var createKeyRequest = new CreateTagKeyRequest + { + TagKey = new TagKey + { + Parent = $"projects/{projectId}", + ShortName = tagKeyShortName, + } + }; + + try + { + var createKeyOperation = _tagKeysClient.CreateTagKey(createKeyRequest); + TagKey tagKey = createKeyOperation.PollUntilCompleted().Result; + _tagKeyName = tagKey.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag key: {e.Message}"); + } + + var createValueRequest = new CreateTagValueRequest + { + TagValue = new TagValue + { + Parent = _tagKeyName, + ShortName = tagValueShortName, + } + }; + + try + { + var createValueOperation = _tagValuesClient.CreateTagValue(createValueRequest); + TagValue tagValue = createValueOperation.PollUntilCompleted().Result; + _tagValueName = tagValue.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag value: {e.Message}"); + } + } + + private void CleanupResources() + { + + // Delete the tag binding if it exists + if (!string.IsNullOrEmpty(_tagBindingName)) + { + try + { + var deleteBindingRequest = new DeleteTagBindingRequest { Name = _tagBindingName }; + _tagBindingsClient.DeleteTagBinding(deleteBindingRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag binding: {e.Message}"); + } + } + + // Delete the tag value if it exists + if (!string.IsNullOrEmpty(_tagValueName)) + { + try + { + var deleteValueRequest = new DeleteTagValueRequest { Name = _tagValueName }; + _tagValuesClient.DeleteTagValue(deleteValueRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag value: {e.Message}"); + } + } + + // Delete the tag key if it exists + if (!string.IsNullOrEmpty(_tagKeyName)) + { + try + { + var deleteKeyRequest = new DeleteTagKeyRequest { Name = _tagKeyName }; + _tagKeysClient.DeleteTagKey(deleteKeyRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag key: {e.Message}"); + } + } + } + + [Fact] + public async Task BindsTagToRegionalSecretAsync() + { + // Create a tag key and value for testing + CreateTagKeyAndValue(_fixture.ProjectId); + + SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Call the method being tested + await _sample.BindTagsToRegionalSecretAsync( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Check console output + Assert.Contains($"Created regional secret:", consoleOutput); + Assert.Contains($"Created tag binding:", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean up all resources + _fixture.DeleteSecret(secretName); + CleanupResources(); + + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs new file mode 100644 index 00000000000..5b3ce7ab59c --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs @@ -0,0 +1,173 @@ +/* + * Copyright 2026 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.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.IO; +using System.Threading.Tasks; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class BindTagsToSecretTests +{ + private readonly SecretManagerFixture _fixture; + private readonly BindTagsToSecretSample _sample; + private readonly TagKeysClient _tagKeysClient; + private readonly TagValuesClient _tagValuesClient; + private readonly TagBindingsClient _tagBindingsClient; + private string _tagKeyName; + private string _tagValueName; + private string _tagBindingName; + + public BindTagsToSecretTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new BindTagsToSecretSample(); + _tagKeysClient = TagKeysClient.Create(); + _tagValuesClient = TagValuesClient.Create(); + _tagBindingsClient = TagBindingsClient.Create(); + } + + private void CreateTagKeyAndValue(string projectId) + { + // Generate unique names for tag key and value + string tagKeyShortName = $"test-key-{_fixture.RandomId()}"; + string tagValueShortName = $"test-value-{_fixture.RandomId()}"; + + var createKeyRequest = new CreateTagKeyRequest + { + TagKey = new TagKey + { + Parent = $"projects/{projectId}", + ShortName = tagKeyShortName, + } + }; + + try + { + var createKeyOperation = _tagKeysClient.CreateTagKey(createKeyRequest); + TagKey tagKey = createKeyOperation.PollUntilCompleted().Result; + _tagKeyName = tagKey.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag key: {e.Message}"); + } + + var createValueRequest = new CreateTagValueRequest + { + TagValue = new TagValue + { + Parent = _tagKeyName, + ShortName = tagValueShortName, + } + }; + + try + { + var createValueOperation = _tagValuesClient.CreateTagValue(createValueRequest); + TagValue tagValue = createValueOperation.PollUntilCompleted().Result; + _tagValueName = tagValue.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag value: {e.Message}"); + } + } + + private void CleanupResources() + { + // Delete the tag binding if it exists + if (!string.IsNullOrEmpty(_tagBindingName)) + { + try + { + var deleteBindingRequest = new DeleteTagBindingRequest { Name = _tagBindingName }; + _tagBindingsClient.DeleteTagBinding(deleteBindingRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag binding: {e.Message}"); + } + } + + // Delete the tag value if it exists + if (!string.IsNullOrEmpty(_tagValueName)) + { + try + { + var deleteValueRequest = new DeleteTagValueRequest { Name = _tagValueName }; + _tagValuesClient.DeleteTagValue(deleteValueRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag value: {e.Message}"); + } + } + + // Delete the tag key if it exists + if (!string.IsNullOrEmpty(_tagKeyName)) + { + try + { + var deleteKeyRequest = new DeleteTagKeyRequest { Name = _tagKeyName }; + _tagKeysClient.DeleteTagKey(deleteKeyRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag key: {e.Message}"); + } + } + } + + [Fact] + public async Task BindsTagToSecretAsync() + { + // Create a tag key and value for testing + CreateTagKeyAndValue(_fixture.ProjectId); + + // Generate a unique secret ID + SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + // string secretId = _fixture.RandomId(); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + await _sample.BindTagsToSecretAsync( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Verify the result + + Assert.Contains($"Created secret: ", consoleOutput); + Assert.Contains($"Created Tag Binding", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + _fixture.DeleteSecret(secretName); + CleanupResources(); + + } +}; diff --git a/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs new file mode 100644 index 00000000000..57c5ac692aa --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs @@ -0,0 +1,171 @@ +/* + * Copyright 2026 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](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.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.IO; +using System.Threading.Tasks; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class DetachTagFromRegionalSecretTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly BindTagsToRegionalSecretSample _bindSample; + private readonly DetachTagFromRegionalSecretSample _detachSample; + private readonly ListRegionalSecretTagBindingsSample _listSample; + private readonly TagKeysClient _tagKeysClient; + private readonly TagValuesClient _tagValuesClient; + private string _tagKeyName; + private string _tagValueName; + private string _secretId; + + public DetachTagFromRegionalSecretTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _bindSample = new BindTagsToRegionalSecretSample(); + _detachSample = new DetachTagFromRegionalSecretSample(); + _listSample = new ListRegionalSecretTagBindingsSample(); + _tagKeysClient = TagKeysClient.Create(); + _tagValuesClient = TagValuesClient.Create(); + } + + private void CreateTagKeyAndValue(string projectId) + { + // Generate unique names for tag key and value + string tagKeyShortName = $"test-key-{_fixture.RandomId()}"; + string tagValueShortName = $"test-value-{_fixture.RandomId()}"; + + var createKeyRequest = new CreateTagKeyRequest + { + TagKey = new TagKey + { + Parent = $"projects/{projectId}", + ShortName = tagKeyShortName, + } + }; + + try + { + var createKeyOperation = _tagKeysClient.CreateTagKey(createKeyRequest); + TagKey tagKey = createKeyOperation.PollUntilCompleted().Result; + _tagKeyName = tagKey.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag key: {e.Message}"); + } + + var createValueRequest = new CreateTagValueRequest + { + TagValue = new TagValue + { + Parent = _tagKeyName, + ShortName = tagValueShortName, + } + }; + + try + { + var createValueOperation = _tagValuesClient.CreateTagValue(createValueRequest); + TagValue tagValue = createValueOperation.PollUntilCompleted().Result; + _tagValueName = tagValue.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag value: {e.Message}"); + } + } + + private void CleanupResource() + { + + // Delete the tag value + if (!string.IsNullOrEmpty(_tagValueName)) + { + try + { + var deleteValueRequest = new DeleteTagValueRequest { Name = _tagValueName }; + _tagValuesClient.DeleteTagValue(deleteValueRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag value: {e.Message}"); + } + } + + // Delete the tag key + if (!string.IsNullOrEmpty(_tagKeyName)) + { + try + { + var deleteKeyRequest = new DeleteTagKeyRequest { Name = _tagKeyName }; + _tagKeysClient.DeleteTagKey(deleteKeyRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag key: {e.Message}"); + } + } + } + + [Fact] + public async Task DetachesTagFromRegionalSecret() + { + // Create a tag key and value for testing + CreateTagKeyAndValue(_fixture.ProjectId); + + SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + + // Create a regional secret and bind the tag to it + await _bindSample.BindTagsToRegionalSecretAsync( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + + // Clear the console output from binding + sw.GetStringBuilder().Clear(); + + // Detach the tag + await _detachSample.DetachTagFromRegionalSecretAsync( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Verify the result + Assert.Contains($"Detached tag value {_tagValueName}", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean up all resources + _fixture.DeleteSecret(secretName); + CleanupResource(); + } +}; diff --git a/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs new file mode 100644 index 00000000000..f246a5f957f --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs @@ -0,0 +1,185 @@ +/* + * Copyright 2026 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](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.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class DetachTagTests +{ + private readonly SecretManagerFixture _fixture; + private readonly DetachTagSample _detachSample; + private readonly BindTagsToSecretSample _bindSample; + private readonly TagKeysClient _tagKeysClient; + private readonly TagValuesClient _tagValuesClient; + private readonly TagBindingsClient _tagBindingsClient; + private string _tagKeyName; + private string _tagValueName; + private string _tagBindingName; + private string _secretId; + + public DetachTagTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _detachSample = new DetachTagSample(); + _bindSample = new BindTagsToSecretSample(); + _tagKeysClient = TagKeysClient.Create(); + _tagValuesClient = TagValuesClient.Create(); + _tagBindingsClient = TagBindingsClient.Create(); + } + + private void CreateTagKeyAndValue(string projectId) + { + // Generate unique names for tag key and value + string tagKeyShortName = $"test-key-{_fixture.RandomId()}"; + string tagValueShortName = $"test-value-{_fixture.RandomId()}"; + + var createKeyRequest = new CreateTagKeyRequest + { + TagKey = new TagKey + { + Parent = $"projects/{projectId}", + ShortName = tagKeyShortName, + } + }; + + try + { + var createKeyOperation = _tagKeysClient.CreateTagKey(createKeyRequest); + TagKey tagKey = createKeyOperation.PollUntilCompleted().Result; + _tagKeyName = tagKey.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag key: {e.Message}"); + } + + var createValueRequest = new CreateTagValueRequest + { + TagValue = new TagValue + { + Parent = _tagKeyName, + ShortName = tagValueShortName, + } + }; + + try + { + var createValueOperation = _tagValuesClient.CreateTagValue(createValueRequest); + TagValue tagValue = createValueOperation.PollUntilCompleted().Result; + _tagValueName = tagValue.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag value: {e.Message}"); + } + } + + private void CleanupResources() + { + // Delete the secret that was created + if (!string.IsNullOrEmpty(_secretId)) + { + try + { + SecretManagerServiceClient secretClient = SecretManagerServiceClient.Create(); + secretClient.DeleteSecret(new SecretName(_fixture.ProjectId, _secretId)); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting secret: {e.Message}"); + } + } + + // Delete the tag value if it exists + if (!string.IsNullOrEmpty(_tagValueName)) + { + try + { + var deleteValueRequest = new DeleteTagValueRequest { Name = _tagValueName }; + _tagValuesClient.DeleteTagValue(deleteValueRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag value: {e.Message}"); + } + } + + // Delete the tag key if it exists + if (!string.IsNullOrEmpty(_tagKeyName)) + { + try + { + var deleteKeyRequest = new DeleteTagKeyRequest { Name = _tagKeyName }; + _tagKeysClient.DeleteTagKey(deleteKeyRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag key: {e.Message}"); + } + } + } + + [Fact] + public async Task DetachesTagFromSecret() + { + + // Create a tag key and value for testing + CreateTagKeyAndValue(_fixture.ProjectId); + + SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + + // Use the BindTagsToSecret sample to create a secret and bind a tag to it + await _bindSample.BindTagsToSecretAsync( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + + // Verify the binding exists + SecretManagerServiceClient secretClient = SecretManagerServiceClient.Create(); + string resource = $"//secretmanager.googleapis.com/{secretName}"; + + // Capture console output for the detach operation + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Call the method being tested + await _detachSample.DetachTagAsync( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Verify the output contains expected message + Assert.Contains($"Detached tag value {_tagValueName} from {secretName}", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean up all resources + CleanupResources(); + + } +}; diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs new file mode 100644 index 00000000000..ad466fa89f9 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs @@ -0,0 +1,169 @@ +/* + * Copyright 2026 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](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.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading.Tasks; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class ListRegionalTagBindingsTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly BindTagsToRegionalSecretSample _bindSample; + private readonly ListRegionalSecretTagBindingsSample _listSample; + private readonly TagKeysClient _tagKeysClient; + private readonly TagValuesClient _tagValuesClient; + private readonly TagBindingsClient _tagBindingsClient; + private string _tagKeyName; + private string _tagValueName; + private string _secretId; + + public ListRegionalTagBindingsTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _bindSample = new BindTagsToRegionalSecretSample(); + _listSample = new ListRegionalSecretTagBindingsSample(); + _tagKeysClient = TagKeysClient.Create(); + _tagValuesClient = TagValuesClient.Create(); + _tagBindingsClient = TagBindingsClient.Create(); + } + + private void CreateTagKeyAndValue(string projectId) + { + // Generate unique names for tag key and value + string tagKeyShortName = $"test-key-{_fixture.RandomId()}"; + string tagValueShortName = $"test-value-{_fixture.RandomId()}"; + + var createKeyRequest = new CreateTagKeyRequest + { + TagKey = new TagKey + { + Parent = $"projects/{projectId}", + ShortName = tagKeyShortName, + } + }; + + try + { + var createKeyOperation = _tagKeysClient.CreateTagKey(createKeyRequest); + TagKey tagKey = createKeyOperation.PollUntilCompleted().Result; + _tagKeyName = tagKey.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag key: {e.Message}"); + } + + var createValueRequest = new CreateTagValueRequest + { + TagValue = new TagValue + { + Parent = _tagKeyName, + ShortName = tagValueShortName, + } + }; + + try + { + var createValueOperation = _tagValuesClient.CreateTagValue(createValueRequest); + TagValue tagValue = createValueOperation.PollUntilCompleted().Result; + _tagValueName = tagValue.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag value: {e.Message}"); + } + } + + private void CleanupResources() + { + + // Delete the tag value if it exists + if (!string.IsNullOrEmpty(_tagValueName)) + { + try + { + var deleteValueRequest = new DeleteTagValueRequest { Name = _tagValueName }; + _tagValuesClient.DeleteTagValue(deleteValueRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag value: {e.Message}"); + } + } + + // Delete the tag key if it exists + if (!string.IsNullOrEmpty(_tagKeyName)) + { + try + { + var deleteKeyRequest = new DeleteTagKeyRequest { Name = _tagKeyName }; + _tagKeysClient.DeleteTagKey(deleteKeyRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag key: {e.Message}"); + } + } + } + [Fact] + public async Task ListsRegionalSecretTagBindings() + { + + // Create a tag key and two values for testing + CreateTagKeyAndValue(_fixture.ProjectId); + + SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); + + // Capture console output for the list operation + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Create a regional secret and bind the first tag to it + await _bindSample.BindTagsToRegionalSecretAsync( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + + // Call the method being tested + _listSample.ListRegionalSecretTagBindings( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Verify the output contains expected information + Assert.Contains(secretName.SecretId, consoleOutput); + Assert.Contains($"- Tag Value: {_tagValueName}", consoleOutput); + + //// Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean up all resources + _fixture.DeleteSecret(secretName); + CleanupResources(); + } +}; diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs new file mode 100644 index 00000000000..3f0361f6353 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs @@ -0,0 +1,192 @@ +/* + * Copyright 2026 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](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.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.IO; +using System.Threading.Tasks; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class ListTagBindingsTests +{ + private readonly SecretManagerFixture _fixture; + private readonly ListTagBindingsSample _listSample; + private readonly BindTagsToSecretSample _bindSample; + private readonly TagKeysClient _tagKeysClient; + private readonly TagValuesClient _tagValuesClient; + private readonly TagBindingsClient _tagBindingsClient; + private string _tagKeyName; + private string _tagValueName; + private string _tagBindingName; + private string _secretId; + + public ListTagBindingsTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _listSample = new ListTagBindingsSample(); + _bindSample = new BindTagsToSecretSample(); + _tagKeysClient = TagKeysClient.Create(); + _tagValuesClient = TagValuesClient.Create(); + _tagBindingsClient = TagBindingsClient.Create(); + } + + private void CreateTagKeyAndValue(string projectId) + { + // Generate unique names for tag key and value + string tagKeyShortName = $"test-key-{_fixture.RandomId()}"; + string tagValueShortName = $"test-value-{_fixture.RandomId()}"; + + var createKeyRequest = new CreateTagKeyRequest + { + TagKey = new TagKey + { + Parent = $"projects/{projectId}", + ShortName = tagKeyShortName, + } + }; + + try + { + var createKeyOperation = _tagKeysClient.CreateTagKey(createKeyRequest); + TagKey tagKey = createKeyOperation.PollUntilCompleted().Result; + _tagKeyName = tagKey.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag key: {e.Message}"); + } + + var createValueRequest = new CreateTagValueRequest + { + TagValue = new TagValue + { + Parent = _tagKeyName, + ShortName = tagValueShortName, + } + }; + + try + { + var createValueOperation = _tagValuesClient.CreateTagValue(createValueRequest); + TagValue tagValue = createValueOperation.PollUntilCompleted().Result; + _tagValueName = tagValue.Name; + } + catch (Exception e) + { + throw new Exception($"Error creating tag value: {e.Message}"); + } + } + + private void CleanupResources() + { + // Delete the secret that was created + if (!string.IsNullOrEmpty(_secretId)) + { + try + { + SecretManagerServiceClient secretClient = SecretManagerServiceClient.Create(); + secretClient.DeleteSecret(new SecretName(_fixture.ProjectId, _secretId)); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting secret: {e.Message}"); + } + } + + // Delete the tag binding if it exists + if (!string.IsNullOrEmpty(_tagBindingName)) + { + try + { + var deleteBindingRequest = new DeleteTagBindingRequest { Name = _tagBindingName }; + _tagBindingsClient.DeleteTagBinding(deleteBindingRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag binding: {e.Message}"); + } + } + + // Delete the tag value if it exists + if (!string.IsNullOrEmpty(_tagValueName)) + { + try + { + var deleteValueRequest = new DeleteTagValueRequest { Name = _tagValueName }; + _tagValuesClient.DeleteTagValue(deleteValueRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag value: {e.Message}"); + } + } + + // Delete the tag key if it exists + if (!string.IsNullOrEmpty(_tagKeyName)) + { + try + { + var deleteKeyRequest = new DeleteTagKeyRequest { Name = _tagKeyName }; + _tagKeysClient.DeleteTagKey(deleteKeyRequest).PollUntilCompleted(); + } + catch (Exception e) + { + Console.WriteLine($"Error deleting tag key: {e.Message}"); + } + } + } + + [Fact] + public async Task ListsTagBindingsAsync() + { + // Create a tag key and value for testing + CreateTagKeyAndValue(_fixture.ProjectId); + + SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + + // Use the BindTagsToSecret sample to create a secret and bind a tag to it + await _bindSample.BindTagsToSecretAsync( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + + // Capture console output for the list operation + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Call the method being tested + _listSample.ListTagBindings( + projectId: secretName.ProjectId, + secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Verify the output contains expected information + Assert.Contains(secretName.SecretId, consoleOutput); + Assert.Contains($"- Tag Value: {_tagValueName}", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean up all resources + CleanupResources(); + } +}; diff --git a/secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj b/secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj index 86b1075f5f8..df9c081368f 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj +++ b/secretmanager/api/SecretManager.Samples.Tests/SecretManager.Samples.Tests.csproj @@ -5,7 +5,7 @@ - + diff --git a/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs b/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs new file mode 100644 index 00000000000..bf278ec8f59 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs @@ -0,0 +1,82 @@ +/* + * Copyright 2026 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 secretmanager_bind_tags_to_regional_secret] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.Threading.Tasks; + +public class BindTagsToRegionalSecretSample +{ + + public async Task BindTagsToRegionalSecretAsync( + string projectId = "my-project", + string locationId = "us-central1", + string secretId = "my-secret", + string tagValue = "tagValues/123456789012") + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the parent resource name. + LocationName location = new LocationName(projectId, locationId); + + // Build the secret. + Secret secret = new Secret(); + + // Call the API to create the secret. + Secret createdSecret = client.CreateSecret(location, secretId, secret); + + // Print the new secret name. + Console.WriteLine($"Created regional secret: {createdSecret.Name}"); + string resourceManagerApiEndpoint = $"{locationId}-cloudresourcemanager.googleapis.com"; + + // Create the resource manager client + TagBindingsClientBuilder tagBindingsClientBuilder = new TagBindingsClientBuilder + { + Endpoint = resourceManagerApiEndpoint + }; + + TagBindingsClient resourceManagerClient = tagBindingsClientBuilder.Build(); + + // Create the tag binding + CreateTagBindingRequest request = new CreateTagBindingRequest + { + TagBinding = new TagBinding + { + Parent = $"//secretmanager.googleapis.com/{createdSecret.Name}", + TagValue = tagValue + } + }; + + // Create the tag binding + var operation = await resourceManagerClient.CreateTagBindingAsync(request); + + // Wait for the operation to complete + await operation.PollUntilCompletedAsync(); + + // Print the tag binding. + Console.WriteLine($"Created tag binding: {createdSecret.Name}"); + + } +} +// [END secretmanager_bind_tags_to_regional_secret] diff --git a/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs b/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs new file mode 100644 index 00000000000..7089204661a --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs @@ -0,0 +1,74 @@ +/* + * Copyright 2026 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 secretmanager_bind_tags_to_secret] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.Threading.Tasks; + +public class BindTagsToSecretSample +{ + public async Task BindTagsToSecretAsync( + string projectId = "my-project", + string secretId = "my-secret", + string tagValue = "tagValues/123456789012") + { + // Create the Secret Manager client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the parent resource name. + ProjectName projectName = new ProjectName(projectId); + + // Build the secret. + Secret secret = new Secret + { + Replication = new Replication + { + Automatic = new Replication.Types.Automatic(), + }, + }; + + // Call the API. + Secret createdSecret = client.CreateSecret(projectName, secretId, secret); + + // Print the new secret name. + Console.WriteLine($"Created secret: {createdSecret.Name}"); + + // Create the resource manager client. + TagBindingsClient resourceManagerClient = TagBindingsClient.Create(); + + // Format the resource name for the secret. + string resourceName = $"//secretmanager.googleapis.com/{createdSecret.Name}"; + + // Create the tag binding. + TagBinding tagBinding = new TagBinding + { + Parent = resourceName, + TagValue = tagValue + }; + + // Create the tag binding and wait for the operation to complete. + var operation = await resourceManagerClient.CreateTagBindingAsync(tagBinding); + await operation.PollUntilCompletedAsync(); + + Console.WriteLine($"Created Tag Binding: {secret.Name}"); + + } +} +// [END secretmanager_bind_tags_to_secret] diff --git a/secretmanager/api/SecretManager.Samples/DetachRegionalTagBinding.cs b/secretmanager/api/SecretManager.Samples/DetachRegionalTagBinding.cs new file mode 100644 index 00000000000..652871420b6 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/DetachRegionalTagBinding.cs @@ -0,0 +1,85 @@ +/* + * Copyright 2026 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 secretmanager_detach_tag_from_regional_secret] + +using Google.Cloud.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.Linq; +using System.Threading.Tasks; + +public class DetachTagFromRegionalSecretSample +{ + public async Task DetachTagFromRegionalSecretAsync( + string projectId = "my-project", + string locationId = "us-central1", + string secretId = "my-secret", + string tagValue = "tagValues/123456789012") + { + // Set up the endpoint for the regional resource manager + string rmEndpoint = $"{locationId}-cloudresourcemanager.googleapis.com"; + + // Create the Tag Bindings client with the regional endpoint + TagBindingsClient tagBindingsClient = new TagBindingsClientBuilder + { + Endpoint = rmEndpoint + }.Build(); + + // Build the secret name + string name = $"projects/{projectId}/locations/{locationId}/secrets/{secretId}"; + + // Format the resource name for the secret + string resourceName = $"//secretmanager.googleapis.com/{name}"; + + // List all tag bindings for the secret to find the one to detach + ListTagBindingsRequest listRequest = new ListTagBindingsRequest + { + Parent = resourceName + }; + + // Find the binding that matches the tag value + TagBinding bindingToDelete = null; + foreach (var binding in tagBindingsClient.ListTagBindings(listRequest)) + { + if (binding.TagValue == tagValue) + { + bindingToDelete = binding; + break; + } + } + + if (bindingToDelete == null) + { + Console.WriteLine($"No tag binding found for tag value {tagValue} on {name}"); + return; + } + + // Create the delete request + DeleteTagBindingRequest deleteRequest = new DeleteTagBindingRequest + { + Name = bindingToDelete.Name + }; + + // Delete the tag binding and wait for the operation to complete + var operation = await tagBindingsClient.DeleteTagBindingAsync(deleteRequest); + await operation.PollUntilCompletedAsync(); + + Console.WriteLine($"Detached tag value {tagValue} from {name}"); + + } +} +// [END secretmanager_detach_tag_from_regional_secret] diff --git a/secretmanager/api/SecretManager.Samples/DetachTagBinding.cs b/secretmanager/api/SecretManager.Samples/DetachTagBinding.cs new file mode 100644 index 00000000000..501ce3fcc94 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/DetachTagBinding.cs @@ -0,0 +1,80 @@ +/* + * Copyright 2026 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 secretmanager_detach_tag] + +using Google.Cloud.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.Threading.Tasks; + +public class DetachTagSample +{ + public async Task DetachTagAsync( + string projectId = "my-project", + string secretId = "my-secret", + string tagValue = "tagValues/123456789012") + { + // Create the Resource Manager client. + TagBindingsClient rmClient = TagBindingsClient.Create(); + + // Create the Secret Manager client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name of the parent secret. + SecretName secretName = new SecretName(projectId, secretId); + + // Format the parent resource for tag bindings. + string parent = $"//secretmanager.googleapis.com/{secretName}"; + + // Find the binding name for the given tag value + string bindingName = null; + ListTagBindingsRequest listRequest = new ListTagBindingsRequest + { + Parent = parent + }; + + // Search for the binding with the specified tag value + var bindings = rmClient.ListTagBindings(listRequest); + foreach (var binding in bindings) + { + if (binding.TagValue == tagValue) + { + bindingName = binding.Name; + break; + } + } + + if (bindingName == null) + { + Console.WriteLine($"Tag binding for value {tagValue} not found on {secretName}."); + return; + } + + // Delete the tag binding + DeleteTagBindingRequest deleteRequest = new DeleteTagBindingRequest + { + Name = bindingName + }; + + // Delete the binding and wait for the operation to complete + var operation = await rmClient.DeleteTagBindingAsync(deleteRequest); + await operation.PollUntilCompletedAsync(); + + Console.WriteLine($"Detached tag value {tagValue} from {secretName}"); + } +} +// [END secretmanager_detach_tag] diff --git a/secretmanager/api/SecretManager.Samples/ListRegionalTagBindings.cs b/secretmanager/api/SecretManager.Samples/ListRegionalTagBindings.cs new file mode 100644 index 00000000000..de30dacfdf3 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/ListRegionalTagBindings.cs @@ -0,0 +1,71 @@ +/* + * Copyright 2026 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 secretmanager_list_regional_secret_tag_bindings] + +using Google.Cloud.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +public class ListRegionalSecretTagBindingsSample +{ + public void ListRegionalSecretTagBindings( + string projectId = "my-project", + string locationId = "us-central1", + string secretId = "my-regional-secret") + { + // Set up the endpoint for the regional resource manager + string rmEndpoint = $"{locationId}-cloudresourcemanager.googleapis.com"; + + // Create the Tag Bindings client with the regional endpoint + TagBindingsClient tagBindingsClient = new TagBindingsClientBuilder + { + Endpoint = rmEndpoint + }.Build(); + + string name = $"projects/{projectId}/locations/{locationId}/secrets/{secretId}"; + + // Format the parent resource for the tag bindings request + string parent = $"//secretmanager.googleapis.com/{name}"; + + // List the tag bindings + Console.WriteLine($"Tag bindings for {name}:"); + bool foundBindings = false; + + // Use the ListTagBindings method to get all tag bindings + ListTagBindingsRequest request = new ListTagBindingsRequest + { + Parent = parent + }; + + // Iterate through the results + foreach (var binding in tagBindingsClient.ListTagBindings(request)) + { + Console.WriteLine($"- Tag Value: {binding.TagValue}"); + foundBindings = true; + } + + if (!foundBindings) + { + Console.WriteLine($"No tag bindings found for {name}."); + } + + } +} +// [END secretmanager_list_regional_secret_tag_bindings] diff --git a/secretmanager/api/SecretManager.Samples/ListTagBindings.cs b/secretmanager/api/SecretManager.Samples/ListTagBindings.cs new file mode 100644 index 00000000000..b2beca84e42 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/ListTagBindings.cs @@ -0,0 +1,61 @@ +/* + * Copyright 2026 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 secretmanager_list_tag_bindings] + +using Google.Cloud.ResourceManager.V3; +using Google.Cloud.SecretManager.V1; +using System; + +public class ListTagBindingsSample +{ + public void ListTagBindings( + string projectId = "my-project", + string secretId = "my-secret") + { + // Create the Resource Manager client. + TagBindingsClient client = TagBindingsClient.Create(); + + // Build the resource name of the parent secret. + SecretName secretName = new SecretName(projectId, secretId); + + // Format the parent resource for tag bindings. + string parent = $"//secretmanager.googleapis.com/{secretName}"; + + // Create the request. + ListTagBindingsRequest request = new ListTagBindingsRequest + { + Parent = parent + }; + + // List all tag bindings. + var bindings = client.ListTagBindings(request); + bool foundBindings = false; + + Console.WriteLine($"Tag bindings for {secretName}:"); + foreach (var binding in bindings) + { + Console.WriteLine($"- Tag Value: {binding.TagValue}"); + foundBindings = true; + } + + if (!foundBindings) + { + Console.WriteLine($"No tag bindings found for {secretName}."); + } + } +} +// [END secretmanager_list_tag_bindings] diff --git a/secretmanager/api/SecretManager.Samples/SecretManager.Samples.csproj b/secretmanager/api/SecretManager.Samples/SecretManager.Samples.csproj index 85fcc1a2b63..33887a8a06d 100644 --- a/secretmanager/api/SecretManager.Samples/SecretManager.Samples.csproj +++ b/secretmanager/api/SecretManager.Samples/SecretManager.Samples.csproj @@ -5,6 +5,7 @@ + From d21d2a7d4616ef7f07bd80ef8656874b046aaa9b Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Tue, 27 Jan 2026 12:46:21 +0530 Subject: [PATCH 05/19] feat(secretmanager): Adding secrets expiration samples --- ...CreateRegionalSecretWithExpirationTests.cs | 66 +++++++++++++++++++ .../CreateSecretWithExpirationTests.cs | 63 ++++++++++++++++++ .../DeleteRegionalSecretExpirationTests.cs | 64 ++++++++++++++++++ .../DeleteSecretExpirationTests.cs | 65 ++++++++++++++++++ .../UpdateRegionalSecretExpirationTests.cs | 65 ++++++++++++++++++ .../UpdateSecretExpirationTests.cs | 64 ++++++++++++++++++ .../CreateRegionalSecretWithExpiration.cs | 59 +++++++++++++++++ .../CreateSecretWithExpiration.cs | 56 ++++++++++++++++ .../DeleteRegionalSecretExpiration.cs | 56 ++++++++++++++++ .../DeleteSecretExpiration.cs | 49 ++++++++++++++ .../UpdateRegionalSecretExpiration.cs | 61 +++++++++++++++++ .../UpdateSecretExpiration.cs | 55 ++++++++++++++++ 12 files changed, 723 insertions(+) create mode 100644 secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs create mode 100644 secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithExpiration.cs create mode 100644 secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs create mode 100644 secretmanager/api/SecretManager.Samples/DeleteRegionalSecretExpiration.cs create mode 100644 secretmanager/api/SecretManager.Samples/DeleteSecretExpiration.cs create mode 100644 secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs create mode 100644 secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.cs diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs new file mode 100644 index 00000000000..cdd2c18d385 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs @@ -0,0 +1,66 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class CreateRegionalSecretWithExpireTimeTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly CreateRegionalSecretWithExpireTimeSample _sample; + + public CreateRegionalSecretWithExpireTimeTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new CreateRegionalSecretWithExpireTimeSample(); + } + + [Fact] + public void CreatesRegionalSecretWithExpireTime() + { + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Get the SecretName from the set ProjectId & LocationId. + SecretName secretName = SecretName.FromProjectLocationSecret( + _fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); + + // Run the code sample. + _sample.CreateRegionalSecretWithExpireTime( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + locationId: secretName.LocationId); + + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Created secret", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs new file mode 100644 index 00000000000..51164855612 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs @@ -0,0 +1,63 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class CreateSecretWithExpirationTests +{ + private readonly SecretManagerFixture _fixture; + private readonly CreateSecretWithExpirationSample _sample; + + public CreateSecretWithExpirationTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new CreateSecretWithExpirationSample(); + } + + [Fact] + public void CreatesSecretsWithExpiration() + { + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Get the SecretName to create Secret. + SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + + // Create the secret with expiration. + _sample.CreateSecretWithExpiration( + projectId: secretName.ProjectId, secretId: secretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Created secret", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs new file mode 100644 index 00000000000..da866f57311 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs @@ -0,0 +1,64 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class DeleteRegionalSecretExpirationTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly DeleteRegionalSecretExpirationSample _deleteSample; + + public DeleteRegionalSecretExpirationTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _deleteSample = new DeleteRegionalSecretExpirationSample(); + } + + [Fact] + public void DeletesRegionalSecretExpiration() + { + Secret initialSecret = _fixture.CreateSecretWithExpireTime(); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Delete the expiration time + _deleteSample.DeleteRegionalSecretExpiration( + projectId: initialSecret.SecretName.ProjectId, + secretId: initialSecret.SecretName.SecretId, + locationId: initialSecret.SecretName.LocationId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Removed expiration from secret", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret + _fixture.DeleteSecret(initialSecret.SecretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs new file mode 100644 index 00000000000..6c2cfc504d5 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs @@ -0,0 +1,65 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class DeleteSecretExpirationTests +{ + private readonly SecretManagerFixture _fixture; + private readonly DeleteSecretExpirationSample _deleteSample; + + public DeleteSecretExpirationTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _deleteSample = new DeleteSecretExpirationSample(); + } + + [Fact] + public void DeletesSecretExpiration() + { + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Get the SecretName to create Secret. + SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + + // First, create a secret with an expiration time + Secret secret = _fixture.CreateSecretWithExpiration(); + + // Delete the expiration time + _deleteSample.DeleteSecretExpiration( + projectId: secret.SecretName.ProjectId, secretId: secret.SecretName.SecretId); + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Removed expiration from secret", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean up the created secret + _fixture.DeleteSecret(secretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs new file mode 100644 index 00000000000..0ce3c356912 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs @@ -0,0 +1,65 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(RegionalSecretManagerFixture))] +public class UpdateRegionalSecretExpirationTests +{ + private readonly RegionalSecretManagerFixture _fixture; + private readonly UpdateRegionalSecretExpirationSample _updateSample; + + public UpdateRegionalSecretExpirationTests(RegionalSecretManagerFixture fixture) + { + _fixture = fixture; + _updateSample = new UpdateRegionalSecretExpirationSample(); + } + + [Fact] + public void UpdatesRegionalSecretExpiration() + { + + // First, create a secret with initial expiration time (1 hour) + Secret initialSecret = _fixture.CreateSecretWithExpireTime(); + + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Update the secret to have a 2-hour expiration + _updateSample.UpdateRegionalSecretExpiration( + projectId: initialSecret.SecretName.ProjectId, + secretId: initialSecret.SecretName.SecretId, + locationId: initialSecret.SecretName.LocationId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Updated secret ", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean the created secret + _fixture.DeleteSecret(initialSecret.SecretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs new file mode 100644 index 00000000000..4c562fd6941 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs @@ -0,0 +1,64 @@ +/* + * Copyright 2026 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.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; +using System.IO; +using Xunit; + +[Collection(nameof(SecretManagerFixture))] +public class UpdateSecretExpirationTests +{ + private readonly SecretManagerFixture _fixture; + private readonly UpdateSecretExpirationSample _sample; + + public UpdateSecretExpirationTests(SecretManagerFixture fixture) + { + _fixture = fixture; + _sample = new UpdateSecretExpirationSample(); + } + + [Fact] + public void UpdatesSecretExpiration() + { + // Capture console output + StringWriter sw = new StringWriter(); + Console.SetOut(sw); + + // Create a new secret with no expiration time + Secret secret = _fixture.CreateSecretWithExpiration(); + + // Update the secret with an expiration time + _sample.UpdateSecretExpiration( + projectId: secret.SecretName.ProjectId, + secretId: secret.SecretName.SecretId); + + // Get the console output + string consoleOutput = sw.ToString().Trim(); + + // Assert that the output contains the expected message + Assert.Contains($"Updated secret", consoleOutput); + + // Reset console + var standardOutput = new StreamWriter(Console.OpenStandardOutput()); + standardOutput.AutoFlush = true; + Console.SetOut(standardOutput); + + // Clean up the created secret + _fixture.DeleteSecret(secret.SecretName); + } +} diff --git a/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithExpiration.cs b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithExpiration.cs new file mode 100644 index 00000000000..075b7ce9894 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithExpiration.cs @@ -0,0 +1,59 @@ +/* + * 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 secretmanager_create_regional_secret_with_expire_time] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class CreateRegionalSecretWithExpireTimeSample +{ + public void CreateRegionalSecretWithExpireTime( + string projectId = "my-project", + string secretId = "my-secret-with-expiry", + string locationId = "us-central1") + { + // Set expiration time to 1 hour from now + DateTime expireTime = DateTime.UtcNow.AddHours(1); + + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the parent resource name. + LocationName location = new LocationName(projectId, locationId); + + // Convert DateTime to Timestamp + Timestamp timestamp = Timestamp.FromDateTime(expireTime); + + // Build the secret with expiration time + Secret secret = new Secret + { + ExpireTime = timestamp + }; + + // Call the API. + Secret createdSecret = client.CreateSecret(location, secretId, secret); + + Console.WriteLine($"Created secret {createdSecret.SecretName} with expiration time {expireTime}"); + + } +} +// [END secretmanager_create_regional_secret_with_expire_time] diff --git a/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs b/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs new file mode 100644 index 00000000000..70c7e94b861 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs @@ -0,0 +1,56 @@ +/* + * Copyright 2026 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 secretmanager_create_secret_with_expiration] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class CreateSecretWithExpirationSample +{ + public void CreateSecretWithExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiry") + { + // Calculate expiration time (1 hour from now) + DateTime expireTime = DateTime.UtcNow.AddHours(1); + + // Create the client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the parent resource name. + ProjectName projectName = new ProjectName(projectId); + + // Convert DateTime to Timestamp + Timestamp timestamp = Timestamp.FromDateTime(expireTime.ToUniversalTime()); + + // Build the secret with automatic replication and expiration time. + Secret secret = new Secret + { + Replication = new Replication + { + Automatic = new Replication.Types.Automatic(), + }, + ExpireTime = timestamp + }; + + // Call the API. + Secret createdSecret = client.CreateSecret(projectName, secretId, secret); + + Console.WriteLine($"Created secret {createdSecret.SecretName} with expiration time {expireTime}"); + } +} +// [END secretmanager_create_secret_with_expiration] diff --git a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretExpiration.cs new file mode 100644 index 00000000000..a875176feba --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretExpiration.cs @@ -0,0 +1,56 @@ +/* + * Copyright 2026 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 secretmanager_delete_regional_secret_expiration] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class DeleteRegionalSecretExpirationSample +{ + + public void DeleteRegionalSecretExpiration( + string projectId = "my-project", + string secretId = "my-secret", + string locationId = "us-central1") + { + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the secret name + SecretName secretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId); + + // Build the secret with no ExpireTime set (which will clear it) + Secret secret = new Secret + { + SecretName = secretName, + }; + + // Build the field mask for the fields to update + FieldMask updateMask = new FieldMask { Paths = { "expire_time" } }; + + // Update the secret to remove the expiration time + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + Console.WriteLine($"Removed expiration from secret {updatedSecret.SecretName}"); + + } +} +// [END secretmanager_delete_regional_secret_expiration] diff --git a/secretmanager/api/SecretManager.Samples/DeleteSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/DeleteSecretExpiration.cs new file mode 100644 index 00000000000..6306f602bb3 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/DeleteSecretExpiration.cs @@ -0,0 +1,49 @@ +/* + * Copyright 2026 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 secretmanager_delete_secret_expiration] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class DeleteSecretExpirationSample +{ + public void DeleteSecretExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiration") + { + // Create the client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name of the secret. + SecretName secretName = new SecretName(projectId, secretId); + + // Create the secret with the expire_time field explicitly set to null + Secret secret = new Secret + { + SecretName = secretName, + // ExpireTime is not set, which will clear it + }; + + // Create the update mask for the fields to update + FieldMask updateMask = new FieldMask { Paths = { "expire_time" } }; + + // Update the secret + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + Console.WriteLine($"Removed expiration from secret {updatedSecret.SecretName}"); + } +} +// [END secretmanager_delete_secret_expiration] diff --git a/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs new file mode 100644 index 00000000000..28993161332 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs @@ -0,0 +1,61 @@ +/* + * Copyright 2026 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 secretmanager_update_regional_secret_expiration] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class UpdateRegionalSecretExpirationSample +{ + public void UpdateRegionalSecretExpiration( + string projectId = "my-project", + string secretId = "my-secret", + string locationId = "us-central1") + { + // Set new expiration time to 2 hours from now + DateTime newExpireTime = DateTime.UtcNow.AddHours(2); + + // Create the Regional Secret Manager Client. + SecretManagerServiceClient client = new SecretManagerServiceClientBuilder + { + Endpoint = $"secretmanager.{locationId}.rep.googleapis.com" + }.Build(); + + // Build the secret name + SecretName secretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId); + + // Convert DateTime to Timestamp + Timestamp timestamp = Timestamp.FromDateTime(newExpireTime); + + // Build the secret with the new expiration time + Secret secret = new Secret + { + SecretName = secretName, + ExpireTime = timestamp + }; + + // Build the field mask for the fields to update + FieldMask updateMask = new FieldMask { Paths = { "expire_time" } }; + + // Update the secret + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + Console.WriteLine($"Updated secret {updatedSecret.SecretName} expiration time to {newExpireTime}"); + } +} +// [END secretmanager_update_regional_secret_expiration] diff --git a/secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.cs new file mode 100644 index 00000000000..ad393b87669 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.cs @@ -0,0 +1,55 @@ +/* + * Copyright 2026 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 secretmanager_update_secret_expiration] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class UpdateSecretExpirationSample +{ + public void UpdateSecretExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiration") + { + // Calculate new expiration time (2 hours from now) + DateTime newExpireTime = DateTime.UtcNow.AddHours(2); + + // Create the client. + SecretManagerServiceClient client = SecretManagerServiceClient.Create(); + + // Build the resource name of the secret. + SecretName secretName = new SecretName(projectId, secretId); + + // Convert DateTime to Timestamp + Timestamp timestamp = Timestamp.FromDateTime(newExpireTime); + + // Create the secret to update with the new expiration time + Secret secret = new Secret + { + SecretName = secretName, + ExpireTime = timestamp + }; + + // Create the update mask for the fields to update + FieldMask updateMask = new FieldMask { Paths = { "expire_time" } }; + + // Update the secret + Secret updatedSecret = client.UpdateSecret(secret, updateMask); + + Console.WriteLine($"Updated secret {updatedSecret.SecretName} expiration time to {newExpireTime}"); + } +} +// [END secretmanager_update_secret_expiration] From 5832e342b72093d9cd3f030076d72bc017b8bc63 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Tue, 27 Jan 2026 15:13:03 +0530 Subject: [PATCH 06/19] feat(secretmanager): Update labels annotations samples --- .../DeleteRegionalSecretAnnotationTests.cs | 17 ++--------------- .../DeleteRegionalSecretLabelTests.cs | 18 ++---------------- .../DeleteSecretAnnotationTests.cs | 18 +++--------------- .../DeleteSecretLabelTests.cs | 18 +++--------------- .../EditSecretLabelTests.cs | 17 ++--------------- .../DeleteRegionalSecretAnnotation.cs | 3 ++- .../DeleteRegionalSecretLabel.cs | 3 ++- .../DeleteSecretAnnotation.cs | 3 ++- .../SecretManager.Samples/DeleteSecretLabel.cs | 3 ++- .../SecretManager.Samples/EditSecretLabel.cs | 3 ++- 10 files changed, 22 insertions(+), 81 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs index 8e5277e51b5..9e167638e08 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs @@ -45,26 +45,13 @@ public void DeleteRegionalSecretAnnotation() Assert.NotEmpty(secret.Annotations); Assert.True(secret.Annotations.ContainsKey(annotationKey)); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Run the sample code to delete the annotation - _sample.DeleteRegionalSecretAnnotation( + Secret result = _sample.DeleteRegionalSecretAnnotation( projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected messages - Assert.Contains($"Updated secret:", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + Assert.Empty(result.Annotations); // Clean the created secret. _fixture.DeleteSecret(secretName); diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs index 010428401d0..1559a2680d3 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs @@ -40,28 +40,14 @@ public void DeleteRegionalSecretLabel() // Verify the secret has labels Assert.NotEmpty(secret.Labels); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Run the sample code to delete all labels - _sample.DeleteRegionalSecretLabel( + Secret result = _sample.DeleteRegionalSecretLabel( projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected messages - Assert.Contains($"Updated secret:", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); - + Assert.Empty(result.Labels); // Clean the created secret. _fixture.DeleteSecret(secretName); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs index a82f9cf0ee1..b31004672f6 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs @@ -40,23 +40,11 @@ public void DeleteSecretAnnotation() // Get the secretName from the created secret. SecretName secretName = secret.SecretName; - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Call the code sample function to delete the label - _sample.DeleteSecretAnnotation( + Secret result = _sample.DeleteSecretAnnotation( projectId: secretName.ProjectId, secretId: secretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains("Updated secret:", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + // Assert that the label is deleted. + Assert.Empty(result.Annotations); } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs index f2805cb536c..92127ec5c0f 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs @@ -40,23 +40,11 @@ public void DeleteSecretLabel() // Get the secretName from the created secret. SecretName secretName = secret.SecretName; - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Call the code sample function to delete the label - _sample.DeleteSecretLabel( + Secret result = _sample.DeleteSecretLabel( projectId: secretName.ProjectId, secretId: secretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains("Updated secret:", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + // Assert that the label is deleted. + Assert.Empty(result.Labels); } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs index 18b246d7216..6da6560a7dd 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs @@ -37,23 +37,10 @@ public void EditSecretLabel() // Get the SecretName to create Secret. SecretName secretName = _fixture.Secret.SecretName; - - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Call the code sample function. - _sample.EditSecretLabel( + Secret result = _sample.EditSecretLabel( projectId: secretName.ProjectId, secretId: secretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - Assert.Contains($"Updated secret: ", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + Assert.Equal("my-label-value", result.Labels["my-label-key"]); } } diff --git a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs index 4045f2c66ae..db4174c05c9 100644 --- a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs +++ b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs @@ -22,7 +22,7 @@ public class DeleteRegionalSecretAnnotationSample { - public void DeleteRegionalSecretAnnotation( + public Secret DeleteRegionalSecretAnnotation( string projectId = "my-project", string locationId = "my-location", string secretId = "my-secret") @@ -50,6 +50,7 @@ public void DeleteRegionalSecretAnnotation( // Print the new secret name. Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + return updatedSecret; } } // [END secretmanager_delete_regional_secret_annotation] diff --git a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs index e11906c1395..9a53e561905 100644 --- a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs +++ b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs @@ -22,7 +22,7 @@ public class DeleteRegionalSecretLabelSample { - public void DeleteRegionalSecretLabel( + public Secret DeleteRegionalSecretLabel( string projectId = "my-project", string locationId = "my-location", string secretId = "my-secret") @@ -50,6 +50,7 @@ public void DeleteRegionalSecretLabel( // Print the new secret name. Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + return updatedSecret; } } // [END secretmanager_delete_regional_secret_label] diff --git a/secretmanager/api/SecretManager.Samples/DeleteSecretAnnotation.cs b/secretmanager/api/SecretManager.Samples/DeleteSecretAnnotation.cs index 165288d5e17..778f32b2c3c 100644 --- a/secretmanager/api/SecretManager.Samples/DeleteSecretAnnotation.cs +++ b/secretmanager/api/SecretManager.Samples/DeleteSecretAnnotation.cs @@ -22,7 +22,7 @@ public class DeleteSecretAnnotationSample { - public void DeleteSecretAnnotation( + public Secret DeleteSecretAnnotation( string projectId = "my-project", string secretId = "my-secret") { // Create the client. @@ -45,6 +45,7 @@ public void DeleteSecretAnnotation( // Print the new secret name. Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + return updatedSecret; } } // [END secretmanager_delete_secret_annotation] diff --git a/secretmanager/api/SecretManager.Samples/DeleteSecretLabel.cs b/secretmanager/api/SecretManager.Samples/DeleteSecretLabel.cs index b25307f7edb..1be1d78d131 100644 --- a/secretmanager/api/SecretManager.Samples/DeleteSecretLabel.cs +++ b/secretmanager/api/SecretManager.Samples/DeleteSecretLabel.cs @@ -22,7 +22,7 @@ public class DeleteSecretLabelSample { - public void DeleteSecretLabel( + public Secret DeleteSecretLabel( string projectId = "my-project", string secretId = "my-secret") { // Create the client. @@ -45,6 +45,7 @@ public void DeleteSecretLabel( // Print the new secret name. Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + return updatedSecret; } } // [END secretmanager_delete_secret_label] diff --git a/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs b/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs index 5d0281949e3..808f91ad857 100644 --- a/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs +++ b/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs @@ -23,7 +23,7 @@ public class EditSecretLabelSample { - public void EditSecretLabel( + public Secret EditSecretLabel( string projectId = "my-project", string secretId = "my-secret") { string labelKey = "my-label-key"; @@ -50,6 +50,7 @@ public void EditSecretLabel( // Print the new secret name. Console.WriteLine($"Updated secret: {updatedSecret.Name}"); + return updatedSecret; } } // [END secretmanager_edit_secret_label] From 7b77ac3c5b6ee4d55c5ea5fa62ab8b6c9b1b3018 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Tue, 27 Jan 2026 15:50:07 +0530 Subject: [PATCH 07/19] feat(secretmanager): Update cmek filter samples --- .../ListSecretVersionsWithFilterTests.cs | 27 ++++++++----------- .../ListSecretsWithFilterTests.cs | 24 +++++++---------- .../createRegionalSecretWithCmekTests.cs | 17 ++---------- .../createSecretWithCmekTests.cs | 18 ++----------- .../ListSecretVersionsWithFilter.cs | 6 ++++- .../ListSecretsWithFilter.cs | 8 +++++- .../createRegionalSecretWithCmek.cs | 3 ++- .../createSecretWithCmek.cs | 3 ++- 8 files changed, 41 insertions(+), 65 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs index 1de2d88cdde..cd50cced3e8 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs @@ -45,25 +45,20 @@ public void ListSecretVersionsWithFilter() SecretVersion secretVersion = _fixture.AddSecretVersion(secret); _fixture.DisableSecretVersion(secretVersion); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Run the sample code - _sample.ListSecretVersionsWithFilter( - projectId: secretName.ProjectId, - secretId: secretName.SecretId); - - // Get the console output - string consoleOutput = sw.ToString().Trim(); + IList versions = _sample.ListSecretVersionsWithFilter( + projectId: secretName.ProjectId, + secretId: secretName.SecretId); - // Assert that the output contains the expected message - Assert.Contains($"Found secret version", consoleOutput); + // Verify we got results + Assert.NotNull(versions); + Assert.NotEmpty(versions); - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + // Verify only disabled versions are returned + foreach (var version in versions) + { + Assert.Equal(SecretVersion.Types.State.Disabled, version.State); + } // Clean the created secret. _fixture.DeleteSecret(secretName); diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs index 317751fdcb2..638af81edab 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs @@ -19,6 +19,7 @@ using System.Collections.Generic; using Xunit; using Google.Cloud.SecretManager.V1; +using System.Linq; [Collection(nameof(SecretManagerFixture))] public class ListSecretsWithFilterTests @@ -37,24 +38,19 @@ public void ListsSecretsWithFilter() { Secret secret = _fixture.CreateSecret(_fixture.RandomId()); SecretName secretName = secret.SecretName; - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Run the sample code - _sample.ListSecretsWithFilter( - projectId: _fixture.ProjectId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); + IList secrets = _sample.ListSecretsWithFilter( + projectId: _fixture.ProjectId); - // Assert that the output contains the created secret - Assert.Contains(secretName.ToString(), consoleOutput); + // Verify we got results + Assert.NotNull(secrets); + Assert.NotEmpty(secrets); + + // Verify our specific secret is in the results + bool foundSecret = secrets.Any(s => s.SecretName.SecretId == secretName.SecretId); + Assert.True(foundSecret, $"The secret {secretName.SecretId} with label my-label-key=my-label-value should be in the results"); - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); _fixture.DeleteSecret(secretName); } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs b/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs index c6eaa0822d2..d080c782660 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs @@ -43,27 +43,14 @@ public void CreatesRegionalSecretWithCmek() // Get the SecretName from the set ProjectId & LocationId. SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Create the regional secret with CMEK. - _sample.CreateRegionalSecretWithCmek( + Secret result = _sample.CreateRegionalSecretWithCmek( projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId, kmsKeyName: _fixture.KmsKeyName); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains($"Created secret ", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + Assert.Equal(result.CustomerManagedEncryption.KmsKeyName, _fixture.KmsKeyName); // Clean the created secret. _fixture.DeleteSecret(secretName); diff --git a/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs b/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs index 9c5d0242f16..06dc894b282 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs @@ -43,27 +43,13 @@ public void CreatesSecretWithCmek() // Get the SecretName to create Secret. SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Create the secret with CMEK. - _sample.CreateSecretWithCmek( + Secret result = _sample.CreateSecretWithCmek( projectId: secretName.ProjectId, secretId: secretName.SecretId, kmsKeyName: _fixture.KmsKeyName); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains($"Created secret", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); - + Assert.Equal(result.Replication.Automatic.CustomerManagedEncryption.KmsKeyName, _fixture.KmsKeyName); // Clean the created secret. _fixture.DeleteSecret(secretName); diff --git a/secretmanager/api/SecretManager.Samples/ListSecretVersionsWithFilter.cs b/secretmanager/api/SecretManager.Samples/ListSecretVersionsWithFilter.cs index 0b30c19bb02..9468d3af6ab 100644 --- a/secretmanager/api/SecretManager.Samples/ListSecretVersionsWithFilter.cs +++ b/secretmanager/api/SecretManager.Samples/ListSecretVersionsWithFilter.cs @@ -18,10 +18,11 @@ using Google.Cloud.SecretManager.V1; using System; +using System.Collections.Generic; public class ListSecretVersionsWithFilterSample { - public void ListSecretVersionsWithFilter( + public IList ListSecretVersionsWithFilter( string projectId = "my-project", string secretId = "my-secret" ) @@ -40,11 +41,14 @@ public void ListSecretVersionsWithFilter( Filter = filterStr }; + List secretVersions = new List(); // List all secret versions with the provided filter. foreach (SecretVersion version in client.ListSecretVersions(request)) { Console.WriteLine($"Found secret version: {version.Name}"); + secretVersions.Add(version); } + return secretVersions; } } // [END secretmanager_list_secret_versions_with_filter] diff --git a/secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs b/secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs index f7f4718ec61..c4e4274cef4 100644 --- a/secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs +++ b/secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs @@ -19,10 +19,11 @@ using Google.Api.Gax.ResourceNames; using Google.Cloud.SecretManager.V1; using System; +using System.Collections.Generic; public class ListSecretsWithFilterSample { - public void ListSecretsWithFilter(string projectId = "my-project") + public IList ListSecretsWithFilter(string projectId = "my-project") { string filterStr = "labels.my-label-key=my-label-value"; // Create the Secret Manager client. @@ -36,12 +37,17 @@ public void ListSecretsWithFilter(string projectId = "my-project") Filter = filterStr }; + // Create a list to hold the secrets + List secrets = new List(); // List all secrets with the provided filter. foreach (Secret secret in client.ListSecrets(request)) { Console.WriteLine($"Found secret: {secret.Name}"); + secrets.Add(secret); } + + return secrets; } } // [END secretmanager_list_secrets_with_filter] diff --git a/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs b/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs index f756e43259f..5be2e2541d4 100644 --- a/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs +++ b/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs @@ -22,7 +22,7 @@ public class CreateRegionalSecretWithCmekSample { - public void CreateRegionalSecretWithCmek( + public Secret CreateRegionalSecretWithCmek( string projectId = "my-project", string locationId = "us-central1", string secretId = "my-secret", @@ -51,6 +51,7 @@ public void CreateRegionalSecretWithCmek( // Print information about the created secret. Console.WriteLine($"Created secret {createdSecret.Name} with CMEK key {kmsKeyName}"); + return createdSecret; } } // [END secretmanager_create_regional_secret_with_cmek] diff --git a/secretmanager/api/SecretManager.Samples/createSecretWithCmek.cs b/secretmanager/api/SecretManager.Samples/createSecretWithCmek.cs index 74d2bed7a6e..882e7687f99 100644 --- a/secretmanager/api/SecretManager.Samples/createSecretWithCmek.cs +++ b/secretmanager/api/SecretManager.Samples/createSecretWithCmek.cs @@ -22,7 +22,7 @@ public class CreateSecretWithCmekSample { - public void CreateSecretWithCmek( + public Secret CreateSecretWithCmek( string projectId = "my-project", string secretId = "my-secret", string kmsKeyName = "projects/my-project/locations/global/keyRings/my-keyring/cryptoKeys/my-key") @@ -53,6 +53,7 @@ public void CreateSecretWithCmek( // Print information about the created secret. Console.WriteLine($"Created secret {createdSecret.Name} with CMEK key {kmsKeyName}"); + return createdSecret; } } // [END secretmanager_create_secret_with_cmek] From 922ff50540b29220292fea3093b3da26df471379 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Tue, 27 Jan 2026 16:42:27 +0530 Subject: [PATCH 08/19] feat(secretmanager): Update expiration samples --- ...CreateRegionalSecretWithExpirationTests.cs | 19 ++------------- .../CreateSecretWithExpirationTests.cs | 18 ++------------ .../DeleteRegionalSecretExpirationTests.cs | 18 ++------------ .../DeleteSecretExpirationTests.cs | 16 ++----------- .../UpdateRegionalSecretExpirationTests.cs | 24 +++++++------------ .../UpdateSecretExpirationTests.cs | 22 +++++++---------- .../CreateRegionalSecretWithExpiration.cs | 4 ++-- .../CreateSecretWithExpiration.cs | 3 ++- .../DeleteRegionalSecretExpiration.cs | 5 ++-- .../DeleteSecretExpiration.cs | 3 ++- .../UpdateRegionalSecretExpiration.cs | 3 ++- .../UpdateSecretExpiration.cs | 3 ++- 12 files changed, 38 insertions(+), 100 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs index cdd2c18d385..34779509be3 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs @@ -34,32 +34,17 @@ public CreateRegionalSecretWithExpireTimeTests(RegionalSecretManagerFixture fixt [Fact] public void CreatesRegionalSecretWithExpireTime() { - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Get the SecretName from the set ProjectId & LocationId. SecretName secretName = SecretName.FromProjectLocationSecret( _fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); // Run the code sample. - _sample.CreateRegionalSecretWithExpireTime( + Secret result = _sample.CreateRegionalSecretWithExpireTime( projectId: secretName.ProjectId, secretId: secretName.SecretId, locationId: secretName.LocationId); - - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains($"Created secret", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); - + Assert.NotEmpty(result.ExpireTime.ToString()); // Clean the created secret. _fixture.DeleteSecret(secretName); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs index 51164855612..8e0760da51e 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs @@ -35,28 +35,14 @@ public CreateSecretWithExpirationTests(SecretManagerFixture fixture) [Fact] public void CreatesSecretsWithExpiration() { - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Get the SecretName to create Secret. SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); // Create the secret with expiration. - _sample.CreateSecretWithExpiration( + Secret result = _sample.CreateSecretWithExpiration( projectId: secretName.ProjectId, secretId: secretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains($"Created secret", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); - + Assert.NotEmpty(result.ExpireTime.ToString()); // Clean the created secret. _fixture.DeleteSecret(secretName); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs index da866f57311..6cb335e2ed5 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs @@ -37,27 +37,13 @@ public void DeletesRegionalSecretExpiration() { Secret initialSecret = _fixture.CreateSecretWithExpireTime(); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Delete the expiration time - _deleteSample.DeleteRegionalSecretExpiration( + Secret result = _deleteSample.DeleteRegionalSecretExpiration( projectId: initialSecret.SecretName.ProjectId, secretId: initialSecret.SecretName.SecretId, locationId: initialSecret.SecretName.LocationId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains($"Removed expiration from secret", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); - + Assert.Null(result.ExpireTime); // Clean the created secret _fixture.DeleteSecret(initialSecret.SecretName); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs index 6c2cfc504d5..2cd6e9fffc6 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs @@ -35,9 +35,6 @@ public DeleteSecretExpirationTests(SecretManagerFixture fixture) [Fact] public void DeletesSecretExpiration() { - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); // Get the SecretName to create Secret. SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); @@ -46,19 +43,10 @@ public void DeletesSecretExpiration() Secret secret = _fixture.CreateSecretWithExpiration(); // Delete the expiration time - _deleteSample.DeleteSecretExpiration( + Secret result = _deleteSample.DeleteSecretExpiration( projectId: secret.SecretName.ProjectId, secretId: secret.SecretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains($"Removed expiration from secret", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + Assert.Null(result.ExpireTime); // Clean up the created secret _fixture.DeleteSecret(secretName); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs index 0ce3c356912..5ae6962a7a2 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs @@ -38,27 +38,21 @@ public void UpdatesRegionalSecretExpiration() // First, create a secret with initial expiration time (1 hour) Secret initialSecret = _fixture.CreateSecretWithExpireTime(); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Update the secret to have a 2-hour expiration - _updateSample.UpdateRegionalSecretExpiration( + Secret result = _updateSample.UpdateRegionalSecretExpiration( projectId: initialSecret.SecretName.ProjectId, secretId: initialSecret.SecretName.SecretId, locationId: initialSecret.SecretName.LocationId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Assert that the output contains the expected message - Assert.Contains($"Updated secret ", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + int expectedTime = DateTime.UtcNow.AddHours(2).Second; + int actualTime = result.ExpireTime.ToDateTime().Second; + // Allow for a small time difference (e.g., 30 seconds) due to test execution + int timeDifference = actualTime - expectedTime; + Assert.True( + Math.Abs(timeDifference) < 30, + $"Expected expiration time around {expectedTime}, but got {actualTime}" + ); // Clean the created secret _fixture.DeleteSecret(initialSecret.SecretName); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs index 4c562fd6941..7bf06828ffc 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs @@ -35,28 +35,24 @@ public UpdateSecretExpirationTests(SecretManagerFixture fixture) [Fact] public void UpdatesSecretExpiration() { - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); // Create a new secret with no expiration time Secret secret = _fixture.CreateSecretWithExpiration(); // Update the secret with an expiration time - _sample.UpdateSecretExpiration( + Secret result = _sample.UpdateSecretExpiration( projectId: secret.SecretName.ProjectId, secretId: secret.SecretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); + int expectedTime = DateTime.UtcNow.AddHours(2).Second; + int actualTime = result.ExpireTime.ToDateTime().Second; - // Assert that the output contains the expected message - Assert.Contains($"Updated secret", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + // Allow for a small time difference (e.g., 30 seconds) due to test execution + int timeDifference = actualTime - expectedTime; + Assert.True( + Math.Abs(timeDifference) < 30, + $"Expected expiration time around {expectedTime}, but got {actualTime}" + ); // Clean up the created secret _fixture.DeleteSecret(secret.SecretName); diff --git a/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithExpiration.cs b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithExpiration.cs index 075b7ce9894..5dcd9c28ca7 100644 --- a/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithExpiration.cs +++ b/secretmanager/api/SecretManager.Samples/CreateRegionalSecretWithExpiration.cs @@ -23,7 +23,7 @@ public class CreateRegionalSecretWithExpireTimeSample { - public void CreateRegionalSecretWithExpireTime( + public Secret CreateRegionalSecretWithExpireTime( string projectId = "my-project", string secretId = "my-secret-with-expiry", string locationId = "us-central1") @@ -53,7 +53,7 @@ public void CreateRegionalSecretWithExpireTime( Secret createdSecret = client.CreateSecret(location, secretId, secret); Console.WriteLine($"Created secret {createdSecret.SecretName} with expiration time {expireTime}"); - + return createdSecret; } } // [END secretmanager_create_regional_secret_with_expire_time] diff --git a/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs b/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs index 70c7e94b861..d85fa8f79ae 100644 --- a/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs +++ b/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs @@ -23,7 +23,7 @@ public class CreateSecretWithExpirationSample { - public void CreateSecretWithExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiry") + public Secret CreateSecretWithExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiry") { // Calculate expiration time (1 hour from now) DateTime expireTime = DateTime.UtcNow.AddHours(1); @@ -51,6 +51,7 @@ public void CreateSecretWithExpiration(string projectId = "my-project", string s Secret createdSecret = client.CreateSecret(projectName, secretId, secret); Console.WriteLine($"Created secret {createdSecret.SecretName} with expiration time {expireTime}"); + return createdSecret; } } // [END secretmanager_create_secret_with_expiration] diff --git a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretExpiration.cs index a875176feba..2840f865e49 100644 --- a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretExpiration.cs +++ b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretExpiration.cs @@ -22,8 +22,7 @@ public class DeleteRegionalSecretExpirationSample { - - public void DeleteRegionalSecretExpiration( + public Secret DeleteRegionalSecretExpiration( string projectId = "my-project", string secretId = "my-secret", string locationId = "us-central1") @@ -50,7 +49,7 @@ public void DeleteRegionalSecretExpiration( Secret updatedSecret = client.UpdateSecret(secret, updateMask); Console.WriteLine($"Removed expiration from secret {updatedSecret.SecretName}"); - + return updatedSecret; } } // [END secretmanager_delete_regional_secret_expiration] diff --git a/secretmanager/api/SecretManager.Samples/DeleteSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/DeleteSecretExpiration.cs index 6306f602bb3..7a47c90e80c 100644 --- a/secretmanager/api/SecretManager.Samples/DeleteSecretExpiration.cs +++ b/secretmanager/api/SecretManager.Samples/DeleteSecretExpiration.cs @@ -22,7 +22,7 @@ public class DeleteSecretExpirationSample { - public void DeleteSecretExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiration") + public Secret DeleteSecretExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiration") { // Create the client. SecretManagerServiceClient client = SecretManagerServiceClient.Create(); @@ -44,6 +44,7 @@ public void DeleteSecretExpiration(string projectId = "my-project", string secre Secret updatedSecret = client.UpdateSecret(secret, updateMask); Console.WriteLine($"Removed expiration from secret {updatedSecret.SecretName}"); + return updatedSecret; } } // [END secretmanager_delete_secret_expiration] diff --git a/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs index 28993161332..71afada1fc4 100644 --- a/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs +++ b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs @@ -22,7 +22,7 @@ public class UpdateRegionalSecretExpirationSample { - public void UpdateRegionalSecretExpiration( + public Secret UpdateRegionalSecretExpiration( string projectId = "my-project", string secretId = "my-secret", string locationId = "us-central1") @@ -56,6 +56,7 @@ public void UpdateRegionalSecretExpiration( Secret updatedSecret = client.UpdateSecret(secret, updateMask); Console.WriteLine($"Updated secret {updatedSecret.SecretName} expiration time to {newExpireTime}"); + return updatedSecret; } } // [END secretmanager_update_regional_secret_expiration] diff --git a/secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.cs index ad393b87669..0b80912e5ad 100644 --- a/secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.cs +++ b/secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.cs @@ -22,7 +22,7 @@ public class UpdateSecretExpirationSample { - public void UpdateSecretExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiration") + public Secret UpdateSecretExpiration(string projectId = "my-project", string secretId = "my-secret-with-expiration") { // Calculate new expiration time (2 hours from now) DateTime newExpireTime = DateTime.UtcNow.AddHours(2); @@ -50,6 +50,7 @@ public void UpdateSecretExpiration(string projectId = "my-project", string secre Secret updatedSecret = client.UpdateSecret(secret, updateMask); Console.WriteLine($"Updated secret {updatedSecret.SecretName} expiration time to {newExpireTime}"); + return updatedSecret; } } // [END secretmanager_update_secret_expiration] From 68b3c11f7ac545d32c00b11b20345044c25f6363 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Tue, 27 Jan 2026 18:34:16 +0530 Subject: [PATCH 09/19] feat(secretmanager): Update tags samples --- .../BindTagToRegionalSecretTests.cs | 19 ++------------- .../BindTagToSecretTests.cs | 19 ++------------- .../DetachRegionalTagBindingTests.cs | 24 +++++++------------ .../DetachTagBindingTests.cs | 19 +++++++-------- .../ListRegionalTagBindingsTests.cs | 19 +++------------ .../ListTagBindingsTests.cs | 20 ++++------------ .../BindTagToRegionalSecret.cs | 5 ++-- .../SecretManager.Samples/BindTagToSecret.cs | 5 ++-- .../DetachRegionalTagBinding.cs | 6 ++--- .../SecretManager.Samples/DetachTagBinding.cs | 5 ++-- .../ListRegionalTagBindings.cs | 6 +++-- .../SecretManager.Samples/ListTagBindings.cs | 6 ++++- 12 files changed, 50 insertions(+), 103 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs index 8e88c631876..0ecbee304bb 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs @@ -144,29 +144,14 @@ public async Task BindsTagToRegionalSecretAsync() SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Call the method being tested - await _sample.BindTagsToRegionalSecretAsync( + TagBinding tagBinding = await _sample.BindTagsToRegionalSecretAsync( projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId, tagValue: _tagValueName); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Check console output - Assert.Contains($"Created regional secret:", consoleOutput); - Assert.Contains($"Created tag binding:", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); - + Assert.Equal(_tagValueName, tagBinding.TagValue); // Clean up all resources _fixture.DeleteSecret(secretName); CleanupResources(); diff --git a/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs index 5b3ce7ab59c..b3fc008221c 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs @@ -144,27 +144,12 @@ public async Task BindsTagToSecretAsync() SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); // string secretId = _fixture.RandomId(); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - - await _sample.BindTagsToSecretAsync( + TagBinding tagBinding = await _sample.BindTagsToSecretAsync( projectId: secretName.ProjectId, secretId: secretName.SecretId, tagValue: _tagValueName); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Verify the result - - Assert.Contains($"Created secret: ", consoleOutput); - Assert.Contains($"Created Tag Binding", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + Assert.Equal(_tagValueName, tagBinding.TagValue); _fixture.DeleteSecret(secretName); CleanupResources(); diff --git a/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs index 57c5ac692aa..5cd642994a7 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs @@ -131,11 +131,6 @@ public async Task DetachesTagFromRegionalSecret() SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); - // Capture console output - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - - // Create a regional secret and bind the tag to it await _bindSample.BindTagsToRegionalSecretAsync( projectId: secretName.ProjectId, @@ -143,26 +138,23 @@ await _bindSample.BindTagsToRegionalSecretAsync( secretId: secretName.SecretId, tagValue: _tagValueName); - // Clear the console output from binding - sw.GetStringBuilder().Clear(); // Detach the tag - await _detachSample.DetachTagFromRegionalSecretAsync( + string bindingName = await _detachSample.DetachTagFromRegionalSecretAsync( projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId, tagValue: _tagValueName); - // Get the console output - string consoleOutput = sw.ToString().Trim(); + Assert.NotEmpty(bindingName); - // Verify the result - Assert.Contains($"Detached tag value {_tagValueName}", consoleOutput); + bindingName = await _detachSample.DetachTagFromRegionalSecretAsync( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId, + tagValue: _tagValueName); - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + Assert.Empty(bindingName); // Clean up all resources _fixture.DeleteSecret(secretName); diff --git a/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs index f246a5f957f..5250a2af4fa 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs @@ -17,6 +17,7 @@ using Google.Cloud.ResourceManager.V3; using Google.Cloud.SecretManager.V1; using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; @@ -31,6 +32,7 @@ public class DetachTagTests private readonly TagKeysClient _tagKeysClient; private readonly TagValuesClient _tagValuesClient; private readonly TagBindingsClient _tagBindingsClient; + private readonly ListTagBindingsSample _listSample; private string _tagKeyName; private string _tagValueName; private string _tagBindingName; @@ -162,22 +164,19 @@ await _bindSample.BindTagsToSecretAsync( Console.SetOut(sw); // Call the method being tested - await _detachSample.DetachTagAsync( + string bindingName = await _detachSample.DetachTagAsync( projectId: secretName.ProjectId, secretId: secretName.SecretId, tagValue: _tagValueName); - // Get the console output - string consoleOutput = sw.ToString().Trim(); + Assert.NotEmpty(bindingName); - // Verify the output contains expected message - Assert.Contains($"Detached tag value {_tagValueName} from {secretName}", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + bindingName = await _detachSample.DetachTagAsync( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + tagValue: _tagValueName); + Assert.Empty(bindingName); // Clean up all resources CleanupResources(); diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs index ad466fa89f9..98bd2e4b43b 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs @@ -133,10 +133,6 @@ public async Task ListsRegionalSecretTagBindings() SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); - // Capture console output for the list operation - StringWriter sw = new StringWriter(); - Console.SetOut(sw); - // Create a regional secret and bind the first tag to it await _bindSample.BindTagsToRegionalSecretAsync( projectId: secretName.ProjectId, @@ -145,22 +141,13 @@ await _bindSample.BindTagsToRegionalSecretAsync( tagValue: _tagValueName); // Call the method being tested - _listSample.ListRegionalSecretTagBindings( + List tagBindings = _listSample.ListRegionalSecretTagBindings( projectId: secretName.ProjectId, locationId: secretName.LocationId, secretId: secretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Verify the output contains expected information - Assert.Contains(secretName.SecretId, consoleOutput); - Assert.Contains($"- Tag Value: {_tagValueName}", consoleOutput); - - //// Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + Assert.Single(tagBindings); + Assert.Equal(_tagValueName, tagBindings[0].TagValue); // Clean up all resources _fixture.DeleteSecret(secretName); diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs index 3f0361f6353..e676089dca8 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs @@ -17,6 +17,7 @@ using Google.Cloud.ResourceManager.V3; using Google.Cloud.SecretManager.V1; using System; +using System.Collections.Generic; using System.IO; using System.Threading.Tasks; using Xunit; @@ -165,26 +166,15 @@ await _bindSample.BindTagsToSecretAsync( secretId: secretName.SecretId, tagValue: _tagValueName); - // Capture console output for the list operation - StringWriter sw = new StringWriter(); - Console.SetOut(sw); + IList tagBindings = new List(); // Call the method being tested - _listSample.ListTagBindings( + tagBindings = _listSample.ListTagBindings( projectId: secretName.ProjectId, secretId: secretName.SecretId); - // Get the console output - string consoleOutput = sw.ToString().Trim(); - - // Verify the output contains expected information - Assert.Contains(secretName.SecretId, consoleOutput); - Assert.Contains($"- Tag Value: {_tagValueName}", consoleOutput); - - // Reset console - var standardOutput = new StreamWriter(Console.OpenStandardOutput()); - standardOutput.AutoFlush = true; - Console.SetOut(standardOutput); + Assert.Single(tagBindings); + Assert.Equal(_tagValueName, tagBindings[0].TagValue); // Clean up all resources CleanupResources(); diff --git a/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs b/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs index bf278ec8f59..df6bf6b38c3 100644 --- a/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs +++ b/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs @@ -25,7 +25,7 @@ public class BindTagsToRegionalSecretSample { - public async Task BindTagsToRegionalSecretAsync( + public async Task BindTagsToRegionalSecretAsync( string projectId = "my-project", string locationId = "us-central1", string secretId = "my-secret", @@ -73,10 +73,11 @@ public async Task BindTagsToRegionalSecretAsync( // Wait for the operation to complete await operation.PollUntilCompletedAsync(); + TagBinding tagBinding = operation.Result; // Print the tag binding. Console.WriteLine($"Created tag binding: {createdSecret.Name}"); - + return tagBinding; } } // [END secretmanager_bind_tags_to_regional_secret] diff --git a/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs b/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs index 7089204661a..7261b8b1f2d 100644 --- a/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs +++ b/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs @@ -24,7 +24,7 @@ public class BindTagsToSecretSample { - public async Task BindTagsToSecretAsync( + public async Task BindTagsToSecretAsync( string projectId = "my-project", string secretId = "my-secret", string tagValue = "tagValues/123456789012") @@ -66,9 +66,10 @@ public async Task BindTagsToSecretAsync( // Create the tag binding and wait for the operation to complete. var operation = await resourceManagerClient.CreateTagBindingAsync(tagBinding); await operation.PollUntilCompletedAsync(); + TagBinding tagBindingResult = operation.Result; Console.WriteLine($"Created Tag Binding: {secret.Name}"); - + return tagBindingResult; } } // [END secretmanager_bind_tags_to_secret] diff --git a/secretmanager/api/SecretManager.Samples/DetachRegionalTagBinding.cs b/secretmanager/api/SecretManager.Samples/DetachRegionalTagBinding.cs index 652871420b6..0bbfa2e30f7 100644 --- a/secretmanager/api/SecretManager.Samples/DetachRegionalTagBinding.cs +++ b/secretmanager/api/SecretManager.Samples/DetachRegionalTagBinding.cs @@ -24,7 +24,7 @@ public class DetachTagFromRegionalSecretSample { - public async Task DetachTagFromRegionalSecretAsync( + public async Task DetachTagFromRegionalSecretAsync( string projectId = "my-project", string locationId = "us-central1", string secretId = "my-secret", @@ -65,7 +65,7 @@ public async Task DetachTagFromRegionalSecretAsync( if (bindingToDelete == null) { Console.WriteLine($"No tag binding found for tag value {tagValue} on {name}"); - return; + return ""; } // Create the delete request @@ -79,7 +79,7 @@ public async Task DetachTagFromRegionalSecretAsync( await operation.PollUntilCompletedAsync(); Console.WriteLine($"Detached tag value {tagValue} from {name}"); - + return bindingToDelete.Name; } } // [END secretmanager_detach_tag_from_regional_secret] diff --git a/secretmanager/api/SecretManager.Samples/DetachTagBinding.cs b/secretmanager/api/SecretManager.Samples/DetachTagBinding.cs index 501ce3fcc94..5a359b39994 100644 --- a/secretmanager/api/SecretManager.Samples/DetachTagBinding.cs +++ b/secretmanager/api/SecretManager.Samples/DetachTagBinding.cs @@ -23,7 +23,7 @@ public class DetachTagSample { - public async Task DetachTagAsync( + public async Task DetachTagAsync( string projectId = "my-project", string secretId = "my-secret", string tagValue = "tagValues/123456789012") @@ -61,7 +61,7 @@ public async Task DetachTagAsync( if (bindingName == null) { Console.WriteLine($"Tag binding for value {tagValue} not found on {secretName}."); - return; + return ""; } // Delete the tag binding @@ -75,6 +75,7 @@ public async Task DetachTagAsync( await operation.PollUntilCompletedAsync(); Console.WriteLine($"Detached tag value {tagValue} from {secretName}"); + return bindingName; } } // [END secretmanager_detach_tag] diff --git a/secretmanager/api/SecretManager.Samples/ListRegionalTagBindings.cs b/secretmanager/api/SecretManager.Samples/ListRegionalTagBindings.cs index de30dacfdf3..d31cc5a19f2 100644 --- a/secretmanager/api/SecretManager.Samples/ListRegionalTagBindings.cs +++ b/secretmanager/api/SecretManager.Samples/ListRegionalTagBindings.cs @@ -25,7 +25,7 @@ public class ListRegionalSecretTagBindingsSample { - public void ListRegionalSecretTagBindings( + public List ListRegionalSecretTagBindings( string projectId = "my-project", string locationId = "us-central1", string secretId = "my-regional-secret") @@ -54,18 +54,20 @@ public void ListRegionalSecretTagBindings( Parent = parent }; + List tagBindings = new List(); // Iterate through the results foreach (var binding in tagBindingsClient.ListTagBindings(request)) { Console.WriteLine($"- Tag Value: {binding.TagValue}"); foundBindings = true; + tagBindings.Add(binding); } if (!foundBindings) { Console.WriteLine($"No tag bindings found for {name}."); } - + return tagBindings; } } // [END secretmanager_list_regional_secret_tag_bindings] diff --git a/secretmanager/api/SecretManager.Samples/ListTagBindings.cs b/secretmanager/api/SecretManager.Samples/ListTagBindings.cs index b2beca84e42..513712db66f 100644 --- a/secretmanager/api/SecretManager.Samples/ListTagBindings.cs +++ b/secretmanager/api/SecretManager.Samples/ListTagBindings.cs @@ -19,10 +19,11 @@ using Google.Cloud.ResourceManager.V3; using Google.Cloud.SecretManager.V1; using System; +using System.Collections.Generic; public class ListTagBindingsSample { - public void ListTagBindings( + public IList ListTagBindings( string projectId = "my-project", string secretId = "my-secret") { @@ -46,16 +47,19 @@ public void ListTagBindings( bool foundBindings = false; Console.WriteLine($"Tag bindings for {secretName}:"); + List tagBindings = new List(); foreach (var binding in bindings) { Console.WriteLine($"- Tag Value: {binding.TagValue}"); foundBindings = true; + tagBindings.Add(binding); } if (!foundBindings) { Console.WriteLine($"No tag bindings found for {secretName}."); } + return tagBindings; } } // [END secretmanager_list_tag_bindings] From cdd521762bc78c2600cf85429346826154a15640 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 15:33:33 +0530 Subject: [PATCH 10/19] feat(secretmanager): Updating fixture --- .../RegionalSecretManagerFixture.cs | 8 ++++++++ .../SecretManager.Samples.Tests/SecretManagerFixture.cs | 7 +++++++ 2 files changed, 15 insertions(+) diff --git a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs index 6451b6c97cc..0429bc5830f 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs @@ -32,6 +32,8 @@ public class RegionalSecretManagerFixture : IDisposable, ICollectionFixture Date: Wed, 28 Jan 2026 15:37:15 +0530 Subject: [PATCH 11/19] feat(secretmanager): Updating fixture --- .../RegionalSecretManagerFixture.cs | 15 +++++++++++++++ .../SecretManagerFixture.cs | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs index 0429bc5830f..72224a1d980 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs @@ -113,6 +113,21 @@ public Secret CreateSecretWithDelayedDestroy() return Client.CreateSecret(locationName, RandomId(), secret); } + public Secret CreateSecretWithExpireTime() + { + LocationName locationName = new LocationName(ProjectId, LocationId); + + DateTime expireTime = DateTime.UtcNow.AddHours(1); + Timestamp timestamp = Timestamp.FromDateTime(expireTime); + + Secret secret = new Secret + { + ExpireTime = timestamp + }; + + return Client.CreateSecret(locationName, RandomId(), secret); + } + public SecretVersion AddSecretVersion(Secret secret) { SecretPayload payload = new SecretPayload diff --git a/secretmanager/api/SecretManager.Samples.Tests/SecretManagerFixture.cs b/secretmanager/api/SecretManager.Samples.Tests/SecretManagerFixture.cs index 7cdaf81dd3c..0881090f790 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/SecretManagerFixture.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/SecretManagerFixture.cs @@ -117,6 +117,24 @@ public Secret CreateSecretWithDelayedDestroy() return Client.CreateSecret(ProjectName, RandomId(), secret); } + public Secret CreateSecretWithExpiration() + { + DateTime expireTime = DateTime.UtcNow.AddHours(1); + Timestamp timestamp = Timestamp.FromDateTime(expireTime.ToUniversalTime()); + + // Build the secret with automatic replication and expiration time. + Secret secret = new Secret + { + Replication = new Replication + { + Automatic = new Replication.Types.Automatic(), + }, + ExpireTime = timestamp + }; + + return Client.CreateSecret(ProjectName, RandomId(), secret); + } + public SecretVersion AddSecretVersion(Secret secret) { SecretPayload payload = new SecretPayload From 4641d0314d01793d9be94c78b1a9cceb89f748a6 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 17:39:39 +0530 Subject: [PATCH 12/19] feat(secretmanager): Reformat code --- .../DeleteRegionalSecretAnnotationTests.cs | 40 ++++++++++--------- .../DeleteRegionalSecretLabelTests.cs | 33 ++++++++------- .../DeleteSecretAnnotationTests.cs | 20 ++++++---- .../DeleteSecretLabelTests.cs | 19 ++++++--- .../DeleteRegionalSecretAnnotation.cs | 2 +- .../DeleteRegionalSecretLabel.cs | 2 +- .../SecretManager.Samples/EditSecretLabel.cs | 2 - 7 files changed, 69 insertions(+), 49 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs index 9e167638e08..850c2d75ed3 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs @@ -37,23 +37,27 @@ public void DeleteRegionalSecretAnnotation() // Create a secret with annotations Secret secret = _fixture.CreateSecret(_fixture.RandomId()); SecretName secretName = secret.SecretName; - - // Get a key from the existing annotations - string annotationKey = _fixture.AnnotationKey; - - // Verify the secret has annotations - Assert.NotEmpty(secret.Annotations); - Assert.True(secret.Annotations.ContainsKey(annotationKey)); - - // Run the sample code to delete the annotation - Secret result = _sample.DeleteRegionalSecretAnnotation( - projectId: secretName.ProjectId, - locationId: secretName.LocationId, - secretId: secretName.SecretId); - - Assert.Empty(result.Annotations); - - // Clean the created secret. - _fixture.DeleteSecret(secretName); + try + { + // Get a key from the existing annotations + string annotationKey = _fixture.AnnotationKey; + + // Verify the secret has annotations + Assert.NotEmpty(secret.Annotations); + Assert.True(secret.Annotations.ContainsKey(annotationKey)); + + // Run the sample code to delete the annotation + Secret result = _sample.DeleteRegionalSecretAnnotation( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId); + + Assert.Empty(result.Annotations); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs index 1559a2680d3..6c52ee3cae7 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.cs @@ -36,19 +36,24 @@ public void DeleteRegionalSecretLabel() { Secret secret = _fixture.CreateSecret(_fixture.RandomId()); SecretName secretName = secret.SecretName; - - // Verify the secret has labels - Assert.NotEmpty(secret.Labels); - - // Run the sample code to delete all labels - Secret result = _sample.DeleteRegionalSecretLabel( - - projectId: secretName.ProjectId, - locationId: secretName.LocationId, - secretId: secretName.SecretId); - - Assert.Empty(result.Labels); - // Clean the created secret. - _fixture.DeleteSecret(secretName); + try + { + // Verify the secret has labels + Assert.NotEmpty(secret.Labels); + + // Run the sample code to delete all labels + Secret result = _sample.DeleteRegionalSecretLabel( + + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId); + + Assert.Empty(result.Labels); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs index b31004672f6..f24c3798b9a 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs @@ -36,15 +36,21 @@ public void DeleteSecretAnnotation() { // Create the secret. Secret secret = _fixture.CreateSecret(_fixture.RandomId()); - // Get the secretName from the created secret. SecretName secretName = secret.SecretName; + try + { + // Call the code sample function to delete the label + Secret result = _sample.DeleteSecretAnnotation( + projectId: secretName.ProjectId, secretId: secretName.SecretId); - // Call the code sample function to delete the label - Secret result = _sample.DeleteSecretAnnotation( - projectId: secretName.ProjectId, secretId: secretName.SecretId); - - // Assert that the label is deleted. - Assert.Empty(result.Annotations); + // Assert that the label is deleted. + Assert.Empty(result.Annotations); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs index 92127ec5c0f..c25d594140e 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs @@ -39,12 +39,19 @@ public void DeleteSecretLabel() // Get the secretName from the created secret. SecretName secretName = secret.SecretName; + try + { + // Call the code sample function to delete the label + Secret result = _sample.DeleteSecretLabel( + projectId: secretName.ProjectId, secretId: secretName.SecretId); - // Call the code sample function to delete the label - Secret result = _sample.DeleteSecretLabel( - projectId: secretName.ProjectId, secretId: secretName.SecretId); - - // Assert that the label is deleted. - Assert.Empty(result.Labels); + // Assert that the label is deleted. + Assert.Empty(result.Labels); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs index db4174c05c9..6cca53a2f8f 100644 --- a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs +++ b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretAnnotation.cs @@ -34,7 +34,7 @@ public Secret DeleteRegionalSecretAnnotation( }.Build(); // Build the resource name of the secret. - string secretName = $"projects/{projectId}/locations/{locationId}/secrets/{secretId}"; + SecretName secretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId); // Get the secret. Secret secret = client.GetSecret(secretName); diff --git a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs index 9a53e561905..f9ec0746e8c 100644 --- a/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs +++ b/secretmanager/api/SecretManager.Samples/DeleteRegionalSecretLabel.cs @@ -34,7 +34,7 @@ public Secret DeleteRegionalSecretLabel( }.Build(); // Build the resource name of the secret. - string secretName = $"projects/{projectId}/locations/{locationId}/secrets/{secretId}"; + SecretName secretName = SecretName.FromProjectLocationSecret(projectId, locationId, secretId); // Get the secret. Secret secret = client.GetSecret(secretName); diff --git a/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs b/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs index 808f91ad857..71f73db0677 100644 --- a/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs +++ b/secretmanager/api/SecretManager.Samples/EditSecretLabel.cs @@ -37,8 +37,6 @@ public Secret EditSecretLabel( // Get the secret. Secret secret = client.GetSecret(name); - Console.WriteLine($"Updated secret: {secret.Name}"); - // Update the labels secret.Labels[labelKey] = labelValue; From 34e7a7ad001d9f5993ac92e44660bafee9388f1d Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 18:00:18 +0530 Subject: [PATCH 13/19] feat(secretmanager): Reformat code --- .../ListSecretsWithFilterTests.cs | 31 ++++++++++--------- .../createRegionalSecretWithCmekTests.cs | 29 +++++++++-------- .../createSecretWithCmekTests.cs | 24 ++++++++------ 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs index 638af81edab..058bf163ccd 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.cs @@ -38,19 +38,22 @@ public void ListsSecretsWithFilter() { Secret secret = _fixture.CreateSecret(_fixture.RandomId()); SecretName secretName = secret.SecretName; - - - IList secrets = _sample.ListSecretsWithFilter( - projectId: _fixture.ProjectId); - - // Verify we got results - Assert.NotNull(secrets); - Assert.NotEmpty(secrets); - - // Verify our specific secret is in the results - bool foundSecret = secrets.Any(s => s.SecretName.SecretId == secretName.SecretId); - Assert.True(foundSecret, $"The secret {secretName.SecretId} with label my-label-key=my-label-value should be in the results"); - - _fixture.DeleteSecret(secretName); + try + { + IList secrets = _sample.ListSecretsWithFilter( + projectId: _fixture.ProjectId); + + // Verify we got results + Assert.NotNull(secrets); + Assert.NotEmpty(secrets); + + // Verify our specific secret is in the results + bool foundSecret = secrets.Any(s => s.SecretName.SecretId == secretName.SecretId); + Assert.True(foundSecret, $"The secret {secretName.SecretId} with label my-label-key=my-label-value should be in the results"); + } + finally + { + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs b/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs index d080c782660..aa3f071a79a 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/createRegionalSecretWithCmekTests.cs @@ -42,18 +42,21 @@ public void CreatesRegionalSecretWithCmek() // Get the SecretName from the set ProjectId & LocationId. SecretName secretName = SecretName.FromProjectLocationSecret(_fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); - - // Create the regional secret with CMEK. - Secret result = _sample.CreateRegionalSecretWithCmek( - projectId: secretName.ProjectId, - locationId: secretName.LocationId, - secretId: secretName.SecretId, - kmsKeyName: _fixture.KmsKeyName); - - Assert.Equal(result.CustomerManagedEncryption.KmsKeyName, _fixture.KmsKeyName); - - // Clean the created secret. - _fixture.DeleteSecret(secretName); - + try + { + // Create the regional secret with CMEK. + Secret result = _sample.CreateRegionalSecretWithCmek( + projectId: secretName.ProjectId, + locationId: secretName.LocationId, + secretId: secretName.SecretId, + kmsKeyName: _fixture.KmsKeyName); + + Assert.Equal(result.CustomerManagedEncryption.KmsKeyName, _fixture.KmsKeyName); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs b/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs index 06dc894b282..4f5caf14534 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/createSecretWithCmekTests.cs @@ -15,6 +15,7 @@ */ using Google.Cloud.SecretManager.V1; +using Microsoft.VisualBasic; using System; using System.IO; using Xunit; @@ -42,16 +43,21 @@ public void CreatesSecretWithCmek() // Get the SecretName to create Secret. SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + try + { + // Create the secret with CMEK. + Secret result = _sample.CreateSecretWithCmek( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + kmsKeyName: _fixture.KmsKeyName); - // Create the secret with CMEK. - Secret result = _sample.CreateSecretWithCmek( - projectId: secretName.ProjectId, - secretId: secretName.SecretId, - kmsKeyName: _fixture.KmsKeyName); - - Assert.Equal(result.Replication.Automatic.CustomerManagedEncryption.KmsKeyName, _fixture.KmsKeyName); - // Clean the created secret. - _fixture.DeleteSecret(secretName); + Assert.Equal(result.Replication.Automatic.CustomerManagedEncryption.KmsKeyName, _fixture.KmsKeyName); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } } } From 5a4e75a525fc12c0664bb35760f563842ce4efa0 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 18:14:27 +0530 Subject: [PATCH 14/19] feat(secretmanager): Reformat code --- ...CreateRegionalSecretWithExpirationTests.cs | 2 +- .../CreateSecretWithExpirationTests.cs | 4 +- .../DeleteSecretExpirationTests.cs | 27 ++++++++----- .../UpdateRegionalSecretExpirationTests.cs | 40 ++++++++++--------- .../UpdateSecretExpirationTests.cs | 39 +++++++++--------- .../CreateSecretWithExpiration.cs | 2 +- 6 files changed, 63 insertions(+), 51 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs index 34779509be3..e63302614cf 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs @@ -44,7 +44,7 @@ public void CreatesRegionalSecretWithExpireTime() secretId: secretName.SecretId, locationId: secretName.LocationId); - Assert.NotEmpty(result.ExpireTime.ToString()); + Assert.NotNull(result.ExpireTime); // Clean the created secret. _fixture.DeleteSecret(secretName); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs index 8e0760da51e..11df47dafed 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs @@ -33,7 +33,7 @@ public CreateSecretWithExpirationTests(SecretManagerFixture fixture) } [Fact] - public void CreatesSecretsWithExpiration() + public void CreatesSecretWithExpiration() { // Get the SecretName to create Secret. SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); @@ -42,7 +42,7 @@ public void CreatesSecretsWithExpiration() Secret result = _sample.CreateSecretWithExpiration( projectId: secretName.ProjectId, secretId: secretName.SecretId); - Assert.NotEmpty(result.ExpireTime.ToString()); + Assert.NotNull(result.ExpireTime); // Clean the created secret. _fixture.DeleteSecret(secretName); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs index 2cd6e9fffc6..91b596d7c82 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs @@ -38,16 +38,21 @@ public void DeletesSecretExpiration() // Get the SecretName to create Secret. SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); - - // First, create a secret with an expiration time - Secret secret = _fixture.CreateSecretWithExpiration(); - - // Delete the expiration time - Secret result = _deleteSample.DeleteSecretExpiration( - projectId: secret.SecretName.ProjectId, secretId: secret.SecretName.SecretId); - - Assert.Null(result.ExpireTime); - // Clean up the created secret - _fixture.DeleteSecret(secretName); + try + { + // First, create a secret with an expiration time + Secret secret = _fixture.CreateSecretWithExpiration(); + + // Delete the expiration time + Secret result = _deleteSample.DeleteSecretExpiration( + projectId: secret.SecretName.ProjectId, secretId: secret.SecretName.SecretId); + + Assert.Null(result.ExpireTime); + } + finally + { + // Clean up the created secret + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs index 5ae6962a7a2..6c642670131 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateRegionalSecretExpirationTests.cs @@ -37,23 +37,27 @@ public void UpdatesRegionalSecretExpiration() // First, create a secret with initial expiration time (1 hour) Secret initialSecret = _fixture.CreateSecretWithExpireTime(); - - // Update the secret to have a 2-hour expiration - Secret result = _updateSample.UpdateRegionalSecretExpiration( - projectId: initialSecret.SecretName.ProjectId, - secretId: initialSecret.SecretName.SecretId, - locationId: initialSecret.SecretName.LocationId); - - int expectedTime = DateTime.UtcNow.AddHours(2).Second; - int actualTime = result.ExpireTime.ToDateTime().Second; - - // Allow for a small time difference (e.g., 30 seconds) due to test execution - int timeDifference = actualTime - expectedTime; - Assert.True( - Math.Abs(timeDifference) < 30, - $"Expected expiration time around {expectedTime}, but got {actualTime}" - ); - // Clean the created secret - _fixture.DeleteSecret(initialSecret.SecretName); + try + { + // Update the secret to have a 2-hour expiration + Secret result = _updateSample.UpdateRegionalSecretExpiration( + projectId: initialSecret.SecretName.ProjectId, + secretId: initialSecret.SecretName.SecretId, + locationId: initialSecret.SecretName.LocationId); + + DateTime expectedTime = DateTime.UtcNow.AddHours(2); + DateTime actualTime = result.ExpireTime.ToDateTime(); + + // Allow for a small time difference (e.g., 30 seconds) due to test execution + Assert.True( + (actualTime - expectedTime).Duration() < TimeSpan.FromSeconds(60), + $"Expected expiration time around {expectedTime}, but got {actualTime}" + ); + } + finally + { + // Clean the created secret + _fixture.DeleteSecret(initialSecret.SecretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs index 7bf06828ffc..36fafa64e48 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/UpdateSecretExpirationTests.cs @@ -38,23 +38,26 @@ public void UpdatesSecretExpiration() // Create a new secret with no expiration time Secret secret = _fixture.CreateSecretWithExpiration(); - - // Update the secret with an expiration time - Secret result = _sample.UpdateSecretExpiration( - projectId: secret.SecretName.ProjectId, - secretId: secret.SecretName.SecretId); - - int expectedTime = DateTime.UtcNow.AddHours(2).Second; - int actualTime = result.ExpireTime.ToDateTime().Second; - - // Allow for a small time difference (e.g., 30 seconds) due to test execution - int timeDifference = actualTime - expectedTime; - Assert.True( - Math.Abs(timeDifference) < 30, - $"Expected expiration time around {expectedTime}, but got {actualTime}" - ); - - // Clean up the created secret - _fixture.DeleteSecret(secret.SecretName); + try + { + // Update the secret with an expiration time + Secret result = _sample.UpdateSecretExpiration( + projectId: secret.SecretName.ProjectId, + secretId: secret.SecretName.SecretId); + + DateTime expectedTime = DateTime.UtcNow.AddHours(2); + DateTime actualTime = result.ExpireTime.ToDateTime(); + + // Allow for a small time difference (e.g., 30 seconds) due to test execution + Assert.True( + (actualTime - expectedTime).Duration() < TimeSpan.FromSeconds(60), + $"Expected expiration time around {expectedTime}, but got {actualTime}" + ); + } + finally + { + // Clean up the created secret + _fixture.DeleteSecret(secret.SecretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs b/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs index d85fa8f79ae..fc756e54951 100644 --- a/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs +++ b/secretmanager/api/SecretManager.Samples/CreateSecretWithExpiration.cs @@ -35,7 +35,7 @@ public Secret CreateSecretWithExpiration(string projectId = "my-project", string ProjectName projectName = new ProjectName(projectId); // Convert DateTime to Timestamp - Timestamp timestamp = Timestamp.FromDateTime(expireTime.ToUniversalTime()); + Timestamp timestamp = Timestamp.FromDateTime(expireTime); // Build the secret with automatic replication and expiration time. Secret secret = new Secret From 2f0bfb51109f21b52108158c6fa5d43fad920443 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 18:18:17 +0530 Subject: [PATCH 15/19] feat(secretmanager): Reformat code --- .../SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs | 3 ++- .../api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs index f24c3798b9a..b7ffc00c47c 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs @@ -40,7 +40,8 @@ public void DeleteSecretAnnotation() SecretName secretName = secret.SecretName; try { - // Call the code sample function to delete the label + Assert.NotEmpty(secret.Annotations); + // Call the code sample function to delete the annotations Secret result = _sample.DeleteSecretAnnotation( projectId: secretName.ProjectId, secretId: secretName.SecretId); diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs index c25d594140e..7bc8bee0c03 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs @@ -41,6 +41,7 @@ public void DeleteSecretLabel() SecretName secretName = secret.SecretName; try { + Assert.NotEmpty(secret.Labels); // Call the code sample function to delete the label Secret result = _sample.DeleteSecretLabel( projectId: secretName.ProjectId, secretId: secretName.SecretId); From 033b2a6ccda9613400fbd9c1d80c5d946d368a4e Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 19:10:54 +0530 Subject: [PATCH 16/19] feat(secretmanager): Reformat code --- .../BindTagToRegionalSecretTests.cs | 11 +++--- .../BindTagToSecretTests.cs | 10 ++--- .../DetachRegionalTagBindingTests.cs | 9 ++--- .../DetachTagBindingTests.cs | 25 +++--------- .../ListRegionalTagBindingsTests.cs | 9 ++--- .../ListTagBindingsTests.cs | 38 +++---------------- .../BindTagToRegionalSecret.cs | 2 +- .../SecretManager.Samples/BindTagToSecret.cs | 2 +- 8 files changed, 30 insertions(+), 76 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs index 0ecbee304bb..dcfdca86d21 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs @@ -32,7 +32,6 @@ public class BindTagsToRegionalSecretTests private string _tagKeyName; private string _tagValueName; private string _tagBindingName; - private string _secretId; public BindTagsToRegionalSecretTests(RegionalSecretManagerFixture fixture) { @@ -66,7 +65,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag key: {e.Message}"); + throw new Exception("Error creating tag key: " + e.Message, e); } var createValueRequest = new CreateTagValueRequest @@ -86,7 +85,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag value: {e.Message}"); + throw new Exception("Error creating tag value: " + e.Message, e); } } @@ -103,7 +102,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag binding: {e.Message}"); + Console.WriteLine($"Error deleting tag binding: {e.GetType().Name}: {e.Message}"); } } @@ -117,7 +116,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag value: {e.Message}"); + Console.WriteLine($"Error deleting tag value: {e.GetType().Name}: {e.Message}"); } } @@ -131,7 +130,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag key: {e.Message}"); + Console.WriteLine($"Error deleting tag key: {e.GetType().Name}: {e.Message}"); } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs index b3fc008221c..23a211bf434 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs @@ -65,7 +65,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag key: {e.Message}"); + throw new Exception("Error creating tag key: " + e.Message, e); } var createValueRequest = new CreateTagValueRequest @@ -85,7 +85,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag value: {e.Message}"); + throw new Exception("Error creating tag value: " + e.Message, e); } } @@ -101,7 +101,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag binding: {e.Message}"); + Console.WriteLine($"Error deleting tag binding: {e.GetType().Name}: {e.Message}"); } } @@ -115,7 +115,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag value: {e.Message}"); + Console.WriteLine($"Error deleting tag value: {e.GetType().Name}: {e.Message}"); } } @@ -129,7 +129,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag key: {e.Message}"); + Console.WriteLine($"Error deleting tag key: {e.GetType().Name}: {e.Message}"); } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs index 5cd642994a7..c3e212cda48 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs @@ -32,7 +32,6 @@ public class DetachTagFromRegionalSecretTests private readonly TagValuesClient _tagValuesClient; private string _tagKeyName; private string _tagValueName; - private string _secretId; public DetachTagFromRegionalSecretTests(RegionalSecretManagerFixture fixture) { @@ -67,7 +66,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag key: {e.Message}"); + throw new Exception("Error creating tag key: " + e.Message, e); } var createValueRequest = new CreateTagValueRequest @@ -87,7 +86,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag value: {e.Message}"); + throw new Exception("Error creating tag value: " + e.Message, e); } } @@ -104,7 +103,7 @@ private void CleanupResource() } catch (Exception e) { - Console.WriteLine($"Error deleting tag value: {e.Message}"); + Console.WriteLine($"Error deleting tag value: {e.GetType().Name}: {e.Message}"); } } @@ -118,7 +117,7 @@ private void CleanupResource() } catch (Exception e) { - Console.WriteLine($"Error deleting tag key: {e.Message}"); + Console.WriteLine($"Error deleting tag key: {e.GetType().Name}: {e.Message}"); } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs index 5250a2af4fa..93b83d3a77e 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs @@ -35,8 +35,6 @@ public class DetachTagTests private readonly ListTagBindingsSample _listSample; private string _tagKeyName; private string _tagValueName; - private string _tagBindingName; - private string _secretId; public DetachTagTests(SecretManagerFixture fixture) { @@ -71,7 +69,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag key: {e.Message}"); + throw new Exception("Error creating tag key: " + e.Message, e); } var createValueRequest = new CreateTagValueRequest @@ -91,26 +89,12 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag value: {e.Message}"); + throw new Exception("Error creating tag value: " + e.Message, e); } } private void CleanupResources() { - // Delete the secret that was created - if (!string.IsNullOrEmpty(_secretId)) - { - try - { - SecretManagerServiceClient secretClient = SecretManagerServiceClient.Create(); - secretClient.DeleteSecret(new SecretName(_fixture.ProjectId, _secretId)); - } - catch (Exception e) - { - Console.WriteLine($"Error deleting secret: {e.Message}"); - } - } - // Delete the tag value if it exists if (!string.IsNullOrEmpty(_tagValueName)) { @@ -121,7 +105,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag value: {e.Message}"); + Console.WriteLine($"Error deleting tag value: {e.GetType().Name}: {e.Message}"); } } @@ -135,7 +119,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag key: {e.Message}"); + Console.WriteLine($"Error deleting tag key: {e.GetType().Name}: {e.Message}"); } } } @@ -178,6 +162,7 @@ await _bindSample.BindTagsToSecretAsync( Assert.Empty(bindingName); // Clean up all resources + _fixture.DeleteSecret(secretName); CleanupResources(); } diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs index 98bd2e4b43b..c31d549237b 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs @@ -34,7 +34,6 @@ public class ListRegionalTagBindingsTests private readonly TagBindingsClient _tagBindingsClient; private string _tagKeyName; private string _tagValueName; - private string _secretId; public ListRegionalTagBindingsTests(RegionalSecretManagerFixture fixture) { @@ -69,7 +68,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag key: {e.Message}"); + throw new Exception("Error creating tag key: " + e.Message, e); } var createValueRequest = new CreateTagValueRequest @@ -89,7 +88,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag value: {e.Message}"); + throw new Exception("Error creating tag value: " + e.Message, e); } } @@ -106,7 +105,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag value: {e.Message}"); + Console.WriteLine($"Error deleting tag value: {e.GetType().Name}: {e.Message}"); } } @@ -120,7 +119,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag key: {e.Message}"); + Console.WriteLine($"Error deleting tag key: {e.GetType().Name}: {e.Message}"); } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs index e676089dca8..82249f6e43e 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs @@ -33,8 +33,6 @@ public class ListTagBindingsTests private readonly TagBindingsClient _tagBindingsClient; private string _tagKeyName; private string _tagValueName; - private string _tagBindingName; - private string _secretId; public ListTagBindingsTests(SecretManagerFixture fixture) { @@ -69,7 +67,7 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag key: {e.Message}"); + throw new Exception("Error creating tag key: " + e.Message, e); } var createValueRequest = new CreateTagValueRequest @@ -89,39 +87,12 @@ private void CreateTagKeyAndValue(string projectId) } catch (Exception e) { - throw new Exception($"Error creating tag value: {e.Message}"); + throw new Exception("Error creating tag value: " + e.Message, e); } } private void CleanupResources() { - // Delete the secret that was created - if (!string.IsNullOrEmpty(_secretId)) - { - try - { - SecretManagerServiceClient secretClient = SecretManagerServiceClient.Create(); - secretClient.DeleteSecret(new SecretName(_fixture.ProjectId, _secretId)); - } - catch (Exception e) - { - Console.WriteLine($"Error deleting secret: {e.Message}"); - } - } - - // Delete the tag binding if it exists - if (!string.IsNullOrEmpty(_tagBindingName)) - { - try - { - var deleteBindingRequest = new DeleteTagBindingRequest { Name = _tagBindingName }; - _tagBindingsClient.DeleteTagBinding(deleteBindingRequest).PollUntilCompleted(); - } - catch (Exception e) - { - Console.WriteLine($"Error deleting tag binding: {e.Message}"); - } - } // Delete the tag value if it exists if (!string.IsNullOrEmpty(_tagValueName)) @@ -133,7 +104,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag value: {e.Message}"); + Console.WriteLine($"Error deleting tag value: {e.GetType().Name}: {e.Message}"); } } @@ -147,7 +118,7 @@ private void CleanupResources() } catch (Exception e) { - Console.WriteLine($"Error deleting tag key: {e.Message}"); + Console.WriteLine($"Error deleting tag key: {e.GetType().Name}: {e.Message}"); } } } @@ -177,6 +148,7 @@ await _bindSample.BindTagsToSecretAsync( Assert.Equal(_tagValueName, tagBindings[0].TagValue); // Clean up all resources + _fixture.DeleteSecret(secretName); CleanupResources(); } }; diff --git a/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs b/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs index df6bf6b38c3..20a9f930a30 100644 --- a/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs +++ b/secretmanager/api/SecretManager.Samples/BindTagToRegionalSecret.cs @@ -76,7 +76,7 @@ public async Task BindTagsToRegionalSecretAsync( TagBinding tagBinding = operation.Result; // Print the tag binding. - Console.WriteLine($"Created tag binding: {createdSecret.Name}"); + Console.WriteLine($"Created tag binding: {tagBinding.Name}"); return tagBinding; } } diff --git a/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs b/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs index 7261b8b1f2d..3e84c7387bd 100644 --- a/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs +++ b/secretmanager/api/SecretManager.Samples/BindTagToSecret.cs @@ -68,7 +68,7 @@ public async Task BindTagsToSecretAsync( await operation.PollUntilCompletedAsync(); TagBinding tagBindingResult = operation.Result; - Console.WriteLine($"Created Tag Binding: {secret.Name}"); + Console.WriteLine($"Created Tag Binding: {tagBindingResult.Name}"); return tagBindingResult; } } From 2c1de99aee5d6c0912025b65ded25325b0754d2f Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 19:18:45 +0530 Subject: [PATCH 17/19] feat(secretmanager): Reformat code --- ...CreateRegionalSecretWithExpirationTests.cs | 23 +++++++++++-------- .../CreateSecretWithExpirationTests.cs | 19 +++++++++------ .../DeleteRegionalSecretExpirationTests.cs | 23 +++++++++++-------- .../DeleteSecretExpirationTests.cs | 10 +++----- 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs index e63302614cf..cd96835d4b3 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs @@ -37,15 +37,20 @@ public void CreatesRegionalSecretWithExpireTime() // Get the SecretName from the set ProjectId & LocationId. SecretName secretName = SecretName.FromProjectLocationSecret( _fixture.ProjectId, _fixture.LocationId, _fixture.RandomId()); + try + { + // Run the code sample. + Secret result = _sample.CreateRegionalSecretWithExpireTime( + projectId: secretName.ProjectId, + secretId: secretName.SecretId, + locationId: secretName.LocationId); - // Run the code sample. - Secret result = _sample.CreateRegionalSecretWithExpireTime( - projectId: secretName.ProjectId, - secretId: secretName.SecretId, - locationId: secretName.LocationId); - - Assert.NotNull(result.ExpireTime); - // Clean the created secret. - _fixture.DeleteSecret(secretName); + Assert.NotNull(result.ExpireTime); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs index 11df47dafed..6929350720d 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs @@ -37,13 +37,18 @@ public void CreatesSecretWithExpiration() { // Get the SecretName to create Secret. SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + try + { + // Create the secret with expiration. + Secret result = _sample.CreateSecretWithExpiration( + projectId: secretName.ProjectId, secretId: secretName.SecretId); - // Create the secret with expiration. - Secret result = _sample.CreateSecretWithExpiration( - projectId: secretName.ProjectId, secretId: secretName.SecretId); - - Assert.NotNull(result.ExpireTime); - // Clean the created secret. - _fixture.DeleteSecret(secretName); + Assert.NotNull(result.ExpireTime); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs index 6cb335e2ed5..0604bbde140 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs @@ -36,15 +36,20 @@ public DeleteRegionalSecretExpirationTests(RegionalSecretManagerFixture fixture) public void DeletesRegionalSecretExpiration() { Secret initialSecret = _fixture.CreateSecretWithExpireTime(); + try + { + // Delete the expiration time + Secret result = _deleteSample.DeleteRegionalSecretExpiration( + projectId: initialSecret.SecretName.ProjectId, + secretId: initialSecret.SecretName.SecretId, + locationId: initialSecret.SecretName.LocationId); - // Delete the expiration time - Secret result = _deleteSample.DeleteRegionalSecretExpiration( - projectId: initialSecret.SecretName.ProjectId, - secretId: initialSecret.SecretName.SecretId, - locationId: initialSecret.SecretName.LocationId); - - Assert.Null(result.ExpireTime); - // Clean the created secret - _fixture.DeleteSecret(initialSecret.SecretName); + Assert.Null(result.ExpireTime); + } + finally + { + // Clean the created secret + _fixture.DeleteSecret(initialSecret.SecretName); + } } } diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs index 91b596d7c82..191aca84657 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs @@ -35,14 +35,10 @@ public DeleteSecretExpirationTests(SecretManagerFixture fixture) [Fact] public void DeletesSecretExpiration() { - - // Get the SecretName to create Secret. - SecretName secretName = new SecretName(_fixture.ProjectId, _fixture.RandomId()); + // First, create a secret with an expiration time + Secret secret = _fixture.CreateSecretWithExpiration(); try { - // First, create a secret with an expiration time - Secret secret = _fixture.CreateSecretWithExpiration(); - // Delete the expiration time Secret result = _deleteSample.DeleteSecretExpiration( projectId: secret.SecretName.ProjectId, secretId: secret.SecretName.SecretId); @@ -52,7 +48,7 @@ public void DeletesSecretExpiration() finally { // Clean up the created secret - _fixture.DeleteSecret(secretName); + _fixture.DeleteSecret(secret.SecretName); } } } From 26f4e0e0555453208760d199cb126e985ae6ef64 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Thu, 29 Jan 2026 10:36:15 +0530 Subject: [PATCH 18/19] feat(secretmanager): Reformat code --- .../DetachRegionalTagBindingTests.cs | 2 +- .../api/SecretManager.Samples.Tests/DetachTagBindingTests.cs | 2 +- .../SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs | 2 +- .../api/SecretManager.Samples.Tests/ListTagBindingsTests.cs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs index c3e212cda48..7c1ba31c951 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DetachRegionalTagBindingTests.cs @@ -5,7 +5,7 @@ * 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](https://www.apache.org/licenses/LICENSE-2.0) + * 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, diff --git a/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs index 93b83d3a77e..cc6ef2cdcc3 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/DetachTagBindingTests.cs @@ -5,7 +5,7 @@ * 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](https://www.apache.org/licenses/LICENSE-2.0) + * 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, diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs index c31d549237b..9ef7dc28c48 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListRegionalTagBindingsTests.cs @@ -5,7 +5,7 @@ * 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](https://www.apache.org/licenses/LICENSE-2.0) + * 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, diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs index 82249f6e43e..255727db3be 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/ListTagBindingsTests.cs @@ -5,7 +5,7 @@ * 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](https://www.apache.org/licenses/LICENSE-2.0) + * 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, From eced0ba5346079b23680f1611238227a5bbed8ed Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Thu, 29 Jan 2026 11:04:03 +0530 Subject: [PATCH 19/19] feat(secretmanager): Reformat code --- .../BindTagToRegionalSecretTests.cs | 16 ---------------- .../BindTagToSecretTests.cs | 14 -------------- 2 files changed, 30 deletions(-) diff --git a/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs index dcfdca86d21..b1009d0bb6e 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/BindTagToRegionalSecretTests.cs @@ -31,7 +31,6 @@ public class BindTagsToRegionalSecretTests private readonly TagBindingsClient _tagBindingsClient; private string _tagKeyName; private string _tagValueName; - private string _tagBindingName; public BindTagsToRegionalSecretTests(RegionalSecretManagerFixture fixture) { @@ -91,21 +90,6 @@ private void CreateTagKeyAndValue(string projectId) private void CleanupResources() { - - // Delete the tag binding if it exists - if (!string.IsNullOrEmpty(_tagBindingName)) - { - try - { - var deleteBindingRequest = new DeleteTagBindingRequest { Name = _tagBindingName }; - _tagBindingsClient.DeleteTagBinding(deleteBindingRequest).PollUntilCompleted(); - } - catch (Exception e) - { - Console.WriteLine($"Error deleting tag binding: {e.GetType().Name}: {e.Message}"); - } - } - // Delete the tag value if it exists if (!string.IsNullOrEmpty(_tagValueName)) { diff --git a/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs b/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs index 23a211bf434..84e05349ff1 100644 --- a/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs +++ b/secretmanager/api/SecretManager.Samples.Tests/BindTagToSecretTests.cs @@ -31,7 +31,6 @@ public class BindTagsToSecretTests private readonly TagBindingsClient _tagBindingsClient; private string _tagKeyName; private string _tagValueName; - private string _tagBindingName; public BindTagsToSecretTests(SecretManagerFixture fixture) { @@ -91,19 +90,6 @@ private void CreateTagKeyAndValue(string projectId) private void CleanupResources() { - // Delete the tag binding if it exists - if (!string.IsNullOrEmpty(_tagBindingName)) - { - try - { - var deleteBindingRequest = new DeleteTagBindingRequest { Name = _tagBindingName }; - _tagBindingsClient.DeleteTagBinding(deleteBindingRequest).PollUntilCompleted(); - } - catch (Exception e) - { - Console.WriteLine($"Error deleting tag binding: {e.GetType().Name}: {e.Message}"); - } - } // Delete the tag value if it exists if (!string.IsNullOrEmpty(_tagValueName))