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

Added resource and data source for aws ec2 capacity block reservation. #37528

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
5602cec
Added resource and data source for aws ec2 capacity block reservation.
prabhavpawar May 14, 2024
6198823
Add changelog
prabhavpawar May 16, 2024
3d1d7fe
Merge branch 'main' into feature/f-aws_ec2_capacity_block-add
johnsonaj Jun 3, 2024
ce01151
aws_ec2_capacity_block_offering: refactor to framework
johnsonaj Jun 4, 2024
c0ca306
aws_ec2_capacity_block_offering: update documentation
johnsonaj Jun 4, 2024
4bfafad
aws_ec2_capacity_block_offering: update documentation
johnsonaj Jun 4, 2024
c79bcbe
aws_ec2_capacity_block_offering: remove testing directive
johnsonaj Jun 4, 2024
77b77bd
fix semgrep errors
johnsonaj Jun 4, 2024
64936e6
aws_ec2_capacity_block_reservation: WIP framework migration
johnsonaj Jun 5, 2024
195ce73
aws_ec2_capacity_block_reservation: migrate to framework
johnsonaj Jun 5, 2024
b58d0e0
chore: semgrep
johnsonaj Jun 6, 2024
70ab38a
chore: fix documentation formatting
johnsonaj Jun 6, 2024
0221a8e
move data source documentation to the correct location
johnsonaj Jun 6, 2024
aa0e053
resolve conflicts
johnsonaj Jun 6, 2024
5695aed
fmt
johnsonaj Jun 6, 2024
c4be0b8
Merge branch 'main' into feature/f-aws_ec2_capacity_block-add
johnsonaj Jun 11, 2024
db7352b
chore: tidy up test names
johnsonaj Jun 11, 2024
58a30ad
aws_ec2_capacity_block_reservation: tidy docs
johnsonaj Jun 11, 2024
bc72e8a
aws_ec2_capacity_block_reservation: add additional attribes to docume…
johnsonaj Jun 11, 2024
f18d457
Merge branch 'main' into feature/f-aws_ec2_capacity_block-add
johnsonaj Jun 11, 2024
a6aa0bc
update CHANGELOG
johnsonaj Jun 11, 2024
51ce353
tidy
johnsonaj Jun 11, 2024
5341a02
aws_ec2_capacity_block_offering: tidy docs
johnsonaj Jun 11, 2024
f347221
aws_ec2_capacity_block_reservation: fix test types
johnsonaj Jun 11, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changelog/37528.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:new-resource
aws_ec2_capacity_block_reservation
```

```release-note:new-data-source
aws_ec2_capacity_block_offering
```

```release-note:note
resource/aws_ec2_capacity_block_reservation: Because we cannot easily test this functionality, it is best effort and we ask for community help in testing
```
145 changes: 145 additions & 0 deletions internal/service/ec2/ec2_capacity_block_offering_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ec2

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/ec2"
awstypes "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/create"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource(name="Capacity Block Offering")
func newDataSourceCapacityBlockOffering(_ context.Context) (datasource.DataSourceWithConfigure, error) {
d := &dataSourceCapacityBlockOffering{}

return d, nil
}

type dataSourceCapacityBlockOffering struct {
framework.DataSourceWithConfigure
}

func (d *dataSourceCapacityBlockOffering) Metadata(_ context.Context, _ datasource.MetadataRequest, response *datasource.MetadataResponse) {
response.TypeName = "aws_ec2_capacity_block_offering"
}

func (d *dataSourceCapacityBlockOffering) Schema(_ context.Context, _ datasource.SchemaRequest, response *datasource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrAvailabilityZone: schema.StringAttribute{
Computed: true,
},
"capacity_block_offering_id": framework.IDAttribute(),
"capacity_duration_hours": schema.Int64Attribute{
Required: true,
},
"currency_code": schema.StringAttribute{
Computed: true,
},
"end_date_range": schema.StringAttribute{
CustomType: timetypes.RFC3339Type{},
Optional: true,
Computed: true,
},
names.AttrInstanceCount: schema.Int64Attribute{
Required: true,
},
names.AttrInstanceType: schema.StringAttribute{
Required: true,
},
"start_date_range": schema.StringAttribute{
CustomType: timetypes.RFC3339Type{},
Optional: true,
Computed: true,
},
"tenancy": schema.StringAttribute{
Computed: true,
},
"upfront_fee": schema.StringAttribute{
Computed: true,
},
},
}
}

const (
DSNameCapacityBlockOffering = "Capacity Block Offering"
)

func (d *dataSourceCapacityBlockOffering) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
conn := d.Meta().EC2Client(ctx)
var data dataSourceCapacityBlockOfferingData

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

if response.Diagnostics.HasError() {
return
}

input := &ec2.DescribeCapacityBlockOfferingsInput{}
response.Diagnostics.Append(fwflex.Expand(ctx, data, input)...)

if response.Diagnostics.HasError() {
return
}

output, err := findCapacityBLockOffering(ctx, conn, input)

if err != nil {
response.Diagnostics.AddError(
create.ProblemStandardMessage(names.EC2, create.ErrActionReading, DSNameCapacityBlockOffering, data.InstanceType.String(), err),
err.Error(),
)
return
}

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

if response.Diagnostics.HasError() {
return
}

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

type dataSourceCapacityBlockOfferingData struct {
AvailabilityZone types.String `tfsdk:"availability_zone"`
CapacityDurationHours types.Int64 `tfsdk:"capacity_duration_hours"`
CurrencyCode types.String `tfsdk:"currency_code"`
EndDateRange timetypes.RFC3339 `tfsdk:"end_date_range"`
CapacityBlockOfferingID types.String `tfsdk:"capacity_block_offering_id"`
InstanceCount types.Int64 `tfsdk:"instance_count"`
InstanceType types.String `tfsdk:"instance_type"`
StartDateRange timetypes.RFC3339 `tfsdk:"start_date_range"`
Tenancy types.String `tfsdk:"tenancy"`
UpfrontFee types.String `tfsdk:"upfront_fee"`
}

func findCapacityBLockOffering(ctx context.Context, conn *ec2.Client, in *ec2.DescribeCapacityBlockOfferingsInput) (*awstypes.CapacityBlockOffering, error) {
output, err := conn.DescribeCapacityBlockOfferings(ctx, in)

if err != nil {
return nil, err
}

if output == nil || len(output.CapacityBlockOfferings) == 0 {
return nil, tfresource.NewEmptyResultError(in)
}

if len(output.CapacityBlockOfferings) > 1 {
return nil, tfresource.NewTooManyResultsError(len(output.CapacityBlockOfferings), in)
}

return tfresource.AssertSingleValueResult(output.CapacityBlockOfferings)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package ec2_test

import (
"fmt"
"testing"
"time"

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

func TestAccEC2CapacityBlockOfferingDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
dataSourceName := "data.aws_ec2_capacity_block_offering.test"
startDate := time.Now().UTC().Add(25 * time.Hour).Format(time.RFC3339)
endDate := time.Now().UTC().Add(720 * time.Hour).Format(time.RFC3339)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
},
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
ErrorCheck: acctest.ErrorCheck(t, names.EC2),
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccCapacityBlockOfferingDataSourceConfig_basic(startDate, endDate),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(dataSourceName, names.AttrAvailabilityZone),
resource.TestCheckResourceAttr(dataSourceName, "capacity_duration_hours", "24"),
resource.TestCheckResourceAttr(dataSourceName, names.AttrInstanceCount, acctest.Ct1),
resource.TestCheckResourceAttr(dataSourceName, names.AttrInstanceType, "p4d.24xlarge"),
resource.TestCheckResourceAttrSet(dataSourceName, "capacity_block_offering_id"),
resource.TestCheckResourceAttr(dataSourceName, "tenancy", "default"),
resource.TestCheckResourceAttrSet(dataSourceName, "upfront_fee"),
),
},
},
})
}

func testAccCapacityBlockOfferingDataSourceConfig_basic(startDate, endDate string) string {
return fmt.Sprintf(`
data "aws_ec2_capacity_block_offering" "test" {
instance_type = "p4d.24xlarge"
capacity_duration_hours = 24
instance_count = 1
start_date_range = %[1]q
end_date_range = %[2]q
}
`, startDate, endDate)
}
Loading
Loading