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

b/aws_resourceexplorer2_index error create AGGREGATOR index and r/aws_resourceexplorer2_view new argument: scope #39744

Merged
7 changes: 7 additions & 0 deletions .changelog/39744.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_resourceexplorer2_index: Correctly mark incomplete `AGGREGATOR` indexes as [tainted](https://developer.hashicorp.com/terraform/cli/state/taint#the-tainted-status) on Create
```

```release-note:enhancement
resource/aws_resourceexplorer2_view: Add `scope` argument
```
5 changes: 3 additions & 2 deletions internal/service/resourceexplorer2/exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ package resourceexplorer2

// Exports for use in tests only.
var (
ResourceIndex = newIndexResource
ResourceView = newViewResource

FindIndex = findIndex
FindViewByARN = findViewByARN
ResourceIndex = newResourceIndex
ResourceView = newResourceView
)
60 changes: 27 additions & 33 deletions internal/service/resourceexplorer2/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
"github.com/aws/aws-sdk-go-v2/service/resourceexplorer2"
awstypes "github.com/aws/aws-sdk-go-v2/service/resourceexplorer2/types"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
sdkid "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand All @@ -31,26 +32,27 @@

// @FrameworkResource(name="Index")
// @Tags(identifierAttribute="id")
func newResourceIndex(context.Context) (resource.ResourceWithConfigure, error) {
r := &resourceIndex{}
func newIndexResource(context.Context) (resource.ResourceWithConfigure, error) {
r := &indexResource{}

r.SetDefaultCreateTimeout(2 * time.Hour)
r.SetDefaultUpdateTimeout(2 * time.Hour)
r.SetDefaultDeleteTimeout(10 * time.Minute)

return r, nil
}

type resourceIndex struct {
type indexResource struct {
framework.ResourceWithConfigure
framework.WithImportByID
framework.WithTimeouts
}

func (r *resourceIndex) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
func (*indexResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) {
response.TypeName = "aws_resourceexplorer2_index"
}

func (r *resourceIndex) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
func (r *indexResource) Schema(ctx context.Context, request resource.SchemaRequest, response *resource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrARN: framework.ARNAttributeComputedOnly(),
Expand All @@ -72,19 +74,17 @@
}
}

func (r *resourceIndex) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
func (r *indexResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) {
var data indexResourceModel

response.Diagnostics.Append(request.Plan.Get(ctx, &data)...)

if response.Diagnostics.HasError() {
return
}

conn := r.Meta().ResourceExplorer2Client(ctx)

input := &resourceexplorer2.CreateIndexInput{
ClientToken: aws.String(id.UniqueId()),
ClientToken: aws.String(sdkid.UniqueId()),
Tags: getTagsIn(ctx),
}

Expand All @@ -108,15 +108,15 @@

if data.Type.ValueEnum() == awstypes.IndexTypeAggregator {
input := &resourceexplorer2.UpdateIndexTypeInput{
Arn: flex.StringFromFramework(ctx, data.ID),
Arn: fwflex.StringFromFramework(ctx, data.ID),
Type: awstypes.IndexTypeAggregator,
}

_, err := conn.UpdateIndexType(ctx, input)

if err != nil {
response.State.SetAttribute(ctx, path.Root(names.AttrID), data.ID) // Set 'id' so as to taint the resource.
response.Diagnostics.AddError(fmt.Sprintf("updating Resource Explorer Index (%s)", data.ID.ValueString()), err.Error())

return
}

Expand All @@ -133,11 +133,9 @@
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

func (r *resourceIndex) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
func (r *indexResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) {
var data indexResourceModel

response.Diagnostics.Append(request.State.Get(ctx, &data)...)

if response.Diagnostics.HasError() {
return
}
Expand All @@ -159,7 +157,7 @@
return
}

response.Diagnostics.Append(flex.Flatten(ctx, output, &data)...)
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)

if response.Diagnostics.HasError() {
return
Expand All @@ -170,17 +168,13 @@
response.Diagnostics.Append(response.State.Set(ctx, &data)...)
}

func (r *resourceIndex) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
func (r *indexResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) {
var old, new indexResourceModel

response.Diagnostics.Append(request.State.Get(ctx, &old)...)

if response.Diagnostics.HasError() {
return
}

response.Diagnostics.Append(request.Plan.Get(ctx, &new)...)

if response.Diagnostics.HasError() {
return
}
Expand All @@ -189,7 +183,7 @@
conn := r.Meta().ResourceExplorer2Client(ctx)

input := &resourceexplorer2.UpdateIndexTypeInput{
Arn: flex.StringFromFramework(ctx, new.ARN),
Arn: fwflex.StringFromFramework(ctx, new.ARN),
Type: new.Type.ValueEnum(),
}

Expand All @@ -201,8 +195,7 @@
return
}

updateTimeout := r.UpdateTimeout(ctx, new.Timeouts)
if _, err := waitIndexUpdated(ctx, conn, updateTimeout); err != nil {
if _, err := waitIndexUpdated(ctx, conn, r.UpdateTimeout(ctx, new.Timeouts)); err != nil {
response.Diagnostics.AddError(fmt.Sprintf("waiting for Resource Explorer Index (%s) update", new.ID.ValueString()), err.Error())

return
Expand All @@ -212,11 +205,9 @@
response.Diagnostics.Append(response.State.Set(ctx, &new)...)
}

func (r *resourceIndex) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
func (r *indexResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) {
var data indexResourceModel

response.Diagnostics.Append(request.State.Get(ctx, &data)...)

if response.Diagnostics.HasError() {
return
}
Expand All @@ -227,24 +218,27 @@
names.AttrID: data.ID.ValueString(),
})
_, err := conn.DeleteIndex(ctx, &resourceexplorer2.DeleteIndexInput{
Arn: flex.StringFromFramework(ctx, data.ARN),
Arn: fwflex.StringFromFramework(ctx, data.ARN),
})

if errs.IsAErrorMessageContains[*awstypes.ValidationException](err, "The index is DELETED") {
return
}

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("deleting Resource Explorer Index (%s)", data.ID.ValueString()), err.Error())

return
}

deleteTimeout := r.DeleteTimeout(ctx, data.Timeouts)
if _, err := waitIndexDeleted(ctx, conn, deleteTimeout); err != nil {
if _, err := waitIndexDeleted(ctx, conn, r.DeleteTimeout(ctx, data.Timeouts)); err != nil {
response.Diagnostics.AddError(fmt.Sprintf("waiting for Resource Explorer Index (%s) delete", data.ID.ValueString()), err.Error())

return
}
}

func (r *resourceIndex) ModifyPlan(ctx context.Context, request resource.ModifyPlanRequest, response *resource.ModifyPlanResponse) {
func (r *indexResource) ModifyPlan(ctx context.Context, request resource.ModifyPlanRequest, response *resource.ModifyPlanResponse) {
r.SetTagsAll(ctx, request, response)
}

Expand Down Expand Up @@ -338,7 +332,7 @@
return nil, err
}

func waitIndexDeleted(ctx context.Context, conn *resourceexplorer2.Client, timeout time.Duration) (*resourceexplorer2.GetIndexOutput, error) {
func waitIndexDeleted(ctx context.Context, conn *resourceexplorer2.Client, timeout time.Duration) (*resourceexplorer2.GetIndexOutput, error) { //nolint:unparam

Check failure on line 335 in internal/service/resourceexplorer2/index.go

View workflow job for this annotation

GitHub Actions / 3 of 3

directive `//nolint:unparam` is unused for linter "unparam" (nolintlint)
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.IndexStateDeleting),
Target: []string{},
Expand Down
5 changes: 5 additions & 0 deletions internal/service/resourceexplorer2/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ func testAccIndex_type(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, names.AttrType, "LOCAL"),
),
},
{
Config: testAccIndexConfig_type("AGGREGATOR"),
ExpectError: regexache.MustCompile("cool down period has expired"),
Check: testAccCheckIndexDestroy(ctx),
},
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
)

func TestAccResourceExplorer2_serial(t *testing.T) {
t.Parallel()

testCases := map[string]map[string]func(t *testing.T){
"Index": {
acctest.CtBasic: testAccIndex_basic,
Expand All @@ -24,6 +22,7 @@ func TestAccResourceExplorer2_serial(t *testing.T) {
"defaultView": testAccView_defaultView,
acctest.CtDisappears: testAccView_disappears,
"filter": testAccView_filter,
"scope": testAccView_scope,
"tags": testAccView_tags,
},
"SearchDataSource": {
Expand Down
4 changes: 2 additions & 2 deletions internal/service/resourceexplorer2/service_package_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/service/resourceexplorer2/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func sweepIndexes(region string) error {
}

for _, v := range page.Indexes {
sweepResources = append(sweepResources, framework.NewSweepResource(newResourceIndex, client,
sweepResources = append(sweepResources, framework.NewSweepResource(newIndexResource, client,
framework.NewAttribute(names.AttrARN, aws.ToString(v.Arn)),
))
}
Expand Down
Loading
Loading