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

Skip to content

fix: Parsing dynamic values for agent results in error #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 8 additions & 21 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,25 +285,21 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
}
if envRaw, has := resource.Expressions["env"]; has {
env, ok := envRaw.ConstantValue.(map[string]string)
if !ok {
return nil, xerrors.Errorf("unexpected type %T for env map", envRaw.ConstantValue)
if ok {
agent.Env = env
}
agent.Env = env
}
if startupScriptRaw, has := resource.Expressions["startup_script"]; has {
startupScript, ok := startupScriptRaw.ConstantValue.(string)
if !ok {
return nil, xerrors.Errorf("unexpected type %T for startup script", startupScriptRaw.ConstantValue)
if ok {
agent.StartupScript = startupScript
}
agent.StartupScript = startupScript
}
if instanceIDRaw, has := resource.Expressions["instance_id"]; has {
instanceID, ok := instanceIDRaw.ConstantValue.(string)
if !ok {
return nil, xerrors.Errorf("unexpected type %T for instance_id", instanceIDRaw.ConstantValue)
}
if _, has := resource.Expressions["instance_id"]; has {
// This is a dynamic value. If it's expressed, we know
// it's at least an instance ID, which is better than nothing.
agent.Auth = &proto.Agent_InstanceId{
InstanceId: instanceID,
InstanceId: "",
}
}

Expand Down Expand Up @@ -429,15 +425,6 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
}
}

if agent != nil && agent.GetInstanceId() != "" {
// Make sure the instance has an instance ID!
_, exists := resource.AttributeValues["instance_id"]
if !exists {
// This was a mistake!
agent = nil
}
}

resources = append(resources, &proto.Resource{
Name: resource.Name,
Type: resource.Type,
Expand Down
64 changes: 48 additions & 16 deletions provisioner/terraform/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,7 @@ provider "coder" {
Request: &proto.Provision_Request{
Type: &proto.Provision_Request_Start{
Start: &proto.Provision_Start{
Metadata: &proto.Provision_Metadata{
CoderUrl: "https://example.com",
},
Metadata: &proto.Provision_Metadata{},
},
},
},
Expand All @@ -195,16 +193,14 @@ provider "coder" {
depends_on = [
null_resource.A
]
instance_id = "an-instance"
instance_id = "example"
}
resource "null_resource" "A" {}`,
},
Request: &proto.Provision_Request{
Type: &proto.Provision_Request_Start{
Start: &proto.Provision_Start{
Metadata: &proto.Provision_Metadata{
CoderUrl: "https://example.com",
},
Metadata: &proto.Provision_Metadata{},
},
},
},
Expand All @@ -214,6 +210,11 @@ provider "coder" {
Resources: []*proto.Resource{{
Name: "A",
Type: "null_resource",
Agent: &proto.Agent{
Auth: &proto.Agent_InstanceId{
InstanceId: "example",
},
},
}},
},
},
Expand All @@ -232,10 +233,8 @@ provider "coder" {
Request: &proto.Provision_Request{
Type: &proto.Provision_Request_Start{
Start: &proto.Provision_Start{
DryRun: true,
Metadata: &proto.Provision_Metadata{
CoderUrl: "https://example.com",
},
DryRun: true,
Metadata: &proto.Provision_Metadata{},
},
},
},
Expand Down Expand Up @@ -267,10 +266,43 @@ provider "coder" {
Request: &proto.Provision_Request{
Type: &proto.Provision_Request_Start{
Start: &proto.Provision_Start{
DryRun: true,
Metadata: &proto.Provision_Metadata{
CoderUrl: "https://example.com",
},
DryRun: true,
Metadata: &proto.Provision_Metadata{},
},
},
},
Response: &proto.Provision_Response{
Type: &proto.Provision_Response_Complete{
Complete: &proto.Provision_Complete{
Resources: []*proto.Resource{{
Name: "A",
Type: "null_resource",
Agent: &proto.Agent{
Auth: &proto.Agent_InstanceId{
InstanceId: "",
},
},
}},
},
},
},
}, {
Name: "dryrun-agent-associated-with-resource-instance-id",
Files: map[string]string{
"main.tf": provider + `
resource "coder_agent" "A" {
count = length(null_resource.A)
instance_id = length(null_resource.A)
}
resource "null_resource" "A" {
count = 1
}`,
},
Request: &proto.Provision_Request{
Type: &proto.Provision_Request_Start{
Start: &proto.Provision_Start{
DryRun: true,
Metadata: &proto.Provision_Metadata{},
},
},
},
Expand All @@ -282,7 +314,7 @@ provider "coder" {
Type: "null_resource",
Agent: &proto.Agent{
Auth: &proto.Agent_InstanceId{
InstanceId: "an-instance",
InstanceId: "",
},
},
}},
Expand Down