2014-07-21 20:34:47 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-11-24 21:22:18 +01:00
|
|
|
"math/rand"
|
2014-07-21 20:34:47 +02:00
|
|
|
"testing"
|
2014-11-24 21:22:18 +01:00
|
|
|
"time"
|
2014-07-21 20:34:47 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-02-19 22:38:56 +01:00
|
|
|
|
2015-04-14 18:07:05 +02:00
|
|
|
"github.com/awslabs/aws-sdk-go/aws"
|
|
|
|
"github.com/awslabs/aws-sdk-go/service/s3"
|
2014-07-21 20:34:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccAWSS3Bucket(t *testing.T) {
|
2014-11-24 21:22:18 +01:00
|
|
|
|
2014-07-21 20:34:47 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAWSS3BucketDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
2015-04-26 01:36:00 +02:00
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAWSS3BucketWebsiteConfig,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAWSS3BucketExists("aws_s3_bucket.website"),
|
2014-07-21 20:34:47 +02:00
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSS3BucketDestroy(s *terraform.State) error {
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
2014-07-21 20:34:47 +02:00
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-07-21 20:34:47 +02:00
|
|
|
if rs.Type != "aws_s3_bucket" {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-14 18:07:05 +02:00
|
|
|
_, err := conn.DeleteBucket(&s3.DeleteBucketInput{
|
2015-02-19 22:38:56 +01:00
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-21 20:34:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckAWSS3BucketExists(n string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:44:42 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-07-21 20:34:47 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:44:42 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-07-21 20:34:47 +02:00
|
|
|
return fmt.Errorf("No S3 Bucket ID is set")
|
|
|
|
}
|
|
|
|
|
2014-11-21 17:58:34 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).s3conn
|
2015-04-14 18:07:05 +02:00
|
|
|
_, err := conn.HeadBucket(&s3.HeadBucketInput{
|
2015-02-19 22:38:56 +01:00
|
|
|
Bucket: aws.String(rs.Primary.ID),
|
|
|
|
})
|
|
|
|
|
2014-07-21 20:34:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("S3Bucket error: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-26 01:36:00 +02:00
|
|
|
// These need a bit of randoness as the name can only be used once globally
|
|
|
|
// within AWS
|
2014-11-24 21:22:18 +01:00
|
|
|
var testAccAWSS3BucketConfig = fmt.Sprintf(`
|
2015-04-26 01:36:00 +02:00
|
|
|
resource "aws_s3_bucket" "bucket" {
|
2014-11-24 21:22:18 +01:00
|
|
|
bucket = "tf-test-bucket-%d"
|
2014-07-21 20:34:47 +02:00
|
|
|
acl = "public-read"
|
|
|
|
}
|
2015-02-19 22:38:56 +01:00
|
|
|
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
2015-04-26 01:36:00 +02:00
|
|
|
|
|
|
|
var testAccAWSS3BucketWebsiteConfig = fmt.Sprintf(`
|
|
|
|
resource "aws_s3_bucket" "website" {
|
|
|
|
bucket = "tf-test-bucket-website-%d"
|
|
|
|
acl = "public-read"
|
|
|
|
|
2015-05-01 15:19:54 +02:00
|
|
|
website {
|
|
|
|
index_document = "index.html"
|
|
|
|
}
|
2015-04-26 01:36:00 +02:00
|
|
|
}
|
|
|
|
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|