mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
providers/aws: Change S3 website to block
This commit is contained in:
parent
38e04b3765
commit
30f737c781
@ -32,20 +32,22 @@ func resourceAwsS3Bucket() *schema.Resource {
|
|||||||
},
|
},
|
||||||
|
|
||||||
"website": &schema.Schema{
|
"website": &schema.Schema{
|
||||||
Type: schema.TypeBool,
|
Type: schema.TypeList,
|
||||||
Default: false,
|
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
Elem: &schema.Resource{
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
"index_document": &schema.Schema{
|
"index_document": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
"error_document": &schema.Schema{
|
"error_document": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Optional: true,
|
Optional: true,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
"website_endpoint": &schema.Schema{
|
"website_endpoint": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
@ -98,7 +100,7 @@ func resourceAwsS3BucketUpdate(d *schema.ResourceData, meta interface{}) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := updateWebsite(s3conn, d); err != nil {
|
if err := resourceAwsS3BucketWebsiteUpdate(s3conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,18 +157,32 @@ func resourceAwsS3BucketDelete(d *schema.ResourceData, meta interface{}) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateWebsite(s3conn *s3.S3, d *schema.ResourceData) error {
|
func resourceAwsS3BucketWebsiteUpdate(s3conn *s3.S3, d *schema.ResourceData) error {
|
||||||
website := d.Get("website").(bool)
|
if !d.HasChange("website") {
|
||||||
bucket := d.Get("bucket").(string)
|
return nil
|
||||||
indexDocument := d.Get("index_document").(string)
|
}
|
||||||
errorDocument := d.Get("error_document").(string)
|
|
||||||
|
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{}
|
websiteConfiguration := &s3.WebsiteConfiguration{}
|
||||||
|
|
||||||
if indexDocument != "" {
|
|
||||||
websiteConfiguration.IndexDocument = &s3.IndexDocument{Suffix: aws.String(indexDocument)}
|
websiteConfiguration.IndexDocument = &s3.IndexDocument{Suffix: aws.String(indexDocument)}
|
||||||
}
|
|
||||||
|
|
||||||
if errorDocument != "" {
|
if errorDocument != "" {
|
||||||
websiteConfiguration.ErrorDocument = &s3.ErrorDocument{Key: aws.String(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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Error putting S3 website: %s", err)
|
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)}
|
deleteInput := &s3.DeleteBucketWebsiteInput{Bucket: aws.String(bucket)}
|
||||||
|
|
||||||
log.Printf("[DEBUG] S3 delete bucket website: %s", deleteInput)
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("Error deleting S3 website: %s", err)
|
return fmt.Errorf("Error deleting S3 website: %s", err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (endpoint string, err error) {
|
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 the bucket doesn't have a website configuration, return an empty
|
||||||
if !d.Get("website").(bool) {
|
// endpoint
|
||||||
|
if len(d.Get("website").([]interface{})) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
bucket := d.Get("bucket").(string)
|
bucket := d.Get("bucket").(string)
|
||||||
|
|
||||||
// Lookup the region for this bucket
|
// 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 {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -90,7 +90,8 @@ resource "aws_s3_bucket" "website" {
|
|||||||
bucket = "tf-test-bucket-website-%d"
|
bucket = "tf-test-bucket-website-%d"
|
||||||
acl = "public-read"
|
acl = "public-read"
|
||||||
|
|
||||||
website = true
|
website {
|
||||||
index_document = "index.html"
|
index_document = "index.html"
|
||||||
}
|
}
|
||||||
|
}
|
||||||
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
`, rand.New(rand.NewSource(time.Now().UnixNano())).Int())
|
||||||
|
Loading…
Reference in New Issue
Block a user