Skip to content

Commit bdd7769

Browse files
Add PubSub action to DLP Job Trigger (#6757) (#12929)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent e394fdd commit bdd7769

4 files changed

+138
-2
lines changed

.changelog/6757.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
dlp: added pubsub action to `google_data_loss_prevention_job_trigger`
3+
```

google/resource_data_loss_prevention_job_trigger.go

+66-1
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,25 @@ A duration in seconds with up to nine fractional digits, terminated by 's'. Exam
102102
Description: `A task to execute on the completion of a job.`,
103103
Elem: &schema.Resource{
104104
Schema: map[string]*schema.Schema{
105+
"pub_sub": {
106+
Type: schema.TypeList,
107+
Optional: true,
108+
Description: `Publish a message into a given Pub/Sub topic when the job completes.`,
109+
MaxItems: 1,
110+
Elem: &schema.Resource{
111+
Schema: map[string]*schema.Schema{
112+
"topic": {
113+
Type: schema.TypeString,
114+
Required: true,
115+
Description: `Cloud Pub/Sub topic to send notifications to.`,
116+
},
117+
},
118+
},
119+
ExactlyOneOf: []string{},
120+
},
105121
"save_findings": {
106122
Type: schema.TypeList,
107-
Required: true,
123+
Optional: true,
108124
Description: `Schedule for triggered jobs`,
109125
MaxItems: 1,
110126
Elem: &schema.Resource{
@@ -160,6 +176,7 @@ Only for use with external storage. Possible values: ["BASIC_COLUMNS", "GCS_COLU
160176
},
161177
},
162178
},
179+
ExactlyOneOf: []string{},
163180
},
164181
},
165182
},
@@ -1114,6 +1131,7 @@ func flattenDataLossPreventionJobTriggerInspectJobActions(v interface{}, d *sche
11141131
}
11151132
transformed = append(transformed, map[string]interface{}{
11161133
"save_findings": flattenDataLossPreventionJobTriggerInspectJobActionsSaveFindings(original["saveFindings"], d, config),
1134+
"pub_sub": flattenDataLossPreventionJobTriggerInspectJobActionsPubSub(original["pubSub"], d, config),
11171135
})
11181136
}
11191137
return transformed
@@ -1179,6 +1197,23 @@ func flattenDataLossPreventionJobTriggerInspectJobActionsSaveFindingsOutputConfi
11791197
return v
11801198
}
11811199

1200+
func flattenDataLossPreventionJobTriggerInspectJobActionsPubSub(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1201+
if v == nil {
1202+
return nil
1203+
}
1204+
original := v.(map[string]interface{})
1205+
if len(original) == 0 {
1206+
return nil
1207+
}
1208+
transformed := make(map[string]interface{})
1209+
transformed["topic"] =
1210+
flattenDataLossPreventionJobTriggerInspectJobActionsPubSubTopic(original["topic"], d, config)
1211+
return []interface{}{transformed}
1212+
}
1213+
func flattenDataLossPreventionJobTriggerInspectJobActionsPubSubTopic(v interface{}, d *schema.ResourceData, config *Config) interface{} {
1214+
return v
1215+
}
1216+
11821217
func expandDataLossPreventionJobTriggerDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
11831218
return v, nil
11841219
}
@@ -1701,6 +1736,13 @@ func expandDataLossPreventionJobTriggerInspectJobActions(v interface{}, d Terraf
17011736
transformed["saveFindings"] = transformedSaveFindings
17021737
}
17031738

1739+
transformedPubSub, err := expandDataLossPreventionJobTriggerInspectJobActionsPubSub(original["pub_sub"], d, config)
1740+
if err != nil {
1741+
return nil, err
1742+
} else if val := reflect.ValueOf(transformedPubSub); val.IsValid() && !isEmptyValue(val) {
1743+
transformed["pubSub"] = transformedPubSub
1744+
}
1745+
17041746
req = append(req, transformed)
17051747
}
17061748
return req, nil
@@ -1800,6 +1842,29 @@ func expandDataLossPreventionJobTriggerInspectJobActionsSaveFindingsOutputConfig
18001842
return v, nil
18011843
}
18021844

1845+
func expandDataLossPreventionJobTriggerInspectJobActionsPubSub(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1846+
l := v.([]interface{})
1847+
if len(l) == 0 || l[0] == nil {
1848+
return nil, nil
1849+
}
1850+
raw := l[0]
1851+
original := raw.(map[string]interface{})
1852+
transformed := make(map[string]interface{})
1853+
1854+
transformedTopic, err := expandDataLossPreventionJobTriggerInspectJobActionsPubSubTopic(original["topic"], d, config)
1855+
if err != nil {
1856+
return nil, err
1857+
} else if val := reflect.ValueOf(transformedTopic); val.IsValid() && !isEmptyValue(val) {
1858+
transformed["topic"] = transformedTopic
1859+
}
1860+
1861+
return transformed, nil
1862+
}
1863+
1864+
func expandDataLossPreventionJobTriggerInspectJobActionsPubSubTopic(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
1865+
return v, nil
1866+
}
1867+
18031868
func resourceDataLossPreventionJobTriggerEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) {
18041869
newObj := make(map[string]interface{})
18051870
newObj["jobTrigger"] = obj

google/resource_data_loss_prevention_job_trigger_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,31 @@ func TestAccDataLossPreventionJobTrigger_dlpJobTriggerUpdateExample(t *testing.T
4141
})
4242
}
4343

44+
func TestAccDataLossPreventionJobTrigger_dlpJobTriggerPubsub(t *testing.T) {
45+
t.Parallel()
46+
47+
context := map[string]interface{}{
48+
"project": getTestProjectFromEnv(),
49+
}
50+
51+
vcrTest(t, resource.TestCase{
52+
PreCheck: func() { testAccPreCheck(t) },
53+
Providers: testAccProviders,
54+
CheckDestroy: testAccCheckDataLossPreventionJobTriggerDestroyProducer(t),
55+
Steps: []resource.TestStep{
56+
{
57+
Config: testAccDataLossPreventionJobTrigger_publishToPubSub(context),
58+
},
59+
{
60+
ResourceName: "google_data_loss_prevention_job_trigger.pubsub",
61+
ImportState: true,
62+
ImportStateVerify: true,
63+
ImportStateVerifyIgnore: []string{"parent"},
64+
},
65+
},
66+
})
67+
}
68+
4469
func testAccDataLossPreventionJobTrigger_dlpJobTriggerBasic(context map[string]interface{}) string {
4570
return Nprintf(`
4671
resource "google_data_loss_prevention_job_trigger" "basic" {
@@ -114,3 +139,35 @@ resource "google_data_loss_prevention_job_trigger" "basic" {
114139
}
115140
`, context)
116141
}
142+
143+
func testAccDataLossPreventionJobTrigger_publishToPubSub(context map[string]interface{}) string {
144+
return Nprintf(`
145+
resource "google_data_loss_prevention_job_trigger" "pubsub" {
146+
parent = "projects/%{project}"
147+
description = "Starting description"
148+
display_name = "display"
149+
150+
triggers {
151+
schedule {
152+
recurrence_period_duration = "86400s"
153+
}
154+
}
155+
156+
inspect_job {
157+
inspect_template_name = "fake"
158+
actions {
159+
pub_sub {
160+
topic = "projects/%{project}/topics/bar"
161+
}
162+
}
163+
storage_config {
164+
cloud_storage_options {
165+
file_set {
166+
url = "gs://mybucket/directory/"
167+
}
168+
}
169+
}
170+
}
171+
}
172+
`, context)
173+
}

website/docs/r/data_loss_prevention_job_trigger.html.markdown

+12-1
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,15 @@ The following arguments are supported:
321321
<a name="nested_actions"></a>The `actions` block supports:
322322

323323
* `save_findings` -
324-
(Required)
324+
(Optional)
325325
Schedule for triggered jobs
326326
Structure is [documented below](#nested_save_findings).
327327

328+
* `pub_sub` -
329+
(Optional)
330+
Publish a message into a given Pub/Sub topic when the job completes.
331+
Structure is [documented below](#nested_pub_sub).
332+
328333

329334
<a name="nested_save_findings"></a>The `save_findings` block supports:
330335

@@ -368,6 +373,12 @@ The following arguments are supported:
368373
Name of the table. If is not set a new one will be generated for you with the following format:
369374
`dlp_googleapis_yyyy_mm_dd_[dlp_job_id]`. Pacific timezone will be used for generating the date details.
370375

376+
<a name="nested_pub_sub"></a>The `pub_sub` block supports:
377+
378+
* `topic` -
379+
(Required)
380+
Cloud Pub/Sub topic to send notifications to.
381+
371382
## Attributes Reference
372383

373384
In addition to the arguments listed above, the following computed attributes are exported:

0 commit comments

Comments
 (0)