Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit84caed7

Browse files
committed
chore: move prompt param validation to ConvertState with other validations, add test
Signed-off-by: Danny Kopping <dannykopping@gmail.com>
1 parent291082d commit84caed7

File tree

25 files changed

+1217
-95
lines changed

25 files changed

+1217
-95
lines changed

‎provisioner/terraform/resources.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"math"
7+
"slices"
78
"strings"
89

910
"github.com/awalterschulze/gographviz"
@@ -1026,12 +1027,22 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
10261027
externalAuthProviders=append(externalAuthProviders,it)
10271028
}
10281029

1030+
hasAITasks:=hasAITaskResources(graph)
1031+
ifhasAITasks {
1032+
hasPromptParam:=slices.ContainsFunc(parameters,func(param*proto.RichParameter)bool {
1033+
returnparam.Name==provider.TaskPromptParameterName
1034+
})
1035+
if!hasPromptParam {
1036+
returnnil,xerrors.Errorf("coder_parameter named '%s' is required when 'coder_ai_task' resource is defined",provider.TaskPromptParameterName)
1037+
}
1038+
}
1039+
10291040
return&State{
10301041
Resources:resources,
10311042
Parameters:parameters,
10321043
Presets:presets,
10331044
ExternalAuthProviders:externalAuthProviders,
1034-
HasAITasks:hasAITaskResources(graph),
1045+
HasAITasks:hasAITasks,
10351046
AITasks:aiTasks,
10361047
},nil
10371048
}

‎provisioner/terraform/resources_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import (
1111
"strings"
1212
"testing"
1313

14+
"github.com/coder/terraform-provider-coder/v2/provider"
1415
"github.com/google/go-cmp/cmp"
1516
tfjson"github.com/hashicorp/terraform-json"
1617
"github.com/stretchr/testify/require"
1718
protobuf"google.golang.org/protobuf/proto"
1819

1920
"cdr.dev/slog"
2021
"cdr.dev/slog/sloggers/slogtest"
22+
2123
"github.com/coder/coder/v2/testutil"
2224

2325
"github.com/coder/coder/v2/cryptorand"
@@ -1434,6 +1436,55 @@ func TestInstanceIDAssociation(t *testing.T) {
14341436
}
14351437
}
14361438

1439+
funcTestAITasks(t*testing.T) {
1440+
t.Parallel()
1441+
ctx,logger:=ctxAndLogger(t)
1442+
1443+
t.Run("Prompt parameter is required",func(t*testing.T) {
1444+
t.Parallel()
1445+
1446+
// nolint:dogsled
1447+
_,filename,_,_:=runtime.Caller(0)
1448+
1449+
dir:=filepath.Join(filepath.Dir(filename),"testdata","resources","ai-tasks-missing-prompt")
1450+
tfPlanRaw,err:=os.ReadFile(filepath.Join(dir,"ai-tasks-missing-prompt.tfplan.json"))
1451+
require.NoError(t,err)
1452+
vartfPlan tfjson.Plan
1453+
err=json.Unmarshal(tfPlanRaw,&tfPlan)
1454+
require.NoError(t,err)
1455+
tfPlanGraph,err:=os.ReadFile(filepath.Join(dir,"ai-tasks-missing-prompt.tfplan.dot"))
1456+
require.NoError(t,err)
1457+
1458+
state,err:=terraform.ConvertState(ctx, []*tfjson.StateModule{tfPlan.PlannedValues.RootModule,tfPlan.PriorState.Values.RootModule},string(tfPlanGraph),logger)
1459+
require.Nil(t,state)
1460+
require.ErrorContains(t,err,fmt.Sprintf("coder_parameter named '%s' is required when 'coder_ai_task' resource is defined",provider.TaskPromptParameterName))
1461+
})
1462+
1463+
t.Run("Multiple tasks can be defined",func(t*testing.T) {
1464+
t.Parallel()
1465+
1466+
// nolint:dogsled
1467+
_,filename,_,_:=runtime.Caller(0)
1468+
1469+
dir:=filepath.Join(filepath.Dir(filename),"testdata","resources","ai-tasks-multiple")
1470+
tfPlanRaw,err:=os.ReadFile(filepath.Join(dir,"ai-tasks-multiple.tfplan.json"))
1471+
require.NoError(t,err)
1472+
vartfPlan tfjson.Plan
1473+
err=json.Unmarshal(tfPlanRaw,&tfPlan)
1474+
require.NoError(t,err)
1475+
tfPlanGraph,err:=os.ReadFile(filepath.Join(dir,"ai-tasks-multiple.tfplan.dot"))
1476+
require.NoError(t,err)
1477+
1478+
state,err:=terraform.ConvertState(ctx, []*tfjson.StateModule{tfPlan.PlannedValues.RootModule,tfPlan.PriorState.Values.RootModule},string(tfPlanGraph),logger)
1479+
require.NotNil(t,state)
1480+
require.NoError(t,err)
1481+
require.True(t,state.HasAITasks)
1482+
// Multiple coder_ai_tasks resources can be defined, but only 1 is allowed.
1483+
// This is validated once all parameters are resolved etc as part of the workspace build, but for now we can allow it.
1484+
require.Len(t,state.AITasks,2)
1485+
})
1486+
}
1487+
14371488
// sortResource ensures resources appear in a consistent ordering
14381489
// to prevent tests from flaking.
14391490
funcsortResources(resources []*proto.Resource) {

‎provisioner/terraform/testdata/resources/ai-tasks-missing-prompt/ai-tasks-missing-prompt.tfplan.dot

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more aboutcustomizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp