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

Skip to content

Commit 21fdb80

Browse files
authored
fix: Parsing dynamic values for agent results in error (#564)
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 21fdb80

File tree

2 files changed

+56
-37
lines changed

2 files changed

+56
-37
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

+48-16
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,43 @@ 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{},
271+
},
272+
},
273+
},
274+
Response: &proto.Provision_Response{
275+
Type: &proto.Provision_Response_Complete{
276+
Complete: &proto.Provision_Complete{
277+
Resources: []*proto.Resource{{
278+
Name: "A",
279+
Type: "null_resource",
280+
Agent: &proto.Agent{
281+
Auth: &proto.Agent_InstanceId{
282+
InstanceId: "",
283+
},
284+
},
285+
}},
286+
},
287+
},
288+
},
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{},
274306
},
275307
},
276308
},
@@ -282,7 +314,7 @@ provider "coder" {
282314
Type: "null_resource",
283315
Agent: &proto.Agent{
284316
Auth: &proto.Agent_InstanceId{
285-
InstanceId: "an-instance",
317+
InstanceId: "",
286318
},
287319
},
288320
}},

0 commit comments

Comments
 (0)