Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 66e8dbb

Browse files
authored
feat: persist generated coder_app id (#18487)
1 parent 49fcffc commit 66e8dbb

File tree

9 files changed

+50
-4
lines changed

9 files changed

+50
-4
lines changed

coderd/agentapi/apps.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (a *AppsAPI) BatchUpdateAppHealths(ctx context.Context, req *agentproto.Bat
9292
Health: app.Health,
9393
})
9494
if err != nil {
95-
return nil, xerrors.Errorf("update workspace app health for app %q (%q): %w", err, app.ID, app.Slug)
95+
return nil, xerrors.Errorf("update workspace app health for app %q (%q): %w", app.ID, app.Slug, err)
9696
}
9797
}
9898

coderd/provisionerdserver/provisionerdserver.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2595,8 +2595,19 @@ func InsertWorkspaceResource(ctx context.Context, db database.Store, jobID uuid.
25952595
openIn = database.WorkspaceAppOpenInSlimWindow
25962596
}
25972597

2598+
var appID string
2599+
if app.Id == "" || app.Id == uuid.Nil.String() {
2600+
appID = uuid.NewString()
2601+
} else {
2602+
appID = app.Id
2603+
}
2604+
id, err := uuid.Parse(appID)
2605+
if err != nil {
2606+
return xerrors.Errorf("parse app uuid: %w", err)
2607+
}
2608+
25982609
dbApp, err := db.InsertWorkspaceApp(ctx, database.InsertWorkspaceAppParams{
2599-
ID: uuid.New(),
2610+
ID: id,
26002611
CreatedAt: dbtime.Now(),
26012612
AgentID: dbAgent.ID,
26022613
Slug: slug,

provisioner/terraform/resources.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/awalterschulze/gographviz"
10+
"github.com/google/uuid"
1011
tfjson "github.com/hashicorp/terraform-json"
1112
"github.com/mitchellh/mapstructure"
1213
"golang.org/x/xerrors"
@@ -93,6 +94,7 @@ type agentDisplayAppsAttributes struct {
9394

9495
// A mapping of attributes on the "coder_app" resource.
9596
type agentAppAttributes struct {
97+
ID string `mapstructure:"id"`
9698
AgentID string `mapstructure:"agent_id"`
9799
// Slug is required in terraform, but to avoid breaking existing users we
98100
// will default to the resource name if it is not specified.
@@ -522,7 +524,17 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
522524
continue
523525
}
524526

527+
id := attrs.ID
528+
if id == "" {
529+
// This should never happen since the "id" attribute is set on creation:
530+
// https://github.com/coder/terraform-provider-coder/blob/cfa101df4635e405e66094fa7779f9a89d92f400/provider/app.go#L37
531+
logger.Warn(ctx, "coder_app's id was unexpectedly empty", slog.F("name", attrs.Name))
532+
533+
id = uuid.NewString()
534+
}
535+
525536
agent.Apps = append(agent.Apps, &proto.App{
537+
Id: id,
526538
Slug: attrs.Slug,
527539
DisplayName: attrs.DisplayName,
528540
Command: attrs.Command,

provisioner/terraform/resources_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,9 @@ func TestConvertResources(t *testing.T) {
967967
if agent.GetInstanceId() != "" {
968968
agent.Auth = &proto.Agent_InstanceId{}
969969
}
970+
for _, app := range agent.Apps {
971+
app.Id = ""
972+
}
970973
}
971974
}
972975

@@ -1037,6 +1040,9 @@ func TestConvertResources(t *testing.T) {
10371040
if agent.GetInstanceId() != "" {
10381041
agent.Auth = &proto.Agent_InstanceId{}
10391042
}
1043+
for _, app := range agent.Apps {
1044+
app.Id = ""
1045+
}
10401046
}
10411047
}
10421048
// Convert expectedNoMetadata and resources into a

provisionerd/proto/version.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import "github.com/coder/coder/v2/apiversion"
3737
// - Add new field named `scheduling` to `Prebuild`, with fields for timezone
3838
// and schedule rules to define cron-based scaling of prebuilt workspace
3939
// instances based on time patterns.
40+
// - Added new field named `id` to `App`, which transports the ID generated by the coder_app provider to be persisted.
4041
const (
4142
CurrentMajor = 1
4243
CurrentMinor = 7

provisionersdk/proto/provisioner.pb.go

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

provisionersdk/proto/provisioner.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ message App {
266266
bool hidden = 11;
267267
AppOpenIn open_in = 12;
268268
string group = 13;
269+
string id = 14; // If nil, new UUID will be generated.
269270
}
270271

271272
// Healthcheck represents configuration for checking for app readiness.

site/e2e/provisionerGenerated.ts

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

site/e2e/tests/app.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ test("app", async ({ context, page }) => {
4242
token,
4343
apps: [
4444
{
45+
id: randomUUID(),
4546
url: `http://localhost:${addr.port}`,
4647
displayName: appName,
4748
order: 0,

0 commit comments

Comments
 (0)