Skip to content

Commit ccd6f2f

Browse files
Add support for intercept_children in sinks (#10402) (#17932)
[upstream:5015ca8af0e86875340017deb2207f4cd6e77b3c] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent d481e3e commit ccd6f2f

5 files changed

+143
-10
lines changed

google/services/logging/resource_logging_folder_sink.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ func ResourceLoggingFolderSink() *schema.Resource {
3636
schm.Schema["include_children"] = &schema.Schema{
3737
Type: schema.TypeBool,
3838
Optional: true,
39-
ForceNew: true,
4039
Default: false,
4140
Description: `Whether or not to include children folders in the sink export. If true, logs associated with child projects are also exported; otherwise only logs relating to the provided folder are included.`,
4241
}
42+
schm.Schema["intercept_children"] = &schema.Schema{
43+
Type: schema.TypeBool,
44+
Optional: true,
45+
Default: false,
46+
Description: `Whether or not to intercept logs from child projects. If true, matching logs will not match with sinks in child resources, except _Required sinks. This sink will be visible to child resources when listing sinks.`,
47+
}
4348

4449
return schm
4550
}
@@ -54,6 +59,7 @@ func resourceLoggingFolderSinkCreate(d *schema.ResourceData, meta interface{}) e
5459
folder := resourcemanager.ParseFolderId(d.Get("folder"))
5560
id, sink := expandResourceLoggingSink(d, "folders", folder)
5661
sink.IncludeChildren = d.Get("include_children").(bool)
62+
sink.InterceptChildren = d.Get("intercept_children").(bool)
5763

5864
// The API will reject any requests that don't explicitly set 'uniqueWriterIdentity' to true.
5965
_, err = config.NewLoggingClient(userAgent).Folders.Sinks.Create(id.parent(), sink).UniqueWriterIdentity(true).Do()
@@ -85,6 +91,10 @@ func resourceLoggingFolderSinkRead(d *schema.ResourceData, meta interface{}) err
8591
return fmt.Errorf("Error setting include_children: %s", err)
8692
}
8793

94+
if err := d.Set("intercept_children", sink.InterceptChildren); err != nil {
95+
return fmt.Errorf("Error setting intercept_children: %s", err)
96+
}
97+
8898
return nil
8999
}
90100

@@ -96,10 +106,6 @@ func resourceLoggingFolderSinkUpdate(d *schema.ResourceData, meta interface{}) e
96106
}
97107

98108
sink, updateMask := expandResourceLoggingSinkForUpdate(d)
99-
// It seems the API might actually accept an update for include_children; this is not in the list of updatable
100-
// properties though and might break in the future. Always include the value to prevent it changing.
101-
sink.IncludeChildren = d.Get("include_children").(bool)
102-
sink.ForceSendFields = append(sink.ForceSendFields, "IncludeChildren")
103109

104110
// The API will reject any requests that don't explicitly set 'uniqueWriterIdentity' to true.
105111
_, err = config.NewLoggingClient(userAgent).Folders.Sinks.Patch(d.Id(), sink).

google/services/logging/resource_logging_folder_sink_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,53 @@ func testAccCheckLoggingFolderSink(sink *logging.LogSink, n string) resource.Tes
337337
return fmt.Errorf("mismatch on include_children: api has %v but client has %v", sink.IncludeChildren, includeChildren)
338338
}
339339

340+
interceptChildren := false
341+
if attributes["intercept_children"] != "" {
342+
includeChildren, err = strconv.ParseBool(attributes["intercept_children"])
343+
if err != nil {
344+
return err
345+
}
346+
}
347+
if sink.InterceptChildren != interceptChildren {
348+
return fmt.Errorf("mismatch on intercept_children: api has %v but client has %v", sink.InterceptChildren, interceptChildren)
349+
}
350+
340351
return nil
341352
}
342353
}
343354

