Skip to content

Commit a8324ca

Browse files
committed
fix provisioner, add resource test
1 parent 1139e44 commit a8324ca

File tree

7 files changed

+527
-9
lines changed

7 files changed

+527
-9
lines changed

provisioner/terraform/resources.go

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ type agentAttributes struct {
5757
DisplayApps []agentDisplayAppsAttributes `mapstructure:"display_apps"`
5858
Order int64 `mapstructure:"order"`
5959
ResourcesMonitoring []agentResourcesMonitoring `mapstructure:"resources_monitoring"`
60-
Devcontainers []agentDevcontainer `mapstructure:"devcontainers"`
6160
}
6261

63-
type agentDevcontainer struct {
62+
type agentDevcontainerAttributes struct {
63+
AgentID string `mapstructure:"agent_id"`
6464
WorkspaceFolder string `mapstructure:"workspace_folder"`
6565
ConfigPath string `mapstructure:"config_path"`
6666
}
@@ -353,13 +353,6 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
353353
agent.Auth = &proto.Agent_InstanceId{}
354354
}
355355

356-
for _, devcontainer := range attrs.Devcontainers {
357-
agent.Devcontainers = append(agent.Devcontainers, &proto.Devcontainer{
358-
WorkspaceFolder: devcontainer.WorkspaceFolder,
359-
ConfigPath: devcontainer.ConfigPath,
360-
})
361-
}
362-
363356
// The label is used to find the graph node!
364357
agentLabel := convertAddressToLabel(tfResource.Address)
365358

@@ -603,6 +596,32 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
603596
}
604597
}
605598

599+
// Associate Dev Containers with agents.
600+
for _, resources := range tfResourcesByLabel {
601+
for _, resource := range resources {
602+
if resource.Type != "coder_devcontainer" {
603+
continue
604+
}
605+
var attrs agentDevcontainerAttributes
606+
err = mapstructure.Decode(resource.AttributeValues, &attrs)
607+
if err != nil {
608+
return nil, xerrors.Errorf("decode script attributes: %w", err)
609+
}
610+
for _, agents := range resourceAgents {
611+
for _, agent := range agents {
612+
// Find agents with the matching ID and associate them!
613+
if !dependsOnAgent(graph, agent, attrs.AgentID, resource) {
614+
continue
615+
}
616+
agent.Devcontainers = append(agent.Devcontainers, &proto.Devcontainer{
617+
WorkspaceFolder: attrs.WorkspaceFolder,
618+
ConfigPath: attrs.ConfigPath,
619+
})
620+
}
621+
}
622+
}
623+
}
624+
606625
// Associate metadata blocks with resources.
607626
resourceMetadata := map[string][]*proto.Resource_Metadata{}
608627
resourceHidden := map[string]bool{}

provisioner/terraform/resources_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,34 @@ func TestConvertResources(t *testing.T) {
830830
}},
831831
}},
832832
},
833+
"devcontainer": {
834+
resources: []*proto.Resource{
835+
{
836+
Name: "dev",
837+
Type: "null_resource",
838+
Agents: []*proto.Agent{{
839+
Name: "main",
840+
OperatingSystem: "linux",
841+
Architecture: "amd64",
842+
Auth: &proto.Agent_Token{},
843+
ConnectionTimeoutSeconds: 120,
844+
DisplayApps: &displayApps,
845+
ResourcesMonitoring: &proto.ResourcesMonitoring{},
846+
Devcontainers: []*proto.Devcontainer{
847+
{
848+
WorkspaceFolder: "/workspace1",
849+
},
850+
{
851+
WorkspaceFolder: "/workspace2",
852+
ConfigPath: "/workspace2/.devcontainer/devcontainer.json",
853+
},
854+
},
855+
}},
856+
},
857+
{Name: "dev1", Type: "coder_devcontainer"},
858+
{Name: "dev2", Type: "coder_devcontainer"},
859+
},
860+
},
833861
} {
834862
folderName := folderName
835863
expected := expected
@@ -1375,6 +1403,9 @@ func sortResources(resources []*proto.Resource) {
13751403
sort.Slice(agent.Scripts, func(i, j int) bool {
13761404
return agent.Scripts[i].DisplayName < agent.Scripts[j].DisplayName
13771405
})
1406+
sort.Slice(agent.Devcontainers, func(i, j int) bool {
1407+
return agent.Devcontainers[i].WorkspaceFolder < agent.Devcontainers[j].WorkspaceFolder
1408+
})
13781409
}
13791410
sort.Slice(resource.Agents, func(i, j int) bool {
13801411
return resource.Agents[i].Name < resource.Agents[j].Name
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
terraform {
2+
required_providers {
3+
coder = {
4+
source = "coder/coder"
5+
version = ">=2.0.0"
6+
}
7+
}
8+
}
9+
10+
resource "coder_agent" "main" {
11+
os = "linux"
12+
arch = "amd64"
13+
}
14+
15+
resource "coder_devcontainer" "dev1" {
16+
agent_id = coder_agent.main.id
17+
workspace_folder = "/workspace1"
18+
}
19+
20+
resource "coder_devcontainer" "dev2" {
21+
agent_id = coder_agent.main.id
22+
workspace_folder = "/workspace2"
23+
config_path = "/workspace2/.devcontainer/devcontainer.json"
24+
}
25+
26+
resource "null_resource" "dev" {
27+
depends_on = [
28+
coder_agent.main
29+
]
30+
}

provisioner/terraform/testdata/devcontainer/devcontainer.tfplan.dot

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

0 commit comments

Comments
 (0)