From c45398170cfff65565f22636e7079ddd0a685e74 Mon Sep 17 00:00:00 2001 From: Stefan Freitag Date: Thu, 1 Aug 2024 16:15:39 +0200 Subject: [PATCH 1/7] feat: add new resource aws_ebs_snapshot_block_public_access --- .../ec2/ebs_snapshot_block_public_access.go | 102 ++++++++++++++++++ .../ebs_snapshot_block_public_access_test.go | 75 +++++++++++++ internal/service/ec2/service_package_gen.go | 4 + ...snapshot_block_public_access.html.markdown | 48 +++++++++ 4 files changed, 229 insertions(+) create mode 100644 internal/service/ec2/ebs_snapshot_block_public_access.go create mode 100644 internal/service/ec2/ebs_snapshot_block_public_access_test.go create mode 100644 website/docs/r/ebs_snapshot_block_public_access.html.markdown diff --git a/internal/service/ec2/ebs_snapshot_block_public_access.go b/internal/service/ec2/ebs_snapshot_block_public_access.go new file mode 100644 index 00000000000..6d27eb3f028 --- /dev/null +++ b/internal/service/ec2/ebs_snapshot_block_public_access.go @@ -0,0 +1,102 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ec2 + +import ( + "context" + "github.com/hashicorp/terraform-provider-aws/names" + + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "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/enum" + "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" +) + +// @SDKResource("aws_ebs_snapshot_block_public_access", name="EBS Snapshot Block Public Access") +func resourceEBSSnapshotBlockPublicAccess() *schema.Resource { + return &schema.Resource{ + CreateWithoutTimeout: resourceEBSSnapshotBlockPublicAccessCreate, + ReadWithoutTimeout: resourceEBSSnapshotBlockPublicAccessRead, + UpdateWithoutTimeout: resourceEBSSnapshotBlockPublicAccessUpdate, + DeleteWithoutTimeout: resourceEBSSnapshotBlockPublicAccessDelete, + + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + + Schema: map[string]*schema.Schema{ + names.AttrState: { + Type: schema.TypeString, + Required: true, + ValidateDiagFunc: enum.Validate[types.SnapshotBlockPublicAccessState](), + }, + }, + } +} + +func resourceEBSSnapshotBlockPublicAccessCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).EC2Client(ctx) + + input := &ec2.EnableSnapshotBlockPublicAccessInput{ + State: types.SnapshotBlockPublicAccessState(d.Get(names.AttrState).(string)), + } + + _, err := conn.EnableSnapshotBlockPublicAccess(ctx, input) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "enabling EBS snapshot block public access: %s", err) + } + + d.SetId(meta.(*conns.AWSClient).Region) + + return append(diags, resourceEBSSnapshotBlockPublicAccessRead(ctx, d, meta)...) +} + +func resourceEBSSnapshotBlockPublicAccessRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).EC2Client(ctx) + + resp, err := conn.GetSnapshotBlockPublicAccessState(ctx, &ec2.GetSnapshotBlockPublicAccessStateInput{}) + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading EBS snapshot block public access: %s", err) + } + + d.Set(names.AttrState, string(resp.State)) + + return diags +} + +func resourceEBSSnapshotBlockPublicAccessUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).EC2Client(ctx) + + input := &ec2.EnableSnapshotBlockPublicAccessInput{ + State: types.SnapshotBlockPublicAccessState(d.Get(names.AttrState).(string)), + } + + _, err := conn.EnableSnapshotBlockPublicAccess(ctx, input) + + if err != nil { + return sdkdiag.AppendErrorf(diags, "update EBS snapshot block public access: %s", err) + } + + return append(diags, resourceEBSSnapshotBlockPublicAccessRead(ctx, d, meta)...) +} + +func resourceEBSSnapshotBlockPublicAccessDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + var diags diag.Diagnostics + conn := meta.(*conns.AWSClient).EC2Client(ctx) + + // Removing the resource disables blocking of EBS snapshot sharing. + _, err := conn.DisableSnapshotBlockPublicAccess(ctx, &ec2.DisableSnapshotBlockPublicAccessInput{}) + if err != nil { + return sdkdiag.AppendErrorf(diags, "disabling EBS snapshot block public access: %s", err) + } + + return diags +} diff --git a/internal/service/ec2/ebs_snapshot_block_public_access_test.go b/internal/service/ec2/ebs_snapshot_block_public_access_test.go new file mode 100644 index 00000000000..a3aa4a79974 --- /dev/null +++ b/internal/service/ec2/ebs_snapshot_block_public_access_test.go @@ -0,0 +1,75 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package ec2_test + +import ( + "context" + "fmt" + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-provider-aws/internal/conns" + "github.com/hashicorp/terraform-provider-aws/names" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" +) + +func TestAccSnapshotBlockPublicAccess_basic(t *testing.T) { + ctx := acctest.Context(t) + resourceName := "aws_ebs_snapshot_block_public_access.this" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + WorkingDir: "/tmp", + CheckDestroy: testAccCheckSnapshotBlockAccessDestroy(ctx), + Steps: []resource.TestStep{ + { + ResourceName: resourceName, + Config: testAccEbsSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockAllSharing)), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, names.AttrState, "block-all-sharing"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: resourceName, + Config: testAccEbsSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockNewSharing)), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(resourceName, names.AttrState, "block-new-sharing"), + ), + }, + }, + }) +} + +func testAccCheckSnapshotBlockAccessDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) + response, err := conn.GetSnapshotBlockPublicAccessState(ctx, &ec2.GetSnapshotBlockPublicAccessStateInput{}) + if err != nil { + return err + } + + if response.State != types.SnapshotBlockPublicAccessStateUnblocked { + return fmt.Errorf("EBS encryption by default is not in expected state (%s)", types.SnapshotBlockPublicAccessStateUnblocked) + } + return nil + } +} + +func testAccEbsSnapshotBlockPublicAccess_basic(state string) string { + return fmt.Sprintf(` +resource "aws_ebs_snapshot_block_public_access" "this" { + state = "%[1]s" +} +`, state) +} diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index 04d5d5977c8..fc150dc9abd 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -635,6 +635,10 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka IdentifierAttribute: names.AttrID, }, }, + { + Factory: resourceEBSSnapshotBlockPublicAccess, + TypeName: "aws_ebs_snapshot_block_public_access", + }, { Factory: resourceEBSSnapshotCopy, TypeName: "aws_ebs_snapshot_copy", diff --git a/website/docs/r/ebs_snapshot_block_public_access.html.markdown b/website/docs/r/ebs_snapshot_block_public_access.html.markdown new file mode 100644 index 00000000000..c286e1645b1 --- /dev/null +++ b/website/docs/r/ebs_snapshot_block_public_access.html.markdown @@ -0,0 +1,48 @@ +--- +subcategory: "EBS (EC2)" +layout: "aws" +page_title: "AWS: aws_ebs_snapshot_block_public_access" +description: |- + Manages EBS snapshot public access block configuration. +--- + +# Resource: aws_ebs_snapshot_block_public_access + +Provides a resource to manage the state of the "Block public access for snapshots" setting on region level. + +~> **NOTE:** Removing this Terraform resource disables blocking. + +## Example Usage + +```terraform +resource "aws_ebs_snapshot_block_public_access" "example" { + state = "block-all-sharing" +} +``` + +## Argument Reference + +This resource supports the following arguments: + +* `state` - (Required) The mode in which to enable "Block public access for snapshots" for the region. Allowed values are `block-all`, `block-new-sharing`, `unblocked`. + +## Attribute Reference + +This resource exports no additional attributes. + +## Import + +In Terraform v1.5.0 and later, use an [`import` block](https://developer.hashicorp.com/terraform/language/import) to import the current state. For example: + +```terraform +import { + to = aws_ebs_snapshot_block_public_access.example + id = "default" +} +``` + +Using `terraform import`, import the state. For example: + +```console +% terraform import aws_ebs_snapshot_block_public_access.example default +``` From 3824f620a5d70f474eb28bac2303c9aef54d858b Mon Sep 17 00:00:00 2001 From: Stefan Freitag Date: Thu, 1 Aug 2024 16:35:19 +0200 Subject: [PATCH 2/7] chore: add changelog --- .changelog/38641.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/38641.txt diff --git a/.changelog/38641.txt b/.changelog/38641.txt new file mode 100644 index 00000000000..9952876c979 --- /dev/null +++ b/.changelog/38641.txt @@ -0,0 +1,3 @@ +```release-note:new-resource +aws_ebs_snapshot_block_public_access +``` \ No newline at end of file From 63f3e669232f671b9a9a9a6491690215908a7d97 Mon Sep 17 00:00:00 2001 From: Stefan Freitag Date: Thu, 1 Aug 2024 18:41:13 +0200 Subject: [PATCH 3/7] fix: renamed test functions --- internal/service/ec2/ebs_encryption_by_default.go | 1 - .../ec2/ebs_snapshot_block_public_access_test.go | 14 +++++++------- internal/service/ec2/service_package_gen.go | 1 + 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/service/ec2/ebs_encryption_by_default.go b/internal/service/ec2/ebs_encryption_by_default.go index 4e1d72c03c2..b2cf7a6afbd 100644 --- a/internal/service/ec2/ebs_encryption_by_default.go +++ b/internal/service/ec2/ebs_encryption_by_default.go @@ -5,7 +5,6 @@ package ec2 import ( "context" - "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" diff --git a/internal/service/ec2/ebs_snapshot_block_public_access_test.go b/internal/service/ec2/ebs_snapshot_block_public_access_test.go index a3aa4a79974..16d6615be08 100644 --- a/internal/service/ec2/ebs_snapshot_block_public_access_test.go +++ b/internal/service/ec2/ebs_snapshot_block_public_access_test.go @@ -17,7 +17,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/acctest" ) -func TestAccSnapshotBlockPublicAccess_basic(t *testing.T) { +func TestAccEC2EBSSnapshotBlockPublicAccess_basic(t *testing.T) { ctx := acctest.Context(t) resourceName := "aws_ebs_snapshot_block_public_access.this" @@ -26,11 +26,11 @@ func TestAccSnapshotBlockPublicAccess_basic(t *testing.T) { ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, WorkingDir: "/tmp", - CheckDestroy: testAccCheckSnapshotBlockAccessDestroy(ctx), + CheckDestroy: testAccEC2EBSCheckSnapshotBlockAccessDestroy(ctx), Steps: []resource.TestStep{ { ResourceName: resourceName, - Config: testAccEbsSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockAllSharing)), + Config: testAccEC2EBSSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockAllSharing)), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, names.AttrState, "block-all-sharing"), ), @@ -42,7 +42,7 @@ func TestAccSnapshotBlockPublicAccess_basic(t *testing.T) { }, { ResourceName: resourceName, - Config: testAccEbsSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockNewSharing)), + Config: testAccEC2EBSSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockNewSharing)), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, names.AttrState, "block-new-sharing"), ), @@ -51,7 +51,7 @@ func TestAccSnapshotBlockPublicAccess_basic(t *testing.T) { }) } -func testAccCheckSnapshotBlockAccessDestroy(ctx context.Context) resource.TestCheckFunc { +func testAccEC2EBSCheckSnapshotBlockAccessDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) response, err := conn.GetSnapshotBlockPublicAccessState(ctx, &ec2.GetSnapshotBlockPublicAccessStateInput{}) @@ -66,10 +66,10 @@ func testAccCheckSnapshotBlockAccessDestroy(ctx context.Context) resource.TestCh } } -func testAccEbsSnapshotBlockPublicAccess_basic(state string) string { +func testAccEC2EBSSnapshotBlockPublicAccess_basic(state string) string { return fmt.Sprintf(` resource "aws_ebs_snapshot_block_public_access" "this" { - state = "%[1]s" + state = "%[1]s" } `, state) } diff --git a/internal/service/ec2/service_package_gen.go b/internal/service/ec2/service_package_gen.go index fc150dc9abd..2673423d186 100644 --- a/internal/service/ec2/service_package_gen.go +++ b/internal/service/ec2/service_package_gen.go @@ -638,6 +638,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka { Factory: resourceEBSSnapshotBlockPublicAccess, TypeName: "aws_ebs_snapshot_block_public_access", + Name: "EBS Snapshot Block Public Access", }, { Factory: resourceEBSSnapshotCopy, From 6730786414b54f4e758b33747fa7594c871a48b8 Mon Sep 17 00:00:00 2001 From: Stefan Freitag Date: Thu, 1 Aug 2024 19:44:15 +0200 Subject: [PATCH 4/7] fix: renamed function --- internal/service/ec2/ebs_encryption_by_default.go | 1 + .../ec2/ebs_snapshot_block_public_access_test.go | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/internal/service/ec2/ebs_encryption_by_default.go b/internal/service/ec2/ebs_encryption_by_default.go index b2cf7a6afbd..4e1d72c03c2 100644 --- a/internal/service/ec2/ebs_encryption_by_default.go +++ b/internal/service/ec2/ebs_encryption_by_default.go @@ -5,6 +5,7 @@ package ec2 import ( "context" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" diff --git a/internal/service/ec2/ebs_snapshot_block_public_access_test.go b/internal/service/ec2/ebs_snapshot_block_public_access_test.go index 16d6615be08..730d40dbd21 100644 --- a/internal/service/ec2/ebs_snapshot_block_public_access_test.go +++ b/internal/service/ec2/ebs_snapshot_block_public_access_test.go @@ -26,11 +26,11 @@ func TestAccEC2EBSSnapshotBlockPublicAccess_basic(t *testing.T) { ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, WorkingDir: "/tmp", - CheckDestroy: testAccEC2EBSCheckSnapshotBlockAccessDestroy(ctx), + CheckDestroy: testAccEBSCheckSnapshotBlockAccessDestroy(ctx), Steps: []resource.TestStep{ { ResourceName: resourceName, - Config: testAccEC2EBSSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockAllSharing)), + Config: testAccEBSSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockAllSharing)), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, names.AttrState, "block-all-sharing"), ), @@ -42,7 +42,7 @@ func TestAccEC2EBSSnapshotBlockPublicAccess_basic(t *testing.T) { }, { ResourceName: resourceName, - Config: testAccEC2EBSSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockNewSharing)), + Config: testAccEBSSnapshotBlockPublicAccess_basic(string(types.SnapshotBlockPublicAccessStateBlockNewSharing)), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(resourceName, names.AttrState, "block-new-sharing"), ), @@ -51,7 +51,7 @@ func TestAccEC2EBSSnapshotBlockPublicAccess_basic(t *testing.T) { }) } -func testAccEC2EBSCheckSnapshotBlockAccessDestroy(ctx context.Context) resource.TestCheckFunc { +func testAccEBSCheckSnapshotBlockAccessDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) response, err := conn.GetSnapshotBlockPublicAccessState(ctx, &ec2.GetSnapshotBlockPublicAccessStateInput{}) @@ -66,7 +66,7 @@ func testAccEC2EBSCheckSnapshotBlockAccessDestroy(ctx context.Context) resource. } } -func testAccEC2EBSSnapshotBlockPublicAccess_basic(state string) string { +func testAccEBSSnapshotBlockPublicAccess_basic(state string) string { return fmt.Sprintf(` resource "aws_ebs_snapshot_block_public_access" "this" { state = "%[1]s" From bc0d992227d8ca1c4c57d9a97bcb03d395c7ac74 Mon Sep 17 00:00:00 2001 From: Stefan Freitag Date: Thu, 1 Aug 2024 20:05:20 +0200 Subject: [PATCH 5/7] fix: update imports --- internal/service/ec2/ebs_snapshot_block_public_access.go | 2 +- .../service/ec2/ebs_snapshot_block_public_access_test.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/service/ec2/ebs_snapshot_block_public_access.go b/internal/service/ec2/ebs_snapshot_block_public_access.go index 6d27eb3f028..2c3527976f1 100644 --- a/internal/service/ec2/ebs_snapshot_block_public_access.go +++ b/internal/service/ec2/ebs_snapshot_block_public_access.go @@ -5,7 +5,6 @@ package ec2 import ( "context" - "github.com/hashicorp/terraform-provider-aws/names" "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" @@ -14,6 +13,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/enum" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_ebs_snapshot_block_public_access", name="EBS Snapshot Block Public Access") diff --git a/internal/service/ec2/ebs_snapshot_block_public_access_test.go b/internal/service/ec2/ebs_snapshot_block_public_access_test.go index 730d40dbd21..bcdf5cf399b 100644 --- a/internal/service/ec2/ebs_snapshot_block_public_access_test.go +++ b/internal/service/ec2/ebs_snapshot_block_public_access_test.go @@ -6,15 +6,15 @@ package ec2_test import ( "context" "fmt" + "testing" + "github.com/aws/aws-sdk-go-v2/service/ec2" "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" + "github.com/hashicorp/terraform-provider-aws/internal/acctest" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/names" - "testing" - - "github.com/hashicorp/terraform-plugin-testing/helper/resource" - "github.com/hashicorp/terraform-provider-aws/internal/acctest" ) func TestAccEC2EBSSnapshotBlockPublicAccess_basic(t *testing.T) { From b12ad394bbd1e81e0f5a857c990d0f608e5008c3 Mon Sep 17 00:00:00 2001 From: Stefan Freitag Date: Thu, 1 Aug 2024 20:36:25 +0200 Subject: [PATCH 6/7] fix: update function name --- internal/service/ec2/ebs_snapshot_block_public_access_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/service/ec2/ebs_snapshot_block_public_access_test.go b/internal/service/ec2/ebs_snapshot_block_public_access_test.go index bcdf5cf399b..58f1d0aa826 100644 --- a/internal/service/ec2/ebs_snapshot_block_public_access_test.go +++ b/internal/service/ec2/ebs_snapshot_block_public_access_test.go @@ -26,7 +26,7 @@ func TestAccEC2EBSSnapshotBlockPublicAccess_basic(t *testing.T) { ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, WorkingDir: "/tmp", - CheckDestroy: testAccEBSCheckSnapshotBlockAccessDestroy(ctx), + CheckDestroy: testAccCheckEBSSnapshotBlockAccessDestroy(ctx), Steps: []resource.TestStep{ { ResourceName: resourceName, @@ -51,7 +51,7 @@ func TestAccEC2EBSSnapshotBlockPublicAccess_basic(t *testing.T) { }) } -func testAccEBSCheckSnapshotBlockAccessDestroy(ctx context.Context) resource.TestCheckFunc { +func testAccCheckEBSSnapshotBlockAccessDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) response, err := conn.GetSnapshotBlockPublicAccessState(ctx, &ec2.GetSnapshotBlockPublicAccessStateInput{}) From 73f55a945f27014a0ab93dacb0a7f3d6139288a7 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 2 Aug 2024 09:14:25 -0400 Subject: [PATCH 7/7] r/aws_ebs_snapshot_block_public_access: Cosmetics. --- .../ec2/ebs_snapshot_block_public_access.go | 43 +++++++------------ .../ebs_snapshot_block_public_access_test.go | 6 +-- 2 files changed, 19 insertions(+), 30 deletions(-) diff --git a/internal/service/ec2/ebs_snapshot_block_public_access.go b/internal/service/ec2/ebs_snapshot_block_public_access.go index 2c3527976f1..23342a0e00b 100644 --- a/internal/service/ec2/ebs_snapshot_block_public_access.go +++ b/internal/service/ec2/ebs_snapshot_block_public_access.go @@ -19,9 +19,9 @@ import ( // @SDKResource("aws_ebs_snapshot_block_public_access", name="EBS Snapshot Block Public Access") func resourceEBSSnapshotBlockPublicAccess() *schema.Resource { return &schema.Resource{ - CreateWithoutTimeout: resourceEBSSnapshotBlockPublicAccessCreate, + CreateWithoutTimeout: resourceEBSSnapshotBlockPublicAccessPut, ReadWithoutTimeout: resourceEBSSnapshotBlockPublicAccessRead, - UpdateWithoutTimeout: resourceEBSSnapshotBlockPublicAccessUpdate, + UpdateWithoutTimeout: resourceEBSSnapshotBlockPublicAccessPut, DeleteWithoutTimeout: resourceEBSSnapshotBlockPublicAccessDelete, Importer: &schema.ResourceImporter{ @@ -38,21 +38,24 @@ func resourceEBSSnapshotBlockPublicAccess() *schema.Resource { } } -func resourceEBSSnapshotBlockPublicAccessCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { +func resourceEBSSnapshotBlockPublicAccessPut(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) + state := d.Get(names.AttrState).(string) input := &ec2.EnableSnapshotBlockPublicAccessInput{ - State: types.SnapshotBlockPublicAccessState(d.Get(names.AttrState).(string)), + State: types.SnapshotBlockPublicAccessState(state), } _, err := conn.EnableSnapshotBlockPublicAccess(ctx, input) if err != nil { - return sdkdiag.AppendErrorf(diags, "enabling EBS snapshot block public access: %s", err) + return sdkdiag.AppendErrorf(diags, "enabling EBS Snapshot Block Public Access (%s): %s", state, err) } - d.SetId(meta.(*conns.AWSClient).Region) + if d.IsNewResource() { + d.SetId(meta.(*conns.AWSClient).Region) + } return append(diags, resourceEBSSnapshotBlockPublicAccessRead(ctx, d, meta)...) } @@ -61,41 +64,27 @@ func resourceEBSSnapshotBlockPublicAccessRead(ctx context.Context, d *schema.Res var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) - resp, err := conn.GetSnapshotBlockPublicAccessState(ctx, &ec2.GetSnapshotBlockPublicAccessStateInput{}) + input := &ec2.GetSnapshotBlockPublicAccessStateInput{} + output, err := conn.GetSnapshotBlockPublicAccessState(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "reading EBS snapshot block public access: %s", err) + return sdkdiag.AppendErrorf(diags, "reading EBS Snapshot Block Public Access: %s", err) } - d.Set(names.AttrState, string(resp.State)) + d.Set(names.AttrState, output.State) return diags } -func resourceEBSSnapshotBlockPublicAccessUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).EC2Client(ctx) - - input := &ec2.EnableSnapshotBlockPublicAccessInput{ - State: types.SnapshotBlockPublicAccessState(d.Get(names.AttrState).(string)), - } - - _, err := conn.EnableSnapshotBlockPublicAccess(ctx, input) - - if err != nil { - return sdkdiag.AppendErrorf(diags, "update EBS snapshot block public access: %s", err) - } - - return append(diags, resourceEBSSnapshotBlockPublicAccessRead(ctx, d, meta)...) -} - func resourceEBSSnapshotBlockPublicAccessDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).EC2Client(ctx) // Removing the resource disables blocking of EBS snapshot sharing. _, err := conn.DisableSnapshotBlockPublicAccess(ctx, &ec2.DisableSnapshotBlockPublicAccessInput{}) + if err != nil { - return sdkdiag.AppendErrorf(diags, "disabling EBS snapshot block public access: %s", err) + return sdkdiag.AppendErrorf(diags, "disabling EBS Snapshot Block Public Access: %s", err) } return diags diff --git a/internal/service/ec2/ebs_snapshot_block_public_access_test.go b/internal/service/ec2/ebs_snapshot_block_public_access_test.go index 58f1d0aa826..5f7c7514271 100644 --- a/internal/service/ec2/ebs_snapshot_block_public_access_test.go +++ b/internal/service/ec2/ebs_snapshot_block_public_access_test.go @@ -19,7 +19,7 @@ import ( func TestAccEC2EBSSnapshotBlockPublicAccess_basic(t *testing.T) { ctx := acctest.Context(t) - resourceName := "aws_ebs_snapshot_block_public_access.this" + resourceName := "aws_ebs_snapshot_block_public_access.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, @@ -68,8 +68,8 @@ func testAccCheckEBSSnapshotBlockAccessDestroy(ctx context.Context) resource.Tes func testAccEBSSnapshotBlockPublicAccess_basic(state string) string { return fmt.Sprintf(` -resource "aws_ebs_snapshot_block_public_access" "this" { - state = "%[1]s" +resource "aws_ebs_snapshot_block_public_access" "test" { + state = %[1]q } `, state) }