Skip to content

fix: Remove resource addresses #982

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 1 commit into from
Apr 12, 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
11 changes: 6 additions & 5 deletions cli/cliui/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func WorkspaceResources(writer io.Writer, resources []codersdk.WorkspaceResource
if resource.Transition != database.WorkspaceTransitionStop {
continue
}
addressOnStop[resource.Address] = resource
addressOnStop[resource.Type+"."+resource.Name] = resource
}
// Displayed stores whether a resource has already been shown.
// Resources can be stored with numerous states, which we
Expand Down Expand Up @@ -75,24 +75,25 @@ func WorkspaceResources(writer io.Writer, resources []codersdk.WorkspaceResource
// callers to hide resources eventually.
continue
}
if _, shown := displayed[resource.Address]; shown {
resourceAddress := resource.Type + "." + resource.Name
if _, shown := displayed[resourceAddress]; shown {
// The same resource can have multiple transitions.
continue
}
displayed[resource.Address] = struct{}{}
displayed[resourceAddress] = struct{}{}

// Sort agents by name for consistent output.
sort.Slice(resource.Agents, func(i, j int) bool {
return resource.Agents[i].Name < resource.Agents[j].Name
})
_, existsOnStop := addressOnStop[resource.Address]
_, existsOnStop := addressOnStop[resourceAddress]
resourceState := "ephemeral"
if existsOnStop {
resourceState = "persistent"
}
// Display a line for the resource.
tableWriter.AppendRow(table.Row{
Styles.Bold.Render(resource.Type + "." + resource.Name),
Styles.Bold.Render(resourceAddress),
Styles.Placeholder.Render(resourceState),
"",
})
Expand Down
3 changes: 0 additions & 3 deletions cli/cliui/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,14 @@ func TestWorkspaceResources(t *testing.T) {
disconnected := database.Now().Add(-4 * time.Second)
go func() {
err := cliui.WorkspaceResources(ptty.Output(), []codersdk.WorkspaceResource{{
Address: "disk",
Transition: database.WorkspaceTransitionStart,
Type: "google_compute_disk",
Name: "root",
}, {
Address: "disk",
Transition: database.WorkspaceTransitionStop,
Type: "google_compute_disk",
Name: "root",
}, {
Address: "another",
Transition: database.WorkspaceTransitionStart,
Type: "google_compute_instance",
Name: "dev",
Expand Down
3 changes: 0 additions & 3 deletions cmd/cliui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,17 +188,14 @@ func main() {
RunE: func(cmd *cobra.Command, args []string) error {
disconnected := database.Now().Add(-4 * time.Second)
return cliui.WorkspaceResources(cmd.OutOrStdout(), []codersdk.WorkspaceResource{{
Address: "disk",
Transition: database.WorkspaceTransitionStart,
Type: "google_compute_disk",
Name: "root",
}, {
Address: "disk",
Transition: database.WorkspaceTransitionStop,
Type: "google_compute_disk",
Name: "root",
}, {
Address: "another",
Transition: database.WorkspaceTransitionStart,
Type: "google_compute_instance",
Name: "dev",
Expand Down
1 change: 0 additions & 1 deletion coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -1024,7 +1024,6 @@ func (q *fakeQuerier) InsertWorkspaceResource(_ context.Context, arg database.In
CreatedAt: arg.CreatedAt,
JobID: arg.JobID,
Transition: arg.Transition,
Address: arg.Address,
Type: arg.Type,
Name: arg.Name,
}
Expand Down
1 change: 0 additions & 1 deletion coderd/database/dump.sql

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

1 change: 0 additions & 1 deletion coderd/database/migrations/000004_jobs.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ CREATE TABLE workspace_resources (
created_at timestamptz NOT NULL,
job_id uuid NOT NULL REFERENCES provisioner_jobs (id) ON DELETE CASCADE,
transition workspace_transition NOT NULL,
address varchar(256) NOT NULL,
type varchar(192) NOT NULL,
name varchar(64) NOT NULL,
PRIMARY KEY (id)
Expand Down
1 change: 0 additions & 1 deletion coderd/database/models.go

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

21 changes: 4 additions & 17 deletions coderd/database/queries.sql.go

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

12 changes: 2 additions & 10 deletions coderd/database/queries/workspaceresources.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,6 @@ WHERE

-- name: InsertWorkspaceResource :one
INSERT INTO
workspace_resources (
id,
created_at,
job_id,
transition,
address,
type,
name
)
workspace_resources (id, created_at, job_id, transition, type, name)
VALUES
($1, $2, $3, $4, $5, $6, $7) RETURNING *;
($1, $2, $3, $4, $5, $6) RETURNING *;
20 changes: 5 additions & 15 deletions coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/coder/coder/coderd/httpapi"
"github.com/coder/coder/coderd/parameter"
"github.com/coder/coder/provisionerd/proto"
"github.com/coder/coder/provisionersdk"
sdkproto "github.com/coder/coder/provisionersdk/proto"
)

Expand Down Expand Up @@ -475,18 +474,14 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr
database.WorkspaceTransitionStart: jobType.TemplateImport.StartResources,
database.WorkspaceTransitionStop: jobType.TemplateImport.StopResources,
} {
addresses, err := provisionersdk.ResourceAddresses(resources)
if err != nil {
return nil, xerrors.Errorf("compute resource addresses: %w", err)
}
for index, resource := range resources {
for _, resource := range resources {
server.Logger.Info(ctx, "inserting template import job resource",
slog.F("job_id", job.ID.String()),
slog.F("resource_name", resource.Name),
slog.F("resource_type", resource.Type),
slog.F("transition", transition))

err = insertWorkspaceResource(ctx, server.Database, jobID, transition, resource, addresses[index])
err = insertWorkspaceResource(ctx, server.Database, jobID, transition, resource)
if err != nil {
return nil, xerrors.Errorf("insert resource: %w", err)
}
Expand Down Expand Up @@ -540,13 +535,9 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr
if err != nil {
return xerrors.Errorf("update workspace build: %w", err)
}
addresses, err := provisionersdk.ResourceAddresses(jobType.WorkspaceBuild.Resources)
if err != nil {
return xerrors.Errorf("compute resource addresses: %w", err)
}
// This could be a bulk insert to improve performance.
for index, protoResource := range jobType.WorkspaceBuild.Resources {
err = insertWorkspaceResource(ctx, db, job.ID, workspaceBuild.Transition, protoResource, addresses[index])
for _, protoResource := range jobType.WorkspaceBuild.Resources {
err = insertWorkspaceResource(ctx, db, job.ID, workspaceBuild.Transition, protoResource)
if err != nil {
return xerrors.Errorf("insert provisioner job: %w", err)
}
Expand Down Expand Up @@ -578,13 +569,12 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr
return &proto.Empty{}, nil
}

func insertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.UUID, transition database.WorkspaceTransition, protoResource *sdkproto.Resource, address string) error {
func insertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.UUID, transition database.WorkspaceTransition, protoResource *sdkproto.Resource) error {
resource, err := db.InsertWorkspaceResource(ctx, database.InsertWorkspaceResourceParams{
ID: uuid.New(),
CreatedAt: database.Now(),
JobID: jobID,
Transition: transition,
Address: address,
Type: protoResource.Type,
Name: protoResource.Name,
})
Expand Down
1 change: 0 additions & 1 deletion coderd/workspacebuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func convertWorkspaceResource(resource database.WorkspaceResource, agents []code
CreatedAt: resource.CreatedAt,
JobID: resource.JobID,
Transition: resource.Transition,
Address: resource.Address,
Type: resource.Type,
Name: resource.Name,
Agents: agents,
Expand Down
1 change: 0 additions & 1 deletion codersdk/workspaceresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ type WorkspaceResource struct {
CreatedAt time.Time `json:"created_at"`
JobID uuid.UUID `json:"job_id"`
Transition database.WorkspaceTransition `json:"workspace_transition"`
Address string `json:"address"`
Type string `json:"type"`
Name string `json:"name"`
Agents []WorkspaceAgent `json:"agents,omitempty"`
Expand Down
49 changes: 0 additions & 49 deletions provisionersdk/resources.go

This file was deleted.

63 changes: 0 additions & 63 deletions provisionersdk/resources_test.go

This file was deleted.