355+
func TestAccLoggingFolderSink_updateInterceptChildren(t *testing.T) {
356+
t.Parallel()
357+
358+
sinkName := "tf-test-sink-" + acctest.RandString(t, 10)
359+
folderName := "intercepting-sink-folder"
360+
folderParent := "organizations/" + envvar.GetTestOrgFromEnv(t)
361+
362+
acctest.VcrTest(t, resource.TestCase{
363+
PreCheck: func() { acctest.AccTestPreCheck(t) },
364+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
365+
CheckDestroy: testAccCheckLoggingFolderSinkDestroyProducer(t),
366+
Steps: []resource.TestStep{
367+
{
368+
Config: testAccLoggingFolderSink_intercept_updated(sinkName, true, folderName, folderParent),
369+
},
370+
{
371+
ResourceName: "google_logging_folder_sink.intercept_update",
372+
ImportState: true,
373+
ImportStateVerify: true,
374+
},
375+
{
376+
Config: testAccLoggingFolderSink_intercept_updated(sinkName, false, folderName, folderParent),
377+
},
378+
{
379+
ResourceName: "google_logging_folder_sink.intercept_update",
380+
ImportState: true,
381+
ImportStateVerify: true,
382+
},
383+
},
384+
})
385+
}
386+
344387
func testAccLoggingFolderSink_basic(sinkName, bucketName, folderName, folderParent string) string {
345388
return fmt.Sprintf(`
346389
resource "google_logging_folder_sink" "basic" {
@@ -527,3 +570,21 @@ resource "google_folder" "my-folder" {
527570
parent = "%s"
528571
}`, sinkName, envvar.GetTestProjectFromEnv(), envvar.GetTestProjectFromEnv(), bqDatasetID, folderName, folderParent)
529572
}
573+
574+
func testAccLoggingFolderSink_intercept_updated(sinkName string, intercept_children bool, folderName string, folderParent string) string {
575+
return fmt.Sprintf(`
576+
resource "google_logging_folder_sink" "intercept_update" {
577+
name = "%s"
578+
folder = "${google_folder.intercept_folder.folder_id}"
579+
destination = "logging.googleapis.com/projects/%s"
580+
filter = "logName=\"projects/%s/logs/compute.googleapis.com%%2Factivity_log\" AND severity>=ERROR"
581+
include_children = true
582+
intercept_children = %t
583+
}
584+
585+
resource "google_folder" "intercept_folder" {
586+
display_name = "%s"
587+
parent = "%s"
588+
}
589+
`, sinkName, envvar.GetTestProjectFromEnv(), envvar.GetTestProjectFromEnv(), intercept_children, folderName, folderParent)
590+
}

google/services/logging/resource_logging_organization_sink.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ func ResourceLoggingOrganizationSink() *schema.Resource {
3434
schm.Schema["include_children"] = &schema.Schema{
3535
Type: schema.TypeBool,
3636
Optional: true,
37-
ForceNew: true,
3837
Default: false,
3938
Description: `Whether or not to include children organizations in the sink export. If true, logs associated with child projects are also exported; otherwise only logs relating to the provided organization are included.`,
4039
}
40+
schm.Schema["intercept_children"] = &schema.Schema{
41+
Type: schema.TypeBool,
42+
Optional: true,
43+
Default: false,
44+
Description: `Whether or not to intercept logs from child projects. If true, matching logs will not match with sinks in child resources, except _Required sinks. This sink will be visible to child resources when listing sinks.`,
45+
}
4146

4247
return schm
4348
}
@@ -52,6 +57,7 @@ func resourceLoggingOrganizationSinkCreate(d *schema.ResourceData, meta interfac
5257
org := d.Get("org_id").(string)
5358
id, sink := expandResourceLoggingSink(d, "organizations", org)
5459
sink.IncludeChildren = d.Get("include_children").(bool)
60+
sink.InterceptChildren = d.Get("intercept_children").(bool)
5561

5662
// Must use a unique writer, since all destinations are in projects.
5763
// The API will reject any requests that don't explicitly set 'uniqueWriterIdentity' to true.
@@ -84,6 +90,10 @@ func resourceLoggingOrganizationSinkRead(d *schema.ResourceData, meta interface{
8490
return fmt.Errorf("Error setting include_children: %s", err)
8591
}
8692

93+
if err := d.Set("intercept_children", sink.InterceptChildren); err != nil {
94+
return fmt.Errorf("Error setting intercept_children: %s", err)
95+
}
96+
8797
return nil
8898
}
8999

@@ -95,10 +105,6 @@ func resourceLoggingOrganizationSinkUpdate(d *schema.ResourceData, meta interfac
95105
}
96106

97107
sink, updateMask := expandResourceLoggingSinkForUpdate(d)
98-
// It seems the API might actually accept an update for include_children; this is not in the list of updatable
99-
// properties though and might break in the future. Always include the value to prevent it changing.
100-
sink.IncludeChildren = d.Get("include_children").(bool)
101-
sink.ForceSendFields = append(sink.ForceSendFields, "IncludeChildren")
102108

103109
// The API will reject any requests that don't explicitly set 'uniqueWriterIdentity' to true.
104110
_, err = config.NewLoggingClient(userAgent).Organizations.Sinks.Patch(d.Id(), sink).

google/services/logging/resource_logging_organization_sink_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,52 @@ func testAccCheckLoggingOrganizationSink(sink *logging.LogSink, n string) resour
266266
return fmt.Errorf("mismatch on include_children: api has %v but client has %v", sink.IncludeChildren, includeChildren)
267267
}
268268

