Skip to content

Commit e0ee53d

Browse files
author
Takashi Matsuo
committed
use parent with the global location specified
1 parent 26d4b4b commit e0ee53d

File tree

2 files changed

+38
-62
lines changed

2 files changed

+38
-62
lines changed

dlp/inspect_content.py

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -459,11 +459,12 @@ def inspect_gcs_file(
459459
url = "gs://{}/{}".format(bucket, filename)
460460
storage_config = {"cloud_storage_options": {"file_set": {"url": url}}}
461461

462-
# Convert the project id into a full resource id.
463-
parent = dlp.project_path(project)
462+
# Convert the project id into full resource ids.
463+
topic = google.cloud.pubsub.PublisherClient.topic_path(project, topic_id)
464+
parent = dlp.location_path(project, 'global')
464465

465466
# Tell the API where to send a notification when the job is complete.
466-
actions = [{"pub_sub": {"topic": "{}/topics/{}".format(parent, topic_id)}}]
467+
actions = [{"pub_sub": {"topic": topic}}]
467468

468469
# Construct the inspect_job, which defines the entire inspect content task.
469470
inspect_job = {
@@ -486,11 +487,7 @@ def inspect_gcs_file(
486487

487488
def callback(message):
488489
try:
489-
# The DlpJobName in the Pub/Sub message has the location indicator
490-
# and we need to remove that part for comparison.
491-
dlp_job_name = message.attributes["DlpJobName"].replace(
492-
'/locations/global', '')
493-
if dlp_job_name == operation.name:
490+
if message.attributes["DlpJobName"] == operation.name:
494491
# This is the message we're looking for, so acknowledge it.
495492
message.ack()
496493

@@ -627,11 +624,12 @@ def inspect_datastore(
627624
}
628625
}
629626

630-
# Convert the project id into a full resource id.
631-
parent = dlp.project_path(project)
627+
# Convert the project id into full resource ids.
628+
topic = google.cloud.pubsub.PublisherClient.topic_path(project, topic_id)
629+
parent = dlp.location_path(project, 'global')
632630

633631
# Tell the API where to send a notification when the job is complete.
634-
actions = [{"pub_sub": {"topic": "{}/topics/{}".format(parent, topic_id)}}]
632+
actions = [{"pub_sub": {"topic": topic}}]
635633

636634
# Construct the inspect_job, which defines the entire inspect content task.
637635
inspect_job = {
@@ -654,11 +652,7 @@ def inspect_datastore(
654652

655653
def callback(message):
656654
try:
657-
# The DlpJobName in the Pub/Sub message has the location indicator
658-
# and we need to remove that part for comparison.
659-
dlp_job_name = message.attributes["DlpJobName"].replace(
660-
'/locations/global', '')
661-
if dlp_job_name == operation.name:
655+
if message.attributes["DlpJobName"] == operation.name:
662656
# This is the message we're looking for, so acknowledge it.
663657
message.ack()
664658

@@ -798,11 +792,12 @@ def inspect_bigquery(
798792
}
799793
}
800794

801-
# Convert the project id into a full resource id.
802-
parent = dlp.project_path(project)
795+
# Convert the project id into full resource ids.
796+
topic = google.cloud.pubsub.PublisherClient.topic_path(project, topic_id)
797+
parent = dlp.location_path(project, 'global')
803798

804799
# Tell the API where to send a notification when the job is complete.
805-
actions = [{"pub_sub": {"topic": "{}/topics/{}".format(parent, topic_id)}}]
800+
actions = [{"pub_sub": {"topic": topic}}]
806801

807802
# Construct the inspect_job, which defines the entire inspect content task.
808803
inspect_job = {
@@ -825,11 +820,7 @@ def inspect_bigquery(
825820

826821
def callback(message):
827822
try:
828-
# The DlpJobName in the Pub/Sub message has the location indicator
829-
# and we need to remove that part for comparison.
830-
dlp_job_name = message.attributes["DlpJobName"].replace(
831-
'/locations/global', '')
832-
if dlp_job_name == operation.name:
823+
if message.attributes["DlpJobName"] == operation.name:
833824
# This is the message we're looking for, so acknowledge it.
834825
message.ack()
835826

dlp/risk.py

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ def numerical_risk_analysis(
5959
# Instantiate a client.
6060
dlp = google.cloud.dlp_v2.DlpServiceClient()
6161

62-
# Convert the project id into a full resource id.
63-
parent = dlp.project_path(project)
62+
# Convert the project id into full resource ids.
63+
topic = google.cloud.pubsub.PublisherClient.topic_path(project, topic_id)
64+
parent = dlp.location_path(project, 'global')
6465

6566
# Location info of the BigQuery table.
6667
source_table = {
@@ -70,7 +71,7 @@ def numerical_risk_analysis(
7071
}
7172

7273
# Tell the API where to send a notification when the job is complete.
73-
actions = [{"pub_sub": {"topic": "{}/topics/{}".format(parent, topic_id)}}]
74+
actions = [{"pub_sub": {"topic": topic}}]
7475

7576
# Configure risk analysis job
7677
# Give the name of the numeric column to compute risk metrics for
@@ -86,11 +87,7 @@ def numerical_risk_analysis(
8687
operation = dlp.create_dlp_job(parent, risk_job=risk_job)
8788

8889
def callback(message):
89-
# The DlpJobName in the Pub/Sub message has the location indicator
90-
# and we need to remove that part for comparison.
91-
dlp_job_name = message.attributes["DlpJobName"].replace(
92-
'/locations/global', '')
93-
if dlp_job_name == operation.name:
90+
if message.attributes["DlpJobName"] == operation.name:
9491
# This is the message we're looking for, so acknowledge it.
9592
message.ack()
9693

@@ -173,8 +170,9 @@ def categorical_risk_analysis(
173170
# Instantiate a client.
174171
dlp = google.cloud.dlp_v2.DlpServiceClient()
175172

176-
# Convert the project id into a full resource id.
177-
parent = dlp.project_path(project)
173+
# Convert the project id into full resource ids.
174+
topic = google.cloud.pubsub.PublisherClient.topic_path(project, topic_id)
175+
parent = dlp.location_path(project, 'global')
178176

179177
# Location info of the BigQuery table.
180178
source_table = {
@@ -184,7 +182,7 @@ def categorical_risk_analysis(
184182
}
185183

186184
# Tell the API where to send a notification when the job is complete.
187-
actions = [{"pub_sub": {"topic": "{}/topics/{}".format(parent, topic_id)}}]
185+
actions = [{"pub_sub": {"topic": topic}}]
188186

189187
# Configure risk analysis job
190188
# Give the name of the numeric column to compute risk metrics for
@@ -200,11 +198,7 @@ def categorical_risk_analysis(
200198
operation = dlp.create_dlp_job(parent, risk_job=risk_job)
201199

202200
def callback(message):
203-
# The DlpJobName in the Pub/Sub message has the location indicator
204-
# and we need to remove that part for comparison.
205-
dlp_job_name = message.attributes["DlpJobName"].replace(
206-
'/locations/global', '')
207-
if dlp_job_name == operation.name:
201+
if message.attributes["DlpJobName"] == operation.name:
208202
# This is the message we're looking for, so acknowledge it.
209203
message.ack()
210204

@@ -302,7 +296,8 @@ def get_values(obj):
302296
dlp = google.cloud.dlp_v2.DlpServiceClient()
303297

304298
# Convert the project id into a full resource id.
305-
parent = dlp.project_path(project)
299+
topic = google.cloud.pubsub.PublisherClient.topic_path(project, topic_id)
300+
parent = dlp.location_path(project, 'global')
306301

307302
# Location info of the BigQuery table.
308303
source_table = {
@@ -318,7 +313,7 @@ def map_fields(field):
318313
quasi_ids = map(map_fields, quasi_ids)
319314

320315
# Tell the API where to send a notification when the job is complete.
321-
actions = [{"pub_sub": {"topic": "{}/topics/{}".format(parent, topic_id)}}]
316+
actions = [{"pub_sub": {"topic": topic}}]
322317

323318
# Configure risk analysis job
324319
# Give the name of the numeric column to compute risk metrics for
@@ -332,11 +327,7 @@ def map_fields(field):
332327
operation = dlp.create_dlp_job(parent, risk_job=risk_job)
333328

334329
def callback(message):
335-
# The DlpJobName in the Pub/Sub message has the location indicator
336-
# and we need to remove that part for comparison.
337-
dlp_job_name = message.attributes["DlpJobName"].replace(
338-
'/locations/global', '')
339-
if dlp_job_name == operation.name:
330+
if message.attributes["DlpJobName"] == operation.name:
340331
# This is the message we're looking for, so acknowledge it.
341332
message.ack()
342333

@@ -437,7 +428,8 @@ def get_values(obj):
437428
dlp = google.cloud.dlp_v2.DlpServiceClient()
438429

439430
# Convert the project id into a full resource id.
440-
parent = dlp.project_path(project)
431+
topic = google.cloud.pubsub.PublisherClient.topic_path(project, topic_id)
432+
parent = dlp.location_path(project, 'global')
441433

442434
# Location info of the BigQuery table.
443435
source_table = {
@@ -453,7 +445,7 @@ def map_fields(field):
453445
quasi_ids = map(map_fields, quasi_ids)
454446

455447
# Tell the API where to send a notification when the job is complete.
456-
actions = [{"pub_sub": {"topic": "{}/topics/{}".format(parent, topic_id)}}]
448+
actions = [{"pub_sub": {"topic": topic}}]
457449

458450
# Configure risk analysis job
459451
# Give the name of the numeric column to compute risk metrics for
@@ -472,11 +464,7 @@ def map_fields(field):
472464
operation = dlp.create_dlp_job(parent, risk_job=risk_job)
473465

474466
def callback(message):
475-
# The DlpJobName in the Pub/Sub message has the location indicator
476-
# and we need to remove that part for comparison.
477-
dlp_job_name = message.attributes["DlpJobName"].replace(
478-
'/locations/global', '')
479-
if dlp_job_name == operation.name:
467+
if message.attributes["DlpJobName"] == operation.name:
480468
# This is the message we're looking for, so acknowledge it.
481469
message.ack()
482470

@@ -590,8 +578,9 @@ def get_values(obj):
590578
# Instantiate a client.
591579
dlp = google.cloud.dlp_v2.DlpServiceClient()
592580

593-
# Convert the project id into a full resource id.
594-
parent = dlp.project_path(project)
581+
# Convert the project id into full resource ids.
582+
topic = google.cloud.pubsub.PublisherClient.topic_path(project, topic_id)
583+
parent = dlp.location_path(project, 'global')
595584

596585
# Location info of the BigQuery table.
597586
source_table = {
@@ -614,7 +603,7 @@ def map_fields(quasi_id, info_type):
614603
quasi_ids = map(map_fields, quasi_ids, info_types)
615604

616605
# Tell the API where to send a notification when the job is complete.
617-
actions = [{"pub_sub": {"topic": "{}/topics/{}".format(parent, topic_id)}}]
606+
actions = [{"pub_sub": {"topic": topic}}]
618607

619608
# Configure risk analysis job
620609
# Give the name of the numeric column to compute risk metrics for
@@ -633,11 +622,7 @@ def map_fields(quasi_id, info_type):
633622
operation = dlp.create_dlp_job(parent, risk_job=risk_job)
634623

635624
def callback(message):
636-
# The DlpJobName in the Pub/Sub message has the location indicator
637-
# and we need to remove that part for comparison.
638-
dlp_job_name = message.attributes["DlpJobName"].replace(
639-
'/locations/global', '')
640-
if dlp_job_name == operation.name:
625+
if message.attributes["DlpJobName"] == operation.name:
641626
# This is the message we're looking for, so acknowledge it.
642627
message.ack()
643628

0 commit comments

Comments
 (0)