Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .changelog/45124.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:new-data-source
aws_bedrock_prompt_router
```

```release-note:new-data-source
aws_bedrock_prompt_routers
```
144 changes: 144 additions & 0 deletions internal/service/bedrock/prompt_router_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package bedrock

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/bedrock"
awstypes "github.com/aws/aws-sdk-go-v2/service/bedrock/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-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/framework"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @FrameworkDataSource("aws_bedrock_prompt_router", name="Prompt Router")
func newPromptRouterDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
return &promptRouterDataSource{}, nil
}

type promptRouterDataSource struct {
framework.DataSourceWithModel[promptRouterDataSourceModel]
}

func (d *promptRouterDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
names.AttrCreatedAt: schema.StringAttribute{
CustomType: timetypes.RFC3339Type{},
Computed: true,
},
names.AttrDescription: schema.StringAttribute{
Computed: true,
},
"fallback_model": framework.DataSourceComputedListOfObjectAttribute[promptRouterTargetModelModel](ctx),
"models": framework.DataSourceComputedListOfObjectAttribute[promptRouterTargetModelModel](ctx),
"prompt_router_arn": schema.StringAttribute{
CustomType: fwtypes.ARNType,
Required: true,
},
"prompt_router_name": schema.StringAttribute{
Computed: true,
},
"routing_criteria": framework.DataSourceComputedListOfObjectAttribute[routingCriteriaModel](ctx),
names.AttrStatus: schema.StringAttribute{
CustomType: fwtypes.StringEnumType[awstypes.PromptRouterStatus](),
Computed: true,
},
names.AttrType: schema.StringAttribute{
CustomType: fwtypes.StringEnumType[awstypes.PromptRouterType](),
Computed: true,
},
"updated_at": schema.StringAttribute{
CustomType: timetypes.RFC3339Type{},
Computed: true,
},
},
}
}

func (d *promptRouterDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
var data promptRouterDataSourceModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := d.Meta().BedrockClient(ctx)

output, err := findPromptRouterByARN(ctx, conn, data.PromptRouterARN.ValueString())

if err != nil {
response.Diagnostics.AddError(fmt.Sprintf("reading Bedrock Prompt Router (%s)", data.PromptRouterARN.ValueString()), err.Error())
return
}

response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
if response.Diagnostics.HasError() {
return
}

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

func findPromptRouterByARN(ctx context.Context, conn *bedrock.Client, arn string) (*bedrock.GetPromptRouterOutput, error) {
input := &bedrock.GetPromptRouterInput{
PromptRouterArn: aws.String(arn),
}

return findPromptRouter(ctx, conn, input)
}

func findPromptRouter(ctx context.Context, conn *bedrock.Client, input *bedrock.GetPromptRouterInput) (*bedrock.GetPromptRouterOutput, error) {
output, err := conn.GetPromptRouter(ctx, input)

if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output, nil
}

type promptRouterDataSourceModel struct {
framework.WithRegionModel
CreatedAt timetypes.RFC3339 `tfsdk:"created_at"`
Description types.String `tfsdk:"description"`
FallbackModel fwtypes.ListNestedObjectValueOf[promptRouterTargetModelModel] `tfsdk:"fallback_model"`
Models fwtypes.ListNestedObjectValueOf[promptRouterTargetModelModel] `tfsdk:"models"`
PromptRouterARN fwtypes.ARN `tfsdk:"prompt_router_arn"`
PromptRouterName types.String `tfsdk:"prompt_router_name"`
RoutingCriteria fwtypes.ListNestedObjectValueOf[routingCriteriaModel] `tfsdk:"routing_criteria"`
Status fwtypes.StringEnum[awstypes.PromptRouterStatus] `tfsdk:"status"`
Type fwtypes.StringEnum[awstypes.PromptRouterType] `tfsdk:"type"`
UpdatedAt timetypes.RFC3339 `tfsdk:"updated_at"`
}

type promptRouterTargetModelModel struct {
ModelARN types.String `tfsdk:"model_arn"`
}

type routingCriteriaModel struct {
ResponseQualityDifference types.Float64 `tfsdk:"response_quality_difference"`
}
46 changes: 46 additions & 0 deletions internal/service/bedrock/prompt_router_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package bedrock_test

import (
"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 TestAccBedrockPromptRouterDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
datasourceName := "data.aws_bedrock_prompt_router.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.BedrockEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.BedrockServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccPromptRouterDataSourceConfig_basic(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet(datasourceName, "prompt_router_arn"),
resource.TestCheckResourceAttrSet(datasourceName, "prompt_router_name"),
resource.TestCheckResourceAttrSet(datasourceName, names.AttrStatus),
resource.TestCheckResourceAttrSet(datasourceName, names.AttrType),
resource.TestCheckResourceAttrSet(datasourceName, names.AttrCreatedAt),
resource.TestCheckResourceAttrSet(datasourceName, "updated_at"),
),
},
},
})
}

func testAccPromptRouterDataSourceConfig_basic() string {
return `
data "aws_bedrock_prompt_routers" "test" {}

