/// <summary>Snippet for CreateDlpJob</summary> public void CreateDlpJob_RequestObject() { // Snippet: CreateDlpJob(CreateDlpJobRequest,CallSettings) // Create client DlpServiceClient dlpServiceClient = DlpServiceClient.Create(); // Initialize request argument(s) CreateDlpJobRequest request = new CreateDlpJobRequest { ParentAsProjectName = new ProjectName("[PROJECT]"), }; // Make the request DlpJob response = dlpServiceClient.CreateDlpJob(request); // End snippet }
/// <summary>Snippet for CreateDlpJobAsync</summary> public async Task CreateDlpJobAsync_RequestObject() { // Snippet: CreateDlpJobAsync(CreateDlpJobRequest,CallSettings) // Additional: CreateDlpJobAsync(CreateDlpJobRequest,CancellationToken) // Create client DlpServiceClient dlpServiceClient = await DlpServiceClient.CreateAsync(); // Initialize request argument(s) CreateDlpJobRequest request = new CreateDlpJobRequest { ParentAsProjectName = new ProjectName("[PROJECT]"), }; // Make the request DlpJob response = await dlpServiceClient.CreateDlpJobAsync(request); // End snippet }
public async Task CreateDlpJobAsync() { Mock <DlpService.DlpServiceClient> mockGrpcClient = new Mock <DlpService.DlpServiceClient>(MockBehavior.Strict); CreateDlpJobRequest request = new CreateDlpJobRequest { ParentAsProjectName = new ProjectName("[PROJECT]"), }; DlpJob expectedResponse = new DlpJob { Name = "name3373707", JobTriggerName = "jobTriggerName1819490804", }; mockGrpcClient.Setup(x => x.CreateDlpJobAsync(request, It.IsAny <CallOptions>())) .Returns(new Grpc.Core.AsyncUnaryCall <DlpJob>(Task.FromResult(expectedResponse), null, null, null, null)); DlpServiceClient client = new DlpServiceClientImpl(mockGrpcClient.Object, null); DlpJob response = await client.CreateDlpJobAsync(request); Assert.Same(expectedResponse, response); mockGrpcClient.VerifyAll(); }
public void CreateDlpJob() { Mock <DlpService.DlpServiceClient> mockGrpcClient = new Mock <DlpService.DlpServiceClient>(MockBehavior.Strict); CreateDlpJobRequest request = new CreateDlpJobRequest { ParentAsProjectName = new ProjectName("[PROJECT]"), }; DlpJob expectedResponse = new DlpJob { Name = "name3373707", JobTriggerName = "jobTriggerName1819490804", }; mockGrpcClient.Setup(x => x.CreateDlpJob(request, It.IsAny <CallOptions>())) .Returns(expectedResponse); DlpServiceClient client = new DlpServiceClientImpl(mockGrpcClient.Object, null); DlpJob response = client.CreateDlpJob(request); Assert.Same(expectedResponse, response); mockGrpcClient.VerifyAll(); }
public static object Inspect( string projectId, string minLikelihood, int maxFindings, bool includeQuote, string kindName, string namespaceId, IEnumerable <InfoType> infoTypes, IEnumerable <CustomInfoType> customInfoTypes, string datasetId, string tableId) { var inspectJob = new InspectJobConfig { StorageConfig = new StorageConfig { DatastoreOptions = new DatastoreOptions { Kind = new KindExpression { Name = kindName }, PartitionId = new PartitionId { NamespaceId = namespaceId, ProjectId = projectId, } }, TimespanConfig = new StorageConfig.Types.TimespanConfig { StartTime = Timestamp.FromDateTime(System.DateTime.UtcNow.AddYears(-1)), EndTime = Timestamp.FromDateTime(System.DateTime.UtcNow) } }, InspectConfig = new InspectConfig { InfoTypes = { infoTypes }, CustomInfoTypes = { customInfoTypes }, Limits = new FindingLimits { MaxFindingsPerRequest = maxFindings }, ExcludeInfoTypes = false, IncludeQuote = includeQuote, MinLikelihood = (Likelihood)System.Enum.Parse(typeof(Likelihood), minLikelihood) }, Actions = { new Google.Cloud.Dlp.V2.Action { // Save results in BigQuery Table SaveFindings = new Google.Cloud.Dlp.V2.Action.Types.SaveFindings { OutputConfig = new OutputStorageConfig { Table = new Google.Cloud.Dlp.V2.BigQueryTable { ProjectId = projectId, DatasetId = datasetId, TableId = tableId } } }, } } }; // Issue Create Dlp Job Request var client = DlpServiceClient.Create(); var request = new CreateDlpJobRequest { InspectJob = inspectJob, ParentAsProjectName = new ProjectName(projectId), }; // We need created job name var dlpJob = client.CreateDlpJob(request); var jobName = dlpJob.Name; // Make sure the job finishes before inspecting the results. // Alternatively, we can inspect results opportunistically, but // for testing purposes, we want consistent outcome var finishedJob = EnsureJobFinishes(projectId, jobName); var bigQueryClient = BigQueryClient.Create(projectId); var table = bigQueryClient.GetTable(datasetId, tableId); // Return only first page of 10 rows Console.WriteLine("DLP v2 Results:"); var firstPage = table.ListRows(new ListRowsOptions { StartIndex = 0, PageSize = 10 }); foreach (var item in firstPage) { Console.WriteLine($"\t {item[""]}"); } return(finishedJob); }
// [END dlp_inspect_datastore] // [START dlp_inspect_gcs] public static object InspectGCS( string projectId, string minLikelihood, int maxFindings, bool includeQuote, IEnumerable <InfoType> infoTypes, string bucketName, string topicId, string subscriptionId) { var inspectJob = new InspectJobConfig { StorageConfig = new StorageConfig { CloudStorageOptions = new CloudStorageOptions { FileSet = new CloudStorageOptions.Types.FileSet { Url = $"gs://{bucketName}/*.txt" }, BytesLimitPerFile = 1073741824 }, }, InspectConfig = new InspectConfig { InfoTypes = { infoTypes }, ExcludeInfoTypes = false, IncludeQuote = includeQuote, Limits = new FindingLimits { MaxFindingsPerRequest = maxFindings }, MinLikelihood = (Likelihood)System.Enum.Parse(typeof(Likelihood), minLikelihood) }, Actions = { new Google.Cloud.Dlp.V2.Action { // Send results to Pub/Sub topic PubSub = new Google.Cloud.Dlp.V2.Action.Types.PublishToPubSub { Topic = topicId, } } } }; // Issue Create Dlp Job Request DlpServiceClient client = DlpServiceClient.Create(); var request = new CreateDlpJobRequest { InspectJob = inspectJob, ParentAsProjectName = new Google.Cloud.Dlp.V2.ProjectName(projectId), }; // We need created job name var dlpJob = client.CreateDlpJob(request); // Get a pub/sub subscription and listen for DLP results var fireEvent = new ManualResetEventSlim(); var subscriptionName = new SubscriptionName(projectId, subscriptionId); var subscriberClient = SubscriberServiceApiClient.Create(); var subscriber = SubscriberClient.Create(subscriptionName, new[] { subscriberClient }); subscriber.StartAsync( (pubSubMessage, cancellationToken) => { // Given a message that we receive on this subscription, we should either acknowledge or decline it if (pubSubMessage.Attributes["DlpJobName"] == dlpJob.Name) { fireEvent.Set(); return(Task.FromResult(SubscriberClient.Reply.Ack)); } return(Task.FromResult(SubscriberClient.Reply.Nack)); }); // We block here until receiving a signal from a separate thread that is waiting on a message indicating receiving a result of Dlp job if (fireEvent.Wait(TimeSpan.FromMinutes(1))) { // Stop the thread that is listening to messages as a result of StartAsync call earlier subscriber.StopAsync(CancellationToken.None).Wait(); // Now we can inspect full job results var job = client.GetDlpJob(new GetDlpJobRequest { DlpJobName = new DlpJobName(projectId, dlpJob.Name) }); // Inspect Job details Console.WriteLine($"Processed bytes: {job.InspectDetails.Result.ProcessedBytes}"); Console.WriteLine($"Total estimated bytes: {job.InspectDetails.Result.TotalEstimatedBytes}"); var stats = job.InspectDetails.Result.InfoTypeStats; Console.WriteLine("Found stats:"); foreach (var stat in stats) { Console.WriteLine($"{stat.InfoType.Name}"); } } else { Console.WriteLine("Error: The wait failed on timeout"); } return(0); }