Skip to content

Commit

Permalink
Merge pull request #34481 from hashicorp/f-emr_supported_instance_types
Browse files Browse the repository at this point in the history
[New Data Source]: `aws_emr_supported_instance_types`
  • Loading branch information
jar-b authored Nov 21, 2023
2 parents 2144109 + ec8c6be commit cfec785
Show file tree
Hide file tree
Showing 12 changed files with 399 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/34481.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_emr_supported_instance_types
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/docdbelastic v1.5.3
github.com/aws/aws-sdk-go-v2/service/ec2 v1.137.0
github.com/aws/aws-sdk-go-v2/service/eks v1.33.2
github.com/aws/aws-sdk-go-v2/service/emr v1.34.1
github.com/aws/aws-sdk-go-v2/service/emrserverless v1.13.4
github.com/aws/aws-sdk-go-v2/service/finspace v1.17.1
github.com/aws/aws-sdk-go-v2/service/fis v1.19.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ github.com/aws/aws-sdk-go-v2/service/ec2 v1.137.0 h1:XCaAqb6eTyyzEKhLTXDmQRJyIIg
github.com/aws/aws-sdk-go-v2/service/ec2 v1.137.0/go.mod h1:hrBzQzlQQRmiaeYRQPr0SdSx6fdqP+5YcGhb97LCt8M=
github.com/aws/aws-sdk-go-v2/service/eks v1.33.2 h1:V31GdxyniIKfST6Dlfan7zvl0V64pdYbhA/A3+1sIYM=
github.com/aws/aws-sdk-go-v2/service/eks v1.33.2/go.mod h1:DInudKNZjEy7SJ0KfRh4VxaqY04B52Lq2+QRuvObfNQ=
github.com/aws/aws-sdk-go-v2/service/emr v1.34.1 h1:AkAncVuiOap3LS/kLs1QdVDY9LZyYHOMzCRybLtlxJU=
github.com/aws/aws-sdk-go-v2/service/emr v1.34.1/go.mod h1:HCZK6jCgxuYABdZFqiiHE3WT2tvTFVjefMB3ZX9dOxA=
github.com/aws/aws-sdk-go-v2/service/emrserverless v1.13.4 h1:V5unlj0Ky5ZuvLkndDQlHbYe3vshVJwavUZ7aGne8oo=
github.com/aws/aws-sdk-go-v2/service/emrserverless v1.13.4/go.mod h1:EC2zElGORkIzy2vPQV5U1qNGXIDzZHjL/KWbpQD6nc0=
github.com/aws/aws-sdk-go-v2/service/finspace v1.17.1 h1:GcBPj8B8EiVaHOba8Pq+CS+7w7s3q8ndETwPtpleS7A=
Expand Down
5 changes: 5 additions & 0 deletions internal/conns/awsclient_gen.go

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

16 changes: 16 additions & 0 deletions internal/framework/flex/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ func Float64ToFramework(ctx context.Context, v *float64) types.Float64 {
func Float64ToFrameworkLegacy(_ context.Context, v *float64) types.Float64 {
return types.Float64Value(aws.ToFloat64(v))
}

// Float32ToFramework converts a float32 pointer to a Framework Float64 value.
// A nil float32 pointer is converted to a null Float64.
func Float32ToFramework(ctx context.Context, v *float32) types.Float64 {
var output types.Float64

panicOnError(Flatten(ctx, v, &output))

return output
}

// Float32ToFrameworkLegacy converts a float32 pointer to a Framework Float64 value.
// A nil float32 pointer is converted to a zero float64.
func Float32ToFrameworkLegacy(_ context.Context, v *float32) types.Float64 {
return types.Float64Value(float64(aws.ToFloat32(v)))
}
78 changes: 75 additions & 3 deletions internal/framework/flex/float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@ func TestFloat64ToFrameworkLegacy(t *testing.T) {
expected types.Float64
}
tests := map[string]testCase{
"valid int64": {
"valid float64": {
input: aws.Float64(42.1),
expected: types.Float64Value(42.1),
},
"zero int64": {
"zero float64": {
input: aws.Float64(0),
expected: types.Float64Value(0),
},
"nil int64": {
"nil float64": {
input: nil,
expected: types.Float64Value(0),
},
Expand All @@ -84,3 +84,75 @@ func TestFloat64ToFrameworkLegacy(t *testing.T) {
})
}
}

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

type testCase struct {
input *float32
expected types.Float64
}
tests := map[string]testCase{
"valid float32": {
input: aws.Float32(42.0),
expected: types.Float64Value(42.0),
},
"zero float32": {
input: aws.Float32(0),
expected: types.Float64Value(0),
},
"nil float32": {
input: nil,
expected: types.Float64Null(),
},
}

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

got := flex.Float32ToFramework(context.Background(), test.input)

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

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

type testCase struct {
input *float32
expected types.Float64
}
tests := map[string]testCase{
"valid float32": {
input: aws.Float32(42.0),
expected: types.Float64Value(42.0),
},
"zero float32": {
input: aws.Float32(0),
expected: types.Float64Value(0),
},
"nil float32": {
input: nil,
expected: types.Float64Value(0),
},
}

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

got := flex.Float32ToFrameworkLegacy(context.Background(), test.input)

if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("unexpected diff (+wanted, -got): %s", diff)
}
})
}
}
20 changes: 19 additions & 1 deletion internal/service/emr/service_package_gen.go

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

