|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package bedrock |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/bedrock" |
| 12 | + awstypes "github.com/aws/aws-sdk-go-v2/service/bedrock/types" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" |
| 18 | + "github.com/hashicorp/terraform-provider-aws/internal/errs" |
| 19 | + "github.com/hashicorp/terraform-provider-aws/internal/framework" |
| 20 | + fwflex "github.com/hashicorp/terraform-provider-aws/internal/framework/flex" |
| 21 | + fwtypes "github.com/hashicorp/terraform-provider-aws/internal/framework/types" |
| 22 | + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" |
| 23 | + "github.com/hashicorp/terraform-provider-aws/names" |
| 24 | +) |
| 25 | + |
| 26 | +// @FrameworkDataSource("aws_bedrock_prompt_router", name="Prompt Router") |
| 27 | +func newPromptRouterDataSource(context.Context) (datasource.DataSourceWithConfigure, error) { |
| 28 | + return &promptRouterDataSource{}, nil |
| 29 | +} |
| 30 | + |
| 31 | +type promptRouterDataSource struct { |
| 32 | + framework.DataSourceWithModel[promptRouterDataSourceModel] |
| 33 | +} |
| 34 | + |
| 35 | +func (d *promptRouterDataSource) Schema(ctx context.Context, request datasource.SchemaRequest, response *datasource.SchemaResponse) { |
| 36 | + response.Schema = schema.Schema{ |
| 37 | + Attributes: map[string]schema.Attribute{ |
| 38 | + names.AttrCreatedAt: schema.StringAttribute{ |
| 39 | + CustomType: timetypes.RFC3339Type{}, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + names.AttrDescription: schema.StringAttribute{ |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + "fallback_model": framework.DataSourceComputedListOfObjectAttribute[promptRouterTargetModelModel](ctx), |
| 46 | + "models": framework.DataSourceComputedListOfObjectAttribute[promptRouterTargetModelModel](ctx), |
| 47 | + "prompt_router_arn": schema.StringAttribute{ |
| 48 | + CustomType: fwtypes.ARNType, |
| 49 | + Required: true, |
| 50 | + }, |
| 51 | + "prompt_router_name": schema.StringAttribute{ |
| 52 | + Computed: true, |
| 53 | + }, |
| 54 | + "routing_criteria": framework.DataSourceComputedListOfObjectAttribute[routingCriteriaModel](ctx), |
| 55 | + names.AttrStatus: schema.StringAttribute{ |
| 56 | + CustomType: fwtypes.StringEnumType[awstypes.PromptRouterStatus](), |
| 57 | + Computed: true, |
| 58 | + }, |
| 59 | + names.AttrType: schema.StringAttribute{ |
| 60 | + CustomType: fwtypes.StringEnumType[awstypes.PromptRouterType](), |
| 61 | + Computed: true, |
| 62 | + }, |
| 63 | + "updated_at": schema.StringAttribute{ |
| 64 | + CustomType: timetypes.RFC3339Type{}, |
| 65 | + Computed: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (d *promptRouterDataSource) Read(ctx context.Context, request datasource.ReadRequest, response *datasource.ReadResponse) { |
| 72 | + var data promptRouterDataSourceModel |
| 73 | + response.Diagnostics.Append(request.Config.Get(ctx, &data)...) |
| 74 | + if response.Diagnostics.HasError() { |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + conn := d.Meta().BedrockClient(ctx) |
| 79 | + |
| 80 | + output, err := findPromptRouterByARN(ctx, conn, data.PromptRouterARN.ValueString()) |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + response.Diagnostics.AddError(fmt.Sprintf("reading Bedrock Prompt Router (%s)", data.PromptRouterARN.ValueString()), err.Error()) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + response.Diagnostics.Append(fwflex.Flatten(ctx, output, &data)...) |
| 88 | + if response.Diagnostics.HasError() { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + response.Diagnostics.Append(response.State.Set(ctx, &data)...) |
| 93 | +} |
| 94 | + |
| 95 | +func findPromptRouterByARN(ctx context.Context, conn *bedrock.Client, arn string) (*bedrock.GetPromptRouterOutput, error) { |
| 96 | + input := &bedrock.GetPromptRouterInput{ |
| 97 | + PromptRouterArn: aws.String(arn), |
| 98 | + } |
| 99 | + |
| 100 | + return findPromptRouter(ctx, conn, input) |
| 101 | +} |
| 102 | + |
| 103 | +func findPromptRouter(ctx context.Context, conn *bedrock.Client, input *bedrock.GetPromptRouterInput) (*bedrock.GetPromptRouterOutput, error) { |
| 104 | + output, err := conn.GetPromptRouter(ctx, input) |
| 105 | + |
| 106 | + if errs.IsA[*awstypes.ResourceNotFoundException](err) { |
| 107 | + return nil, &retry.NotFoundError{ |
| 108 | + LastError: err, |
| 109 | + LastRequest: input, |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if err != nil { |
| 114 | + return nil, err |
| 115 | + } |
| 116 | + |
| 117 | + if output == nil { |
| 118 | + return nil, tfresource.NewEmptyResultError(input) |
| 119 | + } |
| 120 | + |
| 121 | + return output, nil |
| 122 | +} |
| 123 | + |
| 124 | +type promptRouterDataSourceModel struct { |
| 125 | + framework.WithRegionModel |
| 126 | + CreatedAt timetypes.RFC3339 `tfsdk:"created_at"` |
| 127 | + Description types.String `tfsdk:"description"` |
| 128 | + FallbackModel fwtypes.ListNestedObjectValueOf[promptRouterTargetModelModel] `tfsdk:"fallback_model"` |
| 129 | + Models fwtypes.ListNestedObjectValueOf[promptRouterTargetModelModel] `tfsdk:"models"` |
| 130 | + PromptRouterARN fwtypes.ARN `tfsdk:"prompt_router_arn"` |
| 131 | + PromptRouterName types.String `tfsdk:"prompt_router_name"` |
| 132 | + RoutingCriteria fwtypes.ListNestedObjectValueOf[routingCriteriaModel] `tfsdk:"routing_criteria"` |
| 133 | + Status fwtypes.StringEnum[awstypes.PromptRouterStatus] `tfsdk:"status"` |
| 134 | + Type fwtypes.StringEnum[awstypes.PromptRouterType] `tfsdk:"type"` |
| 135 | + UpdatedAt timetypes.RFC3339 `tfsdk:"updated_at"` |
| 136 | +} |
| 137 | + |
| 138 | +type promptRouterTargetModelModel struct { |
| 139 | + ModelARN types.String `tfsdk:"model_arn"` |
| 140 | +} |
| 141 | + |
| 142 | +type routingCriteriaModel struct { |
| 143 | + ResponseQualityDifference types.Float64 `tfsdk:"response_quality_difference"` |
| 144 | +} |
0 commit comments