2015-05-21 17:28:27 +00:00
package google
import (
2015-06-07 23:18:14 +00:00
"bytes"
2015-05-21 17:28:27 +00:00
"fmt"
2017-05-22 19:38:21 +00:00
"log"
2015-05-21 17:28:27 +00:00
"testing"
2016-01-05 15:06:32 +00:00
"github.com/hashicorp/terraform/helper/acctest"
2015-05-21 17:28:27 +00:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/googleapi"
2017-11-21 17:34:32 +00:00
"google.golang.org/api/storage/v1"
2015-05-21 17:28:27 +00:00
)
2017-05-22 19:38:21 +00:00
func TestAccStorageBucket_basic ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2016-01-05 15:06:32 +00:00
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
2015-05-21 17:28:27 +00:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
2017-05-22 19:38:21 +00:00
CheckDestroy : testAccStorageBucketDestroy ,
2015-05-21 17:28:27 +00:00
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_basic ( bucketName ) ,
2015-05-21 17:28:27 +00:00
Check : resource . ComposeTestCheckFunc (
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "force_destroy" , "false" ) ,
) ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-03-19 23:37:39 +00:00
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
2017-12-14 00:46:48 +00:00
} ,
2019-03-26 21:26:16 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportStateId : fmt . Sprintf ( "%s/%s" , getTestProjectFromEnv ( ) , bucketName ) ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2015-05-21 17:28:27 +00:00
} ,
} )
}
2018-12-13 21:13:07 +00:00
func TestAccStorageBucket_requesterPays ( t * testing . T ) {
t . Parallel ( )
bucketName := fmt . Sprintf ( "tf-test-requester-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2018-12-13 21:13:07 +00:00
Config : testAccStorageBucket_requesterPays ( bucketName , true ) ,
Check : resource . ComposeTestCheckFunc (
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "requester_pays" , "true" ) ,
) ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-12-13 21:13:07 +00:00
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
} ,
} )
}
2017-06-16 16:47:26 +00:00
func TestAccStorageBucket_lowercaseLocation ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2017-06-16 16:47:26 +00:00
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2017-06-16 16:47:26 +00:00
Config : testAccStorageBucket_lowercaseLocation ( bucketName ) ,
2019-03-26 21:26:16 +00:00
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
2017-06-16 16:47:26 +00:00
} ,
} ,
} )
}
2017-05-22 19:38:21 +00:00
func TestAccStorageBucket_customAttributes ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2016-01-05 15:06:32 +00:00
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
2015-05-21 17:28:27 +00:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
2017-05-22 19:38:21 +00:00
CheckDestroy : testAccStorageBucketDestroy ,
2015-05-21 17:28:27 +00:00
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_customAttributes ( bucketName ) ,
2019-03-26 21:26:16 +00:00
Check : resource . ComposeTestCheckFunc (
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "force_destroy" , "true" ) ,
) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
ImportStateVerifyIgnore : [ ] string { "force_destroy" } ,
} ,
} ,
} )
}
func TestAccStorageBucket_lifecycleRulesMultiple ( t * testing . T ) {
t . Parallel ( )
bucketName := fmt . Sprintf ( "tf-test-acc-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
{
Config : testAccStorageBucket_lifecycleRulesMultiple ( bucketName ) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
} ,
} )
}
func TestAccStorageBucket_lifecycleRuleStateLive ( t * testing . T ) {
t . Parallel ( )
var bucket storage . Bucket
bucketName := fmt . Sprintf ( "tf-test-acc-bucket-%d" , acctest . RandInt ( ) )
hashK := resourceGCSBucketLifecycleRuleConditionHash ( map [ string ] interface { } {
"age" : 10 ,
"with_state" : "LIVE" ,
"num_newer_versions" : 0 ,
"created_before" : "" ,
} )
attrPrefix := fmt . Sprintf ( "lifecycle_rule.0.condition.%d." , hashK )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
{
Config : testAccStorageBucket_lifecycleRule_IsLiveTrue ( bucketName ) ,
2015-05-21 17:28:27 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
2019-03-26 21:26:16 +00:00
testAccCheckStorageBucketLifecycleConditionState ( googleapi . Bool ( true ) , & bucket ) ,
2015-05-21 17:28:27 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "is_live" , "true" ) ,
2018-02-22 23:31:55 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "with_state" , "LIVE" ) ,
) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
{
Config : testAccStorageBucket_lifecycleRule_withStateLive ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
testAccCheckStorageBucketLifecycleConditionState ( googleapi . Bool ( true ) , & bucket ) ,
2015-05-21 17:28:27 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "is_live" , "true" ) ,
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , attrPrefix + "with_state" , "LIVE" ) ,
2015-05-21 17:28:27 +00:00
) ,
} ,
2019-03-26 21:26:16 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2015-05-21 17:28:27 +00:00
} ,
} )
}
2019-03-26 21:26:16 +00:00
func TestAccStorageBucket_lifecycleRuleStateArchived ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2017-06-12 12:49:34 +00:00
var bucket storage . Bucket
bucketName := fmt . Sprintf ( "tf-test-acc-bucket-%d" , acctest . RandInt ( ) )
2019-03-26 21:26:16 +00:00
hashK := resourceGCSBucketLifecycleRuleConditionHash ( map [ string ] interface { } {
"age" : 10 ,
"with_state" : "ARCHIVED" ,
"num_newer_versions" : 0 ,
"created_before" : "" ,
} )
attrPrefix := fmt . Sprintf ( "lifecycle_rule.0.condition.%d." , hashK )
2017-06-12 12:49:34 +00:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2019-03-26 21:26:16 +00:00
Config : testAccStorageBucket_lifecycleRule_emptyArchived ( bucketName ) ,
2017-06-12 12:49:34 +00:00
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
2019-03-26 21:26:16 +00:00
testAccCheckStorageBucketLifecycleConditionState ( googleapi . Bool ( false ) , & bucket ) ,
2018-02-22 23:31:55 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "is_live" , "false" ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "with_state" , "ARCHIVED" ) ,
) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
{
Config : testAccStorageBucket_lifecycleRule_isLiveFalse ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
testAccCheckStorageBucketLifecycleConditionState ( googleapi . Bool ( false ) , & bucket ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "is_live" , "false" ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "with_state" , "ARCHIVED" ) ,
) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
{
Config : testAccStorageBucket_lifecycleRule_withStateArchived ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
testAccCheckStorageBucketLifecycleConditionState ( googleapi . Bool ( false ) , & bucket ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "is_live" , "false" ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , attrPrefix + "with_state" , "ARCHIVED" ) ,
) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
} ,
} )
}
func TestAccStorageBucket_lifecycleRuleStateAny ( t * testing . T ) {
t . Parallel ( )
var bucket storage . Bucket
bucketName := fmt . Sprintf ( "tf-test-acc-bucket-%d" , acctest . RandInt ( ) )
hashKLive := resourceGCSBucketLifecycleRuleConditionHash ( map [ string ] interface { } {
"age" : 10 ,
"with_state" : "LIVE" ,
"num_newer_versions" : 0 ,
"created_before" : "" ,
} )
hashKArchived := resourceGCSBucketLifecycleRuleConditionHash ( map [ string ] interface { } {
"age" : 10 ,
"with_state" : "ARCHIVED" ,
"num_newer_versions" : 0 ,
"created_before" : "" ,
} )
hashKAny := resourceGCSBucketLifecycleRuleConditionHash ( map [ string ] interface { } {
"age" : 10 ,
"with_state" : "ANY" ,
"num_newer_versions" : 0 ,
"created_before" : "" ,
} )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
{
Config : testAccStorageBucket_lifecycleRule_withStateArchived ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
testAccCheckStorageBucketLifecycleConditionState ( googleapi . Bool ( false ) , & bucket ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , fmt . Sprintf ( "lifecycle_rule.0.condition.%d.with_state" , hashKArchived ) , "ARCHIVED" ) ,
) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
{
Config : testAccStorageBucket_lifecycleRule_withStateLive ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
testAccCheckStorageBucketLifecycleConditionState ( googleapi . Bool ( true ) , & bucket ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , fmt . Sprintf ( "lifecycle_rule.0.condition.%d.with_state" , hashKLive ) , "LIVE" ) ,
) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
{
Config : testAccStorageBucket_lifecycleRule_withStateAny ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
testAccCheckStorageBucketLifecycleConditionState ( nil , & bucket ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , fmt . Sprintf ( "lifecycle_rule.0.condition.%d.with_state" , hashKAny ) , "ANY" ) ,
) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
{
Config : testAccStorageBucket_lifecycleRule_withStateArchived ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
testAccCheckStorageBucketLifecycleConditionState ( googleapi . Bool ( false ) , & bucket ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
2019-03-26 21:26:16 +00:00
"google_storage_bucket.bucket" , fmt . Sprintf ( "lifecycle_rule.0.condition.%d.with_state" , hashKArchived ) , "ARCHIVED" ) ,
2017-06-12 12:49:34 +00:00
) ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-03-19 23:37:39 +00:00
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
2017-12-14 00:46:48 +00:00
} ,
2017-06-12 12:49:34 +00:00
} ,
} )
}
2017-05-22 19:38:21 +00:00
func TestAccStorageBucket_storageClass ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2017-05-22 19:38:21 +00:00
var bucket storage . Bucket
2019-03-21 23:23:54 +00:00
var updated storage . Bucket
2016-09-21 19:46:35 +00:00
bucketName := fmt . Sprintf ( "tf-test-acc-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
2017-05-22 19:38:21 +00:00
CheckDestroy : testAccStorageBucketDestroy ,
2016-09-21 19:46:35 +00:00
Steps : [ ] resource . TestStep {
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_storageClass ( bucketName , "MULTI_REGIONAL" , "" ) ,
2016-09-21 19:46:35 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
2016-09-21 19:46:35 +00:00
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2016-09-21 19:46:35 +00:00
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_storageClass ( bucketName , "NEARLINE" , "" ) ,
2016-09-21 19:46:35 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketExists (
2019-03-21 23:23:54 +00:00
"google_storage_bucket.bucket" , bucketName , & updated ) ,
// storage_class-only change should not recreate
testAccCheckStorageBucketWasUpdated ( & updated , & bucket ) ,
2016-09-21 19:46:35 +00:00
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2016-09-21 19:46:35 +00:00
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_storageClass ( bucketName , "REGIONAL" , "US-CENTRAL1" ) ,
2016-09-21 19:46:35 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketExists (
2019-03-21 23:23:54 +00:00
"google_storage_bucket.bucket" , bucketName , & updated ) ,
// Location change causes recreate
testAccCheckStorageBucketWasRecreated ( & updated , & bucket ) ,
2016-09-21 19:46:35 +00:00
) ,
} ,
2017-12-14 00:46:48 +00:00
{
2018-03-19 23:37:39 +00:00
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
2017-12-14 00:46:48 +00:00
} ,
2016-09-21 19:46:35 +00:00
} ,
} )
}
2019-03-21 23:23:54 +00:00
2018-12-13 21:13:07 +00:00
func TestAccStorageBucket_update_requesterPays ( t * testing . T ) {
t . Parallel ( )
var bucket storage . Bucket
2019-03-21 23:23:54 +00:00
var updated storage . Bucket
2018-12-13 21:13:07 +00:00
bucketName := fmt . Sprintf ( "tf-test-requester-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2018-12-13 21:13:07 +00:00
Config : testAccStorageBucket_requesterPays ( bucketName , true ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-12-13 21:13:07 +00:00
Config : testAccStorageBucket_requesterPays ( bucketName , false ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
2019-03-21 23:23:54 +00:00
"google_storage_bucket.bucket" , bucketName , & updated ) ,
testAccCheckStorageBucketWasUpdated ( & updated , & bucket ) ,
2018-12-13 21:13:07 +00:00
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2018-12-13 21:13:07 +00:00
} ,
} )
}
2016-09-21 19:46:35 +00:00
2017-05-22 19:38:21 +00:00
func TestAccStorageBucket_update ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2017-05-22 19:38:21 +00:00
var bucket storage . Bucket
2019-03-21 23:23:54 +00:00
var recreated storage . Bucket
var updated storage . Bucket
2016-01-05 15:06:32 +00:00
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
2015-05-21 17:28:27 +00:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
2017-05-22 19:38:21 +00:00
CheckDestroy : testAccStorageBucketDestroy ,
2015-05-21 17:28:27 +00:00
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_basic ( bucketName ) ,
2015-05-21 17:28:27 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
2015-05-21 17:28:27 +00:00
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "force_destroy" , "false" ) ,
2017-06-12 12:49:34 +00:00
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
ImportStateVerifyIgnore : [ ] string { "force_destroy" } ,
} ,
2018-12-14 20:51:11 +00:00
{
2017-06-12 12:49:34 +00:00
Config : testAccStorageBucket_customAttributes ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
2019-03-21 23:23:54 +00:00
"google_storage_bucket.bucket" , bucketName , & recreated ) ,
testAccCheckStorageBucketWasRecreated ( & recreated , & bucket ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "force_destroy" , "true" ) ,
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
ImportStateVerifyIgnore : [ ] string { "force_destroy" } ,
} ,
2018-12-14 20:51:11 +00:00
{
2017-06-12 12:49:34 +00:00
Config : testAccStorageBucket_customAttributes_withLifecycle1 ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
2019-03-21 23:23:54 +00:00
"google_storage_bucket.bucket" , bucketName , & updated ) ,
testAccCheckStorageBucketWasUpdated ( & updated , & recreated ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "force_destroy" , "true" ) ,
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
ImportStateVerifyIgnore : [ ] string { "force_destroy" } ,
} ,
2018-12-14 20:51:11 +00:00
{
2017-06-12 12:49:34 +00:00
Config : testAccStorageBucket_customAttributes_withLifecycle2 ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
2019-03-21 23:23:54 +00:00
"google_storage_bucket.bucket" , bucketName , & updated ) ,
testAccCheckStorageBucketWasUpdated ( & updated , & recreated ) ,
2017-06-12 12:49:34 +00:00
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "force_destroy" , "true" ) ,
2015-05-21 17:28:27 +00:00
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
ImportStateVerifyIgnore : [ ] string { "force_destroy" } ,
} ,
2018-12-14 20:51:11 +00:00
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_customAttributes ( bucketName ) ,
2015-05-21 17:28:27 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketExists (
2019-03-21 23:23:54 +00:00
"google_storage_bucket.bucket" , bucketName , & updated ) ,
testAccCheckStorageBucketWasUpdated ( & updated , & recreated ) ,
2015-05-21 17:28:27 +00:00
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "force_destroy" , "true" ) ,
) ,
} ,
2019-03-21 23:23:54 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
ImportStateVerifyIgnore : [ ] string { "force_destroy" } ,
} ,
2015-05-21 17:28:27 +00:00
} ,
} )
}
2017-05-22 19:38:21 +00:00
func TestAccStorageBucket_forceDestroy ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2017-05-22 19:38:21 +00:00
var bucket storage . Bucket
2017-05-15 16:38:32 +00:00
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
2017-05-22 19:38:21 +00:00
CheckDestroy : testAccStorageBucketDestroy ,
2017-05-15 16:38:32 +00:00
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_customAttributes ( bucketName ) ,
2015-05-21 17:28:27 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
2015-05-21 17:28:27 +00:00
) ,
} ,
2018-12-14 20:51:11 +00:00
{
2017-05-22 19:38:21 +00:00
Config : testAccStorageBucket_customAttributes ( bucketName ) ,
2015-05-21 17:28:27 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketPutItem ( bucketName ) ,
2015-05-21 17:28:27 +00:00
) ,
} ,
2018-12-14 20:51:11 +00:00
{
2017-10-11 23:52:37 +00:00
Config : testAccStorageBucket_customAttributes ( acctest . RandomWithPrefix ( "tf-test-acl-bucket" ) ) ,
2015-05-21 17:28:27 +00:00
Check : resource . ComposeTestCheckFunc (
2017-05-22 19:38:21 +00:00
testAccCheckStorageBucketMissing ( bucketName ) ,
2015-05-21 17:28:27 +00:00
) ,
} ,
} ,
} )
}
2018-04-03 20:21:29 +00:00
func TestAccStorageBucket_forceDestroyWithVersioning ( t * testing . T ) {
t . Parallel ( )
var bucket storage . Bucket
bucketName := fmt . Sprintf ( "tf-test-acc-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2018-04-03 20:21:29 +00:00
Config : testAccStorageBucket_forceDestroyWithVersioning ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
) ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-04-03 20:21:29 +00:00
Config : testAccStorageBucket_forceDestroyWithVersioning ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketPutItem ( bucketName ) ,
) ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-04-03 20:21:29 +00:00
Config : testAccStorageBucket_forceDestroyWithVersioning ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketPutItem ( bucketName ) ,
) ,
} ,
} ,
} )
}
2017-09-15 15:36:01 +00:00
func TestAccStorageBucket_versioning ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2017-09-15 15:36:01 +00:00
var bucket storage . Bucket
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2017-09-15 15:36:01 +00:00
Config : testAccStorageBucket_versioning ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckStorageBucketExists (
"google_storage_bucket.bucket" , bucketName , & bucket ) ,
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "versioning.#" , "1" ) ,
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "versioning.0.enabled" , "true" ) ,
) ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-03-19 23:37:39 +00:00
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
2017-12-14 00:46:48 +00:00
} ,
2017-09-15 15:36:01 +00:00
} ,
} )
}
2018-01-25 19:02:08 +00:00
func TestAccStorageBucket_logging ( t * testing . T ) {
t . Parallel ( )
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2018-01-25 19:02:08 +00:00
Config : testAccStorageBucket_logging ( bucketName , "log-bucket" ) ,
Check : resource . ComposeTestCheckFunc (
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "logging.#" , "1" ) ,
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "logging.0.log_bucket" , "log-bucket" ) ,
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "logging.0.log_object_prefix" , bucketName ) ,
) ,
} ,
2019-03-26 21:26:16 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-01-25 19:02:08 +00:00
Config : testAccStorageBucket_loggingWithPrefix ( bucketName , "another-log-bucket" , "object-prefix" ) ,
Check : resource . ComposeTestCheckFunc (
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "logging.#" , "1" ) ,
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "logging.0.log_bucket" , "another-log-bucket" ) ,
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "logging.0.log_object_prefix" , "object-prefix" ) ,
) ,
} ,
2019-03-26 21:26:16 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-01-25 19:02:08 +00:00
Config : testAccStorageBucket_basic ( bucketName ) ,
Check : resource . ComposeTestCheckFunc (
resource . TestCheckResourceAttr (
"google_storage_bucket.bucket" , "logging.#" , "0" ) ,
) ,
} ,
2019-03-26 21:26:16 +00:00
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2018-01-25 19:02:08 +00:00
} ,
} )
}
2017-05-31 19:44:25 +00:00
func TestAccStorageBucket_cors ( t * testing . T ) {
2017-10-12 22:07:29 +00:00
t . Parallel ( )
2017-05-31 19:44:25 +00:00
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2017-05-31 19:44:25 +00:00
Config : testGoogleStorageBucketsCors ( bucketName ) ,
2019-03-26 21:26:16 +00:00
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
2017-05-31 19:44:25 +00:00
} ,
} ,
} )
}
2018-08-17 21:33:27 +00:00
func TestAccStorageBucket_encryption ( t * testing . T ) {
t . Parallel ( )
2019-03-26 21:26:16 +00:00
context := map [ string ] interface { } {
"organization" : getTestOrgFromEnv ( t ) ,
"billing_account" : getTestBillingAccountFromEnv ( t ) ,
"random_suffix" : acctest . RandString ( 10 ) ,
"random_int" : acctest . RandInt ( ) ,
}
2018-08-17 21:33:27 +00:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
Steps : [ ] resource . TestStep {
2018-12-14 20:51:11 +00:00
{
2019-03-26 21:26:16 +00:00
Config : testAccStorageBucket_encryption ( context ) ,
} ,
{
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
2018-08-17 21:33:27 +00:00
} ,
} ,
} )
}
2017-10-30 22:48:26 +00:00
func TestAccStorageBucket_labels ( t * testing . T ) {
t . Parallel ( )
bucketName := fmt . Sprintf ( "tf-test-acl-bucket-%d" , acctest . RandInt ( ) )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccStorageBucketDestroy ,
Steps : [ ] resource . TestStep {
2018-05-29 21:23:37 +00:00
// Going from two labels
2018-12-14 20:51:11 +00:00
{
2018-05-29 21:23:37 +00:00
Config : testAccStorageBucket_updateLabels ( bucketName ) ,
2017-10-30 22:48:26 +00:00
} ,
2018-12-14 20:51:11 +00:00
{
2018-05-29 21:23:37 +00:00
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
// Down to only one label (test single label deletion)
2018-12-14 20:51:11 +00:00
{
2018-05-29 21:23:37 +00:00
Config : testAccStorageBucket_labels ( bucketName ) ,
2017-10-30 22:48:26 +00:00
} ,
2018-12-14 20:51:11 +00:00
{
2018-05-29 21:23:37 +00:00
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
// And make sure deleting all labels work
2018-12-14 20:51:11 +00:00
{
2017-10-30 22:48:26 +00:00
Config : testAccStorageBucket_basic ( bucketName ) ,
} ,
2018-12-14 20:51:11 +00:00
{
2018-05-29 21:23:37 +00:00
ResourceName : "google_storage_bucket.bucket" ,
ImportState : true ,
ImportStateVerify : true ,
} ,
2017-10-30 22:48:26 +00:00
} ,
} )
}
2017-05-22 19:38:21 +00:00
func testAccCheckStorageBucketExists ( n string , bucketName string , bucket * storage . Bucket ) resource . TestCheckFunc {
2015-05-21 17:28:27 +00: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 15:06:32 +00:00
if found . Name != bucketName {
return fmt . Errorf ( "expected name %s, got %s" , bucketName , found . Name )
}
2017-05-22 19:38:21 +00:00
* bucket = * found
2015-05-21 17:28:27 +00:00
return nil
}
}
2019-03-21 23:23:54 +00:00
func testAccCheckStorageBucketWasUpdated ( newBucket * storage . Bucket , b * storage . Bucket ) resource . TestCheckFunc {
return func ( s * terraform . State ) error {
if newBucket . TimeCreated != b . TimeCreated {
return fmt . Errorf ( "expected storage bucket to have been updated (had same creation time), instead was recreated - old creation time %s, new creation time %s" , newBucket . TimeCreated , b . TimeCreated )
}
return nil
}
}
func testAccCheckStorageBucketWasRecreated ( newBucket * storage . Bucket , b * storage . Bucket ) resource . TestCheckFunc {
return func ( s * terraform . State ) error {
if newBucket . TimeCreated == b . TimeCreated {
return fmt . Errorf ( "expected storage bucket to have been recreated, instead had same creation time (%s)" , b . TimeCreated )
}
return nil
}
}
2017-05-22 19:38:21 +00:00
func testAccCheckStorageBucketPutItem ( bucketName string ) resource . TestCheckFunc {
2015-05-21 17:28:27 +00: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 15:06:32 +00:00
if res , err := config . clientStorage . Objects . Insert ( bucketName , object ) . Media ( dataReader ) . Do ( ) ; err == nil {
2017-05-22 19:38:21 +00:00
log . Printf ( "[INFO] Created object %v at location %v\n\n" , res . Name , res . SelfLink )
2015-05-21 17:28:27 +00:00
} else {
return fmt . Errorf ( "Objects.Insert failed: %v" , err )
}
return nil
}
}
2017-05-22 19:38:21 +00:00
func testAccCheckStorageBucketMissing ( bucketName string ) resource . TestCheckFunc {
2015-05-21 17:28:27 +00:00
return func ( s * terraform . State ) error {
config := testAccProvider . Meta ( ) . ( * Config )
2016-01-05 15:06:32 +00:00
_ , err := config . clientStorage . Buckets . Get ( bucketName ) . Do ( )
2015-05-21 17:28:27 +00:00
if err == nil {
2016-01-05 15:06:32 +00:00
return fmt . Errorf ( "Found %s" , bucketName )
2015-05-21 17:28:27 +00:00
}
if gerr , ok := err . ( * googleapi . Error ) ; ok && gerr . Code == 404 {
return nil
}
2016-01-05 15:06:32 +00:00
return err
2015-05-21 17:28:27 +00:00
}
}
2019-03-26 21:26:16 +00:00
func testAccCheckStorageBucketLifecycleConditionState ( expected * bool , b * storage . Bucket ) resource . TestCheckFunc {
return func ( s * terraform . State ) error {
actual := b . Lifecycle . Rule [ 0 ] . Condition . IsLive
if expected == nil && b . Lifecycle . Rule [ 0 ] . Condition . IsLive == nil {
return nil
}
if expected == nil {
return fmt . Errorf ( "expected condition isLive to be unset, instead got %t" , * actual )
}
if actual == nil {
return fmt . Errorf ( "expected condition isLive to be %t, instead got nil (unset)" , * expected )
}
if * expected != * actual {
return fmt . Errorf ( "expected condition isLive to be %t, instead got %t" , * expected , * actual )
}
return nil
}
}
2017-05-22 19:38:21 +00:00
func testAccStorageBucketDestroy ( s * terraform . State ) error {
2015-05-21 17:28:27 +00:00
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
}
2017-05-22 19:38:21 +00:00
func testAccStorageBucket_basic ( bucketName string ) string {
2016-01-05 15:06:32 +00:00
return fmt . Sprintf ( `
2015-05-21 17:28:27 +00:00
resource "google_storage_bucket" "bucket" {
2016-01-05 15:06:32 +00:00
name = "%s"
}
` , bucketName )
2015-05-21 17:28:27 +00:00
}
2018-12-13 21:13:07 +00:00
func testAccStorageBucket_requesterPays ( bucketName string , pays bool ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
requester_pays = % t
}
` , bucketName , pays )
}
2017-06-16 16:47:26 +00:00
func testAccStorageBucket_lowercaseLocation ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "eu"
}
` , bucketName )
}
2017-05-22 19:38:21 +00:00
func testAccStorageBucket_customAttributes ( bucketName string ) string {
2016-01-05 15:06:32 +00:00
return fmt . Sprintf ( `
2015-05-21 17:28:27 +00:00
resource "google_storage_bucket" "bucket" {
2016-01-05 15:06:32 +00:00
name = "%s"
2015-05-21 17:28:27 +00:00
location = "EU"
force_destroy = "true"
}
2016-01-05 15:06:32 +00:00
` , bucketName )
}
2016-09-21 19:46:35 +00:00
2017-06-12 12:49:34 +00:00
func testAccStorageBucket_customAttributes_withLifecycle1 ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "EU"
force_destroy = "true"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
}
}
}
` , bucketName )
}
func testAccStorageBucket_customAttributes_withLifecycle2 ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
location = "EU"
force_destroy = "true"
lifecycle_rule {
action {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition {
age = 2
}
}
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
2017-06-20 17:47:58 +00:00
num_newer_versions = 2
2017-06-12 12:49:34 +00:00
}
}
}
` , bucketName )
}
2017-05-22 19:38:21 +00:00
func testAccStorageBucket_storageClass ( bucketName , storageClass , location string ) string {
2017-03-03 23:51:36 +00:00
var locationBlock string
if location != "" {
locationBlock = fmt . Sprintf ( `
location = "%s" ` , location )
}
2016-09-21 19:46:35 +00:00
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
2017-03-03 23:51:36 +00:00
storage_class = "%s" % s
2016-09-21 19:46:35 +00:00
}
2017-03-03 23:51:36 +00:00
` , bucketName , storageClass , locationBlock )
2016-09-21 19:46:35 +00:00
}
2017-05-31 19:44:25 +00:00
func testGoogleStorageBucketsCors ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
cors {
origin = [ "abc" , "def" ]
method = [ "a1a" ]
response_header = [ "123" , "456" , "789" ]
max_age_seconds = 10
}
cors {
origin = [ "ghi" , "jkl" ]
method = [ "z9z" ]
response_header = [ "000" ]
max_age_seconds = 5
}
}
` , bucketName )
}
2017-06-12 12:49:34 +00:00
2018-04-03 20:21:29 +00:00
func testAccStorageBucket_forceDestroyWithVersioning ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
force_destroy = "true"
2019-02-01 22:45:29 +00:00
versioning {
enabled = "true"
2018-04-03 20:21:29 +00:00
}
}
` , bucketName )
}
2017-09-15 15:36:01 +00:00
func testAccStorageBucket_versioning ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
2019-02-01 22:45:29 +00:00
versioning {
enabled = "true"
2017-09-15 15:36:01 +00:00
}
}
` , bucketName )
}
2018-01-25 19:02:08 +00:00
func testAccStorageBucket_logging ( bucketName string , logBucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
2019-02-01 22:45:29 +00:00
logging {
2018-01-25 19:02:08 +00:00
log_bucket = "%s"
}
}
` , bucketName , logBucketName )
}
func testAccStorageBucket_loggingWithPrefix ( bucketName string , logBucketName string , prefix string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
2019-02-01 22:45:29 +00:00
logging {
2018-01-25 19:02:08 +00:00
log_bucket = "%s"
log_object_prefix = "%s"
}
}
` , bucketName , logBucketName , prefix )
}
2019-03-26 21:26:16 +00:00
func testAccStorageBucket_lifecycleRulesMultiple ( bucketName string ) string {
2017-06-12 12:49:34 +00:00
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
lifecycle_rule {
action {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition {
2019-03-26 21:26:16 +00:00
matches_storage_class = [ "COLDLINE" ]
2017-06-12 12:49:34 +00:00
age = 2
}
}
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
}
}
2019-03-26 21:26:16 +00:00
lifecycle_rule {
action {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition {
created_before = "2019-01-01"
}
}
lifecycle_rule {
action {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition {
num_newer_versions = 10
}
}
}
` , bucketName )
}
func testAccStorageBucket_lifecycleRule_emptyArchived ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
}
}
}
` , bucketName )
}
func testAccStorageBucket_lifecycleRule_isLiveFalse ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
is_live = false
}
}
}
` , bucketName )
}
func testAccStorageBucket_lifecycleRule_withStateArchived ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
with_state = "ARCHIVED"
}
}
}
` , bucketName )
}
func testAccStorageBucket_lifecycleRule_IsLiveTrue ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
is_live = true
}
}
}
` , bucketName )
}
func testAccStorageBucket_lifecycleRule_withStateLive ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
with_state = "LIVE"
}
}
}
` , bucketName )
}
func testAccStorageBucket_lifecycleRule_withStateAny ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 10
with_state = "ANY"
}
}
2017-06-12 12:49:34 +00:00
}
` , bucketName )
}
2017-10-30 22:48:26 +00:00
func testAccStorageBucket_labels ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
2018-12-27 15:51:23 +00:00
labels = {
2017-10-30 22:48:26 +00:00
my - label = "my-label-value"
}
}
` , bucketName )
}
2019-03-26 21:26:16 +00:00
func testAccStorageBucket_encryption ( context map [ string ] interface { } ) string {
return Nprintf ( `
2018-08-17 21:33:27 +00:00
resource "google_project" "acceptance" {
2019-03-26 21:26:16 +00:00
name = "terraform-%{random_suffix}"
project_id = "terraform-%{random_suffix}"
org_id = "%{organization}"
billing_account = "%{billing_account}"
2018-08-17 21:33:27 +00:00
}
resource "google_project_services" "acceptance" {
project = "${google_project.acceptance.project_id}"
services = [
"cloudkms.googleapis.com" ,
]
}
resource "google_kms_key_ring" "key_ring" {
2019-03-26 21:26:16 +00:00
name = "tf-test-%{random_suffix}"
2018-08-17 21:33:27 +00:00
project = "${google_project_services.acceptance.project}"
location = "us"
}
resource "google_kms_crypto_key" "crypto_key" {
2019-03-26 21:26:16 +00:00
name = "tf-test-%{random_suffix}"
2018-08-17 21:33:27 +00:00
key_ring = "${google_kms_key_ring.key_ring.id}"
rotation_period = "1000000s"
}
resource "google_storage_bucket" "bucket" {
2019-03-26 21:26:16 +00:00
name = "tf-test-crypto-bucket-%{random_int}"
2018-08-17 21:33:27 +00:00
encryption {
default_kms_key_name = "${google_kms_crypto_key.crypto_key.self_link}"
}
}
2019-03-26 21:26:16 +00:00
` , context )
2018-08-17 21:33:27 +00:00
}
2017-10-30 22:48:26 +00:00
func testAccStorageBucket_updateLabels ( bucketName string ) string {
return fmt . Sprintf ( `
resource "google_storage_bucket" "bucket" {
name = "%s"
2018-12-27 15:51:23 +00:00
labels = {
2017-10-30 22:48:26 +00:00
my - label = "my-updated-label-value"
a - new - label = "a-new-label-value"
}
}
` , bucketName )
}