Skip to content

Commit

Permalink
provider/aws: Support MFA delete for s3 bucket versioning
Browse files Browse the repository at this point in the history
Fixes #7902
  • Loading branch information
stack72 committed Dec 12, 2016
1 parent 5016a56 commit 25518f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
35 changes: 24 additions & 11 deletions builtin/providers/aws/resource_aws_s3_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,24 @@ func resourceAwsS3Bucket() *schema.Resource {
},

"versioning": {
Type: schema.TypeSet,
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"mfa_delete": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
},
Set: func(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%t-", m["enabled"].(bool)))

return hashcode.String(buf.String())
},
},

"logging": {
Expand Down Expand Up @@ -647,14 +647,20 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error {
return err
}
log.Printf("[DEBUG] S3 Bucket: %s, versioning: %v", d.Id(), versioning)
if versioning.Status != nil && *versioning.Status == s3.BucketVersioningStatusEnabled {
if versioning != nil {
vcl := make([]map[string]interface{}, 0, 1)
vc := make(map[string]interface{})
if *versioning.Status == s3.BucketVersioningStatusEnabled {
if versioning.Status != nil && *versioning.Status == s3.BucketVersioningStatusEnabled {
vc["enabled"] = true
} else {
vc["enabled"] = false
}

if versioning.MFADelete != nil && *versioning.MFADelete == s3.MFADeleteEnabled {
vc["mfa_delete"] = true
} else {
vc["mfa_delete"] = false
}
vcl = append(vcl, vc)
if err := d.Set("versioning", vcl); err != nil {
return err
Expand Down Expand Up @@ -1250,7 +1256,7 @@ func resourceAwsS3BucketAclUpdate(s3conn *s3.S3, d *schema.ResourceData) error {
}

func resourceAwsS3BucketVersioningUpdate(s3conn *s3.S3, d *schema.ResourceData) error {
v := d.Get("versioning").(*schema.Set).List()
v := d.Get("versioning").([]interface{})
bucket := d.Get("bucket").(string)
vc := &s3.VersioningConfiguration{}

Expand All @@ -1262,6 +1268,13 @@ func resourceAwsS3BucketVersioningUpdate(s3conn *s3.S3, d *schema.ResourceData)
} else {
vc.Status = aws.String(s3.BucketVersioningStatusSuspended)
}

if c["mfa_delete"].(bool) {
vc.MFADelete = aws.String(s3.MFADeleteEnabled)
} else {
vc.MFADelete = aws.String(s3.MFADeleteDisabled)
}

} else {
vc.Status = aws.String(s3.BucketVersioningStatusSuspended)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ The `CORS` object supports the following:
The `versioning` object supports the following:

* `enabled` - (Optional) Enable versioning. Once you version-enable a bucket, it can never return to an unversioned state. You can, however, suspend versioning on that bucket.
* `mfa_delete` - (Optional) Enable MFA delete for either `Change the versioning state of your bucket` or `Permanently delete an object version`. Default is `false`.

The `logging` object supports the following:

Expand Down

0 comments on commit 25518f9

Please sign in to comment.