Skip to content

Commit 3e68650

Browse files
authored
feat: support order property of coder_app resource (coder#12077)
1 parent 1e9a3c9 commit 3e68650

File tree

18 files changed

+306
-195
lines changed

18 files changed

+306
-195
lines changed

coderd/database/db2sdk/db2sdk.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/url"
8+
"sort"
89
"strconv"
910
"strings"
1011
"time"
@@ -414,6 +415,16 @@ func AppSubdomain(dbApp database.WorkspaceApp, agentName, workspaceName, ownerNa
414415
}
415416

416417
func Apps(dbApps []database.WorkspaceApp, agent database.WorkspaceAgent, ownerName string, workspace database.Workspace) []codersdk.WorkspaceApp {
418+
sort.Slice(dbApps, func(i, j int) bool {
419+
if dbApps[i].DisplayOrder != dbApps[j].DisplayOrder {
420+
return dbApps[i].DisplayOrder < dbApps[j].DisplayOrder
421+
}
422+
if dbApps[i].DisplayName != dbApps[j].DisplayName {
423+
return dbApps[i].DisplayName < dbApps[j].DisplayName
424+
}
425+
return dbApps[i].Slug < dbApps[j].Slug
426+
})
427+
417428
apps := make([]codersdk.WorkspaceApp, 0)
418429
for _, dbApp := range dbApps {
419430
apps = append(apps, codersdk.WorkspaceApp{

coderd/database/dbgen/dbgen.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ func WorkspaceApp(t testing.TB, db database.Store, orig database.WorkspaceApp) d
465465
HealthcheckInterval: takeFirst(orig.HealthcheckInterval, 60),
466466
HealthcheckThreshold: takeFirst(orig.HealthcheckThreshold, 60),
467467
Health: takeFirst(orig.Health, database.WorkspaceAppHealthHealthy),
468+
DisplayOrder: takeFirst(orig.DisplayOrder, 1),
468469
})
469470
require.NoError(t, err, "insert app")
470471
return resource

coderd/database/dbmem/dbmem.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5826,6 +5826,7 @@ func (q *FakeQuerier) InsertWorkspaceApp(_ context.Context, arg database.InsertW
58265826
HealthcheckInterval: arg.HealthcheckInterval,
58275827
HealthcheckThreshold: arg.HealthcheckThreshold,
58285828
Health: arg.Health,
5829+
DisplayOrder: arg.DisplayOrder,
58295830
}
58305831
q.workspaceApps = append(q.workspaceApps, workspaceApp)
58315832
return workspaceApp, nil

coderd/database/dump.sql

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE workspace_apps DROP COLUMN display_order;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
ALTER TABLE workspace_apps ADD COLUMN display_order integer NOT NULL DEFAULT 0;
2+
3+
COMMENT ON COLUMN workspace_apps.display_order
4+
IS 'Specifies the order in which to display agent app in user interfaces.';

coderd/database/models.go

Lines changed: 2 additions & 0 deletions
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: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/workspaceapps.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ INSERT INTO
2727
healthcheck_url,
2828
healthcheck_interval,
2929
healthcheck_threshold,
30-
health
30+
health,
31+
display_order
3132
)
3233
VALUES
33-
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15) RETURNING *;
34+
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16) RETURNING *;
3435

3536
-- name: UpdateWorkspaceAppHealthByID :exec
3637
UPDATE

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,6 +1648,7 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
16481648
HealthcheckInterval: app.Healthcheck.Interval,
16491649
HealthcheckThreshold: app.Healthcheck.Threshold,
16501650
Health: health,
1651+
DisplayOrder: int32(app.Order),
16511652
})
16521653
if err != nil {
16531654
return xerrors.Errorf("insert app: %w", err)

coderd/workspaces_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2543,6 +2543,66 @@ func TestWorkspaceResource(t *testing.T) {
25432543
require.EqualValues(t, app.Healthcheck.Threshold, got.Healthcheck.Threshold)
25442544
})
25452545

