Skip to content

Commit a337403

Browse files
committed
add and populate the module_path column to workspace_resources
1 parent fabdbf5 commit a337403

File tree

14 files changed

+299
-246
lines changed

14 files changed

+299
-246
lines changed

coderd/database/dbgen/dbgen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,10 @@ func WorkspaceResource(t testing.TB, db database.Store, orig database.WorkspaceR
657657
Valid: takeFirst(orig.InstanceType.Valid, false),
658658
},
659659
DailyCost: takeFirst(orig.DailyCost, 0),
660+
ModulePath: sql.NullString{
661+
String: takeFirst(orig.ModulePath.String, ""),
662+
Valid: takeFirst(orig.ModulePath.Valid, true),
663+
},
660664
})
661665
require.NoError(t, err, "insert resource")
662666
return resource

coderd/database/dump.sql

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/migrations/000274_workspace_modules.down.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ DROP TABLE workspace_modules;
22

33
ALTER TABLE
44
workspace_resources
5-
DROP COLUMN module;
5+
DROP COLUMN module_path;

coderd/database/migrations/000274_workspace_modules.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
ALTER TABLE
22
workspace_resources
33
ADD
4-
COLUMN module TEXT;
4+
COLUMN module_path TEXT;
55

66
CREATE TABLE workspace_modules (
77
id uuid NOT NULL,

coderd/database/models.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

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

coderd/database/queries/workspaceresources.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ SELECT * FROM workspace_resources WHERE created_at > $1;
2727

2828
-- name: InsertWorkspaceResource :one
2929
INSERT INTO
30-
workspace_resources (id, created_at, job_id, transition, type, name, hide, icon, instance_type, daily_cost)
30+
workspace_resources (id, created_at, job_id, transition, type, name, hide, icon, instance_type, daily_cost, module_path)
3131
VALUES
32-
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING *;
32+
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *;
3333

3434
-- name: GetWorkspaceResourceMetadataByResourceIDs :many
3535
SELECT

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,6 +1799,11 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
17991799
String: protoResource.InstanceType,
18001800
Valid: protoResource.InstanceType != "",
18011801
},
1802+
ModulePath: sql.NullString{
1803+
String: protoResource.ModulePath,
1804+
// empty string is root module
1805+
Valid: true,
1806+
},
18021807
})
18031808
if err != nil {
18041809
return xerrors.Errorf("insert provisioner job resource %q: %w", protoResource.Name, err)

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ require (
344344
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
345345
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
346346
github.com/hashicorp/go-hclog v1.6.3 // indirect
347+
github.com/hashicorp/go-terraform-address v0.0.0-20240523040243-ccea9d309e0c
347348
github.com/hashicorp/go-uuid v1.0.3 // indirect
348349
github.com/hashicorp/hcl v1.0.1-vault-5 // indirect
349350
github.com/hashicorp/hcl/v2 v2.22.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,8 @@ github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9
562562
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4=
563563
github.com/hashicorp/go-sockaddr v1.0.2 h1:ztczhD1jLxIRjVejw8gFomI1BQZOe2WoVOu0SyteCQc=
564564
github.com/hashicorp/go-sockaddr v1.0.2/go.mod h1:rB4wwRAUzs07qva3c5SdrY/NEtAUjGlgmH/UkBUC97A=
565+
github.com/hashicorp/go-terraform-address v0.0.0-20240523040243-ccea9d309e0c h1:5v6L/m/HcAZYbrLGYBpPkcCVtDWwIgFxq2+FUmfPxPk=
566+
github.com/hashicorp/go-terraform-address v0.0.0-20240523040243-ccea9d309e0c/go.mod h1:xoy1vl2+4YvqSQEkKcFjNYxTk7cll+o1f1t2wxnHIX8=
565567
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
566568
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
567569
github.com/hashicorp/go-version v1.7.0 h1:5tqGy27NaOTB8yJKUZELlFAS/LTKJkrmONwQKeRZfjY=

provisioner/terraform/resources.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111

1212
"github.com/coder/terraform-provider-coder/provider"
1313

14+
tfaddr "github.com/hashicorp/go-terraform-address"
15+
1416
"github.com/coder/coder/v2/coderd/util/slice"
1517
stringutil "github.com/coder/coder/v2/coderd/util/strings"
1618
"github.com/coder/coder/v2/codersdk"
@@ -593,6 +595,13 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
593595
continue
594596
}
595597
label := convertAddressToLabel(resource.Address)
598+
modulePath, err := convertAddressToModulePath(resource.Address)
599+
if err != nil {
600+
// Module path recording was added primarily to keep track of
601+
// modules in telemetry. We're adding this fallback so we can
602+
// detect if there are any issues with the address parsing.
603+
modulePath = fmt.Sprintf("error parsing address: %s", resource.Address)
604+
}
596605

597606
agents, exists := resourceAgents[label]
598607
if exists {
@@ -608,6 +617,7 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
608617
Icon: resourceIcon[label],
609618
DailyCost: resourceCost[label],
610619
InstanceType: applyInstanceType(resource),
620+
ModulePath: modulePath,
611621
})
612622
}
613623
}
@@ -752,6 +762,20 @@ func convertAddressToLabel(address string) string {
752762
return cut
753763
}
754764

765+
// convertAddressToModulePath returns the module path from a Terraform address.
766+
// eg. "module.ec2_dev.ec2_instance.dev[0]" becomes "module.ec2_dev".
767+
// Empty string is returned for the root module.
768+
//
769+
// Module paths are defined in the Terraform spec:
770+
// https://github.com/hashicorp/terraform/blob/ef071f3d0e49ba421ae931c65b263827a8af1adb/website/docs/internals/resource-addressing.html.markdown#module-path
771+
func convertAddressToModulePath(address string) (string, error) {
772+
addr, err := tfaddr.NewAddress(address)
773+
if err != nil {
774+
return "", xerrors.Errorf("parse address: %w", err)
775+
}
776+
return addr.ModulePath.String(), nil
777+
}
778+
755779
func dependsOnAgent(graph *gographviz.Graph, agent *proto.Agent, resourceAgentID string, resource *tfjson.StateResource) bool {
756780
// Plan: we need to find if there is edge between the agent and the resource.
757781
if agent.Id == "" && resourceAgentID == "" {

0 commit comments

Comments
 (0)