179 changes: 179 additions & 0 deletions internal/service/emr/supported_instance_types_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package emr

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/emr"
awstypes "github.com/aws/aws-sdk-go-v2/service/emr/types"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
"github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource(name="Supported Instance Types")
func newDataSourceSupportedInstanceTypes(context.Context) (datasource.DataSourceWithConfigure, error) {
return &dataSourceSupportedInstanceTypes{}, nil
}

const (
DSNameSupportedInstanceTypes = "Supported Instance Types Data Source"
)

type dataSourceSupportedInstanceTypes struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceSupportedInstanceTypes) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { // nosemgrep:ci.meta-in-func-name
resp.TypeName = "aws_emr_supported_instance_types"
}

func (d *dataSourceSupportedInstanceTypes) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"id": framework.IDAttribute(),
"release_label": schema.StringAttribute{
Required: true,
},
},
Blocks: map[string]schema.Block{
"supported_instance_types": schema.ListNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"architecture": schema.StringAttribute{
Computed: true,
},
"ebs_optimized_available": schema.BoolAttribute{
Computed: true,
},
"ebs_optimized_by_default": schema.BoolAttribute{
Computed: true,
},
"ebs_storage_only": schema.BoolAttribute{
Computed: true,
},
"instance_family_id": schema.StringAttribute{
Computed: true,
},
"is_64_bits_only": schema.BoolAttribute{
Computed: true,
},
"memory_gb": schema.Float64Attribute{
Computed: true,
},
"number_of_disks": schema.Int64Attribute{
Computed: true,
},
"storage_gb": schema.Int64Attribute{
Computed: true,
},
"type": schema.StringAttribute{
Computed: true,
},
"vcpu": schema.Int64Attribute{
Computed: true,
},
},
},
},
},
}
}
func (d *dataSourceSupportedInstanceTypes) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
conn := d.Meta().EMRClient(ctx)

var data dataSourceSupportedInstanceTypesData
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
data.ID = types.StringValue(data.ReleaseLabel.ValueString())

input := &emr.ListSupportedInstanceTypesInput{
ReleaseLabel: aws.String(data.ReleaseLabel.ValueString()),
}

var results []awstypes.SupportedInstanceType
paginator := emr.NewListSupportedInstanceTypesPaginator(conn, input)
for paginator.HasMorePages() {
output, err := paginator.NextPage(ctx)
if err != nil {
resp.Diagnostics.AddError(
create.ProblemStandardMessage(names.EMR, create.ErrActionReading, DSNameSupportedInstanceTypes, data.ID.String(), err),
err.Error(),
)
return
}
results = append(results, output.SupportedInstanceTypes...)
}

supportedInstanceTypes, diag := flattenSupportedInstanceTypes(ctx, results)
resp.Diagnostics.Append(diag...)
data.SupportedInstanceTypes = supportedInstanceTypes

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

type dataSourceSupportedInstanceTypesData struct {
ID types.String `tfsdk:"id"`
ReleaseLabel types.String `tfsdk:"release_label"`
SupportedInstanceTypes types.List `tfsdk:"supported_instance_types"`
}

var supportedInstanceTypeAttrTypes = map[string]attr.Type{
"architecture": types.StringType,
"ebs_optimized_available": types.BoolType,
"ebs_optimized_by_default": types.BoolType,
"ebs_storage_only": types.BoolType,
"instance_family_id": types.StringType,
"is_64_bits_only": types.BoolType,
"memory_gb": types.Float64Type,
"number_of_disks": types.Int64Type,
"storage_gb": types.Int64Type,
"type": types.StringType,
"vcpu": types.Int64Type,
}

func flattenSupportedInstanceTypes(ctx context.Context, apiObjects []awstypes.SupportedInstanceType) (types.List, diag.Diagnostics) {
var diags diag.Diagnostics
elemType := types.ObjectType{AttrTypes: supportedInstanceTypeAttrTypes}

if len(apiObjects) == 0 {
return types.ListNull(elemType), diags
}

elems := []attr.Value{}
for _, apiObject := range apiObjects {
obj := map[string]attr.Value{
"architecture": flex.StringToFramework(ctx, apiObject.Architecture),
"ebs_optimized_available": flex.BoolToFramework(ctx, apiObject.EbsOptimizedAvailable),
"ebs_optimized_by_default": flex.BoolToFramework(ctx, apiObject.EbsOptimizedByDefault),
"ebs_storage_only": flex.BoolToFramework(ctx, apiObject.EbsStorageOnly),
"instance_family_id": flex.StringToFramework(ctx, apiObject.InstanceFamilyId),
"is_64_bits_only": flex.BoolToFramework(ctx, apiObject.Is64BitsOnly),
"memory_gb": flex.Float32ToFramework(ctx, apiObject.MemoryGB),
"number_of_disks": flex.Int32ToFramework(ctx, apiObject.NumberOfDisks),
"storage_gb": flex.Int32ToFramework(ctx, apiObject.StorageGB),
"type": flex.StringToFramework(ctx, apiObject.Type),
"vcpu": flex.Int32ToFramework(ctx, apiObject.VCPU),
}
objVal, d := types.ObjectValue(supportedInstanceTypeAttrTypes, obj)
diags.Append(d...)

elems = append(elems, objVal)
}

listVal, d := types.ListValue(elemType, elems)
diags.Append(d...)

return listVal, diags
}
Loading

0 comments on commit cfec785

Please sign in to comment.