From d485b65e755d4cd901359174c0d774d11ba71754 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Tue, 17 Jul 2018 11:52:07 -0700 Subject: [PATCH] use customizediff to allow all role-entity pairs to be unordered --- google/resource_storage_bucket_acl.go | 4 +- google/resource_storage_default_object_acl.go | 12 +++-- ...esource_storage_default_object_acl_test.go | 31 +++++++++++++ google/resource_storage_object_acl.go | 10 ++-- google/resource_storage_object_acl_test.go | 46 ++++++++++++++++++- 5 files changed, 91 insertions(+), 12 deletions(-) diff --git a/google/resource_storage_bucket_acl.go b/google/resource_storage_bucket_acl.go index 2773563cfe1..e260607ac02 100644 --- a/google/resource_storage_bucket_acl.go +++ b/google/resource_storage_bucket_acl.go @@ -17,7 +17,7 @@ func resourceStorageBucketAcl() *schema.Resource { Read: resourceStorageBucketAclRead, Update: resourceStorageBucketAclUpdate, Delete: resourceStorageBucketAclDelete, - CustomizeDiff: resourceStorageBucketAclCustomizeDiff, + CustomizeDiff: resourceStorageRoleEntityCustomizeDiff, Schema: map[string]*schema.Schema{ "bucket": &schema.Schema{ @@ -49,7 +49,7 @@ func resourceStorageBucketAcl() *schema.Resource { } } -func resourceStorageBucketAclCustomizeDiff(diff *schema.ResourceDiff, meta interface{}) error { +func resourceStorageRoleEntityCustomizeDiff(diff *schema.ResourceDiff, meta interface{}) error { keys := diff.GetChangedKeysPrefix("role_entity") if len(keys) < 1 { return nil diff --git a/google/resource_storage_default_object_acl.go b/google/resource_storage_default_object_acl.go index 8e86a4aea94..a88d08231a7 100644 --- a/google/resource_storage_default_object_acl.go +++ b/google/resource_storage_default_object_acl.go @@ -10,10 +10,11 @@ import ( func resourceStorageDefaultObjectAcl() *schema.Resource { return &schema.Resource{ - Create: resourceStorageDefaultObjectAclCreate, - Read: resourceStorageDefaultObjectAclRead, - Update: resourceStorageDefaultObjectAclUpdate, - Delete: resourceStorageDefaultObjectAclDelete, + Create: resourceStorageDefaultObjectAclCreate, + Read: resourceStorageDefaultObjectAclRead, + Update: resourceStorageDefaultObjectAclUpdate, + Delete: resourceStorageDefaultObjectAclDelete, + CustomizeDiff: resourceStorageRoleEntityCustomizeDiff, Schema: map[string]*schema.Schema{ "bucket": &schema.Schema{ @@ -24,7 +25,8 @@ func resourceStorageDefaultObjectAcl() *schema.Resource { "role_entity": &schema.Schema{ Type: schema.TypeList, - Required: true, + Optional: true, + Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, MinItems: 1, }, diff --git a/google/resource_storage_default_object_acl_test.go b/google/resource_storage_default_object_acl_test.go index 7b9ace5ea8e..bf4de1b9ff4 100644 --- a/google/resource_storage_default_object_acl_test.go +++ b/google/resource_storage_default_object_acl_test.go @@ -105,6 +105,24 @@ func TestAccStorageDefaultObjectAcl_downgrade(t *testing.T) { }) } +// Test that we allow the API to reorder our role entities without perma-diffing. +func TestAccStorageDefaultObjectAcl_unordered(t *testing.T) { + t.Parallel() + + bucketName := testBucketName() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccStorageDefaultObjectAclDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testGoogleStorageDefaultObjectAclUnordered(bucketName), + }, + }, + }) +} + func testAccCheckGoogleStorageDefaultObjectAcl(bucket, roleEntityS string) resource.TestCheckFunc { return func(s *terraform.State) error { roleEntity, _ := getRoleEntityPair(roleEntityS) @@ -185,3 +203,16 @@ resource "google_storage_default_object_acl" "acl" { } `, bucketName, roleEntity1, roleEntity2) } + +func testGoogleStorageDefaultObjectAclUnordered(bucketName string) string { + return fmt.Sprintf(` +resource "google_storage_bucket" "bucket" { + name = "%s" +} + +resource "google_storage_default_object_acl" "acl" { + bucket = "${google_storage_bucket.bucket.name}" + role_entity = ["%s", "%s", "%s", "%s", "%s"] +} +`, bucketName, roleEntityBasic1, roleEntityViewers, roleEntityOwners, roleEntityBasic2, roleEntityEditors) +} diff --git a/google/resource_storage_object_acl.go b/google/resource_storage_object_acl.go index 05b3962069f..11f7eda209e 100644 --- a/google/resource_storage_object_acl.go +++ b/google/resource_storage_object_acl.go @@ -11,10 +11,11 @@ import ( func resourceStorageObjectAcl() *schema.Resource { return &schema.Resource{ - Create: resourceStorageObjectAclCreate, - Read: resourceStorageObjectAclRead, - Update: resourceStorageObjectAclUpdate, - Delete: resourceStorageObjectAclDelete, + Create: resourceStorageObjectAclCreate, + Read: resourceStorageObjectAclRead, + Update: resourceStorageObjectAclUpdate, + Delete: resourceStorageObjectAclDelete, + CustomizeDiff: resourceStorageRoleEntityCustomizeDiff, Schema: map[string]*schema.Schema{ "bucket": &schema.Schema{ @@ -38,6 +39,7 @@ func resourceStorageObjectAcl() *schema.Resource { "role_entity": &schema.Schema{ Type: schema.TypeList, Optional: true, + Computed: true, Elem: &schema.Schema{Type: schema.TypeString}, }, }, diff --git a/google/resource_storage_object_acl_test.go b/google/resource_storage_object_acl_test.go index fd13e5fd599..3b10b6b0c34 100644 --- a/google/resource_storage_object_acl_test.go +++ b/google/resource_storage_object_acl_test.go @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - //"google.golang.org/api/storage/v1" ) var tfObjectAcl, errObjectAcl = ioutil.TempFile("", "tf-gce-test") @@ -177,6 +176,31 @@ func TestAccStorageObjectAcl_predefined(t *testing.T) { }) } +// Test that we allow the API to reorder our role entities without perma-diffing. +func TestAccStorageObjectAcl_unordered(t *testing.T) { + t.Parallel() + + bucketName := testBucketName() + objectName := testAclObjectName() + objectData := []byte("data data data") + ioutil.WriteFile(tfObjectAcl.Name(), objectData, 0644) + resource.Test(t, resource.TestCase{ + PreCheck: func() { + if errObjectAcl != nil { + panic(errObjectAcl) + } + testAccPreCheck(t) + }, + Providers: testAccProviders, + CheckDestroy: testAccStorageObjectAclDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testGoogleStorageObjectAclUnordered(bucketName, objectName), + }, + }, + }) +} + func testAccCheckGoogleStorageObjectAcl(bucket, object, roleEntityS string) resource.TestCheckFunc { return func(s *terraform.State) error { roleEntity, _ := getRoleEntityPair(roleEntityS) @@ -336,3 +360,23 @@ resource "google_storage_object_acl" "acl" { } `, bucketName, objectName, tfObjectAcl.Name()) } + +func testGoogleStorageObjectAclUnordered(bucketName, objectName string) string { + return fmt.Sprintf(` +resource "google_storage_bucket" "bucket" { + name = "%s" +} + +resource "google_storage_bucket_object" "object" { + name = "%s" + bucket = "${google_storage_bucket.bucket.name}" + source = "%s" +} + +resource "google_storage_object_acl" "acl" { + object = "${google_storage_bucket_object.object.name}" + bucket = "${google_storage_bucket.bucket.name}" + role_entity = ["%s", "%s", "%s", "%s", "%s"] +} +`, bucketName, objectName, tfObjectAcl.Name(), roleEntityBasic1, roleEntityViewers, roleEntityOwners, roleEntityBasic2, roleEntityEditors) +}