Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versioning on s3 outposts #40759

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions examples/cloudhsm/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@ output "hsm_ip_address" {
output "cluster_data_certificate" {
value = data.aws_cloudhsm_v2_cluster.cluster.cluster_certificates[0].cluster_csr
}
output "s3_bucket_arn" {
value = aws_s3control_bucket.bucket_name.arn
}

output "s3_access_point_arn" {
value = aws_s3_access_point.op_access_point.arn
}
22 changes: 22 additions & 0 deletions examples/cloudhsm/s3_outpost.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# s3_outpost.tf
resource "aws_s3control_bucket" "bucket_name" {
bucket = "test0001"
outpost_id = var.outpost_id
}

resource "aws_s3_access_point" "op_access_point" {
bucket = aws_s3control_bucket.bucket_name.id
name = "ap-test0001"

vpc_configuration {
vpc_id = var.vpc_id
}
}

resource "aws_s3control_bucket_versioning" "backend_outpost_local" {
bucket = aws_s3control_bucket.bucket_name.arn

versioning_configuration {
status = "Enabled"
}
}
9 changes: 9 additions & 0 deletions examples/cloudhsm/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ variable "subnets" {
default = ["10.0.1.0/24", "10.0.2.0/24"]
type = list(string)
}
variable "vpc_id" {
type = string
description = "vpc id"
}

variable "outpost_id" {
type = string
description = "outpost id"
}
226 changes: 226 additions & 0 deletions internal/service/s3control/aws_s3control_bucket_versioning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// Copyright (c) HashiCorp, Inc.

Check failure on line 1 in internal/service/s3control/aws_s3control_bucket_versioning.go

View workflow job for this annotation

GitHub Actions / 1 of 3

File is not properly formatted (gofmt)
// SPDX-License-Identifier: MPL-2.0

package s3control

import (
"context"
"fmt"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/s3control"
"github.com/aws/aws-sdk-go-v2/service/s3control/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_s3control_bucket_versioning")
func ResourceBucketVersioning() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceBucketVersioningCreate,
ReadWithoutTimeout: resourceBucketVersioningRead,
UpdateWithoutTimeout: resourceBucketVersioningUpdate,
DeleteWithoutTimeout: resourceBucketVersioningDelete,

Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
names.AttrBucket: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateS3ControlBucketName,
},
"versioning_configuration": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{"Enabled", "Suspended"}, false),
},
},
},
},
},
}
}

func resourceBucketVersioningCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).S3ControlClient(ctx)

bucket := d.Get(names.AttrBucket).(string)

accountID := meta.(*conns.AWSClient).AccountID(ctx)
if strings.HasPrefix(bucket, "arn:") {
parsedARN, err := arn.Parse(bucket)
if err != nil {
return sdkdiag.AppendErrorf(diags, "parsing S3 Control Bucket ARN (%s): %s", bucket, err)
}
accountID = parsedARN.AccountID
}

input := &s3control.PutBucketVersioningInput{
AccountId: aws.String(accountID),
Bucket: aws.String(bucket),
VersioningConfiguration: &types.VersioningConfiguration{
Status: types.BucketVersioningStatus(expandVersioningStatus(d.Get("versioning_configuration").([]interface{}))),
},
}

_, err := conn.PutBucketVersioning(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating S3 Control Bucket Versioning (%s): %s", bucket, err)
}

d.SetId(bucket)

return append(diags, resourceBucketVersioningRead(ctx, d, meta)...)
}

func resourceBucketVersioningRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).S3ControlClient(ctx)

bucket := d.Id()
accountID := meta.(*conns.AWSClient).AccountID(ctx)

if strings.HasPrefix(bucket, "arn:") {
parsedARN, err := arn.Parse(bucket)
if err != nil {
return sdkdiag.AppendErrorf(diags, "parsing S3 Control Bucket ARN (%s): %s", bucket, err)
}
accountID = parsedARN.AccountID
}

input := &s3control.GetBucketVersioningInput{
AccountId: aws.String(accountID),
Bucket: aws.String(bucket),
}

output, err := conn.GetBucketVersioning(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading S3 Control Bucket Versioning (%s): %s", d.Id(), err)
}

d.Set(names.AttrBucket, bucket)
if err := d.Set("versioning_configuration", flattenVersioningConfiguration(output)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting versioning_configuration: %s", err)
}

return diags
}

func resourceBucketVersioningUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).S3ControlClient(ctx)

bucket := d.Id()
accountID := meta.(*conns.AWSClient).AccountID(ctx)
if strings.HasPrefix(bucket, "arn:") {
parsedARN, err := arn.Parse(bucket)
if err != nil {
return sdkdiag.AppendErrorf(diags, "parsing S3 Control Bucket ARN (%s): %s", bucket, err)
}
accountID = parsedARN.AccountID
}

input := &s3control.PutBucketVersioningInput{
AccountId: aws.String(accountID),
Bucket: aws.String(bucket),
VersioningConfiguration: &types.VersioningConfiguration{
Status: types.BucketVersioningStatus(expandVersioningStatus(d.Get("versioning_configuration").([]interface{}))),
},
}

_, err := conn.PutBucketVersioning(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating S3 Control Bucket Versioning (%s): %s", d.Id(), err)
}

return append(diags, resourceBucketVersioningRead(ctx, d, meta)...)
}

func resourceBucketVersioningDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).S3ControlClient(ctx)

bucket := d.Id()
accountID := meta.(*conns.AWSClient).AccountID(ctx)

if strings.HasPrefix(bucket, "arn:") {
parsedARN, err := arn.Parse(bucket)
if err != nil {
return sdkdiag.AppendErrorf(diags, "parsing S3 Control Bucket ARN (%s): %s", bucket, err)
}
accountID = parsedARN.AccountID
}

input := &s3control.PutBucketVersioningInput{
AccountId: aws.String(accountID),
Bucket: aws.String(bucket),
VersioningConfiguration: &types.VersioningConfiguration{
Status: types.BucketVersioningStatusSuspended,
},
}

_, err := conn.PutBucketVersioning(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "suspending S3 Control Bucket Versioning (%s): %s", d.Id(), err)
}

return diags
}
func isValidOutpostBucketArn(value string) bool {
prefix := "arn:aws:s3-outposts:"

Check failure on line 187 in internal/service/s3control/aws_s3control_bucket_versioning.go

View workflow job for this annotation

GitHub Actions / providerlint

AWSAT005: avoid hardcoded ARN AWS partitions, use aws_partition data source
return strings.HasPrefix(value, prefix)
}

func validateS3ControlBucketName(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)

if strings.HasPrefix(value, "arn:") {
if !isValidOutpostBucketArn(value) {
errors = append(errors, fmt.Errorf("%q must be a valid S3 Outposts bucket ARN", k))
}
} else {
if len(value) < 1 || len(value) > 63 {
errors = append(errors, fmt.Errorf("%q must be between 1 and 63 characters", k))
}
}

return
}

func expandVersioningStatus(l []interface{}) string {
if len(l) == 0 || l[0] == nil {
return ""
}

m := l[0].(map[string]interface{})
return m["status"].(string)
}

func flattenVersioningConfiguration(output *s3control.GetBucketVersioningOutput) []interface{} {
if output == nil {
return []interface{}{}
}

m := map[string]interface{}{
"status": aws.String(string(output.Status)),
}

return []interface{}{m}
}
Loading