Skip to content

Commit

Permalink
Merge pull request #39486 from sasidhar-aws/f-aws_bedrockagent_agent_…
Browse files Browse the repository at this point in the history
…action_group-prepare_agent_arg

r/aws_bedrockagent_agent_action_group: use configured prepare_agent value
  • Loading branch information
ewbankkit authored Oct 3, 2024
2 parents 652a182 + 8becdad commit 7b5017f
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/39486.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_bedrockagent_agent_action_group: Add `prepare_agent` argument
```
42 changes: 40 additions & 2 deletions internal/service/bedrockagent/agent_action_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/bedrockagent"
awstypes "github.com/aws/aws-sdk-go-v2/service/bedrockagent/types"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
Expand All @@ -38,14 +40,14 @@ import (
func newAgentActionGroupResource(context.Context) (resource.ResourceWithConfigure, error) {
r := &agentActionGroupResource{}

r.SetDefaultDeleteTimeout(120 * time.Minute)
r.SetDefaultCreateTimeout(30 * time.Minute)
r.SetDefaultUpdateTimeout(30 * time.Minute)

return r, nil
}

type agentActionGroupResource struct {
framework.ResourceWithConfigure
framework.WithImportByID
framework.WithTimeouts
}

Expand Down Expand Up @@ -91,6 +93,14 @@ func (r *agentActionGroupResource) Schema(ctx context.Context, request resource.
CustomType: fwtypes.StringEnumType[awstypes.ActionGroupSignature](),
Optional: true,
},
"prepare_agent": schema.BoolAttribute{
Optional: true,
Computed: true,
Default: booldefault.StaticBool(true),
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
},
"skip_resource_in_use_check": schema.BoolAttribute{
Optional: true,
Computed: true,
Expand Down Expand Up @@ -222,6 +232,10 @@ func (r *agentActionGroupResource) Schema(ctx context.Context, request resource.
},
},
},
names.AttrTimeouts: timeouts.Block(ctx, timeouts.Opts{
Create: true,
Update: true,
}),
},
}
}
Expand Down Expand Up @@ -259,6 +273,14 @@ func (r *agentActionGroupResource) Create(ctx context.Context, request resource.
}
data.ID = types.StringValue(id)

if data.PrepareAgent.ValueBool() {
if _, err := prepareAgent(ctx, conn, data.AgentID.ValueString(), r.CreateTimeout(ctx, data.Timeouts)); err != nil {
response.Diagnostics.AddError("preparing Agent", err.Error())

return
}
}

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

Expand Down Expand Up @@ -342,6 +364,14 @@ func (r *agentActionGroupResource) Update(ctx context.Context, request resource.
return
}

if new.PrepareAgent.ValueBool() {
if _, err := prepareAgent(ctx, conn, new.AgentID.ValueString(), r.UpdateTimeout(ctx, new.Timeouts)); err != nil {
response.Diagnostics.AddError("preparing Agent", err.Error())

return
}
}

new.ActionGroupState = fwtypes.StringEnumValue(output.ActionGroupState)

response.Diagnostics.Append(response.State.Set(ctx, &new)...)
Expand Down Expand Up @@ -374,6 +404,12 @@ func (r *agentActionGroupResource) Delete(ctx context.Context, request resource.
}
}

func (r *agentActionGroupResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root(names.AttrID), req.ID)...)
// Set prepare_agent to default value on import
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("prepare_agent"), true)...)
}

func findAgentActionGroupByThreePartKey(ctx context.Context, conn *bedrockagent.Client, actionGroupID, agentID, agentVersion string) (*awstypes.AgentActionGroup, error) {
input := &bedrockagent.GetAgentActionGroupInput{
ActionGroupId: aws.String(actionGroupID),
Expand Down Expand Up @@ -413,7 +449,9 @@ type agentActionGroupResourceModel struct {
FunctionSchema fwtypes.ListNestedObjectValueOf[functionSchemaModel] `tfsdk:"function_schema"`
ID types.String `tfsdk:"id"`
ParentActionGroupSignature fwtypes.StringEnum[awstypes.ActionGroupSignature] `tfsdk:"parent_action_group_signature"`
PrepareAgent types.Bool `tfsdk:"prepare_agent"`
SkipResourceInUseCheck types.Bool `tfsdk:"skip_resource_in_use_check"`
Timeouts timeouts.Value `tfsdk:"timeouts"`
}

const (
Expand Down
58 changes: 58 additions & 0 deletions internal/service/bedrockagent/agent_action_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ import (
awstypes "github.com/aws/aws-sdk-go-v2/service/bedrockagent/types"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/knownvalue"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
"github.com/hashicorp/terraform-plugin-testing/statecheck"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
tfplancheck "github.com/hashicorp/terraform-provider-aws/internal/acctest/plancheck"
tfstatecheck "github.com/hashicorp/terraform-provider-aws/internal/acctest/statecheck"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfbedrockagent "github.com/hashicorp/terraform-provider-aws/internal/service/bedrockagent"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -49,6 +55,7 @@ func TestAccBedrockAgentAgentActionGroup_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "api_schema.0.payload"),
resource.TestCheckResourceAttr(resourceName, "api_schema.0.s3.#", acctest.Ct0),
resource.TestCheckResourceAttr(resourceName, "function_schema.#", acctest.Ct0),
resource.TestCheckResourceAttr(resourceName, "prepare_agent", acctest.CtTrue),
),
},
{
Expand All @@ -61,6 +68,57 @@ func TestAccBedrockAgentAgentActionGroup_basic(t *testing.T) {
})
}

func TestAccBedrockAgentAgentActionGroup_upgradeToPrepareAgent(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_bedrockagent_agent_action_group.test"
var v awstypes.AgentActionGroup

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, names.BedrockEndpointID) },
ErrorCheck: acctest.ErrorCheck(t, names.BedrockAgentServiceID),
CheckDestroy: testAccCheckAgentActionGroupDestroy(ctx),
Steps: []resource.TestStep{
{
ExternalProviders: map[string]resource.ExternalProvider{
"aws": {
Source: "hashicorp/aws",
VersionConstraint: "5.69.0",
},
},
Config: testAccAgentActionGroupConfig_basic(rName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionCreate),
},
},
ConfigStateChecks: []statecheck.StateCheck{
tfstatecheck.ExpectNoValue(resourceName, tfjsonpath.New("prepare_agent")),
},
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAgentActionGroupExists(ctx, resourceName, &v),
),
},
{
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Config: testAccAgentActionGroupConfig_basic(rName),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
tfplancheck.ExpectKnownValueChange(resourceName, tfjsonpath.New("prepare_agent"), knownvalue.Null(), knownvalue.Bool(true)),
},
},
ConfigStateChecks: []statecheck.StateCheck{
statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("prepare_agent"), knownvalue.Bool(true)),
},
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAgentActionGroupExists(ctx, resourceName, &v),
),
},
},
})
}

func TestAccBedrockAgentAgentActionGroup_APISchema_s3(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
Expand Down
4 changes: 3 additions & 1 deletion website/docs/r/bedrockagent_agent_action_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ The following arguments are optional:
Each function represents an action in an action group.
See [`function_schema` Block](#function_schema-block) for details.
* `parent_action_group_signature` - (Optional) To allow your agent to request the user for additional information when trying to complete a task, set this argument to `AMAZON.UserInput`. You must leave the `description`, `api_schema`, and `action_group_executor` arguments blank for this action group. Valid values: `AMAZON.UserInput`.
* `prepare_agent` - (Optional) Whether or not to prepare the agent after creation or modification. Defaults to `true`.
* `skip_resource_in_use_check` - (Optional) Whether the in-use check is skipped when deleting the action group.

### `action_group_executor` Block
Expand Down Expand Up @@ -189,7 +190,8 @@ This resource exports the following attributes in addition to the arguments abov

[Configuration options](https://developer.hashicorp.com/terraform/language/resources/syntax#operation-timeouts):

* `delete` - (Default `120m`)
* `create` - (Default `30m`)
* `update` - (Default `30m`)

## Import

Expand Down

0 comments on commit 7b5017f

Please sign in to comment.