providers/aws: Initial S3 bucket website support

This commit is contained in:
Justin Campbell 2015-04-25 19:36:00 -04:00
parent cebcee5c63
commit e6d9dcfb1a
2 changed files with 73 additions and 4 deletions

View File

@ -31,6 +31,19 @@ func resourceAwsS3Bucket() *schema.Resource {
ForceNew: true,
},
"website": &schema.Schema{
Type: schema.TypeBool,
Default: false,
Optional: true,
ForceNew: false,
},
"index_document": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"tags": tagsSchema(),
},
}
@ -75,6 +88,11 @@ func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {
if err := setTagsS3(s3conn, d); err != nil {
return err
}
if err := updateWebsite(s3conn, d); err != nil {
return err
}
return resourceAwsS3BucketRead(d, meta)
}
@ -118,3 +136,38 @@ func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error {
}
return nil
}
func updateWebsite(s3conn *s3.S3, d *schema.ResourceData) error {
website := d.Get("website").(bool)
bucket := d.Get("bucket").(string)
indexDocument := d.Get("index_document").(string)
websiteConfiguration := &s3.WebsiteConfiguration{
IndexDocument: &s3.IndexDocument{Suffix: aws.String(indexDocument)},
}
if website {
input := &s3.PutBucketWebsiteInput{
Bucket: aws.String(bucket),
WebsiteConfiguration: websiteConfiguration,
}
log.Printf("[DEBUG] S3 put bucket website: %s", input)
_, err := s3conn.PutBucketWebsite(input)
if err != nil {
return fmt.Errorf("Error putting S3 website: %s", err)
}
} else {
deleteInput := &s3.DeleteBucketWebsiteInput{Bucket: aws.String(bucket)}
log.Printf("[DEBUG] S3 delete bucket website: %s", deleteInput)
_, err := s3conn.DeleteBucketWebsite(deleteInput)
if err != nil {
return fmt.Errorf("Error deleting S3 website: %s", err)
}
}
return nil
}

View File

@ -23,7 +23,13 @@ func TestAccAWSS3Bucket(t *testing.T) {
resource.TestStep{
Config: testAccAWSS3BucketConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists("aws_s3_bucket.bar"),
testAccCheckAWSS3BucketExists("aws_s3_bucket.bucket"),
),
},
resource.TestStep{
Config: testAccAWSS3BucketWebsiteConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSS3BucketExists("aws_s3_bucket.website"),
),
},
},
@ -70,11 +76,21 @@ func testAccCheckAWSS3BucketExists(n string) resource.TestCheckFunc {
}
}
// This needs a bit of randoness as the name can only be
// used once globally within AWS
// These need a bit of randoness as the name can only be used once globally
// within AWS
var testAccAWSS3BucketConfig = fmt.Sprintf(`
resource "aws_s3_bucket" "bar" {
resource "aws_s3_bucket" "bucket" {
bucket = "tf-test-bucket-%d"
acl = "public-read"
}
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
var testAccAWSS3BucketWebsiteConfig = fmt.Sprintf(`
resource "aws_s3_bucket" "website" {
bucket = "tf-test-bucket-website-%d"
acl = "public-read"
website = true
index_document = "index.html"
}
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())