Suppress perpetual diff on logging sink filter with surrounding whitespaces (#1206)

This commit is contained in:
Shinichi TAMURA 2018-03-21 01:49:50 +09:00 committed by Nathan McKinley
parent 82878e8f00
commit fc6a821433
6 changed files with 204 additions and 2 deletions

View File

@ -76,6 +76,31 @@ func TestAccLoggingBillingAccountSink_update(t *testing.T) {
}
}
func TestAccLoggingBillingAccountSink_heredoc(t *testing.T) {
t.Parallel()
sinkName := "tf-test-sink-" + acctest.RandString(10)
bucketName := "tf-test-sink-bucket-" + acctest.RandString(10)
billingAccount := getTestBillingAccountFromEnv(t)
var sink logging.LogSink
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLoggingBillingAccountSinkDestroy,
Steps: []resource.TestStep{
{
Config: testAccLoggingBillingAccountSink_heredoc(sinkName, bucketName, billingAccount),
Check: resource.ComposeTestCheckFunc(
testAccCheckLoggingBillingAccountSinkExists("google_logging_billing_account_sink.heredoc", &sink),
testAccCheckLoggingBillingAccountSink(&sink, "google_logging_billing_account_sink.heredoc"),
),
},
},
})
}
func testAccCheckLoggingBillingAccountSinkDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
@ -163,3 +188,24 @@ resource "google_storage_bucket" "log-bucket" {
name = "%s"
}`, name, billingAccount, getTestProjectFromEnv(), bucketName)
}
func testAccLoggingBillingAccountSink_heredoc(name, bucketName, billingAccount string) string {
return fmt.Sprintf(`
resource "google_logging_billing_account_sink" "heredoc" {
name = "%s"
billing_account = "%s"
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}"
filter = <<EOS
logName="projects/%s/logs/compute.googleapis.com%%2Factivity_log"
AND severity>=ERROR
EOS
}
resource "google_storage_bucket" "log-bucket" {
name = "%s"
}`, name, billingAccount, getTestProjectFromEnv(), bucketName)
}

View File

@ -106,6 +106,32 @@ func TestAccLoggingFolderSink_update(t *testing.T) {
}
}
func TestAccLoggingFolderSink_heredoc(t *testing.T) {
t.Parallel()
org := getTestOrgFromEnv(t)
sinkName := "tf-test-sink-" + acctest.RandString(10)
bucketName := "tf-test-sink-bucket-" + acctest.RandString(10)
folderName := "tf-test-folder-" + acctest.RandString(10)
var sink logging.LogSink
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLoggingFolderSinkDestroy,
Steps: []resource.TestStep{
{
Config: testAccLoggingFolderSink_heredoc(sinkName, bucketName, folderName, "organizations/"+org),
Check: resource.ComposeTestCheckFunc(
testAccCheckLoggingFolderSinkExists("google_logging_folder_sink.heredoc", &sink),
testAccCheckLoggingFolderSink(&sink, "google_logging_folder_sink.heredoc"),
),
},
},
})
}
func testAccCheckLoggingFolderSinkDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
@ -216,3 +242,30 @@ resource "google_folder" "my-folder" {
parent = "%s"
}`, sinkName, getTestProjectFromEnv(), bucketName, folderName, folderParent)
}
func testAccLoggingFolderSink_heredoc(sinkName, bucketName, folderName, folderParent string) string {
return fmt.Sprintf(`
resource "google_logging_folder_sink" "heredoc" {
name = "%s"
folder = "${element(split("/", google_folder.my-folder.name), 1)}"
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}"
filter = <<EOS
logName="projects/%s/logs/compute.googleapis.com%%2Factivity_log"
AND severity>=ERROR
EOS
include_children = true
}
resource "google_storage_bucket" "log-bucket" {
name = "%s"
}
resource "google_folder" "my-folder" {
display_name = "%s"
parent = "%s"
}`, sinkName, getTestProjectFromEnv(), bucketName, folderName, folderParent)
}

View File

