Skip to content

Commit

Permalink
Merge pull request #34521 from markoskandylis/f-securitylake-data-lake
Browse files Browse the repository at this point in the history
F securitylake data lake
  • Loading branch information
ewbankkit authored Dec 7, 2023
2 parents 017bbcd + 69a092d commit 7e706c1
Show file tree
Hide file tree
Showing 15 changed files with 1,797 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/34521.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
resource/aws_securitylake_data_lake
```
6 changes: 6 additions & 0 deletions internal/create/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ func ProblemStandardMessage(service, action, resource, id string, gotError error
}

if gotError == nil {
if id == "" {
return fmt.Sprintf("%s %s %s", action, hf, resource)
}
return fmt.Sprintf("%s %s %s (%s)", action, hf, resource, id)
}

if id == "" {
return fmt.Sprintf("%s %s %s: %s", action, hf, resource, gotError)
}
return fmt.Sprintf("%s %s %s (%s): %s", action, hf, resource, id, gotError)
}

Expand Down
2 changes: 1 addition & 1 deletion internal/framework/types/list_nested_objectof.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func nestedObjectTypeNewObjectSlice[T any](_ context.Context, len, cap int) ([]*
return make([]*T, len, cap), diags
}

// ListNestedObjectValueOf represents a Terraform Plugin Framework List value whose elements are of type ObjectTypeOf.
// ListNestedObjectValueOf represents a Terraform Plugin Framework List value whose elements are of type `ObjectTypeOf[T]`.
type ListNestedObjectValueOf[T any] struct {
basetypes.ListValue
}
Expand Down
2 changes: 1 addition & 1 deletion internal/framework/types/set_nested_objectof.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (t setNestedObjectTypeOf[T]) ValueFromObjectSlice(ctx context.Context, slic
return nil, diags
}

// SetNestedObjectValueOf represents a Terraform Plugin Framework Set value whose elements are of type ObjectTypeOf.
// SetNestedObjectValueOf represents a Terraform Plugin Framework Set value whose elements are of type `ObjectTypeOf[T]`.
type SetNestedObjectValueOf[T any] struct {
basetypes.SetValue
}
Expand Down
142 changes: 142 additions & 0 deletions internal/framework/types/setof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package types

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-provider-aws/internal/errs/fwdiag"
)

// setTypeOf is the attribute type of a SetValueOf.
type setTypeOf[T attr.Value] struct {
basetypes.SetType
}

var (
SetOfStringType = setTypeOf[basetypes.StringValue]{basetypes.SetType{ElemType: basetypes.StringType{}}}
)

var (
_ basetypes.SetTypable = (*setTypeOf[basetypes.StringValue])(nil)
_ basetypes.SetValuable = (*SetValueOf[basetypes.StringValue])(nil)
)

func newAttrTypeOf[T attr.Value](ctx context.Context) attr.Type {
var zero T
return zero.Type(ctx)
}

func NewSetTypeOf[T attr.Value](ctx context.Context) setTypeOf[T] {
return setTypeOf[T]{basetypes.SetType{ElemType: newAttrTypeOf[T](ctx)}}
}

func (t setTypeOf[T]) Equal(o attr.Type) bool {
other, ok := o.(setTypeOf[T])

if !ok {
return false
}

return t.SetType.Equal(other.SetType)
}

func (t setTypeOf[T]) String() string {
var zero T
return fmt.Sprintf("SetTypeOf[%T]", zero)
}

func (t setTypeOf[T]) ValueFromSet(ctx context.Context, in basetypes.SetValue) (basetypes.SetValuable, diag.Diagnostics) {
var diags diag.Diagnostics

if in.IsNull() {
return NewSetValueOfNull[T](ctx), diags
}
if in.IsUnknown() {
return NewSetValueOfUnknown[T](ctx), diags
}

setValue, d := basetypes.NewSetValue(newAttrTypeOf[T](ctx), in.Elements())
diags.Append(d...)
if diags.HasError() {
return NewSetValueOfUnknown[T](ctx), diags
}

value := SetValueOf[T]{
SetValue: setValue,
}

return value, diags
}

func (t setTypeOf[T]) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Value, error) {
attrValue, err := t.SetType.ValueFromTerraform(ctx, in)

if err != nil {
return nil, err
}

setValue, ok := attrValue.(basetypes.SetValue)

if !ok {
return nil, fmt.Errorf("unexpected value type of %T", attrValue)
}

setValuable, diags := t.ValueFromSet(ctx, setValue)

if diags.HasError() {
return nil, fmt.Errorf("unexpected error converting SetValue to SetValuable: %v", diags)
}

return setValuable, nil
}

func (t setTypeOf[T]) ValueType(ctx context.Context) attr.Value {
return SetValueOf[T]{}
}

// SetValueOf represents a Terraform Plugin Framework Set value whose elements are of type `T`.
type SetValueOf[T attr.Value] struct {
basetypes.SetValue
}

func (v SetValueOf[T]) Equal(o attr.Value) bool {
other, ok := o.(SetValueOf[T])

if !ok {
return false
}

return v.SetValue.Equal(other.SetValue)
}

func (v SetValueOf[T]) Type(ctx context.Context) attr.Type {
return NewSetTypeOf[T](ctx)
}

func NewSetValueOfNull[T attr.Value](ctx context.Context) SetValueOf[T] {
return SetValueOf[T]{SetValue: basetypes.NewSetNull(newAttrTypeOf[T](ctx))}
}

func NewSetValueOfUnknown[T attr.Value](ctx context.Context) SetValueOf[T] {
return SetValueOf[T]{SetValue: basetypes.NewSetUnknown(newAttrTypeOf[T](ctx))}
}

func NewSetValueOf[T attr.Value](ctx context.Context, elements []attr.Value) (SetValueOf[T], diag.Diagnostics) {
v, diags := basetypes.NewSetValue(newAttrTypeOf[T](ctx), elements)
if diags.HasError() {
return NewSetValueOfUnknown[T](ctx), diags
}

return SetValueOf[T]{SetValue: v}, diags
}

func NewSetValueOfMust[T attr.Value](ctx context.Context, elements []attr.Value) SetValueOf[T] {
return fwdiag.Must(NewSetValueOf[T](ctx, elements))
}
57 changes: 57 additions & 0 deletions internal/framework/types/setof_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package types_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-plugin-go/tftypes"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
)

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

ctx := context.Background()
tests := map[string]struct {
val tftypes.Value
expected attr.Value
}{
"values": {
val: tftypes.NewValue(tftypes.Set{
ElementType: tftypes.String,
}, []tftypes.Value{
tftypes.NewValue(tftypes.String, "red"),
tftypes.NewValue(tftypes.String, "blue"),
tftypes.NewValue(tftypes.String, "green"),
}),
expected: fwtypes.NewSetValueOfMust[types.String](ctx, []attr.Value{
types.StringValue("red"),
types.StringValue("blue"),
types.StringValue("green"),
}),
},
}

for name, test := range tests {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()

val, err := fwtypes.SetOfStringType.ValueFromTerraform(ctx, test.val)

if err != nil {
t.Fatalf("got unexpected error: %s", err)
}

if diff := cmp.Diff(val, test.expected); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
})
}
}
Loading

0 comments on commit 7e706c1

Please sign in to comment.