-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
New resource: aws_ec2_instance_metadata_defaults #36589
Merged
ewbankkit
merged 11 commits into
hashicorp:main
from
christophetd:f-aws-ec2-instance-metadata-defaults
Mar 27, 2024
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
82e67a0
New resource: aws_ec2_instance_metadata_defaults (closes #36577)
christophetd 1b5f037
Fix linting warnings
christophetd f4e7cd4
lint: Avoid using 'EC2' in const and function names in EC2 package
christophetd 60b91f0
lint: use aws.ToBool
christophetd 563d178
Factorize code to avoid duplication
christophetd e6aecb6
make gen
christophetd 3a78d5a
Add docs
christophetd 41cd843
lint: fix style for golangci-lint
christophetd 5a5e089
Add CHANGELOG entry.
ewbankkit 48a0e4e
r/aws_ec2_instance_metadata_defaults: Use AutoFlEx.
ewbankkit 71925fb
r/aws_ec2_instance_metadata_defaults: Corrections.
ewbankkit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_instance_metadata_defaults | ||
``` |
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,210 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package ec2 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework" | ||
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" | ||
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" | ||
"github.com/hashicorp/terraform-provider-aws/internal/tfresource" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
const ( | ||
httpPutResponseHopLimitNoPreference = -1 | ||
) | ||
|
||
// @FrameworkResource(name="Instance Metadata Defaults") | ||
func newInstanceMetadataDefaultsResource(_ context.Context) (resource.ResourceWithConfigure, error) { | ||
r := &instanceMetadataDefaultsResource{} | ||
|
||
return r, nil | ||
} | ||
|
||
type instanceMetadataDefaultsResource struct { | ||
framework.ResourceWithConfigure | ||
framework.WithImportByID | ||
} | ||
|
||
func (*instanceMetadataDefaultsResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { | ||
response.TypeName = "aws_ec2_instance_metadata_defaults" | ||
} | ||
|
||
func (r *instanceMetadataDefaultsResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) { | ||
httpEndpointType := fwtypes.StringEnumType[awstypes.DefaultInstanceMetadataEndpointState]() | ||
httpTokensType := fwtypes.StringEnumType[awstypes.MetadataDefaultHttpTokensState]() | ||
instanceMetadataTagsType := fwtypes.StringEnumType[awstypes.DefaultInstanceMetadataTagsState]() | ||
|
||
response.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"http_endpoint": schema.StringAttribute{ | ||
CustomType: httpEndpointType, | ||
Optional: true, | ||
Computed: true, | ||
Default: httpEndpointType.AttributeDefault(awstypes.DefaultInstanceMetadataEndpointStateNoPreference), | ||
}, | ||
"http_put_response_hop_limit": schema.Int64Attribute{ | ||
Optional: true, | ||
Computed: true, | ||
Default: int64default.StaticInt64(httpPutResponseHopLimitNoPreference), | ||
Validators: []validator.Int64{ | ||
int64validator.Between(-1, 64), | ||
}, | ||
}, | ||
"http_tokens": schema.StringAttribute{ | ||
CustomType: httpTokensType, | ||
Optional: true, | ||
Computed: true, | ||
Default: httpTokensType.AttributeDefault(awstypes.MetadataDefaultHttpTokensStateNoPreference), | ||
}, | ||
names.AttrID: framework.IDAttribute(), | ||
"instance_metadata_tags": schema.StringAttribute{ | ||
CustomType: instanceMetadataTagsType, | ||
Optional: true, | ||
Computed: true, | ||
Default: instanceMetadataTagsType.AttributeDefault(awstypes.DefaultInstanceMetadataTagsStateNoPreference), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r *instanceMetadataDefaultsResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { | ||
var data instanceMetadataDefaultsModel | ||
response.Diagnostics.Append(request.Plan.Get(ctx, &data)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
conn := r.Meta().EC2Client(ctx) | ||
|
||
input := &ec2.ModifyInstanceMetadataDefaultsInput{} | ||
response.Diagnostics.Append(fwflex.Expand(ctx, data, input)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
_, err := conn.ModifyInstanceMetadataDefaults(ctx, input) | ||
|
||
if err != nil { | ||
response.Diagnostics.AddError("creating EC2 Instance Metadata Defaults", err.Error()) | ||
|
||
return | ||
} | ||
|
||
response.Diagnostics.Append(response.State.Set(ctx, data)...) | ||
} | ||
|
||
func (r *instanceMetadataDefaultsResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) { | ||
var data instanceMetadataDefaultsModel | ||
response.Diagnostics.Append(request.State.Get(ctx, &data)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
conn := r.Meta().EC2Client(ctx) | ||
|
||
output, err := findInstanceMetadataDefaults(ctx, conn) | ||
|
||
if tfresource.NotFound(err) { | ||
response.Diagnostics.Append(fwdiag.NewResourceNotFoundWarningDiagnostic(err)) | ||
response.State.RemoveResource(ctx) | ||
|
||
return | ||
} | ||
|
||
if err != nil { | ||
response.Diagnostics.AddError("reading EC2 Instance Metadata Defaults", err.Error()) | ||
|
||
return | ||
} | ||
|
||
// Set attributes for import. | ||
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
response.Diagnostics.Append(response.State.Set(ctx, &data)...) | ||
} | ||
|
||
// Update is very similar to Create as AWS has a single API call ModifyInstanceMetadataDefaults | ||
func (r *instanceMetadataDefaultsResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) { | ||
var new instanceMetadataDefaultsModel | ||
response.Diagnostics.Append(request.Plan.Get(ctx, &new)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
conn := r.Meta().EC2Client(ctx) | ||
|
||
input := &ec2.ModifyInstanceMetadataDefaultsInput{} | ||
response.Diagnostics.Append(fwflex.Expand(ctx, new, input)...) | ||
if response.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
_, err := conn.ModifyInstanceMetadataDefaults(ctx, input) | ||
|
||
if err != nil { | ||
response.Diagnostics.AddError("updating EC2 Instance Metadata Defaults", err.Error()) | ||
|
||
return | ||
} | ||
|
||
response.Diagnostics.Append(response.State.Set(ctx, &new)...) | ||
} | ||
|
||
func (r *instanceMetadataDefaultsResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) { | ||
conn := r.Meta().EC2Client(ctx) | ||
|
||
input := &ec2.ModifyInstanceMetadataDefaultsInput{ | ||
HttpEndpoint: awstypes.DefaultInstanceMetadataEndpointStateNoPreference, | ||
HttpPutResponseHopLimit: aws.Int32(httpPutResponseHopLimitNoPreference), | ||
HttpTokens: awstypes.MetadataDefaultHttpTokensStateNoPreference, | ||
InstanceMetadataTags: awstypes.DefaultInstanceMetadataTagsStateNoPreference, | ||
} | ||
|
||
_, err := conn.ModifyInstanceMetadataDefaults(ctx, input) | ||
|
||
if err != nil { | ||
response.Diagnostics.AddError("deleting EC2 Instance Metadata Defaults", err.Error()) | ||
|
||
return | ||
} | ||
} | ||
|
||
func findInstanceMetadataDefaults(ctx context.Context, conn *ec2.Client) (*awstypes.InstanceMetadataDefaultsResponse, error) { | ||
input := &ec2.GetInstanceMetadataDefaultsInput{} | ||
|
||
output, err := conn.GetInstanceMetadataDefaults(ctx, &ec2.GetInstanceMetadataDefaultsInput{}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if output == nil || output.AccountLevel == nil { | ||
return nil, tfresource.NewEmptyResultError(input) | ||
} | ||
|
||
return output.AccountLevel, nil | ||
} | ||
|
||
type instanceMetadataDefaultsModel struct { | ||
HttpEndpoint types.String `tfsdk:"http_endpoint"` | ||
HttpPutResponseHopLimit types.Int64 `tfsdk:"http_put_response_hop_limit"` | ||
HttpTokens types.String `tfsdk:"http_tokens"` | ||
InstanceMetadataTags types.String `tfsdk:"instance_metadata_tags"` | ||
} |
154 changes: 154 additions & 0 deletions
154
internal/service/ec2/ec2_instance_metadata_defaults_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,154 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package ec2_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
awstypes "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" | ||
tfec2 "github.com/hashicorp/terraform-provider-aws/internal/service/ec2" | ||
"github.com/hashicorp/terraform-provider-aws/internal/tfresource" | ||
"github.com/hashicorp/terraform-provider-aws/names" | ||
) | ||
|
||
func TestAccEC2InstanceMetadataDefaults_serial(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]func(t *testing.T){ | ||
"basic": testAccInstanceMetadataDefaults_basic, | ||
"update": testAccInstanceMetadataDefaults_update, | ||
} | ||
|
||
acctest.RunSerialTests1Level(t, testCases, 0) | ||
} | ||
|
||
func testAccInstanceMetadataDefaults_basic(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
resourceName := "aws_ec2_instance_metadata_defaults.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(ctx, t) }, | ||
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccCheckInstanceMetadataDefaultsDestroy(ctx), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccInstanceMetadataDefaultsConfig_basic(), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccCheckInstanceMetadataDefaultsExists(ctx, resourceName, &awstypes.InstanceMetadataDefaultsResponse{ | ||
HttpTokens: awstypes.HttpTokensState(awstypes.MetadataDefaultHttpTokensStateRequired), | ||
HttpPutResponseHopLimit: aws.Int32(1), | ||
HttpEndpoint: awstypes.InstanceMetadataEndpointStateEnabled, | ||
InstanceMetadataTags: awstypes.InstanceMetadataTagsStateDisabled, | ||
}), | ||
resource.TestCheckResourceAttr(resourceName, "http_tokens", "required"), | ||
resource.TestCheckResourceAttr(resourceName, "instance_metadata_tags", "disabled"), | ||
resource.TestCheckResourceAttr(resourceName, "http_endpoint", "enabled"), | ||
resource.TestCheckResourceAttr(resourceName, "http_put_response_hop_limit", "1"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccInstanceMetadataDefaults_update(t *testing.T) { | ||
ctx := acctest.Context(t) | ||
resourceName := "aws_ec2_instance_metadata_defaults.test" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acctest.PreCheck(ctx, t) }, | ||
ErrorCheck: acctest.ErrorCheck(t, names.EC2ServiceID), | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, | ||
CheckDestroy: testAccCheckInstanceMetadataDefaultsDestroy(ctx), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccInstanceMetadataDefaultsConfig_partial(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckInstanceMetadataDefaultsExists(ctx, resourceName, &awstypes.InstanceMetadataDefaultsResponse{ | ||
HttpTokens: awstypes.HttpTokensState(awstypes.MetadataDefaultHttpTokensStateRequired), | ||
HttpPutResponseHopLimit: aws.Int32(2), | ||
HttpEndpoint: "", | ||
InstanceMetadataTags: "", | ||
}), | ||
resource.TestCheckResourceAttr(resourceName, "http_tokens", "required"), | ||
resource.TestCheckResourceAttr(resourceName, "http_put_response_hop_limit", "2"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckInstanceMetadataDefaultsDestroy(ctx context.Context) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_ec2_instance_metadata_defaults" { | ||
continue | ||
} | ||
|
||
_, err := tfec2.FindInstanceMetadataDefaults(ctx, conn) | ||
|
||
if tfresource.NotFound(err) { | ||
continue | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return fmt.Errorf("EC2 Instance Metadata Defaults %s still exists", rs.Primary.ID) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckInstanceMetadataDefaultsExists(ctx context.Context, n string, v *awstypes.InstanceMetadataDefaultsResponse) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
_, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
conn := acctest.Provider.Meta().(*conns.AWSClient).EC2Client(ctx) | ||
|
||
output, err := tfec2.FindInstanceMetadataDefaults(ctx, conn) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
*v = *output | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccInstanceMetadataDefaultsConfig_basic() string { | ||
return ` | ||
resource "aws_ec2_instance_metadata_defaults" "test" { | ||
http_tokens = "required" # non-default | ||
instance_metadata_tags = "disabled" | ||
http_endpoint = "enabled" | ||
http_put_response_hop_limit = 1 | ||
} | ||
` | ||
} | ||
|
||
func testAccInstanceMetadataDefaultsConfig_partial() string { | ||
return ` | ||
resource "aws_ec2_instance_metadata_defaults" "test" { | ||
http_tokens = "required" # non-default | ||
http_put_response_hop_limit = 2 # non-default | ||
} | ||
` | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GetInstanceMetadataDefaults always return something (even if no defaults were explicitely set through ModifyInstanceMetadataDefaults), so I think this check is not necessary FWIW