data "aws_bedrock_prompt_router" "test" {
prompt_router_arn = data.aws_bedrock_prompt_routers.test.prompt_router_summaries[0].prompt_router_arn
}
`
}
104 changes: 104 additions & 0 deletions internal/service/bedrock/prompt_routers_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package bedrock

import (
"context"

"github.com/aws/aws-sdk-go-v2/service/bedrock"
awstypes "github.com/aws/aws-sdk-go-v2/service/bedrock/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/framework"
fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex"
fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types"
)

// @FrameworkDataSource("aws_bedrock_prompt_routers", name="Prompt Routers")
func newPromptRoutersDataSource(context.Context) (datasource.DataSourceWithConfigure, error) {
return &promptRoutersDataSource{}, nil
}

type promptRoutersDataSource struct {
framework.DataSourceWithModel[promptRoutersDataSourceModel]
}

func (d *promptRoutersDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) {
response.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"prompt_router_summaries": framework.DataSourceComputedListOfObjectAttribute[promptRouterSummaryModel](ctx),
},
}
}

func (d *promptRoutersDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) {
var data promptRoutersDataSourceModel
response.Diagnostics.Append(request.Config.Get(ctx, &data)...)
if response.Diagnostics.HasError() {
return
}

conn := d.Meta().BedrockClient(ctx)

input := &bedrock.ListPromptRoutersInput{}

response.Diagnostics.Append(fwflex.Expand(ctx, data, input)...)
if response.Diagnostics.HasError() {
return
}

promptRouters, err := findPromptRouters(ctx, conn, input)

if err != nil {
response.Diagnostics.AddError("listing Bedrock Prompt Routers", err.Error())
return
}

output := &bedrock.ListPromptRoutersOutput{
PromptRouterSummaries: promptRouters,
}
response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...)
if response.Diagnostics.HasError() {
return
}

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

func findPromptRouters(ctx context.Context, conn *bedrock.Client, input *bedrock.ListPromptRoutersInput) ([]awstypes.PromptRouterSummary, error) {
var output = make([]awstypes.PromptRouterSummary, 0)

pages := bedrock.NewListPromptRoutersPaginator(conn, input)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
return nil, err
}

output = append(output, page.PromptRouterSummaries...)
}

return output, nil
}

type promptRoutersDataSourceModel struct {
framework.WithRegionModel
PromptRouterSummaries fwtypes.ListNestedObjectValueOf[promptRouterSummaryModel] `tfsdk:"prompt_router_summaries"`
}

type promptRouterSummaryModel struct {
CreatedAt timetypes.RFC3339 `tfsdk:"created_at"`
Description types.String `tfsdk:"description"`
FallbackModel fwtypes.ListNestedObjectValueOf[promptRouterTargetModelModel] `tfsdk:"fallback_model"`
Models fwtypes.ListNestedObjectValueOf[promptRouterTargetModelModel] `tfsdk:"models"`
PromptRouterARN fwtypes.ARN `tfsdk:"prompt_router_arn"`
PromptRouterName types.String `tfsdk:"prompt_router_name"`
RoutingCriteria fwtypes.ListNestedObjectValueOf[routingCriteriaModel] `tfsdk:"routing_criteria"`
Status fwtypes.StringEnum[awstypes.PromptRouterStatus] `tfsdk:"status"`
Type fwtypes.StringEnum[awstypes.PromptRouterType] `tfsdk:"type"`
UpdatedAt timetypes.RFC3339 `tfsdk:"updated_at"`
}
37 changes: 37 additions & 0 deletions internal/service/bedrock/prompt_routers_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package bedrock_test

import (
"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 TestAccBedrockPromptRoutersDataSource_basic(t *testing.T) {
ctx := acctest.Context(t)
datasourceName := "data.aws_bedrock_prompt_routers.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.BedrockEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.BedrockServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccPromptRoutersDataSourceConfig_basic(),
Check: resource.ComposeTestCheckFunc(
acctest.CheckResourceAttrGreaterThanOrEqualValue(datasourceName, "prompt_router_summaries.#", 0),
),
},
},
})
}

func testAccPromptRoutersDataSourceConfig_basic() string {
return `
data "aws_bedrock_prompt_routers" "test" {}
`
}
12 changes: 12 additions & 0 deletions internal/service/bedrock/service_package_gen.go

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

Loading
Loading