Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclusions option for logging sink resources #7335

Merged
merged 5 commits into from
Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions google/resource_logging_project_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ func TestAccLoggingProjectSink_heredoc(t *testing.T) {
})
}

func TestAccLoggingProjectSink_loggingbucket(t *testing.T) {
t.Parallel()

sinkName := "tf-test-sink-" + randString(t, 10)
logBucketID := "tf-test-logbucket-" + randString(t, 10)

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLoggingProjectSinkDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccLoggingProjectSink_loggingbucket(sinkName, getTestProjectFromEnv(), logBucketID),
},
{
ResourceName: "google_logging_project_sink.loggingbucket",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckLoggingProjectSinkDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := googleProviderConfig(t)
Expand Down Expand Up @@ -248,3 +271,32 @@ resource "google_bigquery_dataset" "logging_sink" {
}
`, sinkName, getTestProjectFromEnv(), getTestProjectFromEnv(), bqDatasetID)
}

func testAccLoggingProjectSink_loggingbucket(name, project, logBucketID string) string {
return fmt.Sprintf(`
resource "google_logging_project_sink" "loggingbucket" {
name = "%s"
project = "%s"
destination = "logging.googleapis.com/projects/%s/locations/global/buckets/${google_logging_project_bucket_config.logbucket.id}"
exclusions {
name = "ex1"
description = "test"
filter = "resource.type = k8s_container"
}

exclusions {
name = "ex2"
description = "test-2"
filter = "resource.type = k8s_container"
}

unique_writer_identity = false
}

resource "google_logging_project_bucket_config" "logbucket" {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this needs a project field defined.

Getting this error: on config992620966/terraform_plugin_test.tf line 21, in resource "google_logging_project_bucket_config" "logbucket": 21: resource "google_logging_project_bucket_config" "logbucket" { The argument "project" is required, but no definition was found. --- FAIL: TestAccLoggingProjectSink_loggingbucket (2.32s)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for this! I was running tests incorrectly locally and I thought they ran in CI. I have fixed this issue and a couple issues more.

stAccLoggingProjectSink_loggingbucket'
==> Checking source code against gofmt...
==> Checking that code complies with gofmt requirements...
go generate  ./...
TF_ACC=1 TF_SCHEMA_PANIC_ON_ERROR=1 go test ./google -v -run=TestAccLoggingProjectSink_loggingbucket -timeout 240m -ldflags="-X=github.com/hashicorp/terraform-provider-google/version.ProviderVersion=acc"
=== RUN   TestAccLoggingProjectSink_loggingbucket
=== PAUSE TestAccLoggingProjectSink_loggingbucket
=== CONT  TestAccLoggingProjectSink_loggingbucket
--- PASS: TestAccLoggingProjectSink_loggingbucket (9.01s)
PASS
ok  	github.com/hashicorp/terraform-provider-google/google	9.955s

location = "global"
retention_days = 30
bucket_id = "%s"
}
`, name, project, project, logBucketID)
}
82 changes: 82 additions & 0 deletions google/resource_logging_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -30,6 +31,38 @@ func resourceLoggingSinkSchema() map[string]*schema.Schema {
Description: `The filter to apply when exporting logs. Only log entries that match the filter are exported.`,
},

"exclusions": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: `Log entries that match any of the exclusion filters will not be exported. If a log entry is matched by both filter and one of exclusion_filters it will not be exported.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: `A client-assigned identifier, such as "load-balancer-exclusion". Identifiers are limited to 100 characters and can include only letters, digits, underscores, hyphens, and periods. First character has to be alphanumeric.`,
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: `A description of this exclusion.`,
},
"filter": {
Type: schema.TypeString,
Required: true,
Description: `An advanced logs filter that matches the log entries to be excluded. By using the sample function, you can exclude less than 100% of the matching log entries`,
},
"disabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: `If set to True, then this exclusion is disabled and it does not exclude any log entries`,
},
},
},
},

"writer_identity": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -66,6 +99,7 @@ func expandResourceLoggingSink(d *schema.ResourceData, resourceType, resourceId
Name: d.Get("name").(string),
Destination: d.Get("destination").(string),
Filter: d.Get("filter").(string),
Exclusions: expandLoggingSinkExclusions(d.Get("exclusions")),
BigqueryOptions: expandLoggingSinkBigqueryOptions(d.Get("bigquery_options")),
}
return id, &sink
Expand All @@ -84,6 +118,9 @@ func flattenResourceLoggingSink(d *schema.ResourceData, sink *logging.LogSink) e
if err := d.Set("writer_identity", sink.WriterIdentity); err != nil {
return fmt.Errorf("Error setting writer_identity: %s", err)
}
if err := d.Set("exclusions", flattenLoggingSinkExclusion(sink.Exclusions)); err != nil {
return fmt.Errorf("Error setting exclusions: %s", err)
}
if err := d.Set("bigquery_options", flattenLoggingSinkBigqueryOptions(sink.BigqueryOptions)); err != nil {
return fmt.Errorf("Error setting bigquery_options: %s", err)
}
Expand All @@ -107,6 +144,10 @@ func expandResourceLoggingSinkForUpdate(d *schema.ResourceData) (sink *logging.L
if d.HasChange("filter") {
updateFields = append(updateFields, "filter")
}
if d.HasChange("exclusions") {
sink.Exclusions = expandLoggingSinkExclusions(d.Get("exclusions"))
updateFields = append(updateFields, "exclusions")
}
if d.HasChange("bigquery_options") {
sink.BigqueryOptions = expandLoggingSinkBigqueryOptions(d.Get("bigquery_options"))
updateFields = append(updateFields, "bigqueryOptions")
Expand Down Expand Up @@ -141,6 +182,47 @@ func flattenLoggingSinkBigqueryOptions(o *logging.BigQueryOptions) []map[string]
return []map[string]interface{}{oMap}
}

func expandLoggingSinkExclusions(v interface{}) []*logging.LogExclusion {
if v == nil {
return nil
}
exclusions := v.([]interface{})
if len(exclusions) == 0 {
return nil
}
results := make([]*logging.LogExclusion, 0, len(exclusions))
for _, e := range exclusions {
exclusion := e.(map[string]interface{})
disabled, _ := exclusion["disabled"].(bool)
results = append(results, &logging.LogExclusion{
Name: exclusion["name"].(string),
Description: exclusion["description"].(string),
Filter: exclusion["filter"].(string),
Disabled: disabled,
})
}
return results
}

func flattenLoggingSinkExclusion(exclusions []*logging.LogExclusion) []map[string]interface{} {
if exclusions == nil {
return nil
}
flattenedExclusions := make([]map[string]interface{}, 0, len(exclusions))
for _, e := range exclusions {
flattenedExclusion := map[string]interface{}{
"name": e.Name,
"description": e.Description,
"filter": e.Filter,
"disabled": strconv.FormatBool(e.Disabled),
}
flattenedExclusions = append(flattenedExclusions, flattenedExclusion)

}

return flattenedExclusions
}

func resourceLoggingSinkImportState(sinkType string) schema.StateFunc {
return func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
loggingSinkId, err := parseLoggingSinkId(d.Id())
Expand Down