Skip to content

Commit

Permalink
use customizediff to allow all role-entity pairs to be unordered (#1787)
Browse files Browse the repository at this point in the history
  • Loading branch information
danawillow authored Jul 24, 2018
1 parent b4bb79f commit b8cf81e
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
4 changes: 2 additions & 2 deletions google/resource_storage_bucket_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions google/resource_storage_default_object_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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,
},
Expand Down
31 changes: 31 additions & 0 deletions google/resource_storage_default_object_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
10 changes: 6 additions & 4 deletions google/resource_storage_object_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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},
},
},
Expand Down
46 changes: 45 additions & 1 deletion google/resource_storage_object_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

0 comments on commit b8cf81e

Please sign in to comment.