@ -77,6 +77,31 @@ func TestAccLoggingOrganizationSink_update(t *testing.T) {
}
}
func TestAccLoggingOrganizationSink_heredoc(t *testing.T) {
t.Parallel()
org := getTestOrgFromEnv(t)
sinkName := "tf-test-sink-" + acctest.RandString(10)
bucketName := "tf-test-sink-bucket-" + acctest.RandString(10)
var sink logging.LogSink
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLoggingOrganizationSinkDestroy,
Steps: []resource.TestStep{
{
Config: testAccLoggingOrganizationSink_heredoc(sinkName, bucketName, org),
Check: resource.ComposeTestCheckFunc(
testAccCheckLoggingOrganizationSinkExists("google_logging_organization_sink.heredoc", &sink),
testAccCheckLoggingOrganizationSink(&sink, "google_logging_organization_sink.heredoc"),
),
},
},
})
}
func testAccCheckLoggingOrganizationSinkDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
@ -178,3 +203,25 @@ resource "google_storage_bucket" "log-bucket" {
name = "%s"
}`, sinkName, orgId, getTestProjectFromEnv(), bucketName)
}
func testAccLoggingOrganizationSink_heredoc(sinkName, bucketName, orgId string) string {
return fmt.Sprintf(`
resource "google_logging_organization_sink" "heredoc" {
name = "%s"
org_id = "%s"
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}"
filter = <<EOS
logName="projects/%s/logs/compute.googleapis.com%%2Factivity_log"
AND severity>=ERROR
EOS
include_children = true
}
resource "google_storage_bucket" "log-bucket" {
name = "%s"
}`, sinkName, orgId, getTestProjectFromEnv(), bucketName)
}

View File

@ -101,6 +101,35 @@ func TestAccLoggingProjectSink_updatePreservesUniqueWriter(t *testing.T) {
sinkBefore.WriterIdentity, sinkAfter.WriterIdentity)
}
}
func TestAccLoggingProjectSink_heredoc(t *testing.T) {
t.Parallel()
sinkName := "tf-test-sink-" + acctest.RandString(10)
bucketName := "tf-test-sink-bucket-" + acctest.RandString(10)
var sink logging.LogSink
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLoggingProjectSinkDestroy,
Steps: []resource.TestStep{
{
Config: testAccLoggingProjectSink_heredoc(sinkName, getTestProjectFromEnv(), bucketName),
Check: resource.ComposeTestCheckFunc(
testAccCheckLoggingProjectSinkExists("google_logging_project_sink.heredoc", &sink),
testAccCheckLoggingProjectSink(&sink, "google_logging_project_sink.heredoc"),
),
}, {
ResourceName: "google_logging_project_sink.heredoc",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccCheckLoggingProjectSinkDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
@ -208,3 +237,25 @@ resource "google_storage_bucket" "log-bucket" {
name = "%s"
}`, name, getTestProjectFromEnv(), bucketName)
}
func testAccLoggingProjectSink_heredoc(name, project, bucketName string) string {
return fmt.Sprintf(`
resource "google_logging_project_sink" "heredoc" {
name = "%s"
project = "%s"
destination = "storage.googleapis.com/${google_storage_bucket.log-bucket.name}"
filter = <<EOS
logName="projects/%s/logs/compute.googleapis.com%%2Factivity_log"
AND severity>=ERROR
EOS
unique_writer_identity = false
}
resource "google_storage_bucket" "log-bucket" {
name = "%s"
}`, name, project, project, bucketName)
}

View File

@ -19,8 +19,9 @@ func resourceLoggingSinkSchema() map[string]*schema.Schema {
},
"filter": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: optionalSurroundingSpacesSuppress,
},
"writer_identity": {

View File

@ -184,6 +184,10 @@ func optionalPrefixSuppress(prefix string) schema.SchemaDiffSuppressFunc {
}
}
func optionalSurroundingSpacesSuppress(k, old, new string, d *schema.ResourceData) bool {
return strings.TrimSpace(old) == strings.TrimSpace(new)
}
func emptyOrDefaultStringSuppress(defaultVal string) schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
return (old == "" && new == defaultVal) || (new == "" && old == defaultVal)