Skip to content
Merged
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
fix: Parse resources from Terraform Modules
Fixes when Terraform modules are used to primariy provision
infrastructure!
  • Loading branch information
kylecarbs committed May 17, 2022
commit 2803f845b8bf3bb245c40e2e52343714c4497e42
28 changes: 23 additions & 5 deletions provisioner/terraform/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,17 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
resources := make([]*proto.Resource, 0)
agents := map[string]*proto.Agent{}

tfResources := plan.Config.RootModule.Resources
var appendResources func(mod *tfjson.ConfigModule)
appendResources = func(mod *tfjson.ConfigModule) {
for _, module := range mod.ModuleCalls {
appendResources(module.Module)
}
tfResources = append(tfResources, mod.Resources...)
}

// Store all agents inside the maps!
for _, resource := range plan.Config.RootModule.Resources {
for _, resource := range tfResources {
if resource.Type != "coder_agent" {
continue
}
Expand Down Expand Up @@ -340,7 +349,7 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
agents[resource.Address] = agent
}

for _, resource := range plan.PlannedValues.RootModule.Resources {
for _, resource := range tfResources {
if resource.Mode == tfjson.DataResourceMode {
continue
}
Expand Down Expand Up @@ -407,8 +416,17 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
}
agents := map[string]*proto.Agent{}

tfResources := state.Values.RootModule.Resources
var appendResources func(resource *tfjson.StateModule)
appendResources = func(mod *tfjson.StateModule) {
for _, module := range mod.ChildModules {
appendResources(module)
}
tfResources = append(tfResources, mod.Resources...)
}

// Store all agents inside the maps!
for _, resource := range state.Values.RootModule.Resources {
for _, resource := range tfResources {
if resource.Type != "coder_agent" {
continue
}
Expand Down Expand Up @@ -439,7 +457,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
}

// Manually associate agents with instance IDs.
for _, resource := range state.Values.RootModule.Resources {
for _, resource := range tfResources {
if resource.Type != "coder_agent_instance" {
continue
}
Expand Down Expand Up @@ -471,7 +489,7 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform, state
}
}

for _, resource := range state.Values.RootModule.Resources {
for _, resource := range tfResources {
if resource.Mode == tfjson.DataResourceMode {
continue
}
Expand Down