-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement multi region access point data source
- Implement the data source - Implement the test for the data source - Create draft documentation
- Loading branch information
1 parent
2dbb620
commit a6e4aae
Showing
4 changed files
with
208 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
internal/service/s3control/multi_region_access_point_data_source.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package s3control | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/s3control" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
"github.com/hashicorp/terraform-provider-aws/internal/verify" | ||
) | ||
|
||
func DataSourceMultiRegionAccessPoint() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadWithoutTimeout: dataSourceMultiRegionAccessPointBlockRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"account_id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: verify.ValidAccountID, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"alias": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"created_at": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"public_access_block": { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
}, | ||
"regions": { | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeList, | ||
}, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceMultiRegionAccessPointBlockRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
conn := meta.(*conns.AWSClient).S3ControlConn | ||
|
||
accountID := meta.(*conns.AWSClient).AccountID | ||
if v, ok := d.GetOk("account_id"); ok { | ||
accountID = v.(string) | ||
} | ||
|
||
name := d.Get("name").(string) | ||
|
||
input := &s3control.GetMultiRegionAccessPointInput{ | ||
AccountId: aws.String(accountID), | ||
Name: aws.String(name), | ||
} | ||
|
||
log.Printf("[DEBUG] Reading S3 Multi Region Access Point: %s", input) | ||
|
||
output, err := conn.GetMultiRegionAccessPoint(input) | ||
|
||
if err != nil { | ||
return diag.Errorf("error reading S3 Multi Region Access Point: %s", err) | ||
} | ||
|
||
if output == nil || output.AccessPoint == nil { | ||
return diag.Errorf("error reading S3 Multi Region Access Point (%s): missing access point", accountID) | ||
} | ||
|
||
d.SetId(accountID) | ||
d.Set("created_at", aws.TimeValue(output.AccessPoint.CreatedAt).Format(time.RFC3339)) | ||
d.Set("name", output.AccessPoint.Name) | ||
d.Set("public_access_block", output.AccessPoint.PublicAccessBlock) | ||
d.Set("regions", output.AccessPoint.Regions) | ||
d.Set("status", output.AccessPoint.Status) | ||
|
||
return nil | ||
} |
107 changes: 107 additions & 0 deletions
107
internal/service/s3control/multi_region_access_point_data_source_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package s3control_test | ||
|
||
import ( | ||
"fmt" | ||
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/service/s3control" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
) | ||
|
||
func TestAccS3ControlMultiRegionAccessPointDataSource_basic(t *testing.T) { | ||
resourceName := "aws_s3control_multi_region_access_point.test" | ||
dataSourceName := "data.aws_s3control_multi_region_access_point.test" | ||
|
||
bucket1Name := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
bucket2Name := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) | ||
|
||
if acctest.Partition() == "aws-us-gov" { | ||
t.Skip("S3 Multi-Region Access Point is not supported in GovCloud partition") | ||
} | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckMultipleRegion(t, 2) }, | ||
ErrorCheck: acctest.ErrorCheck(t, s3control.EndpointsID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5FactoriesMultipleRegions(t, 2), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMultiRegionAccessPointDataSourceConfig_basic(bucket1Name, bucket2Name, rName), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestMatchResourceAttr(resourceName, "alias", regexp.MustCompile(`^[a-z][a-z0-9]*[.]mrap$`)), | ||
resource.TestCheckResourceAttr(resourceName, "name", rName), | ||
resource.TestCheckResourceAttrSet(resourceName, "created_at"), | ||
resource.TestCheckResourceAttrPair(resourceName, "details.0.public_access_block.0.block_public_acls", dataSourceName, "public_access_block.0.block_public_acls"), | ||
resource.TestCheckResourceAttrPair(resourceName, "details.0.public_access_block.0.block_public_policy", dataSourceName, "public_access_block.0.block_public_policy"), | ||
resource.TestCheckResourceAttrPair(resourceName, "details.0.public_access_block.0.ignore_public_acls", dataSourceName, "public_access_block.0.ignore_public_acls"), | ||
resource.TestCheckResourceAttrPair(resourceName, "details.0.public_access_block.0.restrict_public_buckets", dataSourceName, "public_access_block.0.restrict_public_buckets"), | ||
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "details.0.region.*", map[string]string{ | ||
"bucket": bucket1Name, | ||
}), | ||
resource.TestCheckTypeSetElemNestedAttrs(resourceName, "details.0.region.*", map[string]string{ | ||
"bucket": bucket2Name, | ||
}), | ||
resource.TestCheckResourceAttr(resourceName, "status", s3control.MultiRegionAccessPointStatusReady), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccMultiRegionAccessPointDataSource_base(bucket1Name string, bucket2Name string, rName string) string { | ||
return acctest.ConfigCompose( | ||
acctest.ConfigMultipleRegionProvider(2), | ||
fmt.Sprintf(` | ||
resource "aws_s3_bucket" "test1" { | ||
provider = aws | ||
bucket = %[1]q | ||
force_destroy = true | ||
} | ||
resource "aws_s3_bucket" "test2" { | ||
provider = awsalternate | ||
bucket = %[2]q | ||
force_destroy = true | ||
} | ||
resource "aws_s3control_multi_region_access_point" "test" { | ||
provider = aws | ||
details { | ||
name = %[3]q | ||
region { | ||
bucket = aws_s3_bucket.test1.id | ||
} | ||
region { | ||
bucket = aws_s3_bucket.test2.id | ||
} | ||
public_access_block = { | ||
block_public_acls = false | ||
block_public_policy = false | ||
ignore_public_acls = false | ||
restrict_public_buckets = false | ||
} | ||
} | ||
} | ||
`, bucket1Name, bucket2Name, rName)) | ||
} | ||
|
||
func testAccMultiRegionAccessPointDataSourceConfig_basic(bucket1Name string, bucket2Name string, rName string) string { | ||
return acctest.ConfigCompose(testAccMultiRegionAccessPointDataSource_base(bucket1Name, bucket2Name, rName), fmt.Sprintf(` | ||
data "aws_s3control_multi_region_access_point" "test" { | ||
provider = aws | ||
name = %[1]q | ||
depends_on = [aws_s3control_multi_region_access_point.test] | ||
} | ||
`, rName)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
subcategory: "S3 Control" | ||
layout: "aws" | ||
page_title: "AWS: aws_s3control_multi_region_access_point" | ||
description: |- | ||
Provides S3 multi region access point | ||
--- |