269+
interceptChildren := false
270+
if attributes["intercept_children"] != "" {
271+
includeChildren, err = strconv.ParseBool(attributes["intercept_children"])
272+
if err != nil {
273+
return err
274+
}
275+
}
276+
if sink.InterceptChildren != interceptChildren {
277+
return fmt.Errorf("mismatch on intercept_children: api has %v but client has %v", sink.InterceptChildren, interceptChildren)
278+
}
279+
269280
return nil
270281
}
271282
}
272283

284+
func TestAccLoggingOrganizationSink_updateInterceptChildren(t *testing.T) {
285+
t.Parallel()
286+
287+
orgId := envvar.GetTestOrgFromEnv(t)
288+
sinkName := "tf-test-sink-" + acctest.RandString(t, 10)
289+
290+
acctest.VcrTest(t, resource.TestCase{
291+
PreCheck: func() { acctest.AccTestPreCheck(t) },
292+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
293+
CheckDestroy: testAccCheckLoggingOrganizationSinkDestroyProducer(t),
294+
Steps: []resource.TestStep{
295+
{
296+
Config: testAccLoggingOrganizationSink_intercept_updated(sinkName, orgId, true),
297+
},
298+
{
299+
ResourceName: "google_logging_organization_sink.intercept_update",
300+
ImportState: true,
301+
ImportStateVerify: true,
302+
},
303+
{
304+
Config: testAccLoggingOrganizationSink_intercept_updated(sinkName, orgId, false),
305+
},
306+
{
307+
ResourceName: "google_logging_organization_sink.intercept_update",
308+
ImportState: true,
309+
ImportStateVerify: true,
310+
},
311+
},
312+
})
313+
}
314+
273315
func testAccLoggingOrganizationSink_basic(sinkName, bucketName, orgId string) string {
274316
return fmt.Sprintf(`
275317
resource "google_logging_organization_sink" "basic" {
@@ -398,3 +440,15 @@ resource "google_bigquery_dataset" "logging_sink" {
398440
description = "Log sink (generated during acc test of terraform-provider-google(-beta))."
399441
}`, sinkName, orgId, envvar.GetTestProjectFromEnv(), envvar.GetTestProjectFromEnv(), bqDatasetID)
400442
}
443+
444+
func testAccLoggingOrganizationSink_intercept_updated(sinkName, orgId string, intercept_children bool) string {
445+
return fmt.Sprintf(`
446+
resource "google_logging_organization_sink" "intercept_update" {
447+
name = "%s"
448+
org_id = "%s"
449+
destination = "logging.googleapis.com/projects/%s"
450+
filter = "logName=\"projects/%s/logs/compute.googleapis.com%%2Factivity_log\" AND severity>=ERROR"
451+
include_children = true
452+
intercept_children = %t
453+
}`, sinkName, orgId, envvar.GetTestProjectFromEnv(), envvar.GetTestProjectFromEnv(), intercept_children)
454+
}

google/services/logging/resource_logging_sink.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ func expandResourceLoggingSinkForUpdate(d *schema.ResourceData) (sink *logging.L
178178
sink.BigqueryOptions = expandLoggingSinkBigqueryOptions(d.Get("bigquery_options"))
179179
updateFields = append(updateFields, "bigqueryOptions")
180180
}
181+
if d.HasChange("include_children") {
182+
updateFields = append(updateFields, "includeChildren")
183+
}
184+
if d.HasChange("intercept_children") {
185+
updateFields = append(updateFields, "interceptChildren")
186+
}
181187
updateMask = strings.Join(updateFields, ",")
182188
return
183189
}

0 commit comments

Comments
 (0)