github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/google/resource_storage_bucket_acl_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/acctest" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 //"google.golang.org/api/storage/v1" 11 ) 12 13 var roleEntityBasic1 = "OWNER:user-omeemail@gmail.com" 14 15 var roleEntityBasic2 = "READER:user-anotheremail@gmail.com" 16 17 var roleEntityBasic3_owner = "OWNER:user-yetanotheremail@gmail.com" 18 19 var roleEntityBasic3_reader = "READER:user-yetanotheremail@gmail.com" 20 21 func testBucketName() string { 22 return fmt.Sprintf("%s-%d", "tf-test-acl-bucket", acctest.RandInt()) 23 } 24 25 func TestAccGoogleStorageBucketAcl_basic(t *testing.T) { 26 bucketName := testBucketName() 27 resource.Test(t, resource.TestCase{ 28 PreCheck: func() { testAccPreCheck(t) }, 29 Providers: testAccProviders, 30 CheckDestroy: testAccGoogleStorageBucketAclDestroy, 31 Steps: []resource.TestStep{ 32 resource.TestStep{ 33 Config: testGoogleStorageBucketsAclBasic1(bucketName), 34 Check: resource.ComposeTestCheckFunc( 35 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic1), 36 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2), 37 ), 38 }, 39 }, 40 }) 41 } 42 43 func TestAccGoogleStorageBucketAcl_upgrade(t *testing.T) { 44 bucketName := testBucketName() 45 resource.Test(t, resource.TestCase{ 46 PreCheck: func() { testAccPreCheck(t) }, 47 Providers: testAccProviders, 48 CheckDestroy: testAccGoogleStorageBucketAclDestroy, 49 Steps: []resource.TestStep{ 50 resource.TestStep{ 51 Config: testGoogleStorageBucketsAclBasic1(bucketName), 52 Check: resource.ComposeTestCheckFunc( 53 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic1), 54 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2), 55 ), 56 }, 57 58 resource.TestStep{ 59 Config: testGoogleStorageBucketsAclBasic2(bucketName), 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2), 62 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic3_owner), 63 ), 64 }, 65 66 resource.TestStep{ 67 Config: testGoogleStorageBucketsAclBasicDelete(bucketName), 68 Check: resource.ComposeTestCheckFunc( 69 testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic1), 70 testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic2), 71 testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic3_owner), 72 ), 73 }, 74 }, 75 }) 76 } 77 78 func TestAccGoogleStorageBucketAcl_downgrade(t *testing.T) { 79 bucketName := testBucketName() 80 resource.Test(t, resource.TestCase{ 81 PreCheck: func() { testAccPreCheck(t) }, 82 Providers: testAccProviders, 83 CheckDestroy: testAccGoogleStorageBucketAclDestroy, 84 Steps: []resource.TestStep{ 85 resource.TestStep{ 86 Config: testGoogleStorageBucketsAclBasic2(bucketName), 87 Check: resource.ComposeTestCheckFunc( 88 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2), 89 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic3_owner), 90 ), 91 }, 92 93 resource.TestStep{ 94 Config: testGoogleStorageBucketsAclBasic3(bucketName), 95 Check: resource.ComposeTestCheckFunc( 96 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic2), 97 testAccCheckGoogleStorageBucketAcl(bucketName, roleEntityBasic3_reader), 98 ), 99 }, 100 101 resource.TestStep{ 102 Config: testGoogleStorageBucketsAclBasicDelete(bucketName), 103 Check: resource.ComposeTestCheckFunc( 104 testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic1), 105 testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic2), 106 testAccCheckGoogleStorageBucketAclDelete(bucketName, roleEntityBasic3_owner), 107 ), 108 }, 109 }, 110 }) 111 } 112 113 func TestAccGoogleStorageBucketAcl_predefined(t *testing.T) { 114 bucketName := testBucketName() 115 resource.Test(t, resource.TestCase{ 116 PreCheck: func() { testAccPreCheck(t) }, 117 Providers: testAccProviders, 118 CheckDestroy: testAccGoogleStorageBucketAclDestroy, 119 Steps: []resource.TestStep{ 120 resource.TestStep{ 121 Config: testGoogleStorageBucketsAclPredefined(bucketName), 122 }, 123 }, 124 }) 125 } 126 127 func testAccCheckGoogleStorageBucketAclDelete(bucket, roleEntityS string) resource.TestCheckFunc { 128 return func(s *terraform.State) error { 129 roleEntity, _ := getRoleEntityPair(roleEntityS) 130 config := testAccProvider.Meta().(*Config) 131 132 _, err := config.clientStorage.BucketAccessControls.Get(bucket, roleEntity.Entity).Do() 133 134 if err != nil { 135 return nil 136 } 137 138 return fmt.Errorf("Error, entity %s still exists", roleEntity.Entity) 139 } 140 } 141 142 func testAccCheckGoogleStorageBucketAcl(bucket, roleEntityS string) resource.TestCheckFunc { 143 return func(s *terraform.State) error { 144 roleEntity, _ := getRoleEntityPair(roleEntityS) 145 config := testAccProvider.Meta().(*Config) 146 147 res, err := config.clientStorage.BucketAccessControls.Get(bucket, roleEntity.Entity).Do() 148 149 if err != nil { 150 return fmt.Errorf("Error retrieving contents of acl for bucket %s: %s", bucket, err) 151 } 152 153 if res.Role != roleEntity.Role { 154 return fmt.Errorf("Error, Role mismatch %s != %s", res.Role, roleEntity.Role) 155 } 156 157 return nil 158 } 159 } 160 161 func testAccGoogleStorageBucketAclDestroy(s *terraform.State) error { 162 config := testAccProvider.Meta().(*Config) 163 164 for _, rs := range s.RootModule().Resources { 165 if rs.Type != "google_storage_bucket_acl" { 166 continue 167 } 168 169 bucket := rs.Primary.Attributes["bucket"] 170 171 _, err := config.clientStorage.BucketAccessControls.List(bucket).Do() 172 173 if err == nil { 174 return fmt.Errorf("Acl for bucket %s still exists", bucket) 175 } 176 } 177 178 return nil 179 } 180 181 func testGoogleStorageBucketsAclBasic1(bucketName string) string { 182 return fmt.Sprintf(` 183 resource "google_storage_bucket" "bucket" { 184 name = "%s" 185 } 186 187 resource "google_storage_bucket_acl" "acl" { 188 bucket = "${google_storage_bucket.bucket.name}" 189 role_entity = ["%s", "%s"] 190 } 191 `, bucketName, roleEntityBasic1, roleEntityBasic2) 192 } 193 194 func testGoogleStorageBucketsAclBasic2(bucketName string) string { 195 return fmt.Sprintf(` 196 resource "google_storage_bucket" "bucket" { 197 name = "%s" 198 } 199 200 resource "google_storage_bucket_acl" "acl" { 201 bucket = "${google_storage_bucket.bucket.name}" 202 role_entity = ["%s", "%s"] 203 } 204 `, bucketName, roleEntityBasic2, roleEntityBasic3_owner) 205 } 206 207 func testGoogleStorageBucketsAclBasicDelete(bucketName string) string { 208 return fmt.Sprintf(` 209 resource "google_storage_bucket" "bucket" { 210 name = "%s" 211 } 212 213 resource "google_storage_bucket_acl" "acl" { 214 bucket = "${google_storage_bucket.bucket.name}" 215 role_entity = [] 216 } 217 `, bucketName) 218 } 219 220 func testGoogleStorageBucketsAclBasic3(bucketName string) string { 221 return fmt.Sprintf(` 222 resource "google_storage_bucket" "bucket" { 223 name = "%s" 224 } 225 226 resource "google_storage_bucket_acl" "acl" { 227 bucket = "${google_storage_bucket.bucket.name}" 228 role_entity = ["%s", "%s"] 229 } 230 `, bucketName, roleEntityBasic2, roleEntityBasic3_reader) 231 } 232 233 func testGoogleStorageBucketsAclPredefined(bucketName string) string { 234 return fmt.Sprintf(` 235 resource "google_storage_bucket" "bucket" { 236 name = "%s" 237 } 238 239 resource "google_storage_bucket_acl" "acl" { 240 bucket = "${google_storage_bucket.bucket.name}" 241 predefined_acl = "projectPrivate" 242 default_acl = "projectPrivate" 243 } 244 `, bucketName) 245 }