2546+
t.Run("Apps_DisplayOrder", func(t *testing.T) {
2547+
t.Parallel()
2548+
client := coderdtest.New(t, &coderdtest.Options{
2549+
IncludeProvisionerDaemon: true,
2550+
})
2551+
user := coderdtest.CreateFirstUser(t, client)
2552+
apps := []*proto.App{
2553+
{
2554+
Slug: "aaa",
2555+
DisplayName: "aaa",
2556+
},
2557+
{
2558+
Slug: "aaa-code-server",
2559+
Order: 4,
2560+
},
2561+
{
2562+
Slug: "bbb-code-server",
2563+
Order: 3,
2564+
},
2565+
{
2566+
Slug: "bbb",
2567+
},
2568+
}
2569+
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
2570+
Parse: echo.ParseComplete,
2571+
ProvisionApply: []*proto.Response{{
2572+
Type: &proto.Response_Apply{
2573+
Apply: &proto.ApplyComplete{
2574+
Resources: []*proto.Resource{{
2575+
Name: "some",
2576+
Type: "example",
2577+
Agents: []*proto.Agent{{
2578+
Id: "something",
2579+
Auth: &proto.Agent_Token{},
2580+
Apps: apps,
2581+
}},
2582+
}},
2583+
},
2584+
},
2585+
}},
2586+
})
2587+
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
2588+
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
2589+
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
2590+
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
2591+
2592+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
2593+
defer cancel()
2594+
2595+
workspace, err := client.Workspace(ctx, workspace.ID)
2596+
require.NoError(t, err)
2597+
require.Len(t, workspace.LatestBuild.Resources[0].Agents, 1)
2598+
agent := workspace.LatestBuild.Resources[0].Agents[0]
2599+
require.Len(t, agent.Apps, 4)
2600+
require.Equal(t, "bbb", agent.Apps[0].Slug) // empty-display-name < "aaa"
2601+
require.Equal(t, "aaa", agent.Apps[1].Slug) // no order < any order
2602+
require.Equal(t, "bbb-code-server", agent.Apps[2].Slug) // order = 3 < order = 4
2603+
require.Equal(t, "aaa-code-server", agent.Apps[3].Slug)
2604+
})
2605+
25462606
t.Run("Metadata", func(t *testing.T) {
25472607
t.Parallel()
25482608
client := coderdtest.New(t, &coderdtest.Options{

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ require (
9696
github.com/coder/flog v1.1.0
9797
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0
9898
github.com/coder/retry v1.5.1
99-
github.com/coder/terraform-provider-coder v0.14.1
99+
github.com/coder/terraform-provider-coder v0.14.2
100100
github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a
101101
github.com/coreos/go-oidc/v3 v3.9.0
102102
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,8 @@ github.com/coder/ssh v0.0.0-20231128192721-70855dedb788 h1:YoUSJ19E8AtuUFVYBpXuO
204204
github.com/coder/ssh v0.0.0-20231128192721-70855dedb788/go.mod h1:aGQbuCLyhRLMzZF067xc84Lh7JDs1FKwCmF1Crl9dxQ=
205205
github.com/coder/tailscale v1.1.1-0.20231205095743-61c97bad8c8b h1:ut/aL6oI8TjGdg4JI8+bKB9w5j73intbe0dJAmcmYyQ=
206206
github.com/coder/tailscale v1.1.1-0.20231205095743-61c97bad8c8b/go.mod h1:L8tPrwSi31RAMEMV8rjb0vYTGs7rXt8rAHbqY/p41j4=
207-
github.com/coder/terraform-provider-coder v0.14.1 h1:a97th+fVIs9i5kBj7WTOA4ssAAxaOTvwISLaYl4mbKw=
208-
github.com/coder/terraform-provider-coder v0.14.1/go.mod h1:pACHRoXSHBGyY696mLeQ1hR/Ag1G2wFk5bw0mT5Zp2g=
207+
github.com/coder/terraform-provider-coder v0.14.2 h1:CfQyQH9bOTI3cauxKJyPCUDkIcHOaMyFifxehkkkz/8=
208+
github.com/coder/terraform-provider-coder v0.14.2/go.mod h1:pACHRoXSHBGyY696mLeQ1hR/Ag1G2wFk5bw0mT5Zp2g=
209209
github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a h1:KhR9LUVllMZ+e9lhubZ1HNrtJDgH5YLoTvpKwmrGag4=
210210
github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a/go.mod h1:QzfptVUdEO+XbkzMKx1kw13i9wwpJlfI1RrZ6SNZ0hA=
211211
github.com/coder/wireguard-go v0.0.0-20230807234434-d825b45ccbf5 h1:eDk/42Kj4xN4yfE504LsvcFEo3dWUiCOaBiWJ2uIH2A=

provisioner/terraform/resources.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ type agentAppAttributes struct {
7676
Share string `mapstructure:"share"`
7777
Subdomain bool `mapstructure:"subdomain"`
7878
Healthcheck []appHealthcheckAttributes `mapstructure:"healthcheck"`
79+
Order int64 `mapstructure:"order"`
7980
}
8081

8182
type agentEnvAttributes struct {
@@ -437,6 +438,7 @@ func ConvertState(modules []*tfjson.StateModule, rawGraph string) (*State, error
437438
Subdomain: attrs.Subdomain,
438439
SharingLevel: sharingLevel,
439440
Healthcheck: healthcheck,
441+
Order: attrs.Order,
440442
})
441443
}
442444
}

0 commit comments

Comments
 (0)