2017-12-07 21:12:25 +00:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
2018-02-23 23:14:24 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2017-12-07 21:12:25 +00:00
|
|
|
)
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func TestAccStorageBucketIamBinding(t *testing.T) {
|
2017-12-07 21:12:25 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := acctest.RandomWithPrefix("tf-test")
|
|
|
|
account := acctest.RandomWithPrefix("tf-test")
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
// Test IAM Binding creation
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccStorageBucketIamBinding_basic(bucket, account),
|
2017-12-07 21:12:25 +00:00
|
|
|
Check: testAccCheckGoogleStorageBucketIam(bucket, "roles/storage.objectViewer", []string{
|
|
|
|
fmt.Sprintf("serviceAccount:%s-1@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Test IAM Binding update
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccStorageBucketIamBinding_update(bucket, account),
|
2017-12-07 21:12:25 +00:00
|
|
|
Check: testAccCheckGoogleStorageBucketIam(bucket, "roles/storage.objectViewer", []string{
|
|
|
|
fmt.Sprintf("serviceAccount:%s-1@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
|
|
|
|
fmt.Sprintf("serviceAccount:%s-2@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func TestAccStorageBucketIamMember(t *testing.T) {
|
2017-12-07 21:12:25 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
bucket := acctest.RandomWithPrefix("tf-test")
|
|
|
|
account := acctest.RandomWithPrefix("tf-test")
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
|
|
|
// Test Iam Member creation (no update for member, no need to test)
|
2018-02-23 23:14:24 +00:00
|
|
|
Config: testAccStorageBucketIamMember_basic(bucket, account),
|
2017-12-07 21:12:25 +00:00
|
|
|
Check: testAccCheckGoogleStorageBucketIam(bucket, "roles/storage.admin", []string{
|
|
|
|
fmt.Sprintf("serviceAccount:%s-1@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckGoogleStorageBucketIam(bucket, role string, members []string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
p, err := config.clientStorage.Buckets.GetIamPolicy(bucket).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, binding := range p.Bindings {
|
|
|
|
if binding.Role == role {
|
|
|
|
sort.Strings(members)
|
|
|
|
sort.Strings(binding.Members)
|
|
|
|
|
|
|
|
if reflect.DeepEqual(members, binding.Members) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Binding found but expected members is %v, got %v", members, binding.Members)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("No binding for role %q", role)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func testAccStorageBucketIamBinding_basic(bucket, account string) string {
|
2017-12-07 21:12:25 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_storage_bucket" "bucket" {
|
|
|
|
name = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_service_account" "test-account-1" {
|
|
|
|
account_id = "%s-1"
|
|
|
|
display_name = "Iam Testing Account"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_storage_bucket_iam_binding" "foo" {
|
|
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
|
|
role = "roles/storage.objectViewer"
|
|
|
|
members = [
|
|
|
|
"serviceAccount:${google_service_account.test-account-1.email}",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`, bucket, account)
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func testAccStorageBucketIamBinding_update(bucket, account string) string {
|
2017-12-07 21:12:25 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_storage_bucket" "bucket" {
|
|
|
|
name = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_service_account" "test-account-1" {
|
|
|
|
account_id = "%s-1"
|
|
|
|
display_name = "Iam Testing Account"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_service_account" "test-account-2" {
|
|
|
|
account_id = "%s-2"
|
|
|
|
display_name = "Iam Testing Account"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_storage_bucket_iam_binding" "foo" {
|
|
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
|
|
role = "roles/storage.objectViewer"
|
|
|
|
members = [
|
|
|
|
"serviceAccount:${google_service_account.test-account-1.email}",
|
|
|
|
"serviceAccount:${google_service_account.test-account-2.email}",
|
|
|
|
]
|
|
|
|
}
|
|
|
|
`, bucket, account, account)
|
|
|
|
}
|
|
|
|
|
2018-02-23 23:14:24 +00:00
|
|
|
func testAccStorageBucketIamMember_basic(bucket, account string) string {
|
2017-12-07 21:12:25 +00:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_storage_bucket" "bucket" {
|
|
|
|
name = "%s"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_service_account" "test-account-1" {
|
|
|
|
account_id = "%s-1"
|
|
|
|
display_name = "Iam Testing Account"
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "google_storage_bucket_iam_member" "foo" {
|
|
|
|
bucket = "${google_storage_bucket.bucket.name}"
|
|
|
|
role = "roles/storage.admin"
|
|
|
|
member = "serviceAccount:${google_service_account.test-account-1.email}"
|
|
|
|
}
|
|
|
|
`, bucket, account)
|
|
|
|
}
|