|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | using System; |
| 18 | +using System.Collections.Generic; |
| 19 | +using Google.Api.Gax.ResourceNames; |
| 20 | +using Google.Cloud.Dlp.V2; |
18 | 21 | using Google.Cloud.ModelArmor.V1; |
19 | 22 | using Xunit; |
20 | 23 |
|
21 | 24 | [CollectionDefinition(nameof(ModelArmorFixture))] |
22 | 25 | public class ModelArmorFixture : IDisposable, ICollectionFixture<ModelArmorFixture> |
23 | 26 | { |
| 27 | + // Environment variable names. |
| 28 | + private const string EnvProjectId = "GOOGLE_PROJECT_ID"; |
| 29 | + private const string EnvLocation = "GOOGLE_CLOUD_LOCATION"; |
| 30 | + |
24 | 31 | public ModelArmorClient Client { get; } |
| 32 | + public DlpServiceClient DlpClient { get; } |
25 | 33 | public string ProjectId { get; } |
26 | 34 | public string LocationId { get; } |
27 | | - public TemplateName TemplateForQuickstartName { get; } |
| 35 | + private readonly List<TemplateName> _maTemplatesToCleanup = new List<TemplateName>(); |
| 36 | + private readonly List<string> _dlpTemplatesToCleanup = new List<string>(); |
28 | 37 |
|
29 | 38 | public ModelArmorFixture() |
30 | 39 | { |
31 | | - // Get the Google Cloud Project ID. |
32 | | - ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID"); |
33 | | - if (string.IsNullOrEmpty(ProjectId)) |
| 40 | + ProjectId = GetRequiredEnvVar(EnvProjectId); |
| 41 | + LocationId = Environment.GetEnvironmentVariable(EnvLocation) ?? "us-central1"; |
| 42 | + |
| 43 | + // Create the Model Armor client. |
| 44 | + ModelArmorClient Client = new ModelArmorClientBuilder |
| 45 | + { |
| 46 | + Endpoint = $"modelarmor.{LocationId}.rep.googleapis.com", |
| 47 | + }.Build(); |
| 48 | + |
| 49 | + // Create the DLP client. |
| 50 | + DlpClient = new DlpServiceClientBuilder { Endpoint = $"dlp.googleapis.com" }.Build(); |
| 51 | + } |
| 52 | + |
| 53 | + private string GetRequiredEnvVar(string name) |
| 54 | + { |
| 55 | + var value = Environment.GetEnvironmentVariable(name); |
| 56 | + if (string.IsNullOrEmpty(value)) |
34 | 57 | { |
35 | | - throw new Exception("Missing GOOGLE_PROJECT_ID environment variable"); |
| 58 | + throw new Exception($"Missing {name} environment variable"); |
36 | 59 | } |
| 60 | + return value; |
| 61 | + } |
37 | 62 |
|
38 | | - // Get location ID from environment variable or use default. |
39 | | - LocationId = Environment.GetEnvironmentVariable("GOOGLE_CLOUD_LOCATION") ?? "us-central1"; |
| 63 | + public string GenerateUniqueId() |
| 64 | + { |
| 65 | + return Guid.NewGuid().ToString("N").Substring(0, 8); |
| 66 | + } |
40 | 67 |
|
41 | | - // Create client. |
42 | | - Client = ModelArmorClient.Create(); |
| 68 | + // Creates a DLP Inspect Template and returns its name. |
| 69 | + public string CreateInspectTemplate(string displayName = "Test Inspect Template") |
| 70 | + { |
| 71 | + var parent = new LocationName(ProjectId, LocationId).ToString(); |
| 72 | + var templateId = $"inspect-{GenerateUniqueId()}"; |
| 73 | + var request = new CreateInspectTemplateRequest |
| 74 | + { |
| 75 | + Parent = parent, |
| 76 | + InspectTemplate = new InspectTemplate |
| 77 | + { |
| 78 | + DisplayName = displayName, |
| 79 | + InspectConfig = new InspectConfig |
| 80 | + { |
| 81 | + InfoTypes = |
| 82 | + { |
| 83 | + new InfoType { Name = "PERSON_NAME" }, |
| 84 | + new InfoType { Name = "EMAIL_ADDRESS" }, |
| 85 | + new InfoType { Name = "US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER" }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + TemplateId = templateId, |
| 90 | + }; |
| 91 | + var response = DlpClient.CreateInspectTemplate(request); |
| 92 | + RegisterDlpTemplateForCleanup(response.Name); |
| 93 | + return response.Name; |
| 94 | + } |
43 | 95 |
|
44 | | - // Create a template name for quickstart test. |
45 | | - string templateId = $"test-csharp-{Guid.NewGuid().ToString("N").Substring(0, 8)}"; |
46 | | - TemplateForQuickstartName = TemplateName.FromProjectLocationTemplate( |
47 | | - ProjectId, |
48 | | - LocationId, |
49 | | - templateId |
50 | | - ); |
| 96 | + // Creates a DLP Deidentify Template and returns its name. |
| 97 | + public string CreateDeidentifyTemplate(string displayName = "Test Deidentify Template") |
| 98 | + { |
| 99 | + var parent = new LocationName(ProjectId, LocationId).ToString(); |
| 100 | + var templateId = $"deidentify-{GenerateUniqueId()}"; |
| 101 | + var request = new CreateDeidentifyTemplateRequest |
| 102 | + { |
| 103 | + Parent = parent, |
| 104 | + DeidentifyTemplate = new DeidentifyTemplate |
| 105 | + { |
| 106 | + DisplayName = displayName, |
| 107 | + DeidentifyConfig = new DeidentifyConfig |
| 108 | + { |
| 109 | + InfoTypeTransformations = new InfoTypeTransformations |
| 110 | + { |
| 111 | + Transformations = |
| 112 | + { |
| 113 | + new InfoTypeTransformations.Types.InfoTypeTransformation |
| 114 | + { |
| 115 | + PrimitiveTransformation = new PrimitiveTransformation |
| 116 | + { |
| 117 | + ReplaceConfig = new ReplaceValueConfig |
| 118 | + { |
| 119 | + NewValue = new Value { StringValue = "[REDACTED]" }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + TemplateId = templateId, |
| 128 | + }; |
| 129 | + var response = DlpClient.CreateDeidentifyTemplate(request); |
| 130 | + RegisterDlpTemplateForCleanup(response.Name); |
| 131 | + return response.Name; |
51 | 132 | } |
52 | 133 |
|
53 | 134 | public void Dispose() |
54 | 135 | { |
55 | 136 | // Clean up resources after tests. |
56 | | - try |
| 137 | + foreach (var resourceName in _maTemplatesToCleanup) |
57 | 138 | { |
58 | | - Client.DeleteTemplate( |
59 | | - new DeleteTemplateRequest { Name = TemplateForQuickstartName.ToString() } |
60 | | - ); |
| 139 | + try |
| 140 | + { |
| 141 | + Client.DeleteTemplate(new DeleteTemplateRequest { TemplateName = resourceName }); |
| 142 | + } |
| 143 | + catch (Exception) |
| 144 | + { |
| 145 | + // Ignore errors during cleanup. |
| 146 | + } |
61 | 147 | } |
62 | | - catch (Exception) |
| 148 | + |
| 149 | + // Clean up DLP templates. |
| 150 | + foreach (var dlpTemplateName in _dlpTemplatesToCleanup) |
| 151 | + { |
| 152 | + try |
| 153 | + { |
| 154 | + if (dlpTemplateName.Contains("inspectTemplates/")) |
| 155 | + { |
| 156 | + DlpClient.DeleteInspectTemplate( |
| 157 | + new DeleteInspectTemplateRequest { Name = dlpTemplateName } |
| 158 | + ); |
| 159 | + } |
| 160 | + else if (dlpTemplateName.Contains("deidentifyTemplates/")) |
| 161 | + { |
| 162 | + DlpClient.DeleteDeidentifyTemplate( |
| 163 | + new DeleteDeidentifyTemplateRequest { Name = dlpTemplateName } |
| 164 | + ); |
| 165 | + } |
| 166 | + } |
| 167 | + catch (Exception) |
| 168 | + { |
| 169 | + // Ignore errors during cleanup. |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + public void RegisterTemplateForCleanup(TemplateName templateName) |
| 175 | + { |
| 176 | + if (templateName != null && !string.IsNullOrEmpty(templateName.ToString())) |
| 177 | + { |
| 178 | + _maTemplatesToCleanup.Add(templateName); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + public void RegisterDlpTemplateForCleanup(string templateName) |
| 183 | + { |
| 184 | + if (!string.IsNullOrEmpty(templateName)) |
63 | 185 | { |
64 | | - // Ignore errors during cleanup. |
| 186 | + _dlpTemplatesToCleanup.Add(templateName); |
65 | 187 | } |
66 | 188 | } |
| 189 | + |
| 190 | + public TemplateName CreateTemplateName(string prefix = "test-dotnet") |
| 191 | + { |
| 192 | + string templateId = $"{prefix}-{GenerateUniqueId()}"; |
| 193 | + return new TemplateName(ProjectId, LocationId, templateId); |
| 194 | + } |
67 | 195 | } |
0 commit comments