-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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 #28841 from hashicorp/f-aws_resourceexplorer2_view
New resource: `aws_resourceexplorer2_view`
- Loading branch information
Showing
16 changed files
with
1,250 additions
and
17 deletions.
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_resourceexplorer2_view | ||
``` |
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,42 @@ | ||
package framework | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
) | ||
|
||
// AttributeTypes returns a map of attribute types for the specified type T. | ||
// T must be a struct and reflection is used to find exported fields of T with the `tfsdk` tag. | ||
func AttributeTypes[T any](ctx context.Context) (map[string]attr.Type, error) { | ||
var t T | ||
val := reflect.ValueOf(t) | ||
typ := val.Type() | ||
|
||
if typ.Kind() != reflect.Struct { | ||
return nil, fmt.Errorf("%T has unsupported type: %s", t, typ) | ||
} | ||
|
||
attributeTypes := make(map[string]attr.Type) | ||
for i := 0; i < typ.NumField(); i++ { | ||
field := typ.Field(i) | ||
if field.PkgPath != "" { | ||
continue // Skip unexported fields. | ||
} | ||
tag := field.Tag.Get(`tfsdk`) | ||
if tag == "-" { | ||
continue // Skip explicitly excluded fields. | ||
} | ||
if tag == "" { | ||
return nil, fmt.Errorf(`%T needs a struct tag for "tfsdk" on %s`, t, field.Name) | ||
} | ||
|
||
if v, ok := val.Field(i).Interface().(attr.Value); ok { | ||
attributeTypes[tag] = v.Type(ctx) | ||
} | ||
} | ||
|
||
return attributeTypes, nil | ||
} |
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,57 @@ | ||
package framework_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-provider-aws/internal/framework" | ||
) | ||
|
||
func TestAttributeTypes(t *testing.T) { | ||
t.Parallel() | ||
|
||
type struct1 struct{} //nolint:unused // Used as a type parameter. | ||
type struct2 struct { //nolint:unused // Used as a type parameter. | ||
ARN types.String `tfsdk:"arn"` | ||
ID types.Int64 `tfsdk:"id"` | ||
IncludeProperty types.Bool `tfsdk:"include_property"` | ||
} | ||
|
||
ctx := context.Background() | ||
got, err := framework.AttributeTypes[struct1](ctx) | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error") | ||
} | ||
|
||
wanted := map[string]attr.Type{} | ||
|
||
if diff := cmp.Diff(got, wanted); diff != "" { | ||
t.Errorf("unexpected diff (+wanted, -got): %s", diff) | ||
} | ||
|
||
_, err = framework.AttributeTypes[int](ctx) | ||
|
||
if err == nil { | ||
t.Fatalf("expected error") | ||
} | ||
|
||
got, err = framework.AttributeTypes[struct2](ctx) | ||
|
||
if err != nil { | ||
t.Fatalf("unexpected error") | ||
} | ||
|
||
wanted = map[string]attr.Type{ | ||
"arn": types.StringType, | ||
"id": types.Int64Type, | ||
"include_property": types.BoolType, | ||
} | ||
|
||
if diff := cmp.Diff(got, wanted); diff != "" { | ||
t.Errorf("unexpected diff (+wanted, -got): %s", diff) | ||
} | ||
} |
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,42 @@ | ||
package boolplanmodifier | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
type defaultValue struct { | ||
val bool | ||
} | ||
|
||
// DefaultValue return a bool plan modifier that sets the specified value if the planned value is Null. | ||
func DefaultValue(b bool) planmodifier.Bool { | ||
return defaultValue{ | ||
val: b, | ||
} | ||
} | ||
|
||
func (m defaultValue) Description(context.Context) string { | ||
return fmt.Sprintf("If value is not configured, defaults to %t", m.val) | ||
} | ||
|
||
func (m defaultValue) MarkdownDescription(ctx context.Context) string { | ||
return m.Description(ctx) | ||
} | ||
|
||
func (m defaultValue) PlanModifyBool(ctx context.Context, req planmodifier.BoolRequest, resp *planmodifier.BoolResponse) { | ||
if !req.ConfigValue.IsNull() { | ||
return | ||
} | ||
|
||
// If the attribute plan is "known" and "not null", then a previous plan modifier in the sequence | ||
// has already been applied, and we don't want to interfere. | ||
if !req.PlanValue.IsUnknown() && !req.PlanValue.IsNull() { | ||
return | ||
} | ||
|
||
resp.PlanValue = types.BoolValue(m.val) | ||
} |
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,67 @@ | ||
package boolplanmodifier | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
func TestDefaultValue(t *testing.T) { | ||
t.Parallel() | ||
|
||
type testCase struct { | ||
plannedValue types.Bool | ||
currentValue types.Bool | ||
defaultValue bool | ||
expectedValue types.Bool | ||
expectError bool | ||
} | ||
tests := map[string]testCase{ | ||
"default bool": { | ||
plannedValue: types.BoolNull(), | ||
currentValue: types.BoolValue(true), | ||
defaultValue: true, | ||
expectedValue: types.BoolValue(true), | ||
}, | ||
"default bool on create": { | ||
plannedValue: types.BoolNull(), | ||
currentValue: types.BoolNull(), | ||
defaultValue: true, | ||
expectedValue: types.BoolValue(true), | ||
}, | ||
} | ||
|
||
for name, test := range tests { | ||
name, test := name, test | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
request := planmodifier.BoolRequest{ | ||
Path: path.Root("test"), | ||
PlanValue: test.plannedValue, | ||
StateValue: test.currentValue, | ||
} | ||
response := planmodifier.BoolResponse{ | ||
PlanValue: request.PlanValue, | ||
} | ||
DefaultValue(test.defaultValue).PlanModifyBool(ctx, request, &response) | ||
|
||
if !response.Diagnostics.HasError() && test.expectError { | ||
t.Fatal("expected error, got no error") | ||
} | ||
|
||
if response.Diagnostics.HasError() && !test.expectError { | ||
t.Fatalf("got unexpected error: %s", response.Diagnostics) | ||
} | ||
|
||
if diff := cmp.Diff(response.PlanValue, test.expectedValue); diff != "" { | ||
t.Errorf("unexpected diff (+wanted, -got): %s", diff) | ||
} | ||
}) | ||
} | ||
} |
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,60 @@ | ||
//go:build sweep | ||
// +build sweep | ||
|
||
package resourceexplorer2 | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/resourceexplorer2" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-provider-aws/internal/conns" | ||
"github.com/hashicorp/terraform-provider-aws/internal/sweep" | ||
) | ||
|
||
func init() { | ||
resource.AddTestSweepers("aws_resourceexplorer2_index", &resource.Sweeper{ | ||
Name: "aws_resourceexplorer2_index", | ||
F: sweepIndexes, | ||
}) | ||
} | ||
|
||
func sweepIndexes(region string) error { | ||
client, err := sweep.SharedRegionalSweepClient(region) | ||
if err != nil { | ||
return fmt.Errorf("error getting client: %s", err) | ||
} | ||
conn := client.(*conns.AWSClient).ResourceExplorer2Client() | ||
input := &resourceexplorer2.ListIndexesInput{} | ||
sweepResources := make([]sweep.Sweepable, 0) | ||
ctx := context.Background() | ||
|
||
pages := resourceexplorer2.NewListIndexesPaginator(conn, input) | ||
for pages.HasMorePages() { | ||
page, err := pages.NextPage(ctx) | ||
|
||
if sweep.SkipSweepError(err) { | ||
log.Printf("[WARN] Skipping Resource Explorer Index sweep for %s: %s", region, err) | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error listing Resource Explorer Indexes (%s): %w", region, err) | ||
} | ||
|
||
for _, v := range page.Indexes { | ||
sweepResources = append(sweepResources, sweep.NewSweepFrameworkResource(newResourceIndex, aws.ToString(v.Arn), client)) | ||
} | ||
} | ||
|
||
err = sweep.SweepOrchestrator(sweepResources) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error sweeping Resource Explorer Indexes (%s): %w", region, err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.