Skip to content

Commit ac1a8da

Browse files
committed
fix provisioner, add resource test
1 parent b5c1ae0 commit ac1a8da

File tree

7 files changed

+536
-9
lines changed

7 files changed

+536
-9
lines changed

provisioner/terraform/resources.go

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package terraform
33
import (
44
"context"
55
"fmt"
6+
"slices"
67
"strings"
78

89
"github.com/awalterschulze/gographviz"
@@ -57,10 +58,10 @@ type agentAttributes struct {
5758
DisplayApps []agentDisplayAppsAttributes `mapstructure:"display_apps"`
5859
Order int64 `mapstructure:"order"`
5960
ResourcesMonitoring []agentResourcesMonitoring `mapstructure:"resources_monitoring"`
60-
Devcontainers []agentDevcontainer `mapstructure:"devcontainers"`
6161
}
6262

63-
type agentDevcontainer struct {
63+
type agentDevcontainerAttributes struct {
64+
AgentID string `mapstructure:"agent_id"`
6465
WorkspaceFolder string `mapstructure:"workspace_folder"`
6566
ConfigPath string `mapstructure:"config_path"`
6667
}
@@ -353,13 +354,6 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
353354
agent.Auth = &proto.Agent_InstanceId{}
354355
}
355356

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-
363357
// The label is used to find the graph node!
364358
agentLabel := convertAddressToLabel(tfResource.Address)
365359

@@ -603,6 +597,43 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
603597
}
604598
}
605599

600+
// Associate Dev Containers with agents.
601+
for _, resources := range tfResourcesByLabel {
602+
for _, resource := range resources {
603+
if resource.Type != "coder_devcontainer" {
604+
continue
605+
}
606+
var attrs agentDevcontainerAttributes
607+
err = mapstructure.Decode(resource.AttributeValues, &attrs)
608+
if err != nil {
609+
return nil, xerrors.Errorf("decode script attributes: %w", err)
610+
}
611+
for _, agents := range resourceAgents {
612+
for _, agent := range agents {
613+
// Find agents with the matching ID and associate them!
614+
if !dependsOnAgent(graph, agent, attrs.AgentID, resource) {
615+
continue
616+
}
617+
agent.Devcontainers = append(agent.Devcontainers, &proto.Devcontainer{
618+
WorkspaceFolder: attrs.WorkspaceFolder,
619+
ConfigPath: attrs.ConfigPath,
620+
})
621+
}
622+
}
623+
}
624+
}
625+
// Stable order.
626+
for _, agents := range resourceAgents {
627+
for _, agent := range agents {
628+
slices.SortStableFunc(agent.Devcontainers, func(a, b *proto.Devcontainer) int {
629+
if n := strings.Compare(a.WorkspaceFolder, b.WorkspaceFolder); n != 0 {
630+
return n
631+
}
632+
return strings.Compare(a.ConfigPath, b.ConfigPath)
633+
})
634+
}
635+
}
636+
606637
// Associate metadata blocks with resources.
607638
resourceMetadata := map[string][]*proto.Resource_Metadata{}
608639
resourceHidden := map[string]bool{}

provisioner/terraform/resources_test.go

Lines changed: 28 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
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy