github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    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  var testAclBucketName = fmt.Sprintf("%s-%d", "tf-test-acl-bucket", genRandInt())
    22  
    23  func TestAccGoogleStorageBucketAcl_basic(t *testing.T) {
    24  	resource.Test(t, resource.TestCase{
    25  		PreCheck:     func() { testAccPreCheck(t) },
    26  		Providers:    testAccProviders,
    27  		CheckDestroy: testAccGoogleStorageBucketAclDestroy,
    28  		Steps: []resource.TestStep{
    29  			resource.TestStep{
    30  				Config: testGoogleStorageBucketsAclBasic1,
    31  				Check: resource.ComposeTestCheckFunc(
    32  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic1),
    33  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic2),
    34  				),
    35  			},
    36  		},
    37  	})
    38  }
    39  
    40  func TestAccGoogleStorageBucketAcl_upgrade(t *testing.T) {
    41  	resource.Test(t, resource.TestCase{
    42  		PreCheck:     func() { testAccPreCheck(t) },
    43  		Providers:    testAccProviders,
    44  		CheckDestroy: testAccGoogleStorageBucketAclDestroy,
    45  		Steps: []resource.TestStep{
    46  			resource.TestStep{
    47  				Config: testGoogleStorageBucketsAclBasic1,
    48  				Check: resource.ComposeTestCheckFunc(
    49  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic1),
    50  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic2),
    51  				),
    52  			},
    53  
    54  			resource.TestStep{
    55  				Config: testGoogleStorageBucketsAclBasic2,
    56  				Check: resource.ComposeTestCheckFunc(
    57  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic2),
    58  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic3_owner),
    59  				),
    60  			},
    61  
    62  			resource.TestStep{
    63  				Config: testGoogleStorageBucketsAclBasicDelete,
    64  				Check: resource.ComposeTestCheckFunc(
    65  					testAccCheckGoogleStorageBucketAclDelete(testAclBucketName, roleEntityBasic1),
    66  					testAccCheckGoogleStorageBucketAclDelete(testAclBucketName, roleEntityBasic2),
    67  					testAccCheckGoogleStorageBucketAclDelete(testAclBucketName, roleEntityBasic3_owner),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func TestAccGoogleStorageBucketAcl_downgrade(t *testing.T) {
    75  	resource.Test(t, resource.TestCase{
    76  		PreCheck:     func() { testAccPreCheck(t) },
    77  		Providers:    testAccProviders,
    78  		CheckDestroy: testAccGoogleStorageBucketAclDestroy,
    79  		Steps: []resource.TestStep{
    80  			resource.TestStep{
    81  				Config: testGoogleStorageBucketsAclBasic2,
    82  				Check: resource.ComposeTestCheckFunc(
    83  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic2),
    84  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic3_owner),
    85  				),
    86  			},
    87  
    88  			resource.TestStep{
    89  				Config: testGoogleStorageBucketsAclBasic3,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic2),
    92  					testAccCheckGoogleStorageBucketAcl(testAclBucketName, roleEntityBasic3_reader),
    93  				),
    94  			},
    95  
    96  			resource.TestStep{
    97  				Config: testGoogleStorageBucketsAclBasicDelete,
    98  				Check: resource.ComposeTestCheckFunc(
    99  					testAccCheckGoogleStorageBucketAclDelete(testAclBucketName, roleEntityBasic1),
   100  					testAccCheckGoogleStorageBucketAclDelete(testAclBucketName, roleEntityBasic2),
   101  					testAccCheckGoogleStorageBucketAclDelete(testAclBucketName, roleEntityBasic3_owner),
   102  				),
   103  			},
   104  		},
   105  	})
   106  }
   107  
   108  func TestAccGoogleStorageBucketAcl_predefined(t *testing.T) {
   109  	resource.Test(t, resource.TestCase{
   110  		PreCheck:     func() { testAccPreCheck(t) },
   111  		Providers:    testAccProviders,
   112  		CheckDestroy: testAccGoogleStorageBucketAclDestroy,
   113  		Steps: []resource.TestStep{
   114  			resource.TestStep{
   115  				Config: testGoogleStorageBucketsAclPredefined,
   116  			},
   117  		},
   118  	})
   119  }
   120  
   121  func testAccCheckGoogleStorageBucketAclDelete(bucket, roleEntityS string) resource.TestCheckFunc {
   122  	return func(s *terraform.State) error {
   123  		roleEntity, _ := getRoleEntityPair(roleEntityS)
   124  		config := testAccProvider.Meta().(*Config)
   125  
   126  		_, err := config.clientStorage.BucketAccessControls.Get(bucket, roleEntity.Entity).Do()
   127  
   128  		if err != nil {
   129  			return nil
   130  		}
   131  
   132  		return fmt.Errorf("Error, entity %s still exists", roleEntity.Entity)
   133  	}
   134  }
   135  
   136  func testAccCheckGoogleStorageBucketAcl(bucket, roleEntityS string) resource.TestCheckFunc {
   137  	return func(s *terraform.State) error {
   138  		roleEntity, _ := getRoleEntityPair(roleEntityS)
   139  		config := testAccProvider.Meta().(*Config)
   140  
   141  		res, err := config.clientStorage.BucketAccessControls.Get(bucket, roleEntity.Entity).Do()
   142  
   143  		if err != nil {
   144  			return fmt.Errorf("Error retrieving contents of acl for bucket %s: %s", bucket, err)
   145  		}
   146  
   147  		if res.Role != roleEntity.Role {
   148  			return fmt.Errorf("Error, Role mismatch %s != %s", res.Role, roleEntity.Role)
   149  		}
   150  
   151  		return nil
   152  	}
   153  }
   154  
   155  func testAccGoogleStorageBucketAclDestroy(s *terraform.State) error {
   156  	config := testAccProvider.Meta().(*Config)
   157  
   158  	for _, rs := range s.RootModule().Resources {
   159  		if rs.Type != "google_storage_bucket_acl" {
   160  			continue
   161  		}
   162  
   163  		bucket := rs.Primary.Attributes["bucket"]
   164  
   165  		_, err := config.clientStorage.BucketAccessControls.List(bucket).Do()
   166  
   167  		if err == nil {
   168  			return fmt.Errorf("Acl for bucket %s still exists", bucket)
   169  		}
   170  	}
   171  
   172  	return nil
   173  }
   174  
   175  var testGoogleStorageBucketsAclBasic1 = fmt.Sprintf(`
   176  resource "google_storage_bucket" "bucket" {
   177  	name = "%s"
   178  }
   179  
   180  resource "google_storage_bucket_acl" "acl" {
   181  	bucket = "${google_storage_bucket.bucket.name}"
   182  	role_entity = ["%s", "%s"]
   183  }
   184  `, testAclBucketName, roleEntityBasic1, roleEntityBasic2)
   185  
   186  var testGoogleStorageBucketsAclBasic2 = fmt.Sprintf(`
   187  resource "google_storage_bucket" "bucket" {
   188  	name = "%s"
   189  }
   190  
   191  resource "google_storage_bucket_acl" "acl" {
   192  	bucket = "${google_storage_bucket.bucket.name}"
   193  	role_entity = ["%s", "%s"]
   194  }
   195  `, testAclBucketName, roleEntityBasic2, roleEntityBasic3_owner)
   196  
   197  var testGoogleStorageBucketsAclBasicDelete = fmt.Sprintf(`
   198  resource "google_storage_bucket" "bucket" {
   199  	name = "%s"
   200  }
   201  
   202  resource "google_storage_bucket_acl" "acl" {
   203  	bucket = "${google_storage_bucket.bucket.name}"
   204  	role_entity = []
   205  }
   206  `, testAclBucketName)
   207  
   208  var testGoogleStorageBucketsAclBasic3 = 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 = ["%s", "%s"]
   216  }
   217  `, testAclBucketName, roleEntityBasic2, roleEntityBasic3_reader)
   218  
   219  var testGoogleStorageBucketsAclPredefined = fmt.Sprintf(`
   220  resource "google_storage_bucket" "bucket" {
   221  	name = "%s"
   222  }
   223  
   224  resource "google_storage_bucket_acl" "acl" {
   225  	bucket = "${google_storage_bucket.bucket.name}"
   226  	predefined_acl = "projectPrivate"
   227  	default_acl = "projectPrivate"
   228  }
   229  `, testAclBucketName)