Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 457de83

Browse files
committed
fix: Parsing dynamic values for agent results in error
The logic required a constant value before, which disallowed dynamic value injection into the agent. This isn't an accurate limitation, so inverting the logic resolves it.
1 parent a06821c commit 457de83

File tree

2 files changed

+55
-36
lines changed

2 files changed

+55
-36
lines changed

provisioner/terraform/provision.go

+8-21
Original file line numberDiff line numberDiff line change
@@ -285,25 +285,21 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
285285
}
286286
if envRaw, has := resource.Expressions["env"]; has {
287287
env, ok := envRaw.ConstantValue.(map[string]string)
288-
if !ok {
289-
return nil, xerrors.Errorf("unexpected type %T for env map", envRaw.ConstantValue)
288+
if ok {
289+
agent.Env = env
290290
}
291-
agent.Env = env
292291
}
293292
if startupScriptRaw, has := resource.Expressions["startup_script"]; has {
294293
startupScript, ok := startupScriptRaw.ConstantValue.(string)
295-
if !ok {
296-
return nil, xerrors.Errorf("unexpected type %T for startup script", startupScriptRaw.ConstantValue)
294+
if ok {
295+
agent.StartupScript = startupScript
297296
}
298-
agent.StartupScript = startupScript
299297
}
300-
if instanceIDRaw, has := resource.Expressions["instance_id"]; has {
301-
instanceID, ok := instanceIDRaw.ConstantValue.(string)
302-
if !ok {
303-
return nil, xerrors.Errorf("unexpected type %T for instance_id", instanceIDRaw.ConstantValue)
304-
}
298+
if _, has := resource.Expressions["instance_id"]; has {
299+
// This is a dynamic value. If it's expressed, we know
300+
// it's at least an instance ID, which is better than nothing.
305301
agent.Auth = &proto.Agent_InstanceId{
306-
InstanceId: instanceID,
302+
InstanceId: "",
307303
}
308304
}
309305

@@ -429,15 +425,6 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
429425
}
430426
}
431427

432-
if agent != nil && agent.GetInstanceId() != "" {
433-
// Make sure the instance has an instance ID!
434-
_, exists := resource.AttributeValues["instance_id"]
435-
if !exists {
436-
// This was a mistake!
437-
agent = nil
438-
}
439-
}
440-
441428
resources = append(resources, &proto.Resource{
442429
Name: resource.Name,
443430
Type: resource.Type,

provisioner/terraform/provision_test.go

+47-15
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,7 @@ provider "coder" {
166166
Request: &proto.Provision_Request{
167167
Type: &proto.Provision_Request_Start{
168168
Start: &proto.Provision_Start{
169-
Metadata: &proto.Provision_Metadata{
170-
CoderUrl: "https://example.com",
171-
},
169+
Metadata: &proto.Provision_Metadata{},
172170
},
173171
},
174172
},
@@ -195,16 +193,14 @@ provider "coder" {
195193
depends_on = [
196194
null_resource.A
197195
]
198-
instance_id = "an-instance"
196+
instance_id = "example"
199197
}
200198
resource "null_resource" "A" {}`,
201199
},
202200
Request: &proto.Provision_Request{
203201
Type: &proto.Provision_Request_Start{
204202
Start: &proto.Provision_Start{
205-
Metadata: &proto.Provision_Metadata{
206-
CoderUrl: "https://example.com",
207-
},
203+
Metadata: &proto.Provision_Metadata{},
208204
},
209205
},
210206
},
@@ -214,6 +210,11 @@ provider "coder" {
214210
Resources: []*proto.Resource{{
215211
Name: "A",
216212
Type: "null_resource",
213+
Agent: &proto.Agent{
214+
Auth: &proto.Agent_InstanceId{
215+
InstanceId: "example",
216+
},
217+
},
217218
}},
218219
},
219220
},
@@ -232,10 +233,8 @@ provider "coder" {
232233
Request: &proto.Provision_Request{
233234
Type: &proto.Provision_Request_Start{
234235
Start: &proto.Provision_Start{
235-
DryRun: true,
236-
Metadata: &proto.Provision_Metadata{
237-
CoderUrl: "https://example.com",
238-
},
236+
DryRun: true,
237+
Metadata: &proto.Provision_Metadata{},
239238
},
240239
},
241240
},
@@ -267,10 +266,8 @@ provider "coder" {
267266
Request: &proto.Provision_Request{
268267
Type: &proto.Provision_Request_Start{
269268
Start: &proto.Provision_Start{
270-
DryRun: true,
271-
Metadata: &proto.Provision_Metadata{
272-
CoderUrl: "https://example.com",
273-
},
269+
DryRun: true,
270+
Metadata: &proto.Provision_Metadata{},
274271
},
275272
},
276273
},
@@ -289,6 +286,41 @@ provider "coder" {
289286
},
290287
},
291288
},
289+
}, {
290+
Name: "dryrun-agent-associated-with-resource-instance-id",
291+
Files: map[string]string{
292+
"main.tf": provider + `
293+
resource "coder_agent" "A" {
294+
count = length(null_resource.A)
295+
instance_id = length(null_resource.A)
296+
}
297+
resource "null_resource" "A" {
298+
count = 1
299+
}`,
300+
},
301+
Request: &proto.Provision_Request{
302+
Type: &proto.Provision_Request_Start{
303+
Start: &proto.Provision_Start{
304+
DryRun: true,
305+
Metadata: &proto.Provision_Metadata{},
306+
},
307+
},
308+
},
309+
Response: &proto.Provision_Response{
310+
Type: &proto.Provision_Response_Complete{
311+
Complete: &proto.Provision_Complete{
312+
Resources: []*proto.Resource{{
313+
Name: "A",
314+
Type: "null_resource",
315+
Agent: &proto.Agent{
316+
Auth: &proto.Agent_InstanceId{
317+
InstanceId: "",
318+
},
319+
},
320+
}},
321+
},
322+
},
323+
},
292324
}} {
293325
testCase := testCase
294326
t.Run(testCase.Name, func(t *testing.T) {

0 commit comments

Comments
 (0)