-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33810 from chilledornaments/f-aws_ec2_image_block…
…_public_access r/aws_ec2_image_block_public_access add new resource
- Loading branch information
Showing
10 changed files
with
265 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_ec2_image_block_public_access | ||
``` |
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
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,113 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package ec2 | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
"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-plugin-sdk/v2/helper/validation" | ||
"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/internal/tfresource" | ||
"golang.org/x/exp/slices" | ||
) | ||
|
||
// @SDKResource("aws_ec2_image_block_public_access", name="Image Block Public Access") | ||
func ResourceImageBlockPublicAccess() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateWithoutTimeout: resourceImageBlockPublicAccessPut, | ||
ReadWithoutTimeout: resourceImageBlockPublicAccessRead, | ||
UpdateWithoutTimeout: resourceImageBlockPublicAccessPut, | ||
DeleteWithoutTimeout: schema.NoopContext, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Update: schema.DefaultTimeout(10 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"state": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice(imageBlockPublicAccessState_Values(), false), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceImageBlockPublicAccessPut(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
conn := meta.(*conns.AWSClient).EC2Client(ctx) | ||
|
||
state := d.Get("state").(string) | ||
|
||
if slices.Contains(imageBlockPublicAccessEnabledState_Values(), state) { | ||
input := &ec2.EnableImageBlockPublicAccessInput{ | ||
ImageBlockPublicAccessState: types.ImageBlockPublicAccessEnabledState(state), | ||
} | ||
|
||
_, err := conn.EnableImageBlockPublicAccess(ctx, input) | ||
|
||
if err != nil { | ||
return sdkdiag.AppendErrorf(diags, "enabling EC2 Image Block Public Access: %s", err) | ||
} | ||
} else { | ||
input := &ec2.DisableImageBlockPublicAccessInput{} | ||
|
||
_, err := conn.DisableImageBlockPublicAccess(ctx, input) | ||
|
||
if err != nil { | ||
return sdkdiag.AppendErrorf(diags, "disabling EC2 Image Block Public Access: %s", err) | ||
} | ||
} | ||
|
||
if d.IsNewResource() { | ||
d.SetId(meta.(*conns.AWSClient).Region) | ||
} | ||
|
||
if err := WaitImageBlockPublicAccessState(ctx, conn, state, d.Timeout(schema.TimeoutUpdate)); err != nil { | ||
return sdkdiag.AppendErrorf(diags, "waiting for EC2 Image Block Public Access state (%s): %s", state, err) | ||
} | ||
|
||
return append(diags, resourceImageBlockPublicAccessRead(ctx, d, meta)...) | ||
} | ||
|
||
func resourceImageBlockPublicAccessRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
conn := meta.(*conns.AWSClient).EC2Client(ctx) | ||
|
||
output, err := FindImageBlockPublicAccessState(ctx, conn) | ||
|
||
if !d.IsNewResource() && tfresource.NotFound(err) { | ||
log.Printf("[WARN] EC2 Image Block Public Access %s not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return diags | ||
} | ||
|
||
if err != nil { | ||
return sdkdiag.AppendErrorf(diags, "reading EC2 Image Block Public Access (%s): %s", d.Id(), err) | ||
} | ||
|
||
d.Set("state", output) | ||
|
||
return diags | ||
} | ||
|
||
func imageBlockPublicAccessDisabledState_Values() []string { | ||
return enum.Values[types.ImageBlockPublicAccessDisabledState]() | ||
} | ||
|
||
func imageBlockPublicAccessEnabledState_Values() []string { | ||
return enum.Values[types.ImageBlockPublicAccessEnabledState]() | ||
} | ||
|
||
func imageBlockPublicAccessState_Values() []string { | ||
return append(imageBlockPublicAccessEnabledState_Values(), imageBlockPublicAccessDisabledState_Values()...) | ||
} |
55 changes: 55 additions & 0 deletions
55
internal/service/ec2/ec2_image_block_public_access_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,55 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package ec2_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/acctest" | ||
) | ||
|
||
func TestAccEC2ImageBlockPublicAccess_serial(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]func(t *testing.T){ | ||
"basic": testAccImageBlockPublicAccess_basic, | ||
} | ||
|
||
acctest.RunSerialTests1Level(t, testCases, 0) | ||
} | ||
|
||
func testAccImageBlockPublicAccess_basic(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
resourceName := "aws_ec2_image_block_public_access.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(ctx, t) }, | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: acctest.CheckDestroyNoop, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccImageBlockPublicAccessConfig_basic("unblocked"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "state", "unblocked"), | ||
), | ||
}, | ||
{ | ||
Config: testAccImageBlockPublicAccessConfig_basic("block-new-sharing"), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr(resourceName, "state", "block-new-sharing"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccImageBlockPublicAccessConfig_basic(state string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_ec2_image_block_public_access" "test" { | ||
state = %[1]q | ||
} | ||
`, state) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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,43 @@ | ||
--- | ||
subcategory: "EC2 (Elastic Compute Cloud)" | ||
layout: "aws" | ||
page_title: "AWS: aws_ec2_image_block_public_access" | ||
description: |- | ||
Provides a regional public access block for AMIs. This prevents AMIs from being made publicly accessible. | ||
--- | ||
|
||
# Resource: aws_ec2_image_block_public_access | ||
|
||
Provides a regional public access block for AMIs. This prevents AMIs from being made publicly accessible. | ||
If you already have public AMIs, they will remain publicly available. | ||
|
||
~> **NOTE:** Deleting this resource does not change the block public access value, the resource in simply removed from state instead. | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
# Prevent making AMIs publicly accessible in the region and account for which the provider is configured | ||
resource "aws_ec2_image_block_public_access" "test" { | ||
state = "block-new-sharing" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
This resource supports the following arguments: | ||
|
||
* `state` - (Required) The state of block public access for AMIs at the account level in the configured AWS Region. Valid values: `unblocked` and `block-new-sharing`. | ||
|
||
## Attribute Reference | ||
|
||
This resource exports no additional attributes. | ||
|
||
## Timeouts | ||
|
||
[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts): | ||
|
||
- `update` - (Default `10m`) | ||
|
||
## Import | ||
|
||
You cannot import this resource. |