providers/aws: Change S3 website to block

This commit is contained in:
Justin Campbell 2015-05-01 09:19:54 -04:00
parent 38e04b3765
commit 30f737c781
2 changed files with 75 additions and 49 deletions

View File

@ -32,20 +32,22 @@ func resourceAwsS3Bucket() *schema.Resource {
},
"website": &schema.Schema{
Type: schema.TypeBool,
Default: false,
Type: schema.TypeList,
Optional: true,
},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"index_document": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Required: true,
},
"error_document": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
},
"website_endpoint": &schema.Schema{
Type: schema.TypeString,
@ -98,7 +100,7 @@ func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {
return err
}
if err := updateWebsite(s3conn, d); err != nil {
if err := resourceAwsS3BucketWebsiteUpdate(s3conn, d); err != nil {
return err
}
@ -155,18 +157,32 @@ 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)
errorDocument := d.Get("error_document").(string)
func resourceAwsS3BucketWebsiteUpdate(s3conn *s3.S3, d *schema.ResourceData) error {
if !d.HasChange("website") {
return nil
}
ws := d.Get("website").([]interface{})
if len(ws) == 1 {
w := ws[0].(map[string]interface{})
return resourceAwsS3BucketWebsitePut(s3conn, d, w)
} else if len(ws) == 0 {
return resourceAwsS3BucketWebsiteDelete(s3conn, d)
} else {
return fmt.Errorf("Cannot specify more than one website.")
}
}
func resourceAwsS3BucketWebsitePut(s3conn *s3.S3, d *schema.ResourceData, website map[string]interface{}) error {
bucket := d.Get("bucket").(string)
indexDocument := website["index_document"].(string)
errorDocument := website["error_document"].(string)
if website {
websiteConfiguration := &s3.WebsiteConfiguration{}
if indexDocument != "" {
websiteConfiguration.IndexDocument = &s3.IndexDocument{Suffix: aws.String(indexDocument)}
}
if errorDocument != "" {
websiteConfiguration.ErrorDocument = &s3.ErrorDocument{Key: aws.String(errorDocument)}
@ -183,7 +199,12 @@ func updateWebsite(s3conn *s3.S3, d *schema.ResourceData) error {
if err != nil {
return fmt.Errorf("Error putting S3 website: %s", err)
}
} else {
return nil
}
func resourceAwsS3BucketWebsiteDelete(s3conn *s3.S3, d *schema.ResourceData) error {
bucket := d.Get("bucket").(string)
deleteInput := &s3.DeleteBucketWebsiteInput{Bucket: aws.String(bucket)}
log.Printf("[DEBUG] S3 delete bucket website: %s", deleteInput)
@ -192,21 +213,25 @@ func updateWebsite(s3conn *s3.S3, d *schema.ResourceData) error {
if err != nil {
return fmt.Errorf("Error deleting S3 website: %s", err)
}
}
return nil
}
func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (endpoint string, err error) {
// If the bucket doess't have a website configuration, return an empty endpoint
if !d.Get("website").(bool) {
// If the bucket doesn't have a website configuration, return an empty
// endpoint
if len(d.Get("website").([]interface{})) == 0 {
return
}
bucket := d.Get("bucket").(string)
// Lookup the region for this bucket
location, err := s3conn.GetBucketLocation(&s3.GetBucketLocationInput{Bucket: aws.String(bucket)})
location, err := s3conn.GetBucketLocation(
&s3.GetBucketLocationInput{
Bucket: aws.String(bucket),
},
)
if err != nil {
return
}

View File

@ -90,7 +90,8 @@ resource "aws_s3_bucket" "website" {
bucket = "tf-test-bucket-website-%d"
acl = "public-read"
website = true
website {
index_document = "index.html"
}
}
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())