Skip to content

Commit e914801

Browse files
committed
convertApps
1 parent ec977f2 commit e914801

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

coderd/provisionerjobs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (api *API) provisionerJobResources(rw http.ResponseWriter, r *http.Request,
149149
}
150150

151151
apiAgent, err := convertWorkspaceAgent(
152-
api.DERPMap(), *api.TailnetCoordinator.Load(), agent, convertApps(dbApps), api.AgentInactiveDisconnectTimeout,
152+
api.DERPMap(), *api.TailnetCoordinator.Load(), agent, convertProvisionedApps(dbApps), api.AgentInactiveDisconnectTimeout,
153153
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
154154
)
155155
if err != nil {

coderd/workspaceagents.go

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,42 @@ func (api *API) workspaceAgent(rw http.ResponseWriter, r *http.Request) {
6464
})
6565
return
6666
}
67+
68+
resource, err := api.Database.GetWorkspaceResourceByID(ctx, workspaceAgent.ResourceID)
69+
if err != nil {
70+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
71+
Message: "Internal error fetching workspace resource.",
72+
Detail: err.Error(),
73+
})
74+
return
75+
}
76+
build, err := api.Database.GetWorkspaceBuildByJobID(ctx, resource.JobID)
77+
if err != nil {
78+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
79+
Message: "Internal error fetching workspace build.",
80+
Detail: err.Error(),
81+
})
82+
return
83+
}
84+
workspace, err := api.Database.GetWorkspaceByID(ctx, build.WorkspaceID)
85+
if err != nil {
86+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
87+
Message: "Internal error fetching workspace.",
88+
Detail: err.Error(),
89+
})
90+
return
91+
}
92+
owner, err := api.Database.GetUserByID(ctx, workspace.OwnerID)
93+
if err != nil {
94+
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
95+
Message: "Internal error fetching workspace owner.",
96+
Detail: err.Error(),
97+
})
98+
return
99+
}
100+
67101
apiAgent, err := convertWorkspaceAgent(
68-
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, convertApps(dbApps), api.AgentInactiveDisconnectTimeout,
102+
api.DERPMap(), *api.TailnetCoordinator.Load(), workspaceAgent, convertApps(dbApps, workspaceAgent, owner, workspace), api.AgentInactiveDisconnectTimeout,
69103
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
70104
)
71105
if err != nil {
@@ -165,7 +199,7 @@ func (api *API) workspaceAgentManifest(rw http.ResponseWriter, r *http.Request)
165199

166200
httpapi.Write(ctx, rw, http.StatusOK, agentsdk.Manifest{
167201
AgentID: apiAgent.ID,
168-
Apps: convertApps(dbApps),
202+
Apps: convertApps(dbApps, workspaceAgent, owner, workspace),
169203
DERPMap: api.DERPMap(),
170204
DERPForceWebSockets: api.DeploymentValues.DERP.Config.ForceWebSockets.Value(),
171205
GitAuthConfigs: len(api.GitAuthConfigs),
@@ -1281,7 +1315,13 @@ func (api *API) workspaceAgentClientCoordinate(rw http.ResponseWriter, r *http.R
12811315
}
12821316
}
12831317

1284-
func convertApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp {
1318+
// convertProvisionedApps converts applications that are in the middle of provisioning process.
1319+
// It means that they may not have an agent or workspace assigned (dry-run job).
1320+
func convertProvisionedApps(dbApps []database.WorkspaceApp) []codersdk.WorkspaceApp {
1321+
return convertApps(dbApps, database.WorkspaceAgent{}, database.User{}, database.Workspace{})
1322+
}
1323+
1324+
func convertApps(dbApps []database.WorkspaceApp, agent database.WorkspaceAgent, owner database.User, workspace database.Workspace) []codersdk.WorkspaceApp {
12851325
apps := make([]codersdk.WorkspaceApp, 0)
12861326
for _, dbApp := range dbApps {
12871327
apps = append(apps, codersdk.WorkspaceApp{

coderd/workspacebuilds.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ func (api *API) convertWorkspaceBuild(
832832
for _, agent := range agents {
833833
apps := appsByAgentID[agent.ID]
834834
apiAgent, err := convertWorkspaceAgent(
835-
api.DERPMap(), *api.TailnetCoordinator.Load(), agent, convertApps(apps), api.AgentInactiveDisconnectTimeout,
835+
api.DERPMap(), *api.TailnetCoordinator.Load(), agent, convertApps(apps, agent, owner, workspace), api.AgentInactiveDisconnectTimeout,
836836
api.DeploymentValues.AgentFallbackTroubleshootingURL.String(),
837837
)
838838
if err != nil {

0 commit comments

Comments
 (0)