From 62f68acf3c4453b148f8a0dde96b8dd23c2874e0 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Thu, 22 Jan 2026 15:12:22 +0530 Subject: [PATCH 01/14] 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/14] 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/14] 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 d21d2a7d4616ef7f07bd80ef8656874b046aaa9b Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Tue, 27 Jan 2026 12:46:21 +0530 Subject: [PATCH 04/14] 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 05/14] 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 06/14] 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 07/14] 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 cdd521762bc78c2600cf85429346826154a15640 Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 15:33:33 +0530 Subject: [PATCH 08/14] 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 09/14] 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 10/14] 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 11/14] 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 12/14] 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 13/14] 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 2c1de99aee5d6c0912025b65ded25325b0754d2f Mon Sep 17 00:00:00 2001 From: khilan maradiya Date: Wed, 28 Jan 2026 19:18:45 +0530 Subject: [PATCH 14/14] 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); } } }