Skip to content

Commit

Permalink
d/aws_emr_supported_instance_types: new data source
Browse files Browse the repository at this point in the history
  • Loading branch information
jar-b committed Nov 20, 2023
1 parent 0e84bd6 commit 25d6709
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 1 deletion.
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
}
50 changes: 50 additions & 0 deletions internal/service/emr/supported_instance_types_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package emr_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/names"
)

func TestAccEMRSupportedInstanceTypesDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
dataSourceName := "data.aws_emr_supported_instance_types.test"
releaseLabel := "emr-6.15.0"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.EMREndpointID)
testAccPreCheck(ctx, t)
},
ErrorCheck: acctest.ErrorCheck(t, names.EMREndpointID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccSupportedInstanceTypesDataSourceConfig_basic(releaseLabel),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "release_label", releaseLabel),
// Verify a known supported type is included in the output
resource.TestCheckTypeSetElemNestedAttrs(dataSourceName, "supported_instance_types.*", map[string]string{
"type": "m5.xlarge",
}),
),
},
},
})
}

func testAccSupportedInstanceTypesDataSourceConfig_basic(releaseLabel string) string {
return fmt.Sprintf(`
data "aws_emr_supported_instance_types" "test" {
release_label = %[1]q
}
`, releaseLabel)
}
47 changes: 47 additions & 0 deletions website/docs/d/emr_supported_instance_types.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
subcategory: "EMR"
layout: "aws"
page_title: "AWS: aws_emr_supported_instance_types"
description: |-
Terraform data source for managing AWS EMR Supported Instance Types.
---

# Data Source: aws_emr_supported_instance_types

Terraform data source for managing AWS EMR Supported Instance Types.

## Example Usage

### Basic Usage

```terraform
data "aws_emr_supported_instance_types" "example" {
release_label = "ebs-6.15.0"
}
```

## Argument Reference

The following arguments are required:

* `release_label` - (Required) Amazon EMR release label. For more information about Amazon EMR releases and their included application versions and features, see the [Amazon EMR Release Guide](https://docs.aws.amazon.com/emr/latest/ReleaseGuide/emr-release-components.html).

## Attribute Reference

This data source exports the following attributes in addition to the arguments above:

* `supported_instance_types` - List of supported instance types. See [`supported_instance_types`](#supported_instance_types-attribute-reference) below.

### `supported_instance_types` Attribute Reference

* `architecture` - CPU architecture.
* `ebs_optimized_available` - Indicates whether the instance type supports Amazon EBS optimization.
* `ebs_optimized_by_default` - Indicates whether the instance type uses Amazon EBS optimization by default.
* `ebs_storage_only` - Indicates whether the instance type only supports Amazon EBS.
* `instance_family_id` - The Amazon EC2 family and generation for the instance type.
* `is_64_bits_only` - Indicates whether the instance type only supports 64-bit architecture.
* `memory_gb` - Memory that is available to Amazon EMR from the instance type.
* `number_of_disks` - Number of disks for the instance type.
* `storage_gb` - Storage capacity of the instance type.
* `type` - Amazon EC2 instance type. For example, `m5.xlarge`.
* `vcpu` - The number of vCPUs available for the instance type.

0 comments on commit 25d6709

Please sign in to comment.