2015-05-21 19:28:27 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
2015-06-08 01:18:14 +02:00
|
|
|
"bytes"
|
2015-05-21 19:28:27 +02:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-01-05 16:06:32 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-05-21 19:28:27 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
|
|
|
|
"google.golang.org/api/googleapi"
|
|
|
|
storage "google.golang.org/api/storage/v1"
|
|
|
|
)
|
|
|
|
|
2015-06-08 01:18:14 +02:00
|
|
|
func TestAccStorage_basic(t *testing.T) {
|
2016-01-05 16:06:32 +01:00
|
|
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
2015-05-21 19:28:27 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccGoogleStorageDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-05 16:06:32 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderDefaults(bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStorageBucketExists(
|
2016-01-05 16:06:32 +01:00
|
|
|
"google_storage_bucket.bucket", bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "location", "US"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "force_destroy", "false"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccStorageCustomAttributes(t *testing.T) {
|
2016-01-05 16:06:32 +01:00
|
|
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
2015-05-21 19:28:27 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccGoogleStorageDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-05 16:06:32 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStorageBucketExists(
|
2016-01-05 16:06:32 +01:00
|
|
|
"google_storage_bucket.bucket", bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "location", "EU"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "force_destroy", "true"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-21 21:46:35 +02:00
|
|
|
func TestAccStorageStorageClass(t *testing.T) {
|
|
|
|
bucketName := fmt.Sprintf("tf-test-acc-bucket-%d", acctest.RandInt())
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccGoogleStorageDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
{
|
2017-03-04 00:51:36 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "MULTI_REGIONAL", ""),
|
2016-09-21 21:46:35 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStorageBucketExists(
|
|
|
|
"google_storage_bucket.bucket", bucketName),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-02-17 15:59:25 +01:00
|
|
|
"google_storage_bucket.bucket", "storage_class", "MULTI_REGIONAL"),
|
2016-09-21 21:46:35 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2017-03-04 00:51:36 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "NEARLINE", ""),
|
2016-09-21 21:46:35 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStorageBucketExists(
|
|
|
|
"google_storage_bucket.bucket", bucketName),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "storage_class", "NEARLINE"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
{
|
2017-03-04 00:51:36 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderStorageClass(bucketName, "REGIONAL", "us-central1"),
|
2016-09-21 21:46:35 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStorageBucketExists(
|
|
|
|
"google_storage_bucket.bucket", bucketName),
|
|
|
|
resource.TestCheckResourceAttr(
|
2017-02-17 15:59:25 +01:00
|
|
|
"google_storage_bucket.bucket", "storage_class", "REGIONAL"),
|
2017-03-04 00:51:36 +01:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "location", "us-central1"),
|
2016-09-21 21:46:35 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-05-21 19:28:27 +02:00
|
|
|
func TestAccStorageBucketUpdate(t *testing.T) {
|
2016-01-05 16:06:32 +01:00
|
|
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
2015-05-21 19:28:27 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccGoogleStorageDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-05 16:06:32 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderDefaults(bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStorageBucketExists(
|
2016-01-05 16:06:32 +01:00
|
|
|
"google_storage_bucket.bucket", bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "location", "US"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "force_destroy", "false"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-01-05 16:06:32 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStorageBucketExists(
|
2016-01-05 16:06:32 +01:00
|
|
|
"google_storage_bucket.bucket", bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "predefined_acl", "publicReadWrite"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "location", "EU"),
|
|
|
|
resource.TestCheckResourceAttr(
|
|
|
|
"google_storage_bucket.bucket", "force_destroy", "true"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAccStorageForceDestroy(t *testing.T) {
|
2016-01-05 16:06:32 +01:00
|
|
|
bucketName := fmt.Sprintf("tf-test-acl-bucket-%d", acctest.RandInt())
|
2015-05-21 19:28:27 +02:00
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccGoogleStorageDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-01-05 16:06:32 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckCloudStorageBucketExists(
|
2016-01-05 16:06:32 +01:00
|
|
|
"google_storage_bucket.bucket", bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-01-05 16:06:32 +01:00
|
|
|
Config: testGoogleStorageBucketsReaderCustomAttributes(bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-01-05 16:06:32 +01:00
|
|
|
testAccCheckCloudStorageBucketPutItem(bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-05-24 00:20:19 +02:00
|
|
|
Config: testGoogleStorageBucketsReaderCustomAttributes("idontexist"),
|
2015-05-21 19:28:27 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2016-01-05 16:06:32 +01:00
|
|
|
testAccCheckCloudStorageBucketMissing(bucketName),
|
2015-05-21 19:28:27 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:06:32 +01:00
|
|
|
func testAccCheckCloudStorageBucketExists(n string, bucketName string) resource.TestCheckFunc {
|
2015-05-21 19:28:27 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[n]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No Project_ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if found.Id != rs.Primary.ID {
|
|
|
|
return fmt.Errorf("Bucket not found")
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:06:32 +01:00
|
|
|
if found.Name != bucketName {
|
|
|
|
return fmt.Errorf("expected name %s, got %s", bucketName, found.Name)
|
|
|
|
}
|
2015-05-21 19:28:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:06:32 +01:00
|
|
|
func testAccCheckCloudStorageBucketPutItem(bucketName string) resource.TestCheckFunc {
|
2015-05-21 19:28:27 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
data := bytes.NewBufferString("test")
|
|
|
|
dataReader := bytes.NewReader(data.Bytes())
|
|
|
|
object := &storage.Object{Name: "bucketDestroyTestFile"}
|
|
|
|
|
|
|
|
// This needs to use Media(io.Reader) call, otherwise it does not go to /upload API and fails
|
2016-01-05 16:06:32 +01:00
|
|
|
if res, err := config.clientStorage.Objects.Insert(bucketName, object).Media(dataReader).Do(); err == nil {
|
2015-05-21 19:28:27 +02:00
|
|
|
fmt.Printf("Created object %v at location %v\n\n", res.Name, res.SelfLink)
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("Objects.Insert failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:06:32 +01:00
|
|
|
func testAccCheckCloudStorageBucketMissing(bucketName string) resource.TestCheckFunc {
|
2015-05-21 19:28:27 +02:00
|
|
|
return func(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
2016-01-05 16:06:32 +01:00
|
|
|
_, err := config.clientStorage.Buckets.Get(bucketName).Do()
|
2015-05-21 19:28:27 +02:00
|
|
|
if err == nil {
|
2016-01-05 16:06:32 +01:00
|
|
|
return fmt.Errorf("Found %s", bucketName)
|
2015-05-21 19:28:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-05 16:06:32 +01:00
|
|
|
|
|
|
|
return err
|
2015-05-21 19:28:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccGoogleStorageDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "google_storage_bucket" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientStorage.Buckets.Get(rs.Primary.ID).Do()
|
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Bucket still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-05 16:06:32 +01:00
|
|
|
func testGoogleStorageBucketsReaderDefaults(bucketName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-05-21 19:28:27 +02:00
|
|
|
resource "google_storage_bucket" "bucket" {
|
2016-01-05 16:06:32 +01:00
|
|
|
name = "%s"
|
|
|
|
}
|
|
|
|
`, bucketName)
|
2015-05-21 19:28:27 +02:00
|
|
|
}
|
|
|
|
|
2016-01-05 16:06:32 +01:00
|
|
|
func testGoogleStorageBucketsReaderCustomAttributes(bucketName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-05-21 19:28:27 +02:00
|
|
|
resource "google_storage_bucket" "bucket" {
|
2016-01-05 16:06:32 +01:00
|
|
|
name = "%s"
|
2015-05-21 19:28:27 +02:00
|
|
|
predefined_acl = "publicReadWrite"
|
|
|
|
location = "EU"
|
|
|
|
force_destroy = "true"
|
|
|
|
}
|
2016-01-05 16:06:32 +01:00
|
|
|
`, bucketName)
|
|
|
|
}
|
2016-09-21 21:46:35 +02:00
|
|
|
|
2017-03-04 00:51:36 +01:00
|
|
|
func testGoogleStorageBucketsReaderStorageClass(bucketName, storageClass, location string) string {
|
|
|
|
var locationBlock string
|
|
|
|
if location != "" {
|
|
|
|
locationBlock = fmt.Sprintf(`
|
|
|
|
location = "%s"`, location)
|
|
|
|
}
|
2016-09-21 21:46:35 +02:00
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "google_storage_bucket" "bucket" {
|
|
|
|
name = "%s"
|
2017-03-04 00:51:36 +01:00
|
|
|
storage_class = "%s"%s
|
2016-09-21 21:46:35 +02:00
|
|
|
}
|
2017-03-04 00:51:36 +01:00
|
|
|
`, bucketName, storageClass, locationBlock)
|
2016-09-21 21:46:35 +02:00
|
|
|
}
|