Skip to content

feat: Allow hide resources #3977

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 14 commits into from
Sep 9, 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
1 change: 1 addition & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,7 @@ func (q *fakeQuerier) InsertWorkspaceResource(_ context.Context, arg database.In
Transition: arg.Transition,
Type: arg.Type,
Name: arg.Name,
Hide: arg.Hide,
}
q.provisionerJobResources = append(q.provisionerJobResources, resource)
return resource, nil
Expand Down
3 changes: 2 additions & 1 deletion coderd/database/dump.sql

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE workspace_resources
DROP COLUMN hide;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE workspace_resources
ADD COLUMN hide boolean DEFAULT false NOT NULL;
1 change: 1 addition & 0 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions coderd/database/queries/workspaceresources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ SELECT * FROM workspace_resources WHERE created_at > $1;

-- name: InsertWorkspaceResource :one
INSERT INTO
workspace_resources (id, created_at, job_id, transition, type, name)
workspace_resources (id, created_at, job_id, transition, type, name, hide)
VALUES
($1, $2, $3, $4, $5, $6) RETURNING *;
($1, $2, $3, $4, $5, $6, $7) RETURNING *;

-- name: GetWorkspaceResourceMetadataByResourceID :many
SELECT
Expand Down
1 change: 1 addition & 0 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,7 @@ func insertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
Transition: transition,
Type: protoResource.Type,
Name: protoResource.Name,
Hide: protoResource.Hide,
})
if err != nil {
return xerrors.Errorf("insert provisioner job resource %q: %w", protoResource.Name, err)
Expand Down
1 change: 1 addition & 0 deletions coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ func convertWorkspaceResource(resource database.WorkspaceResource, agents []code
Transition: codersdk.WorkspaceTransition(resource.Transition),
Type: resource.Type,
Name: resource.Name,
Hide: resource.Hide,
Agents: agents,
Metadata: convertedMetadata,
}
Expand Down
1 change: 1 addition & 0 deletions codersdk/workspaceresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type WorkspaceResource struct {
Transition WorkspaceTransition `json:"workspace_transition"`
Type string `json:"type"`
Name string `json:"name"`
Hide bool `json:"hide"`
Agents []WorkspaceAgent `json:"agents,omitempty"`
Metadata []WorkspaceResourceMetadata `json:"metadata,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion provisioner/terraform/provision_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestProvision_Cancel(t *testing.T) {

cwd, err := os.Getwd()
require.NoError(t, err)
fakeBin := filepath.Join(cwd, "testdata", "bin", "terraform_fake_cancel.sh")
fakeBin := filepath.Join(cwd, "testdata", "fake_cancel.sh")

tests := []struct {
name string
Expand Down
72 changes: 59 additions & 13 deletions provisioner/terraform/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type agentAppAttributes struct {
// A mapping of attributes on the "coder_metadata" resource.
type metadataAttributes struct {
ResourceID string `mapstructure:"resource_id"`
Hide bool `mapstructure:"hide"`
Items []metadataItem `mapstructure:"item"`
}

Expand All @@ -48,6 +49,7 @@ type metadataItem struct {

// ConvertResources consumes Terraform state and a GraphViz representation produced by
// `terraform graph` to produce resources consumable by Coder.
// nolint:gocyclo
func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Resource, error) {
parsedGraph, err := gographviz.ParseString(rawGraph)
if err != nil {
Expand Down Expand Up @@ -137,7 +139,7 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res
}

var agentResource *graphResource
for _, resource := range findResourcesUpGraph(graph, tfResourceByLabel, agentNode.Name, 0) {
for _, resource := range findResourcesInGraph(graph, tfResourceByLabel, agentNode.Name, 0, true) {
if agentResource == nil {
// Default to the first resource because we have nothing to compare!
agentResource = resource
Expand Down Expand Up @@ -234,7 +236,8 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res

// Associate metadata blocks with resources.
resourceMetadata := map[string][]*proto.Resource_Metadata{}
for label, resource := range tfResourceByLabel {
resourceHidden := map[string]bool{}
for _, resource := range tfResourceByLabel {
if resource.Type != "coder_metadata" {
continue
}
Expand All @@ -244,17 +247,54 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res
return nil, xerrors.Errorf("decode metadata attributes: %w", err)
}

var targetLabel string
// This occurs in a plan, because there is no resource ID.
// We attempt to find the closest node, just so we can hide it from the UI.
if attrs.ResourceID == "" {
// TODO: detect this as an error
// At plan time, change.after_unknown.resource_id should be "true".
// At provision time, values.resource_id should be set.
continue
resourceLabel := convertAddressToLabel(resource.Address)

var attachedNode *gographviz.Node
for _, node := range graph.Nodes.Lookup {
// The node attributes surround the label with quotes.
if strings.Trim(node.Attrs["label"], `"`) != resourceLabel {
continue
}
attachedNode = node
break
}
if attachedNode == nil {
continue
}
var attachedResource *graphResource
for _, resource := range findResourcesInGraph(graph, tfResourceByLabel, attachedNode.Name, 0, false) {
if attachedResource == nil {
// Default to the first resource because we have nothing to compare!
attachedResource = resource
continue
}
if resource.Depth < attachedResource.Depth {
// There's a closer resource!
attachedResource = resource
continue
}
if resource.Depth == attachedResource.Depth && resource.Label < attachedResource.Label {
attachedResource = resource
continue
}
}
if attachedResource == nil {
continue
}
targetLabel = attachedResource.Label
}
targetLabel, ok := resourceLabelByID[attrs.ResourceID]
if !ok {
return nil, xerrors.Errorf("attribute %s.resource_id = %q does not refer to a valid resource", label, attrs.ResourceID)
if targetLabel == "" {
targetLabel = resourceLabelByID[attrs.ResourceID]
}
if targetLabel == "" {
continue
}

resourceHidden[targetLabel] = attrs.Hide
for _, item := range attrs.Items {
resourceMetadata[targetLabel] = append(resourceMetadata[targetLabel],
&proto.Resource_Metadata{
Expand Down Expand Up @@ -284,6 +324,7 @@ func ConvertResources(module *tfjson.StateModule, rawGraph string) ([]*proto.Res
Name: resource.Name,
Type: resource.Type,
Agents: agents,
Hide: resourceHidden[label],
Metadata: resourceMetadata[label],
})
}
Expand Down Expand Up @@ -344,14 +385,19 @@ func applyAutomaticInstanceID(resource *tfjson.StateResource, agents []*proto.Ag
}
}

// findResourcesUpGraph traverses upwards in a graph until a resource is found,
// findResourcesInGraph traverses directionally in a graph until a resource is found,
// then it stores the depth it was found at, and continues working up the tree.
func findResourcesUpGraph(graph *gographviz.Graph, tfResourceByLabel map[string]*tfjson.StateResource, nodeName string, currentDepth uint) []*graphResource {
// nolint:revive
func findResourcesInGraph(graph *gographviz.Graph, tfResourceByLabel map[string]*tfjson.StateResource, nodeName string, currentDepth uint, up bool) []*graphResource {
graphResources := make([]*graphResource, 0)
for destination := range graph.Edges.DstToSrcs[nodeName] {
mapping := graph.Edges.DstToSrcs
if !up {
mapping = graph.Edges.SrcToDsts
}
for destination := range mapping[nodeName] {
destinationNode := graph.Nodes.Lookup[destination]
// Work our way up the tree!
graphResources = append(graphResources, findResourcesUpGraph(graph, tfResourceByLabel, destinationNode.Name, currentDepth+1)...)
graphResources = append(graphResources, findResourcesInGraph(graph, tfResourceByLabel, destinationNode.Name, currentDepth+1, up)...)

destinationLabel, exists := destinationNode.Attrs["label"]
if !exists {
Expand Down
7 changes: 5 additions & 2 deletions provisioner/terraform/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func TestConvertResources(t *testing.T) {
"resource-metadata": {{
Name: "about",
Type: "null_resource",
Hide: true,
Metadata: []*proto.Resource_Metadata{{
Key: "hello",
Value: "world",
Expand Down Expand Up @@ -155,11 +156,13 @@ func TestConvertResources(t *testing.T) {
require.NoError(t, err)
sortResources(resources)

// plan does not contain metadata, so clone expected and remove it
var expectedNoMetadata []*proto.Resource
for _, resource := range expected {
resourceCopy, _ := protobuf.Clone(resource).(*proto.Resource)
resourceCopy.Metadata = nil
// plan cannot know whether values are null or not
for _, metadata := range resourceCopy.Metadata {
metadata.IsNull = false
}
expectedNoMetadata = append(expectedNoMetadata, resourceCopy)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.4.2"
version = "0.4.10"
}
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
coder = {
source = "coder/coder"
version = "0.4.2"
version = "0.4.10"
}
}
}
Expand Down
Loading