diff --git a/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.cs new file mode 100644 index 00000000000..cd96835d4b3 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateRegionalSecretWithExpirationTests.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. + */ + +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() + { + // 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); + + 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 new file mode 100644 index 00000000000..6929350720d --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/CreateSecretWithExpirationTests.cs @@ -0,0 +1,54 @@ +/* + * 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 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); + + Assert.NotNull(result.ExpireTime); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.cs new file mode 100644 index 00000000000..850c2d75ed3 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretAnnotationTests.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 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; + 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/DeleteRegionalSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.cs new file mode 100644 index 00000000000..0604bbde140 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretExpirationTests.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. + */ + +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(); + try + { + // Delete the expiration time + Secret result = _deleteSample.DeleteRegionalSecretExpiration( + projectId: initialSecret.SecretName.ProjectId, + secretId: initialSecret.SecretName.SecretId, + locationId: initialSecret.SecretName.LocationId); + + Assert.Null(result.ExpireTime); + } + finally + { + // Clean the created secret + _fixture.DeleteSecret(initialSecret.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..6c52ee3cae7 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteRegionalSecretLabelTests.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(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; + 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 new file mode 100644 index 00000000000..b7ffc00c47c --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretAnnotationTests.cs @@ -0,0 +1,57 @@ +/* + * 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; + try + { + Assert.NotEmpty(secret.Annotations); + // Call the code sample function to delete the annotations + Secret result = _sample.DeleteSecretAnnotation( + projectId: secretName.ProjectId, secretId: secretName.SecretId); + + // 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/DeleteSecretExpirationTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs new file mode 100644 index 00000000000..191aca84657 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretExpirationTests.cs @@ -0,0 +1,54 @@ +/* + * 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() + { + // First, create a secret with an expiration time + Secret secret = _fixture.CreateSecretWithExpiration(); + try + { + // 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(secret.SecretName); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.cs new file mode 100644 index 00000000000..7bc8bee0c03 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/DeleteSecretLabelTests.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. + */ + +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; + try + { + Assert.NotEmpty(secret.Labels); + // 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); + } + finally + { + // Clean the created secret. + _fixture.DeleteSecret(secretName); + } + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs b/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs new file mode 100644 index 00000000000..6da6560a7dd --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/EditSecretLabelTests.cs @@ -0,0 +1,46 @@ +/* + * 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; + + // Call the code sample function. + Secret result = _sample.EditSecretLabel( + projectId: secretName.ProjectId, secretId: secretName.SecretId); + + Assert.Equal("my-label-value", result.Labels["my-label-key"]); + } +} diff --git a/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs b/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.cs new file mode 100644 index 00000000000..cd50cced3e8 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/ListSecretVersionsWithFilterTests.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 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); + + // Run the sample code + IList versions = _sample.ListSecretVersionsWithFilter( + projectId: secretName.ProjectId, + secretId: secretName.SecretId); + + // Verify we got results + Assert.NotNull(versions); + Assert.NotEmpty(versions); + + // 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 new file mode 100644 index 00000000000..058bf163ccd --- /dev/null +++ b/secretmanager/api/SecretManager.Samples.Tests/ListSecretsWithFilterTests.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 System; +using System.IO; +using System.Collections.Generic; +using Xunit; +using Google.Cloud.SecretManager.V1; +using System.Linq; + +[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; + 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/RegionalSecretManagerFixture.cs b/secretmanager/api/SecretManager.Samples.Tests/RegionalSecretManagerFixture.cs index 6451b6c97cc..72224a1d980 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 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 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 new file mode 100644 index 00000000000..c4e4274cef4 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/ListSecretsWithFilter.cs @@ -0,0 +1,53 @@ +/* + * 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; +using System.Collections.Generic; + +public class ListSecretsWithFilterSample +{ + public IList 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 + }; + + // 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/UpdateRegionalSecretExpiration.cs b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.cs new file mode 100644 index 00000000000..71afada1fc4 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/UpdateRegionalSecretExpiration.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. + */ + +// [START secretmanager_update_regional_secret_expiration] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class UpdateRegionalSecretExpirationSample +{ + public Secret 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}"); + return updatedSecret; + } +} +// [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..0b80912e5ad --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/UpdateSecretExpiration.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_update_secret_expiration] + +using Google.Cloud.SecretManager.V1; +using Google.Protobuf.WellKnownTypes; +using System; + +public class UpdateSecretExpirationSample +{ + 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); + + // 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}"); + return updatedSecret; + } +} +// [END secretmanager_update_secret_expiration] diff --git a/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs b/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs new file mode 100644 index 00000000000..5be2e2541d4 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/createRegionalSecretWithCmek.cs @@ -0,0 +1,57 @@ +/* + * 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 Secret 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}"); + 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 new file mode 100644 index 00000000000..882e7687f99 --- /dev/null +++ b/secretmanager/api/SecretManager.Samples/createSecretWithCmek.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. + */ + +// [START secretmanager_create_secret_with_cmek] + +using Google.Api.Gax.ResourceNames; +using Google.Cloud.SecretManager.V1; +using System; + +public class CreateSecretWithCmekSample +{ + public Secret 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}"); + return createdSecret; + } +} +// [END secretmanager_create_secret_with_cmek]